-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparseAndUpdate.ts
210 lines (171 loc) · 8.91 KB
/
parseAndUpdate.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
// This file only handle the parsing process and the adding new records of flyer and discount information of the products
// ** Pending add retry function with preset max no. of retries and retryInterval
import * as fetchType from '../fetching/fetchingType';
import * as parse from '../parsing/parsing';
import * as process from '../services/service';
import Logging from '../../logging/logging';
async function parseAndUpdateProduct(grocery_id: number, flyer_id: number, productdetail:fetchType.ProductDetails):Promise<boolean>{
try{
// Parse fetched product detail object and ready to add new record
const productParsed = await parse.parseProduct(productdetail); // ** retry
const priceHistoryParsed = await parse.parsePriceHistory(productdetail); // ** retry
const pointHistoryParsed = await parse.parsePointHistory(productdetail); // ** retry
// Return if product information is not ready from parsing
if(!productParsed){
const msg = `Fail to parse product details from flyer: ${flyer_id} of product imported code: ${productdetail.code}`;
Logging.error(msg);
return false;
}
// Return if product information is not exist or added to database as unable to further process price / point data
const productID = await process.processProduct(grocery_id,productParsed); // ** retry
if(!productID){
const msg = `Fail to process product details from flyer: ${flyer_id} of product imported code: ${productdetail.code}`;
Logging.error(msg);
return false;
}
// Add new price record if not existed
if(priceHistoryParsed){
const priceHistoryID = await process.processPriceHistory(productID, flyer_id,priceHistoryParsed); // ** retry
if(!priceHistoryID){
const msg = `Fail to process price details from flyer: ${flyer_id} of product: ${productID}`;
Logging.error(msg);
}
}
// Add new point record if not existed
if(pointHistoryParsed){
const pointHistoryID = await process.processPointHistory(productID, flyer_id, pointHistoryParsed); // ** retry
if(!pointHistoryID){
const msg = `Fail to process point details from flyer: ${flyer_id} of product: ${productID}`;
Logging.error(msg);
}
}
return true;
} catch(error){
// Throw new error to parseAndUpdate() to stop operation
const msg = `Fail to parse and update product and related details from flyer ${flyer_id} grocery: ${grocery_id}}`;
Logging.error(msg);
return false;
}
}
export async function parseAndUpdate(grocery_id: number, flyerFetched: fetchType.Flyer, productDetailsList:fetchType.ProductDetails[]):Promise<boolean>{
try{
// Parse flyer to get content for adding new flyer record, return false if failed
const flyerParsed = await parse.parseFlyer(flyerFetched);
if(!flyerParsed){
const msg = `Parse flyer data failed: ${flyerFetched.id} grocery: ${grocery_id}`;
Logging.error(msg);
return false;
}
// Check if flyer has not yet saved will add new flyer record, return false if failed
const flyer = await process.processFlyer(grocery_id, flyerParsed);
if(!flyer.flyerID){
const msg = `Process flyer failed: ${flyerFetched.id} grocery: ${grocery_id}`;
Logging.error(msg);
return false;
}
// Update Flyer-Store information (for database reference purpose)
await process.processFlyerStore(grocery_id, flyer.flyerID, flyerFetched.storeId[0]);
// Return if the flyer exists in database already
if(!flyer.isNew){
const msg = `Flyer already existed: ${flyerFetched.id} grocery: ${grocery_id}`;
Logging.error(msg);
return false;
}
// Problem: if operation stops after processed flyer but not yet update product, price and point details?
// or if flyer parsed and update successfully, but none of the price and point record can be add?
// Add updated flag in flyer table? only change to true when 1 flyer cron job completed
let allProductFailed:boolean = true;
for (const productDetail of productDetailsList){
// Parse all product and related information of the flyer and then add new records to database
const updated = await parseAndUpdateProduct(grocery_id,flyer.flyerID, productDetail);
if (!updated){
// Log down failed to parse/update product
const msg = `Fail to update data of product ${productDetail.code} flyer: ${flyer.flyerID}`;
Logging.error(msg);
} else{
allProductFailed = false;
}
}
// Log down flyer information if all product cannot be parse and update
if (allProductFailed){
const msg = `Fail to update product and related data of all flyers ${flyer.flyerID}`;
Logging.error(msg);
return false;
}
return true;
} catch(error){
const msg =`Fail to parse and update flyer : ${flyerFetched.id} grocery: ${grocery_id} - `;
Logging.error(msg);
return false;
}
}
/*
async function parseAndUpdateProduct(grocery_id: number, flyer_id: number, productdetail:fetchType.ProductDetails):Promise<boolean>{
try{
// Parse fetched product detail object and ready to add new record
const productParsed = await parse.parseProduct(productdetail); // ** retry
const priceHistoryParsed = await parse.parsePriceHistory(productdetail); // ** retry
const pointHistoryParsed = await parse.parsePointHistory(productdetail); // ** retry
// Return if product information is not ready from parsing
if(!productParsed){
const msg = `Fail to parse product details from flyer: ${flyer_id} of product imported code: ${productdetail.code}`;
Logging.error(msg);
return false;
}
// Return if product information is not exist or added to database as unable to further process price / point data
const productID = await process.processProduct(grocery_id,productParsed); // ** retry
if(!productID){
const msg = `Fail to process product details from flyer: ${flyer_id} of product imported code: ${productdetail.code}`;
Logging.error(msg);
return false;
}
// Add new price record if not existed
if(priceHistoryParsed){
const priceHistoryID = await process.processPriceHistory(productID, flyer_id,priceHistoryParsed); // ** retry
if(!priceHistoryID){
const msg = `Fail to process price details from flyer: ${flyer_id} of product: ${productID}`;
Logging.error(msg);
}
}
// Add new point record if not existed
if(pointHistoryParsed){
const pointHistoryID = await process.processPointHistory(productID, flyer_id, pointHistoryParsed); // ** retry
if(!pointHistoryID){
const msg = `Fail to process point details from flyer: ${flyer_id} of product: ${productID}`;
Logging.error(msg);
}
}
return true;
} catch(error){
// Throw new error to parseAndUpdate() to stop operation
const msg = `Fail to parse and update product and related details from flyer ${flyer_id} grocery: ${grocery_id}}`;
Logging.error(msg);
return false;
}
}
async function parseAndUpdateProduct(grocery_id: number, flyer_id: number, productdetail:fetchType.ProductDetails):Promise<boolean>{
try{
// Parse fetched product detail object and ready to add new record
const productParsed = await parse.parseProduct(productdetail); // ** retry
// Return if product information is not ready from parsing
if(!productParsed){
const msg = `Fail to parse product details from flyer: ${flyer_id} of product imported code: ${productdetail.code}`;
Logging.error(msg);
return false;
}
// Return if product information is not exist or added to database as unable to further process price / point data
const productID = await process.processProduct(grocery_id,productParsed); // ** retry
if(!productID){
const msg = `Fail to process product details from flyer: ${flyer_id} of product imported code: ${productdetail.code}`;
Logging.error(msg);
return false;
}
return true;
} catch(error){
// Throw new error to parseAndUpdate() to stop operation
const msg = `Fail to parse and update product and related details from flyer ${flyer_id} grocery: ${grocery_id}}`;
Logging.error(msg);
return false;
}
}
*/