This repository has been archived by the owner on Nov 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
format-csv-config-examples.js
112 lines (106 loc) · 2.21 KB
/
format-csv-config-examples.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
var password = require('password-generator'),
csv = require('fast-csv'),
uuid = require('uuid');
var normalize = require('./lib/normalize'),
relatedData,
config;
var supervisorsConfig = {
columns: {
uuid: uuid,
name: {
use: 'Supervisor',
unique: true
}
}
};
var usersConfig = {
columns: {
name: {
use: 'Surname',
format: normalize.name
},
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
}
};
var usersConfigWithSupervisors = {
columns: {
name: {
use: 'Surname',
format: normalize.name
},
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 = './data/Supervisors-Masajja-Ver2.csv';
if (!relatedData) {
relatedData = {};
}
csv
.fromPath(file, {headers:true})
.on("data", function(obj) {
relatedData[obj.name] = obj.uuid;
})
.on("end", function() {
callback();
});
}
}
};
// set your config here
config = usersConfig;
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);
}
};