-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisit.js
133 lines (107 loc) · 4.79 KB
/
visit.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
let utils = require('./utils');
let strValue = utils.stringValue;
let moveAllTableRecords = utils.moveAllTableRecords;
let beehive = global.beehive;
function prepareVisitTypeInsert(rows, nextId) {
let insert = 'INSERT INTO visit_type(visit_type_id, name, description, '
+ 'creator, date_created, changed_by, date_changed, 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']);
let changedBy = row['changed_by'] === null ? null : beehive.userMap.get(row['changed_by']);
beehive.visitTypeMap.set(row['visit_type_id'], nextId);
toBeinserted += `(${nextId}, ${strValue(row['name'])}, `
+ `${strValue(row['description'])}, `
+ `${beehive.userMap.get(row['creator'])}, `
+ `${strValue(utils.formatDate(row['date_created']))}, ${changedBy}, `
+ `${strValue(utils.formatDate(row['date_changed']))}, `
+ `${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 prepareVisitInsert(rows, nextId) {
let insert = 'INSERT INTO visit(visit_id, patient_id, visit_type_id, '
+ 'date_started, date_stopped, indication_concept_id, location_id, '
+ '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']);
beehive.visitMap.set(row['visit_id'], nextId);
toBeinserted += `(${nextId}, ${beehive.personMap.get(row['patient_id'])}, `
+ `${beehive.visitTypeMap.get(row['visit_type_id'])}, `
+ `${strValue(utils.formatDate(row['date_started']))}, `
+ `${strValue(utils.formatDate(row['date_stopped']))}, `
+ `${row['indication_concept_id']}, ${beehive.locationMap.get(row['location_id'])}, `
+ `${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 insertStatement = insert + toBeinserted;
return [insertStatement, nextId];
}
async function consolidateVisitTypes(srcConn, destConn) {
let query = 'SELECT * FROM visit_type';
let [srcVisitTypes] = await srcConn.query(query);
let [destVisitTypes] = await destConn.query(query);
let missingInDest = [];
srcVisitTypes.forEach(srcVisitType => {
let match = destVisitTypes.find(destVisitType => {
return srcVisitType['name'] === destVisitType['name'];
});
if(match !== undefined && match !== null) {
beehive.visitTypeMap.set(srcVisitType['visit_type_id'],
match['visit_type_id']);
}
else {
missingInDest.push(srcVisitType);
}
});
if(missingInDest.length > 0) {
let nextVisitTypeId =
await utils.getNextAutoIncrementId(destConn, 'visit_type');
let [sql] = prepareVisitTypeInsert(missingInDest, nextVisitTypeId);
utils.logDebug('visit_type insert statement:\n', sql);
let [result] = await destConn.query(sql);
}
}
async function moveVisits(srcConn, destConn) {
return await moveAllTableRecords(srcConn, destConn, 'visit', 'visit_id',
prepareVisitInsert);
}
async function main(srcConn, destConn) {
utils.logInfo('Consolidating visit types...');
await consolidateVisitTypes(srcConn, destConn);
utils.logOk('Ok...');
let srcVisitCount = await utils.getCount(srcConn, 'visit');
let initialDestCount = await utils.getCount(destConn, 'visit');
utils.logInfo('Moving visits...');
let moved = await moveVisits(srcConn, destConn);
let finalDestCount = await utils.getCount(destConn, 'visit');
let expectedFinalCount = initialDestCount + srcVisitCount;
if(finalDestCount === expectedFinalCount) {
utils.logOk(`Ok... ${moved}`);
}
else {
let error = `Problem moving visits: the actual final count ` +
`(${expectedFinalCount}) is not equal to the expected value ` +
`(${finalDestCount})`;
throw new Error(error);
}
}
module.exports = main;