-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (76 loc) · 2.56 KB
/
index.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
// const counts = require("./data/counts.json");
// const collections = require("./data/collectionList.json").collections;
import fs from "fs";
import { exit } from "process";
// Async Arrow Function that reads data from file.
const readJsonFile = (filePath) => {
return new Promise((resolve, reject) => {
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
reject(err);
return;
}
const jsonData = JSON.parse(data);
resolve(jsonData);
});
});
};
const getMaxDifference = (storeCollectionMap) => {
let selectedCollection = "";
let maxDifference = -Infinity;
for (let [key, value] of storeCollectionMap) {
if (value.length >= 0 && value.length <= 2) {
const difference = Math.abs(value[0].counts - value[1].counts);
if (difference > maxDifference) {
maxDifference = difference;
selectedCollection = key;
}
}
}
return [selectedCollection, maxDifference];
};
// Creating auto Trigger function
(async () => {
// Read JSON Files Start
let counts = [];
let collections = [];
try {
counts = await readJsonFile("./data/counts.json", "utf8");
collections = (await readJsonFile("./data/collectionList.json", "utf8")).collections;
} catch (err) {
console.err("Unable to read Counts JSON");
exit(1);
} finally {
// Clean up here
}
// Read JSON Files End
// Get unique Store Id
const uniqueStoreIds = new Set(counts.map((each) => each["storeId:"]));
const result = [];
uniqueStoreIds.forEach((eachStoreId) => {
//Get Unique Collection for Each StoreId
const uniqueCollectionForStores = new Set();
counts.forEach((each) => (each["storeId:"] === eachStoreId ? uniqueCollectionForStores.add(each.collection) : ""));
//Create and populate store and collection Mapping
const storeCollectionMap = new Map();
uniqueCollectionForStores.forEach((eachUniqueCollection) => {
if (collections.includes(eachUniqueCollection)) {
storeCollectionMap.set(
eachUniqueCollection,
counts.filter((each) => each["storeId:"] === eachStoreId && each.collection === eachUniqueCollection)
);
}
});
// Get Max Difference from each Store Collection Map
const [selectedCollection, maxDifference] = getMaxDifference(storeCollectionMap);
if (selectedCollection) {
//Add the Finalized Collection with difference to results array
result.push({
storeId: eachStoreId,
collection: selectedCollection,
difference: maxDifference,
});
}
});
console.log(result);
})();