-
Notifications
You must be signed in to change notification settings - Fork 6
/
populate.js
351 lines (325 loc) · 10.9 KB
/
populate.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
'use strict';
const independentModelFactory = require('./seeds/independent-model-factory');
const dependentModelFactory = require('./seeds/dependent-model-factory');
const userSeeder = require('./seeds/seed-user');
const facilityDependantsMapper = require('./seeds/facility-dependants-mapper');
const server = require('./server/server');
const data = require('./seeds/data');
const faker = require('faker');
const dataSource = server.dataSources.db;
const fs = require('fs');
const csvtojson = require('csvtojson');
const _ = require('lodash');
const loadingSpinner = require('loading-spinner');
const moment = require('moment');
//TODO: implement the factory pattern on this
const getEntityId = async (entities, key, value) => {
return faker.random.arrayElement(entities.map((entity) => entity.id));
};
const getFacilityTypeId = async (facilityTypes, facilityTypeName) => {
if (facilityTypeName) {
const facilityType = facilityTypes.filter(
(facilityType) => facilityType.facility_type == facilityTypeName,
)[0];
if (facilityType) {
return facilityType.id;
}
}
return faker.random.arrayElement(
facilityTypes.map((facilityType) => facilityType.id),
);
};
const getFacilityOwnerId = async (facilityOwners, facilityOwnerName) => {
if (facilityOwnerName) {
const facilityOwner = facilityOwners.filter(
(facilityOwner) => facilityOwner.facility_owner == facilityOwnerName,
)[0];
if (facilityOwner) {
return facilityOwner.id;
}
}
return faker.random.arrayElement(
facilityOwners.map((facilityOwner) => facilityOwner.id),
);
};
const getDistrictId = async (districts, districtName) => {
if (districtName) {
const district = districts.filter(
(district) => district.district_name == districtName,
)[0];
if (district) {
return district.id;
}
}
return faker.random.arrayElement(districts.map((district) => district.id));
};
const formatFacility = async (facility, requiredModels) => {
const facilityTypeId = await getFacilityTypeId(
requiredModels.facilityTypes,
facility['Facility Type'],
);
const facilityOwnerId = await getFacilityOwnerId(
requiredModels.owners,
facility['Facility Ownership'],
);
const districtId = await getDistrictId(
requiredModels.districts,
facility['District'],
);
const regulatoryStatusId = await getEntityId(
requiredModels.regulatoryStatuses,
null,
null,
);
const operationalStatusId = await getEntityId(
requiredModels.operationalStatuses,
null,
null,
);
return {
facility_name: facility['Facility Name']
? facility['Facility Name']
: faker.name.findName(),
facility_type_id: facilityTypeId,
facility_owner_id: facilityOwnerId,
facility_code_dhis2:
facility['DHIS2 CODE'] == null ? null : '' + facility['DHIS2 CODE'],
common_name: faker.company.companyName(),
client_id: faker.random.arrayElement(requiredModels.clientIds),
facility_operational_status_id: operationalStatusId,
facility_regulatory_status_id: regulatoryStatusId,
district_id: districtId,
facility_date_opened: new Date('1970-01-01'),
published_date: moment().format('YYYY-MM-DD'),
};
};
const populateIndependentModels = async (facilities) => {
const facilityTypes = _.uniq(
facilities.map((facility) => facility['Facility Type']),
).map((facilityType) => ({
facility_type: facilityType ? facilityType : faker.lorem.word(),
}));
const owners = _.uniq(
facilities.map((facility) => facility['Facility Ownership']),
).map((owner) => ({
facility_owner: owner ? owner : faker.company.companyName(),
}));
const districts = _.uniq(
facilities.map((facility) => facility['District']),
).map((district) => ({
district_name: district ? district : faker.address.state(),
zone_id: 1,
}));
await independentModelFactory(server.models.FacilityType, facilityTypes);
await independentModelFactory(server.models.FeedbackType, data.feedbackTypes);
await independentModelFactory(server.models.Owner, owners);
await independentModelFactory(server.models.District, districts);
await independentModelFactory(
server.models.RegulatoryStatus,
data.regulatoryStatuses,
);
await independentModelFactory(
server.models.OperationalStatus,
data.operationalStatuses,
);
await independentModelFactory(server.models.ResourceType, data.resourceTypes);
await independentModelFactory(server.models.UtilityType, data.utilityTypes);
await independentModelFactory(server.models.ServiceType, data.serviceTypes);
};
const populateDependentModels = async () => {
await dependentModelFactory(
server.models.ResourceType,
server.models.Resource,
data.resources,
);
await dependentModelFactory(
server.models.UtilityType,
server.models.Utility,
data.utilities,
);
await dependentModelFactory(
server.models.ServiceType,
server.models.Service,
data.services,
);
};
const mapDistrictsToZones = async () => {
const organisationUnits = JSON.parse(
await fs.readFileSync('dhis2-organisation-units.json', 'utf8'),
).organisationUnits.filter(
(organisationUnit) => organisationUnit.name != 'Central Hospital',
);
await server.models.Zone.deleteAll();
const zones = await server.models.Zone.create(
organisationUnits.map((organisationUnit) => ({
zone_name: organisationUnit.name,
})),
);
const mhfrDistricts = await server.models.District.find();
const dhis2DistrictsPlusZones = _.uniqBy(
_.flatten(
organisationUnits.map((organisationUnit) => {
return organisationUnit.children.map((child) => {
const districtNameSegemented = child.name.split('-');
const districtName =
districtNameSegemented.length > 1
? districtNameSegemented.slice(0, -1).join(' ')
: districtNameSegemented[0];
return {
district_name: districtName,
zone_name: organisationUnit.name,
};
});
}),
),
'district_name',
);
const mhfrDistrictsWithZoneIds = _.flatten(
mhfrDistricts.map((mhfrDistrict) => {
return dhis2DistrictsPlusZones.map((dhi2DistrictPlusZone) => {
if (dhi2DistrictPlusZone.district_name == mhfrDistrict.district_name) {
return {
id: mhfrDistrict.id,
zone_id: zones.filter(
(zone) => zone.zone_name == dhi2DistrictPlusZone.zone_name,
)[0].id,
};
}
});
}),
).filter((mhfrDistrictWithZoneId) => mhfrDistrictWithZoneId);
const districtZoneMapping = data.districtZoneMapping;
const districtCodes = _.flatten(
Object.keys(districtZoneMapping).map((key) => {
return districtZoneMapping[key];
}),
);
for (const mhfrDistrictWithZoneId of mhfrDistrictsWithZoneIds) {
const district = await server.models.District.findOne({
where: { id: mhfrDistrictWithZoneId.id },
});
const districtCode =
districtCodes.filter(
(districtCode) =>
_.trim(_.lowerCase(district.district_name)) ==
_.trim(_.lowerCase(districtCode.district_name)),
)[0].district_code || '';
if (district) {
await district.updateAttributes({
zone_id: mhfrDistrictWithZoneId.zone_id,
district_code: districtCode,
});
await district.save();
}
}
await console.log('Mapped Districts to Zones successfully');
};
const loadFromFile = async () => {
const facilityData = await fs.readFileSync(
'health-facilities-v2.csv',
'utf8',
);
const facilities = _.uniqBy(
await csvtojson().fromString(facilityData),
'Facility Name',
).filter(
(facility) =>
facility['Facility Name'] &&
facility['Facility Type'] != 'Village Clinic',
);
const facs = [];
const unwantedCodes = [0, -1, -2, -3];
const dhis2ExistingCodes = [];
for (const facility of facilities) {
if (
unwantedCodes.includes(Number(facility['DHIS2 CODE'])) ||
dhis2ExistingCodes.includes(facility['DHIS2 CODE'])
) {
facility['DHIS2 CODE'] = null;
}
dhis2ExistingCodes.push(facility['DHIS2 CODE']);
facs.push(facility);
}
return facs;
};
const populate = async (facilities) => {
try {
await loadingSpinner.start(100, {
clearChar: true,
});
const uniqueFacilities = await facilities;
const facilitiesNameWithGeocodes = uniqueFacilities.map((facility) => ({
facilityName: facility['Facility Name'],
latitude: facility['LATITUDE'],
longitude: facility['LONGITUDE'],
}));
await populateIndependentModels(uniqueFacilities);
await populateDependentModels();
await mapDistrictsToZones();
await userSeeder(data.users);
const requiredModels = {
facilityTypes: await server.models.FacilityType.find(),
owners: await server.models.Owner.find(),
districts: await server.models.District.find(),
operationalStatuses: await server.models.OperationalStatus.find(),
regulatoryStatuses: await server.models.RegulatoryStatus.find(),
clientIds: (await server.models.Client.find()).map(
(client) => client['id'],
),
};
await server.models.Facility.deleteAll();
const formattedFacilities = uniqueFacilities.map(
async (facility) => await formatFacility(facility, requiredModels),
);
await console.log('Populating facilities');
const awaitedFormattedFacilities = await Promise.all(formattedFacilities);
const savedFacilities = [];
for (const awaitedFormattedFacility of awaitedFormattedFacilities) {
savedFacilities.push(
await server.models.Facility.create(awaitedFormattedFacility),
);
}
// const savedFacilities = await server.models.Facility.create(
// await Promise.all(formattedFacilities)
// );
// console.log(savedFacilities);
await console.log('Facilities populated');
await console.log('Populating facility dependants');
await facilityDependantsMapper();
await console.log('Populating facility geodata');
const facilityGeocodeDataRaw = await facilitiesNameWithGeocodes.map(
(facilityWithGeocodes) => {
return savedFacilities.map((facility) => {
if (
facilityWithGeocodes['facilityName'] == facility['facility_name']
) {
const { latitude, longitude } = facilityWithGeocodes;
return {
facility_id: facility.id,
latitude,
longitude,
};
}
});
},
);
const facilityGeocodeData = _.flatten(facilityGeocodeDataRaw).filter(
(fgd) => fgd,
);
await independentModelFactory(
server.models.Geolocation,
facilityGeocodeData,
);
await console.log('Facility geodata loaded');
await console.log('Done populating the MFHR');
await dataSource.disconnect();
await loadingSpinner.stop();
return;
} catch (error) {
console.log(error);
}
};
module.exports = populate;
if (process.env.NODE_ENV != 'testing') {
populate(loadFromFile());
}