-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocessor.js
More file actions
342 lines (287 loc) · 10.7 KB
/
processor.js
File metadata and controls
342 lines (287 loc) · 10.7 KB
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
// const { userInfoRandomizer } = require('./randomizer')
const mongodb = require('mongodb')
const _ = require('lodash')
module.exports = {
process,
initialize,
}
// let applicationStateTypes = []
let operations = []
let operationStateTypes = []
// let userInfos = []
// let specialists = []
// let operationTypes = []
let target = null
async function initialize(data) {
const { source, target: initTarget } = data
target = initTarget
// do something like load static data to use in process method
// console.log('Load [applicationstatetype] from source')
// applicationStateTypes = await findIds(source, 'applicationstatetype')
// console.log('Load [specialists] from source')
// specialists = await source.collection('specialist').find().toArray()
// console.log('Load [userInfos] from source')
// userInfos = await source.collection('userinfo').find().toArray()
console.log('Load [operations] from source')
operations = await source.collection('operation').find().toArray()
console.log('Load [operationStateTypes] from source')
operationStateTypes = await source.collection('operationstatetype').find().toArray()
// console.log('Load [operationtype] from source')
// operationTypes = await source.collection('operationtype').find().toArray()
// console.log('Load [userInfos] from source')
// userInfos = await source.collection('userinfo').find().toArray()
}
// async function findAndPopulateState(client, collectionName) {
// let results = await client.collection(collectionName).find().toArray()
// console.log('results[0]', JSON.stringify(results[0]))
// results = results.map((item) => {
// if (item && item.state && operationStateTypes && operationStateTypes.length > 0) {
// const state = operationStateTypes.find((ost) => JSON.stringify(ost._id) === JSON.stringify(item._id))
// item.state = state
// }
// return item
// })
// return results
// }
function getId(obj) {
return mongodb.ObjectID.isValid(obj) ? obj : obj._id
}
function normalizeApplication(values = {}) {
const { candidates = [], operations = [], specialist = null, operationInfo = null, owners = [] } = values
values.specialist = specialist ? getId(specialist) : null
values.operationInfo = operationInfo ? getId(operationInfo) : null
values.owners = owners.map(user => (user ? getId(user) : null))
values.owners = values.owners.filter(user => user)
values.candidates = candidates.map(can => ({
operation: can.operation ? getId(can.operation) : null,
range: can.range,
}))
values.operations = operations.map(op => (op ? getId(op) : null))
values.operations = values.operations.filter(op => op)
return values
}
async function convertLocation(values = {}) {
const { atHome, ...rest } = values
return {
...rest,
location: atHome === false ? 'office' : 'home',
}
}
async function addStates(values = {}) {
const { _id } = values
let filteredOperations = operations.filter(op => JSON.stringify(op.info) === JSON.stringify(_id))
let states = []
filteredOperations.forEach(op => {
if (op.state) {
const state = operationStateTypes.find(ost => {
// if (JSON.stringify(ost._id) !== JSON.stringify(op.state)) {
// console.log('JSON.stringify(ost._id)', JSON.stringify(ost._id))
// console.log('JSON.stringify(op.state)', JSON.stringify(op.state))
// console.log('different', JSON.stringify(ost._id) === JSON.stringify(op.state))
// }
return JSON.stringify(ost._id) === JSON.stringify(op.state)
})
// states.push({
// id: op._id,
// state,
// })
if (!state) {
console.log('op.state', op.state)
} else {
states.push(state.code)
}
}
})
if (states.length > 0) {
states = states.filter(state => state)
values.operationStates = states.length > 1 ? states.join(',') : states[0]
}
return values
}
async function copyPricesWeekendToNight(values = {}) {
if (values.home && values.home.fares && values.home.fares.weekend && values.home.fares.weekend.price) {
values.home.fares.night = values.home.fares.weekend
}
if (
values.office &&
values.office.fares &&
values.office.fares.weekend &&
values.office.fares.weekend.price
) {
values.office.fares.night = values.office.fares.weekend
}
return values
}
function getContacts({ firstName = '', lastName = '', email = '', phone = '' }) {
return `${firstName} ${lastName} ${email} ${phone}`.trim()
}
async function addSpecialists(values = {}) {
const { _id } = values
let filteredOperations = operations.filter(op => JSON.stringify(op.info) === JSON.stringify(_id))
let opSpecialists = []
await Promise.all(
filteredOperations.map(async ost => {
const specialist = specialists.find(sp => JSON.stringify(sp._id) === JSON.stringify(ost.specialist))
if (specialist) {
const userInfo = userInfos.find(ui => JSON.stringify(ui._id) === JSON.stringify(specialist.userInfo))
if (userInfo) {
opSpecialists.push(getContacts(userInfo))
}
}
}),
)
if (opSpecialists.length > 0) {
opSpecialists = opSpecialists.filter(s => s)
values.operationSpecialists = opSpecialists.length > 1 ? opSpecialists.join(',') : opSpecialists[0]
}
}
// function normalizeApplication(values = {}) {
// const { candidates = [], operations = [], specialist = null, operationInfo = null, owners = [] } = values
// values.specialist = specialist ? getId(specialist) : null
// values.operationInfo = operationInfo ? getId(operationInfo) : null
// values.owners = owners.map(user => (user ? getId(user) : null))
// values.owners = values.owners.filter(user => user)
// values.candidates = candidates.map(can => ({
// operation: can.operation ? getId(can.operation) : null,
// range: can.range,
// }))
// values.operations = operations.map(op => (op ? getId(op) : null))
// values.operations = values.operations.filter(op => op)
// return values
// }
// async function addStates(values = {}) {
// const { _id } = values
// let filteredOperations = operations.filter(op => JSON.stringify(op.info) === JSON.stringify(_id))
// let states = []
// filteredOperations.forEach(op => {
// if (op.state) {
// const state = operationStateTypes.find(ost => JSON.stringify(ost._id) === JSON.stringify(op.state))
// // states.push({
// // id: op._id,
// // state,
// // })
// states.push(state.code)
// }
// })
// if (states.length > 0) {
// states = states.filter(state => state)
// values.operationStates = states.length > 1 ? states.join(',') : states[0]
// }
// return values
// }
// function getContacts({ firstName = '', lastName = '', email = '', phone = '' }) {
// return `${firstName} ${lastName} ${email} ${phone}`.trim()
// }
// async function addSpecialists(values = {}) {
// const { _id } = values
// let filteredOperations = operations.filter(op => JSON.stringify(op.info) === JSON.stringify(_id))
// let opSpecialists = []
// await Promise.all(
// filteredOperations.map(async ost => {
// const specialist = specialists.find(sp => JSON.stringify(sp._id) === JSON.stringify(ost.specialist))
// if (specialist) {
// const userInfo = userInfos.find(ui => JSON.stringify(ui._id) === JSON.stringify(specialist.userInfo))
// if (userInfo) {
// opSpecialists.push(getContacts(userInfo))
// }
// }
// }),
// )
// if (opSpecialists.length > 0) {
// opSpecialists = opSpecialists.filter(s => s)
// values.operationSpecialists = opSpecialists.length > 1 ? opSpecialists.join(',') : opSpecialists[0]
// }
// return values
// }
async function filterWithOperations(documents) {
return documents.filter(d => {
const ops = operations.find(op => {
if (op.info) {
return `${d._id}` === `${op.info}`
} else {
return false
}
})
return ops != null
})
}
async function process(collectionName, documents) {
// if (collectionName === 'operationtype') {
// documents = await Promise.all(
// documents.map(async sourceType => {
// const { _id, code } = sourceType
// // shit ..
// if (code == null) return null
// const targetType = await target.collection(collectionName).findOne({ _id })
// if (targetType) {
// const { _id, id, ...values } = sourceType
// if (!values.locations) {
// if (values.isStudioOnly) {
// values.locations = ['office']
// } else {
// values.locations = ['home', 'office']
// }
// }
// // if exists update the existing one..
// await target.collection(collectionName).update({ _id }, values)
// // ..and return a big null
// return null
// } else {
// // if not exists create a new type ..
// const { _id, id, ...values } = sourceType
// if (!values.locations) {
// if (values.isStudioOnly) {
// values.locations = ['office']
// } else {
// values.locations = ['home', 'office']
// }
// }
// await target.collection(collectionName).insertMany([values])
// // ..and return a big null
// return null
// }
// }),
// )
// // strip out all undefined/null values
// // documents = _.compact(documents)
// documents = []
// }
// if (collectionName === 'price') {
// documents = await Promise.all(documents.map(price => copyPricesWeekendToNight(price)))
// }
// if (collectionName === 'operationinfo') {
// documents = await Promise.all(documents.map(info => convertLocation(info)))
// }
if (collectionName === 'operationinfo') {
documents = await filterWithOperations(documents)
}
if (collectionName === 'operationinfo') {
documents = await Promise.all(documents.map(info => addStates(info)))
}
// if (collectionName === 'operationinfo') {
// documents = await Promise.all(documents.map(info => addSpecialists(info)))
// }
// if (collectionName === 'application') {
// documents = await documents.map((application) => normalizeApplication(application))
// }
// else if (collectionName === 'userinfo') {
// documents = await documents.map((userinfo) => {
// return userInfoRandomizer(userinfo)
// })
// }
// do something BUT remember to return documents
return documents
}
async function findIds(client, collectionName) {
const ids = []
await client
.collection(collectionName)
.find({})
.toArray()
.then(async documents => {
await documents.map(type => {
ids[type._id] = type
})
})
return ids
}