-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsync.js
74 lines (68 loc) · 1.94 KB
/
sync.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
const OngoingClient = require('./lib/ongoingClient');
const createFyndiq = require('./lib/fyndiqClient');
const createLogClient = require('./lib/slackClient');
const sync = async (
description,
fyndiqSettings,
ongoingSettings,
slackSettings,
) => {
const logger = createLogClient(slackSettings);
const fyndiq = createFyndiq(fyndiqSettings);
const ongoing = new OngoingClient(
ongoingSettings.apiUrl,
ongoingSettings.goodsOwnerId,
ongoingSettings.username,
ongoingSettings.password,
);
logger.log(description);
const [fyndiqOrders, ongoingOrders] = await Promise.all([
fyndiq.pendingOrders(),
ongoing.pendingOrders(),
]);
if (!fyndiqOrders.length) {
logger.log('There are no pending Fyndiq orders');
await logger.print();
return;
}
await Promise.all(
fyndiqOrders
.map(fo => ({
sid: `${fo.id}${fyndiqSettings.suffix}`.trim(),
...fo,
}))
.map(fo => {
const ongoingOrder = ongoingOrders.find(
oo => oo.orderNumber === fo.sid,
);
if (ongoingOrder) fo.ongoingId = ongoingOrder.orderId;
return fo;
})
.filter(fo => fo.ongoingId)
.map(order => {
const filename = `Fyndiq.${order.sid}.pdf`;
return ongoing.orderFiles(order.ongoingId).then(files => {
if (files.some(file => file.fileName === filename)) {
logger.log(`Order ${order.OrderId} HAS file ${fileName}`);
return Promise.resolve();
}
return fyndiq
.deliveryNote(order.id)
.then(pdfData =>
ongoing.uploadDeliveryNote(
order.ongoingId,
filename,
pdfData,
),
)
.then(() =>
logger.log(
`Uploaded ${fileName} to order ${order.OrderId}`,
),
);
});
}),
);
await logger.print();
};
module.exports = sync;