-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
executable file
·303 lines (278 loc) · 9.9 KB
/
index.ts
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
import { getFileStream } from "./helpers/file-helper";
import { AppStoreModel } from "models/Store";
import signale = require("signale");
import {
serviceInit,
getStores,
getStoreTaxonomies,
getProductsByTaxonomyId
} from "./service";
import { AppTaxonomyModel } from "models/Taxonomy";
const HAPPY_FRESH_BASE_URL =
"https://gvg1d6u3wk.execute-api.ap-southeast-1.amazonaws.com/prod";
type MenuOption = {
index: number;
desc: string;
status: "PENDING" | "DONE" | "ERROR" | "STARTED";
currentStep: number;
steps: number;
};
type LogType = {
parent: string;
message: string;
};
class AppLogger {
menu: Array<MenuOption> = [];
logs: Array<LogType> = [{ parent: "", message: "Started..." }];
concur: number = 1;
constructor(options: Array<string>, concur: number = 1) {
options.forEach((o, i) =>
this.menu.push({
index: i,
desc: o,
status: "PENDING",
currentStep: 0,
steps: 1
})
);
this.print();
this.concur = concur;
}
set(
desc: string,
status: "PENDING" | "DONE" | "ERROR" | "STARTED",
currentStep?: number,
totalSteps?: number
) {
// More than one can run at once.
if (this.concur === 1) {
this.menu = this.menu.map(item => {
if (item.desc === desc) {
return {
...item,
currentStep:
(status === "STARTED" && currentStep) || item.currentStep,
steps: (status === "STARTED" && totalSteps) || item.steps,
status: status
};
} else {
return item;
}
});
}
this.print();
}
log(parent: string, message: string) {
this.logs.push({ parent: parent, message: message });
this.print();
}
print() {
console.log(`\n\n`);
console.log(`\n\n`);
console.log(`\n\n`);
console.clear();
this.menu.forEach(m => {
this.printLog(m);
});
console.log(`\n\n`);
console.log("==================================");
this.logs.slice(this.logs.length - 10, this.logs.length).forEach(l => {
signale.log(`~> ${l.message}`);
});
console.log("==================================");
}
printLog(option: MenuOption) {
console.clear();
switch (option.status) {
case "PENDING":
signale.log(
`🌕 - ${option.desc}(${option.currentStep}/${option.steps})`
);
break;
case "DONE":
signale.log(
`✅ - ${option.desc}(${option.currentStep}/${option.steps})`
);
break;
case "ERROR":
signale.log(
`🔴 - ${option.desc}(${option.currentStep}/${option.steps})`
);
break;
case "STARTED":
signale.log(
`🔵 - ${option.desc}(${option.currentStep}/${option.steps})`
);
break;
}
}
}
async function getStoresCount(): Promise<Array<AppStoreModel>> {
const stores: Array<AppStoreModel> = await getStores().catch(() => {
return [];
});
// Save
// let tWrite = getFileStream(`stores`, "csv", false);
// fileWrite(
// tWrite,
// stores,
// v => {
// return `${v.id}; ${v.name}; ${v.address}; ${v.phone}; ${
// v.location.lat
// }; ${v.location.lon}`;
// },
// ["id", "name", "address", "phone", "lat", "lon"]
// );
// tWrite.close();
return stores;
}
async function getTaxonsForStore(
store: AppStoreModel
): Promise<Array<AppTaxonomyModel>> {
const taxons: Array<AppTaxonomyModel> = await getStoreTaxonomies(store.id);
return taxons;
}
async function initApp() {
// 1. Init
serviceInit(HAPPY_FRESH_BASE_URL, {
"X-Spree-Client-Token":
"0115f406e71219ec9ea58e2eaaa4480ef966bdc42e245ec4bf601b23f07bd48e",
"X-API-Key": "HdI3wa6E3L6ECd1XYZZjJ92d4wUGOD4X6CrtO6MM"
});
const logger = new AppLogger(
["Get Stores", "Get Store Taxonomies", "Get Products"],
1
);
// Step 1 - Get Stores
logger.set("Get Stores", "STARTED");
const stores = await getStoresCount();
logger.log("Get Stores", `Found ${stores.length} stores.`);
logger.set("Get Stores", "DONE", 1);
// Step 2 - Get taxonomy for each store
const storeWiseTaxonomies: {
[id: string]: {
store: AppStoreModel;
taxonomies: Array<AppTaxonomyModel>;
};
} = {};
logger.set("Get Store Taxonomies", "STARTED", 0, stores.length);
for (let storeIndex = 0; storeIndex < stores.length; storeIndex++) {
const store = stores[storeIndex];
const storeTaxons: Array<AppTaxonomyModel> = await getTaxonsForStore(store);
logger.log(
"Get Store Taxonomies",
`Found ${storeTaxons.length} taxonomies for ${store.name}(${store.id}).`
);
logger.set("Get Store Taxonomies", "STARTED", storeIndex + 1);
storeWiseTaxonomies[store.id] = {
store: store,
taxonomies: storeTaxons
};
// db.get("taxonomies")
// .push({
// store: store,
// taxonomies: storeTaxons
// })
// .write();
}
logger.set("Get Store Taxonomies", "DONE");
// if (true) {
// return;
// }
// Step 3 - Get products for each taxonomy in each store
const storeIds = Object.values(storeWiseTaxonomies);
let productsWriter = getFileStream(`products`, "csv", false);
productsWriter.write(
`Store Id@ Store Name@ Store Address@ Store Phone@ Store Lat@ Store Long@ Store Photo@ Taxon Id@ Taxon Name@ Taxon Description@ Product Id@ Product Name@ Product description@ Product supermarket_unit_cost_price@ Product display_supermarket_unit_cost_price@ Product normal_price@ Product display_normal_price@ Product price@ Product display_price@ Product unit_price@ Product display_unit_price@ Product display_promo_price_percentage@ Product display_promotion_actions_combination_text@ Product display_unit@ Product supermarket_unit@ Product natural_average_weight@ Product display_average_weight@ Product sku@ \n`
);
for (let sObj = 0; sObj < storeIds.length; sObj++) {
// Get single store with taxons
const storeTaxonObj = storeIds[sObj];
// Loop for all taxons
for (let stObj = 0; stObj < storeTaxonObj.taxonomies.length; stObj++) {
logger.set(
"Get Products",
"STARTED",
(sObj + 1) * stObj + 1,
storeTaxonObj.taxonomies.length * storeIds.length
);
// Single taxon
const taxonObj = storeTaxonObj.taxonomies[stObj];
const products = await getProductsByTaxonomyId(
storeTaxonObj.store.id,
taxonObj.id
);
logger.log(
"Get Products",
`Found ${products.length} products in ${taxonObj.name} of ${
storeTaxonObj.store.name
} store`
);
products.forEach(product => {
// db.get("products")
// .push({
// "Store Id": storeTaxonObj.store.id || "-",
// "Store Name": storeTaxonObj.store.name || "-",
// "Store Address": storeTaxonObj.store.address || "-",
// "Store Phone": storeTaxonObj.store.phone || "-",
// "Store Lat": storeTaxonObj.store.location.lat || "-",
// "Store Long": storeTaxonObj.store.location.lon || "-",
// "Store Photo": storeTaxonObj.store.photo || "-",
// "Taxon Id": taxonObj.id || "-",
// "Taxon Name": taxonObj.name || "-",
// "Taxon Description": taxonObj.description || "-",
// "Product Id": product.id || "-",
// "Product Name": product.name || "-",
// "Product description": product.description || "-",
// "Product supermarket_unit_cost_price":
// product.supermarket_unit_cost_price || "-",
// "Product display_supermarket_unit_cost_price":
// product.display_supermarket_unit_cost_price || "-",
// "Product normal_price": product.normal_price || "-",
// "Product display_normal_price": product.display_normal_price || "-",
// "Product price": product.price || "-",
// "Product display_price": product.display_price || "-",
// "Product unit_price": product.unit_price || "-",
// "Product display_unit_price": product.display_unit_price || "-",
// "Product display_promo_price_percentage":
// product.display_promo_price_percentage || "-",
// "Product display_promotion_actions_combination_text":
// product.display_promotion_actions_combination_text || "-",
// "Product display_unit": product.display_unit || "-",
// "Product supermarket_unit": product.supermarket_unit || "-",
// "Product natural_average_weight":
// product.natural_average_weight || "-",
// "Product display_average_weight":
// product.display_average_weight || "-",
// "Product sku": product.sku || "-"
// })
// .write();
productsWriter.write(
`${storeTaxonObj.store.id}@ ${storeTaxonObj.store.name}@ ${
storeTaxonObj.store.address
}@ ${storeTaxonObj.store.phone}@ ${
storeTaxonObj.store.location.lat
}@ ${storeTaxonObj.store.location.lon}@ ${
storeTaxonObj.store.photo
}@ ${taxonObj.id}@ ${taxonObj.name}@ ${taxonObj.description}@ ${
product.id
}@ ${product.name}@ ${product.description}@ ${
product.supermarket_unit_cost_price
}@ ${product.display_supermarket_unit_cost_price}@ ${
product.normal_price
}@ ${product.display_normal_price}@ ${product.price}@ ${
product.display_price
}@ ${product.unit_price}@ ${product.display_unit_price}@ ${
product.display_promo_price_percentage
}@ ${product.display_promotion_actions_combination_text}@ ${
product.display_unit
}@ ${product.supermarket_unit}@ ${product.natural_average_weight}@ ${
product.display_average_weight
}@ ${product.sku}@ `.replace(new RegExp("\r?\n", "g"), "<br />") +
`\n`
);
});
}
}
}
initApp();