-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-data.js
60 lines (53 loc) · 2.32 KB
/
parse-data.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
const fs = require('fs');
const {
parseEventsData,
parseSkuProductMappingData,
formatEvents,
} = require('./helpers/data-process.js');
const { deleteFile } = require('./helpers/file-manipulation.js');
// We load into memory the csv file generated by dbeaver
const eventsData = fs.readFileSync('data/DB_ALL2.csv', { encoding: 'utf8', flag: 'r' });
// const eventsData = fs.readFileSync('data/STAGING.csv', { encoding: 'utf8', flag: 'r' });
const EVENT_TYPES_TO_KEEP_INBOUND = ['SKU_INBOUNDED', 'SKU_INCREMENTED', 'SKU_FOUND'];
const EVENT_TYPES_TO_KEEP_OUTBOUND = ['SKU_OUTBOUNDED', 'SKU_DECREMENTED', 'SKU_MISSING'];
// We parse the data into Objects
const events = parseEventsData(eventsData);
const WMSevents = events.filter((event) => event.source === 'WMS');
const relevantInboundEvents = WMSevents.filter((event) =>
EVENT_TYPES_TO_KEEP_INBOUND.includes(event.payload.type),
);
const relevantOutboundEvents = WMSevents.filter((event) =>
EVENT_TYPES_TO_KEEP_OUTBOUND.includes(event.payload.type),
);
// const skuMappingData = fs.readFileSync('data/STAGING_SKU_MAPPING.csv', {
const skuMappingData = fs.readFileSync('data/sku-mapping2.csv', {
encoding: 'utf8',
flag: 'r',
});
const skuMapping = parseSkuProductMappingData(skuMappingData);
// We format the events and keep only the relevant information
const { events: eventsInboundFormatted, results: resultsInbound } = formatEvents(
relevantInboundEvents,
skuMapping,
);
const { events: eventsOutBoundFormatted, results: resultsOutbound } = formatEvents(
relevantOutboundEvents,
skuMapping,
);
deleteFile('data/relevantInboundEvents.csv');
deleteFile('data/relevantOutboundEvents.csv');
// We save the events to a file
const eventsInboundWriter = fs.createWriteStream('data/relevantInboundEvents.csv', { flags: 'a' });
const eventsOutboundWriter = fs.createWriteStream('data/relevantOutboundEvents.csv', {
flags: 'a',
});
eventsInboundFormatted.forEach((event) => {
eventsInboundWriter.write(`${Object.values(event).join()}\n`);
});
eventsOutBoundFormatted.forEach((event) => {
eventsOutboundWriter.write(`${Object.values(event).join()}\n`);
});
console.log(`Inbound Events written to file: ${eventsInboundFormatted.length} events`);
console.log(resultsInbound);
console.log(`Outbound Events written to file: ${eventsOutBoundFormatted.length} events`);
console.log(resultsOutbound);