[525] | 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 qcodeMap = { |
---|
| 9 | PT1GF: 'ST1GF' |
---|
| 10 | }; |
---|
| 11 | |
---|
| 12 | var subcodeMap = { |
---|
| 13 | SAPY: { // really called, team psychological safety |
---|
| 14 | 10: '1', |
---|
| 15 | 11: '2', |
---|
| 16 | 12: '3', |
---|
| 17 | 13: '4', |
---|
| 18 | 14: '5', |
---|
| 19 | 15: '6', |
---|
| 20 | 16: '7' |
---|
| 21 | }, |
---|
| 22 | SAPI2: { |
---|
| 23 | 1: '' |
---|
| 24 | }, |
---|
| 25 | PT1GF: { |
---|
| 26 | 10: '1', |
---|
| 27 | 11: '2', |
---|
| 28 | 12: '3', |
---|
| 29 | 13: '4', |
---|
| 30 | 14: '5', |
---|
| 31 | 15: '10', |
---|
| 32 | 16: '11' |
---|
| 33 | }, |
---|
| 34 | SVAG12: { |
---|
| 35 | 0: '' |
---|
| 36 | }, |
---|
| 37 | SVTCQ: { |
---|
| 38 | 10: '1', |
---|
| 39 | 11: '2', |
---|
| 40 | 12: '3', |
---|
| 41 | 13: '4', |
---|
| 42 | 14: '5', |
---|
| 43 | 15: '6', |
---|
| 44 | 16: '7', |
---|
| 45 | 17: '8', |
---|
| 46 | 18: '9', |
---|
| 47 | 19: '10', |
---|
| 48 | 110: '11', |
---|
| 49 | 111: '12', |
---|
| 50 | 112: '13', |
---|
| 51 | 113: '14', |
---|
| 52 | 114: '15', |
---|
| 53 | 115: '16' |
---|
| 54 | }, |
---|
| 55 | SAGE: { |
---|
| 56 | 10: '1', |
---|
| 57 | 11: '2', |
---|
| 58 | 12: '3', |
---|
| 59 | 13: '4', |
---|
| 60 | 14: '5', |
---|
| 61 | 15: '6', |
---|
| 62 | 16: '7', |
---|
| 63 | 17: '8', |
---|
| 64 | 18: '9', |
---|
| 65 | 19: '10', |
---|
| 66 | 110: '11', |
---|
| 67 | 111: '12', |
---|
| 68 | 112: '13', |
---|
| 69 | 113: '14', |
---|
| 70 | 114: '15', |
---|
| 71 | 115: '16', |
---|
| 72 | 116: '17', |
---|
| 73 | 117: '18' |
---|
| 74 | }, |
---|
| 75 | SAPI1: { |
---|
| 76 | 10: 'a', |
---|
| 77 | 11: 'b', |
---|
| 78 | 12: 'c', |
---|
| 79 | 13: 'd' |
---|
| 80 | }, |
---|
| 81 | SVAG2: { |
---|
| 82 | 0: '' |
---|
| 83 | }, |
---|
| 84 | GROUPID: { |
---|
| 85 | 0: '' |
---|
| 86 | }, |
---|
| 87 | RESPONDENTID: { |
---|
| 88 | 0: '' |
---|
| 89 | } |
---|
| 90 | }; |
---|
| 91 | |
---|
| 92 | var fullcodeMap = {}; |
---|
| 93 | _.each(subcodeMap, function(subcodes,oldCode){ |
---|
| 94 | var newCode = oldCode in qcodeMap ? qcodeMap[oldCode] : oldCode; |
---|
| 95 | _.each(subcodes,function(newSubcode,oldSubcode){ |
---|
| 96 | fullcodeMap[oldCode+oldSubcode] = newCode+newSubcode; |
---|
| 97 | }); |
---|
| 98 | }); |
---|
| 99 | |
---|
| 100 | function rewriteQuestion(question) { |
---|
| 101 | if ( question.code in subcodeMap ) { |
---|
| 102 | rewriteContent(question.content,subcodeMap[question.code]); |
---|
| 103 | } |
---|
| 104 | if ( question.code in qcodeMap ) { |
---|
| 105 | question.code = qcodeMap[question.code]; |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | function rewriteContent(obj,map) { |
---|
| 110 | if ( _.isObject(obj) ) { |
---|
| 111 | if ( 'subcode' in obj && obj.subcode in map ) { |
---|
| 112 | obj.subcode = map[obj.subcode]; |
---|
| 113 | } |
---|
| 114 | _.each(obj,function(item){ |
---|
| 115 | rewriteContent(item,map); |
---|
| 116 | }); |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | function rewriteAnswers(answers) { |
---|
| 121 | _.each(answers,function(answer,code){ |
---|
| 122 | if ( code in fullcodeMap ) { |
---|
| 123 | delete answers[code]; |
---|
| 124 | answers[fullcodeMap[code]] = answer; |
---|
| 125 | } |
---|
| 126 | }); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | function rewriteDoc(doc) { |
---|
| 130 | switch(doc.type) { |
---|
| 131 | case 'SurveyRun': |
---|
| 132 | _.each(doc.survey.questions,rewriteQuestion); |
---|
| 133 | return doc; |
---|
| 134 | case 'Survey': |
---|
| 135 | _.each(doc.questions,rewriteQuestion); |
---|
| 136 | return doc; |
---|
| 137 | case 'Question': |
---|
| 138 | rewriteQuestion(doc); |
---|
| 139 | return doc; |
---|
| 140 | case 'Response': |
---|
| 141 | rewriteAnswers(doc.answers); |
---|
| 142 | return doc; |
---|
| 143 | default: |
---|
| 144 | return HTTPResult.fail(); |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | upgradeCouch(env.couchServerURL,env.dbName,rewriteDoc) |
---|
| 149 | .then(function(res){ |
---|
| 150 | console.log("done",res); |
---|
| 151 | }, function(err){ |
---|
| 152 | console.error("fail",err); |
---|
| 153 | }); |
---|