Commit
Finds all dates that have a space in them (implying they are the problematic "17th Apr, 2010" style) and convert them to YYYY-MM-DD as expected. Issue: medic/cht-core#1672
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
var async = require('async'), | ||
db = require('../db'), | ||
moment = require('moment'); | ||
|
||
var temporaryView = { | ||
"map": "function(doc) { if (doc.type === 'person' && doc.date_of_birth && doc.date_of_birth.indexOf(' ') >= 0) { emit(1); }}" | ||
}; | ||
|
||
module.exports = { | ||
name: 'convert-bad-dob-format', | ||
created: new Date(2016, 4, 20), | ||
run: function(callback) { | ||
db.request({ | ||
db: 'medic', | ||
method: 'POST', | ||
path: '_temp_view', | ||
body: temporaryView, | ||
qs: { | ||
include_docs: true, | ||
} | ||
}, function(err, result) { | ||
var docs = result.rows.map(function(row) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
return row.doc; | ||
}); | ||
|
||
for(doc of docs) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
SCdF
Author
Contributor
|
||
var currentDob = doc.date_of_birth; | ||
var convertedDob = moment(doc.date_of_birth, 'MMM Do, YYYY').format('YYYY-MM-DD'); | ||
This comment has been minimized.
Sorry, something went wrong.
alxndrsn
Contributor
|
||
|
||
doc.date_of_birth = convertedDob; | ||
} | ||
|
||
db.medic.bulk({ | ||
docs: docs | ||
}, callback); | ||
}); | ||
} | ||
}; |
Seems weird to iterate twice.