forked from medic/medic-bulk-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format-csv-config.js
74 lines (71 loc) · 1.68 KB
/
format-csv-config.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
var normalize = require('medic-bulk-utils').normalize,
password = require('password-generator'),
csv = require('fast-csv'),
uuid = require('uuid'),
path = require('path'),
relatedData,
config;
var usersConfigWithSupervisors = {
columns: {
name: {
use: 'Surname',
format: [normalize.name, {reverse: true}]
},
phone: {
use: 'Chp_Phone',
format: [normalize.phone, '+256']
},
external_id: 'ChpCode',
supervisor: {
use: 'Supervisor',
optional: true
},
username: {
format: function() {
return normalize.username(this.name);
},
unique: true
},
password: password,
"contact.uuid": uuid,
"place.uuid": uuid,
"place.supervisor": function() {
var key = this.supervisor.trim();
if (!key) return;
if (!relatedData[key]) {
console.error(this);
throw new Error('Supervisor key not found: ' + key);
}
return relatedData[key];
}
},
related: {
load: function(callback) {
var file = [__dirname, 'supervisors', 'new-sups.csv'].join(path.sep);
if (!relatedData) {
relatedData = {};
}
csv
.fromPath(file, {headers:true})
.on("data", function(obj) {
relatedData[obj.name] = obj.uuid;
})
.on("end", function() {
callback();
});
}
}
};
config = usersConfigWithSupervisors;
module.exports = function(callback) {
if (config.related && config.related.load) {
config.related.load(function(err) {
if (err) {
return console.error(err);
}
callback(null, config);
});
} else {
callback(null, config);
}
};