1 | var env = require('../env') |
---|
2 | , upgradeCouch = require('../config/upgrade-couchdb') |
---|
3 | , cryptoken = require('../util/crypto-token') |
---|
4 | , HTTPResult = require('../util/http-result') |
---|
5 | , _ = require('underscore') |
---|
6 | ; |
---|
7 | |
---|
8 | var rewrites = { |
---|
9 | SurveyRun: function(doc) { |
---|
10 | rewriteDate(doc,'publicationDate'); |
---|
11 | rewriteDate(doc,'startDate'); |
---|
12 | rewriteDate(doc,'endDate'); |
---|
13 | rewriteDoc(doc.survey); |
---|
14 | return doc; |
---|
15 | }, |
---|
16 | Survey: function(doc) { |
---|
17 | rewriteDate(doc,'publicationDate'); |
---|
18 | _.each(doc.questions,rewriteDoc); |
---|
19 | return doc; |
---|
20 | }, |
---|
21 | Question: function(doc) { |
---|
22 | rewriteDate(doc,'publicationDate'); |
---|
23 | return doc; |
---|
24 | }, |
---|
25 | Response: function(doc) { |
---|
26 | rewriteDate(doc,'publicationDate'); |
---|
27 | return doc; |
---|
28 | } |
---|
29 | }; |
---|
30 | |
---|
31 | function rewriteDate(doc,field) { |
---|
32 | if ( field in doc ) { |
---|
33 | doc[field] = new Date(doc[field]).toISOString(); |
---|
34 | return true; |
---|
35 | } else { |
---|
36 | return false; |
---|
37 | } |
---|
38 | } |
---|
39 | |
---|
40 | function rewriteDoc(doc) { |
---|
41 | if ( doc.type in rewrites ) { |
---|
42 | return rewrites[doc.type](doc); |
---|
43 | } else { |
---|
44 | return HTTPResult.fail(); |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | upgradeCouch(env.couchServerURL,env.dbName,rewriteDoc) |
---|
49 | .then(function(res){ |
---|
50 | console.log("done",res); |
---|
51 | }, function(err){ |
---|
52 | console.error("fail",err); |
---|
53 | }); |
---|