-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaac.js
215 lines (176 loc) · 8.75 KB
/
gaac.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const utils = require('./utils');
const logTime = utils.logTime;
const strValue = utils.stringValue;
const getCount = utils.getCount;
const moveAllTableRecords = utils.moveAllTableRecords;
const consolidateRecords = utils.consolidateRecords;
let beehive = global.beehive;
beehive.gaacMap = new Map();
beehive.gaacAffinityTypeMap = new Map();
beehive.gaacReasonLeavingTypeMap = new Map();
function _prepareGaacTypeInsertTemplate(rows, nextId, type, typeMap) {
let insert = `INSERT INTO gaac_${type}_type(gaac_${type}_type_id, name, ` +
'description, creator, date_created, retired, retired_by,' +
'date_retired, retire_reason, uuid) VALUES ';
let toBeinserted = '';
rows.forEach(row => {
if (toBeinserted.length > 1) {
toBeinserted += ',';
}
let retiredBy = row['retired_by'] === null ? null : beehive.userMap.get(row['retired_by']);
typeMap.set(row[`gaac_${type}_type_id`], nextId);
toBeinserted += `(${nextId}, ${strValue(row['name'])}, `
+ `${strValue(row['description'])}, `
+ `${beehive.userMap.get(row['creator'])}, `
+ `${strValue(utils.formatDate(row['date_created']))}, `
+ `${row['retired']}, ${retiredBy}, `
+ `${strValue(utils.formatDate(row['date_retired']))}, `
+ `${strValue(row['retire_reason'])}, ${utils.uuid(row['uuid'])})`;
nextId++;
});
let query = insert + toBeinserted;
return [query, nextId];
}
function prepareGaacAffinityTypeInsert(rows, nextId) {
return _prepareGaacTypeInsertTemplate(rows, nextId, 'affinity',
beehive.gaacAffinityTypeMap);
}
function prepareGaacReasonLeavingTypeInsert(rows, nextId) {
return _prepareGaacTypeInsertTemplate(rows, nextId, 'reason_leaving',
beehive.gaacReasonLeavingTypeMap);
}
function prepareGaacInsert(rows, nextId) {
let insert = 'INSERT INTO gaac(gaac_id, name, description, ' +
'gaac_identifier, start_date, end_date, focal_patient_id, ' +
'affinity_type, location_id, crumbled, reason_crumbled, ' +
'date_crumbled, creator, date_created, changed_by, date_changed, ' +
'voided, voided_by, date_voided, void_reason, uuid) VALUES ';
let toBeinserted = '';
rows.forEach(row => {
if (toBeinserted.length > 1) {
toBeinserted += ',';
}
let voidedBy = (row['voided_by'] === null ? null :
beehive.userMap.get(row['voided_by']));
let changedBy = (row['changed_by'] === null ? null :
beehive.userMap.get(row['changed_by']));
let locationId = (row['location_id'] === null ? null :
beehive.locationMap.get(row['location_id']));
let affinityType = (row['affinity_type'] === null ? null :
beehive.gaacAffinityTypeMap.get(row['affinity_type']));
let focalPatientId = (row['focal_patient_id'] === null ? null :
beehive.personMap.get(row['focal_patient_id']));
beehive.gaacMap.set(row['gaac_id'], nextId);
toBeinserted += `(${nextId}, ${strValue(row['name'])}, ` +
`${strValue(row['description'])}, ${strValue(row['gaac_identifier'])}, ` +
`${strValue(utils.formatDate(row['start_date']))}, ` +
`${strValue(utils.formatDate(row['end_date']))}, ` +
`${focalPatientId}, ${affinityType}, ${locationId}, ` +
`${row['crumbled']}, ${strValue(row['reason_crumbled'])}, ` +
`${strValue(utils.formatDate(row['date_crumbled']))}, ` +
`${beehive.userMap.get(row['creator'])}, ` +
`${strValue(utils.formatDate(row['date_created']))}, ` +
`${changedBy}, ${strValue(utils.formatDate(row['date_changed']))}, ` +
`${row['voided']}, ${voidedBy}, ` +
`${strValue(utils.formatDate(row['date_voided']))}, ` +
`${strValue(row['void_reason'])}, ${utils.uuid(row['uuid'])})`;
nextId++;
});
let query = insert + toBeinserted;
return [query, nextId];
}
function prepareGaacMemberInsert(rows) {
let insert = 'INSERT INTO gaac_member(gaac_id, member_id, start_date, ' +
'end_date, reason_leaving_type, description, leaving, restart, ' +
'restart_date, creator, date_created, changed_by, date_changed, ' +
'voided, voided_by, date_voided, void_reason, uuid) VALUES ';
let toBeinserted = '';
rows.forEach(row => {
if (toBeinserted.length > 1) {
toBeinserted += ',';
}
let voidedBy = (row['voided_by'] === null ? null :
beehive.userMap.get(row['voided_by']));
let changedBy = (row['changed_by'] === null ? null :
beehive.userMap.get(row['changed_by']));
let reasonLeavingType = (row['reason_leaving_type'] === null ? null :
beehive.gaacReasonLeavingTypeMap.get(row['reason_leaving_type']));
toBeinserted += `(${beehive.gaacMap.get(row['gaac_id'])}, ` +
`${beehive.personMap.get(row['member_id'])}, ` +
`${strValue(utils.formatDate(row['start_date']))}, ` +
`${strValue(utils.formatDate(row['end_date']))}, ${reasonLeavingType}, ` +
`${strValue(row['description'])}, ${row['leaving']}, ${row['restart']}, ` +
`${strValue(utils.formatDate(row['restart_date']))}, ` +
`${beehive.userMap.get(row['creator'])}, ` +
`${strValue(utils.formatDate(row['date_created']))}, ` +
`${changedBy}, ${strValue(utils.formatDate(row['date_changed']))}, ` +
`${row['voided']}, ${voidedBy}, ` +
`${strValue(utils.formatDate(row['date_voided']))}, ` +
`${strValue(row['void_reason'])}, ${utils.uuid(row['uuid'])})`;
});
let query = insert + toBeinserted;
return [query, -1];
}
async function consolidateGaacAffinityTypes(srcConn, destConn) {
return await consolidateRecords(srcConn, destConn, 'gaac_affinity_type',
'name', 'gaac_affinity_type_id', beehive.gaacAffinityTypeMap,
prepareGaacAffinityTypeInsert);
}
async function consolidateGaacReasonLeavingTypes(srcConn, destConn) {
return await consolidateRecords(srcConn, destConn, 'gaac_reason_leaving_type',
'name', 'gaac_reason_leaving_type_id', beehive.gaacReasonLeavingTypeMap,
prepareGaacReasonLeavingTypeInsert);
}
async function moveGaacs(srcConn, destConn) {
return await moveAllTableRecords(srcConn, destConn, 'gaac', 'gaac_id',
prepareGaacInsert);
}
async function moveGaacMembers(srcConn, destConn) {
return await moveAllTableRecords(srcConn, destConn, 'gaac_member',
'gaac_member_id', prepareGaacMemberInsert);
}
async function main(srcConn, destConn) {
utils.logInfo('Moving GAAC module tables');
utils.logInfo('Checking if gaac module tables exists in source');
let [r] = await srcConn.query(`SHOW TABLES LIKE 'gaac%'`);
if(r.length === 0) {
utils.logInfo('No gaac tables found');
return;
}
utils.logInfo('Consolidating GAAC Affinity types...');
let moved = await consolidateGaacAffinityTypes(srcConn, destConn);
utils.logOk(`Ok...${moved} records from gaac_affinity_type moved`);
utils.logInfo('Consolidating GAAC Reason for leaving types...');
moved = await consolidateGaacReasonLeavingTypes(srcConn, destConn);
utils.logOk(`Ok...${moved} records from gaac_reason_leaving_type moved`);
utils.logInfo('Moving Gaacs...');
let iDestCount = await utils.getCount(destConn, 'gaac');
moved = await moveGaacs(srcConn, destConn);
let fDestCount = await utils.getCount(destConn, 'gaac');
let expectedFinal = iDestCount + moved;
if(fDestCount === expectedFinal) {
utils.logOk(`OK... ${moved} gaac records moved.`);
utils.logInfo('Moving gaac_member records...');
iDestCount = await getCount(destConn, 'gaac_member');
moved = await moveGaacMembers(srcConn, destConn);
fDestCount = await getCount(destConn, 'gaac_member');
expectedFinal = iDestCount + moved;
if (fDestCount === expectedFinal) {
utils.logOk(`OK... ${moved} gaac_member records moved.`);
} else {
let message = 'There is a problem in moving gaac_member records, ' +
'the final expected ' +
`count (${expectedFinal}) does not equal the actual final ` +
`count (${fDestCount})`;
throw new Error(message);
}
}
else {
let message = 'There is a problem in moving gaac records, ' +
'the final expected ' +
`count (${expectedFinal}) does not equal the actual final ` +
`count (${fDestCount})`;
throw new Error(message);
}
}
module.exports = main;