repzo-sap-absjo 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/changelog.md +17 -0
- package/lib/actions/create_invoice.d.ts +26 -0
- package/lib/actions/create_invoice.js +311 -0
- package/lib/actions/create_payment.d.ts +5 -0
- package/lib/actions/create_payment.js +177 -0
- package/lib/actions/create_proforma.d.ts +5 -0
- package/lib/actions/create_proforma.js +241 -0
- package/lib/actions/create_return_invoice.d.ts +5 -0
- package/lib/actions/create_return_invoice.js +268 -0
- package/lib/actions/create_transfer.d.ts +5 -0
- package/lib/actions/create_transfer.js +171 -0
- package/lib/actions/index.d.ts +3 -0
- package/lib/actions/index.js +31 -0
- package/lib/commands/adjust_inventory.d.ts +6 -0
- package/lib/commands/adjust_inventory.js +303 -0
- package/lib/commands/bank.d.ts +7 -0
- package/lib/commands/bank.js +301 -0
- package/lib/commands/basic.d.ts +2 -0
- package/lib/commands/basic.js +73 -0
- package/lib/commands/category.d.ts +4 -0
- package/lib/commands/category.js +193 -0
- package/lib/commands/channel.d.ts +4 -0
- package/lib/commands/channel.js +213 -0
- package/lib/commands/client.d.ts +39 -0
- package/lib/commands/client.js +410 -0
- package/lib/commands/client_disabled.d.ts +4 -0
- package/lib/commands/client_disabled.js +183 -0
- package/lib/commands/index.d.ts +23 -0
- package/lib/commands/index.js +82 -0
- package/lib/commands/join.d.ts +2 -0
- package/lib/commands/join.js +134 -0
- package/lib/commands/measureunit.d.ts +23 -0
- package/lib/commands/measureunit.js +314 -0
- package/lib/commands/measureunit_family.d.ts +12 -0
- package/lib/commands/measureunit_family.js +263 -0
- package/lib/commands/payment_term.d.ts +4 -0
- package/lib/commands/payment_term.js +217 -0
- package/lib/commands/price_list.d.ts +18 -0
- package/lib/commands/price_list.js +608 -0
- package/lib/commands/product.d.ts +39 -0
- package/lib/commands/product.js +454 -0
- package/lib/commands/product_disabled.d.ts +4 -0
- package/lib/commands/product_disabled.js +178 -0
- package/lib/commands/rep.d.ts +2 -0
- package/lib/commands/rep.js +246 -0
- package/lib/commands/tag.d.ts +2 -0
- package/lib/commands/tag.js +195 -0
- package/lib/commands/tax.d.ts +2 -0
- package/lib/commands/tax.js +198 -0
- package/lib/commands/warehouse.d.ts +4 -0
- package/lib/commands/warehouse.js +221 -0
- package/lib/index.d.ts +29 -0
- package/lib/index.js +6 -0
- package/lib/test/actions/create_invoice.d.ts +1 -0
- package/lib/test/actions/create_invoice.js +214 -0
- package/lib/test/actions/create_payment.d.ts +1 -0
- package/lib/test/actions/create_payment.js +131 -0
- package/lib/test/actions/create_proforma.d.ts +1 -0
- package/lib/test/actions/create_proforma.js +242 -0
- package/lib/test/actions/create_return_invoice.d.ts +1 -0
- package/lib/test/actions/create_return_invoice.js +226 -0
- package/lib/test/actions/create_transfer.d.ts +1 -0
- package/lib/test/actions/create_transfer.js +142 -0
- package/lib/test/commands/adjust_inventory.d.ts +1 -0
- package/lib/test/commands/adjust_inventory.js +97 -0
- package/lib/test/commands/bank.d.ts +1 -0
- package/lib/test/commands/bank.js +97 -0
- package/lib/test/commands/basic.d.ts +1 -0
- package/lib/test/commands/basic.js +339 -0
- package/lib/test/commands/category.d.ts +1 -0
- package/lib/test/commands/category.js +97 -0
- package/lib/test/commands/channel.d.ts +1 -0
- package/lib/test/commands/channel.js +97 -0
- package/lib/test/commands/client.d.ts +1 -0
- package/lib/test/commands/client.js +97 -0
- package/lib/test/commands/client_disabled.d.ts +1 -0
- package/lib/test/commands/client_disabled.js +97 -0
- package/lib/test/commands/join.d.ts +1 -0
- package/lib/test/commands/join.js +339 -0
- package/lib/test/commands/measureunit.d.ts +1 -0
- package/lib/test/commands/measureunit.js +97 -0
- package/lib/test/commands/measureunit_family.d.ts +1 -0
- package/lib/test/commands/measureunit_family.js +97 -0
- package/lib/test/commands/payment_term.d.ts +1 -0
- package/lib/test/commands/payment_term.js +97 -0
- package/lib/test/commands/price_list.d.ts +1 -0
- package/lib/test/commands/price_list.js +97 -0
- package/lib/test/commands/product.d.ts +1 -0
- package/lib/test/commands/product.js +97 -0
- package/lib/test/commands/product_disabled.d.ts +1 -0
- package/lib/test/commands/product_disabled.js +97 -0
- package/lib/test/commands/rep.d.ts +1 -0
- package/lib/test/commands/rep.js +97 -0
- package/lib/test/commands/tag.d.ts +1 -0
- package/lib/test/commands/tag.js +97 -0
- package/lib/test/commands/tax.d.ts +1 -0
- package/lib/test/commands/tax.js +97 -0
- package/lib/test/commands/warehouse.d.ts +1 -0
- package/lib/test/commands/warehouse.js +97 -0
- package/lib/types.d.ts +77 -0
- package/lib/types.js +1 -0
- package/lib/util.d.ts +70 -0
- package/lib/util.js +164 -0
- package/package.json +51 -0
- package/src/actions/create_invoice.ts +275 -0
- package/src/actions/create_payment.ts +138 -0
- package/src/actions/create_proforma.ts +187 -0
- package/src/actions/create_return_invoice.ts +217 -0
- package/src/actions/create_transfer.ts +160 -0
- package/src/actions/index.ts +27 -0
- package/src/commands/adjust_inventory.ts +250 -0
- package/src/commands/bank.ts +243 -0
- package/src/commands/basic.ts +64 -0
- package/src/commands/category.ts +176 -0
- package/src/commands/channel.ts +219 -0
- package/src/commands/client.ts +368 -0
- package/src/commands/client_disabled.ts +158 -0
- package/src/commands/index.ts +87 -0
- package/src/commands/join.ts +80 -0
- package/src/commands/measureunit.ts +276 -0
- package/src/commands/measureunit_family.ts +209 -0
- package/src/commands/payment_term.ts +224 -0
- package/src/commands/price_list.ts +434 -0
- package/src/commands/product.ts +359 -0
- package/src/commands/product_disabled.ts +152 -0
- package/src/commands/rep.ts +209 -0
- package/src/commands/tag.ts +173 -0
- package/src/commands/tax.ts +178 -0
- package/src/commands/warehouse.ts +204 -0
- package/src/index.ts +7 -0
- package/src/test/actions/create_invoice.ts +214 -0
- package/src/test/actions/create_payment.ts +131 -0
- package/src/test/actions/create_proforma.ts +242 -0
- package/src/test/actions/create_return_invoice.ts +226 -0
- package/src/test/actions/create_transfer.ts +142 -0
- package/src/test/commands/adjust_inventory.ts +100 -0
- package/src/test/commands/bank.ts +100 -0
- package/src/test/commands/basic.ts +342 -0
- package/src/test/commands/category.ts +100 -0
- package/src/test/commands/channel.ts +100 -0
- package/src/test/commands/client.ts +100 -0
- package/src/test/commands/client_disabled.ts +100 -0
- package/src/test/commands/join.ts +342 -0
- package/src/test/commands/measureunit.ts +100 -0
- package/src/test/commands/measureunit_family.ts +100 -0
- package/src/test/commands/payment_term.ts +100 -0
- package/src/test/commands/price_list.ts +100 -0
- package/src/test/commands/product.ts +100 -0
- package/src/test/commands/product_disabled.ts +100 -0
- package/src/test/commands/rep.ts +100 -0
- package/src/test/commands/tag.ts +100 -0
- package/src/test/commands/tax.ts +100 -0
- package/src/test/commands/warehouse.ts +100 -0
- package/src/types.ts +76 -0
- package/src/util.ts +205 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
import Repzo from "repzo";
|
|
2
|
+
import {
|
|
3
|
+
_create,
|
|
4
|
+
update_bench_time,
|
|
5
|
+
date_formatting,
|
|
6
|
+
set_error,
|
|
7
|
+
} from "../util.js";
|
|
8
|
+
export const sync_product = async (commandEvent) => {
|
|
9
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
|
|
10
|
+
const repzo = new Repzo(
|
|
11
|
+
(_a = commandEvent.app.formData) === null || _a === void 0
|
|
12
|
+
? void 0
|
|
13
|
+
: _a.repzoApiKey,
|
|
14
|
+
{
|
|
15
|
+
env: commandEvent.env,
|
|
16
|
+
}
|
|
17
|
+
);
|
|
18
|
+
const commandLog = new Repzo.CommandLog(
|
|
19
|
+
repzo,
|
|
20
|
+
commandEvent.app,
|
|
21
|
+
commandEvent.command
|
|
22
|
+
);
|
|
23
|
+
try {
|
|
24
|
+
console.log("sync_product");
|
|
25
|
+
const new_bench_time = new Date().toISOString();
|
|
26
|
+
const bench_time_key = "bench_time_product";
|
|
27
|
+
await commandLog.load(commandEvent.sync_id);
|
|
28
|
+
await commandLog.addDetail("Repzo SAP: Started Syncing Products").commit();
|
|
29
|
+
const nameSpace = commandEvent.nameSpace.join("_");
|
|
30
|
+
const result = {
|
|
31
|
+
sap_total: 0,
|
|
32
|
+
repzo_total: 0,
|
|
33
|
+
created: 0,
|
|
34
|
+
updated: 0,
|
|
35
|
+
failed: 0,
|
|
36
|
+
repzo_total_taxes: 0,
|
|
37
|
+
repzo_total_categories: 0,
|
|
38
|
+
};
|
|
39
|
+
const failed_docs_report = [];
|
|
40
|
+
const sap_products = await get_sap_products(
|
|
41
|
+
commandEvent.app.formData.sapHostUrl,
|
|
42
|
+
{ updateAt: commandEvent.app.options_formData[bench_time_key] }
|
|
43
|
+
);
|
|
44
|
+
result.sap_total =
|
|
45
|
+
sap_products === null || sap_products === void 0
|
|
46
|
+
? void 0
|
|
47
|
+
: sap_products.length;
|
|
48
|
+
await commandLog
|
|
49
|
+
.addDetail(
|
|
50
|
+
`${result.sap_total} Items in SAP changed since ${
|
|
51
|
+
commandEvent.app.options_formData[bench_time_key] || "ever"
|
|
52
|
+
}`
|
|
53
|
+
)
|
|
54
|
+
.commit();
|
|
55
|
+
// Product Repzo
|
|
56
|
+
const repzo_products = await repzo.product.find({
|
|
57
|
+
per_page: 50000,
|
|
58
|
+
withVariants: true,
|
|
59
|
+
});
|
|
60
|
+
result.repzo_total =
|
|
61
|
+
(_b =
|
|
62
|
+
repzo_products === null || repzo_products === void 0
|
|
63
|
+
? void 0
|
|
64
|
+
: repzo_products.data) === null || _b === void 0
|
|
65
|
+
? void 0
|
|
66
|
+
: _b.length;
|
|
67
|
+
await commandLog
|
|
68
|
+
.addDetail(
|
|
69
|
+
`${
|
|
70
|
+
(_c =
|
|
71
|
+
repzo_products === null || repzo_products === void 0
|
|
72
|
+
? void 0
|
|
73
|
+
: repzo_products.data) === null || _c === void 0
|
|
74
|
+
? void 0
|
|
75
|
+
: _c.length
|
|
76
|
+
} Products in Repzo`
|
|
77
|
+
)
|
|
78
|
+
.commit();
|
|
79
|
+
// Tax
|
|
80
|
+
const repzo_taxes = await repzo.tax.find({
|
|
81
|
+
per_page: 50000,
|
|
82
|
+
disabled: false,
|
|
83
|
+
});
|
|
84
|
+
result.repzo_total_taxes =
|
|
85
|
+
(_d =
|
|
86
|
+
repzo_taxes === null || repzo_taxes === void 0
|
|
87
|
+
? void 0
|
|
88
|
+
: repzo_taxes.data) === null || _d === void 0
|
|
89
|
+
? void 0
|
|
90
|
+
: _d.length;
|
|
91
|
+
await commandLog
|
|
92
|
+
.addDetail(`${result.repzo_total_taxes} Taxes in Repzo`)
|
|
93
|
+
.commit();
|
|
94
|
+
// Category
|
|
95
|
+
const repzo_categories = await repzo.category.find({
|
|
96
|
+
per_page: 50000,
|
|
97
|
+
disabled: false,
|
|
98
|
+
});
|
|
99
|
+
result.repzo_total_categories =
|
|
100
|
+
(_e =
|
|
101
|
+
repzo_categories === null || repzo_categories === void 0
|
|
102
|
+
? void 0
|
|
103
|
+
: repzo_categories.data) === null || _e === void 0
|
|
104
|
+
? void 0
|
|
105
|
+
: _e.length;
|
|
106
|
+
await commandLog
|
|
107
|
+
.addDetail(`${result.repzo_total_categories} Product Categories in Repzo`)
|
|
108
|
+
.commit();
|
|
109
|
+
for (
|
|
110
|
+
let i = 0;
|
|
111
|
+
i <
|
|
112
|
+
(sap_products === null || sap_products === void 0
|
|
113
|
+
? void 0
|
|
114
|
+
: sap_products.length);
|
|
115
|
+
i++
|
|
116
|
+
) {
|
|
117
|
+
const sap_product = sap_products[i];
|
|
118
|
+
try {
|
|
119
|
+
const repzo_product = repzo_products.data.find((r_product) => {
|
|
120
|
+
var _a;
|
|
121
|
+
return (
|
|
122
|
+
((_a =
|
|
123
|
+
r_product === null || r_product === void 0
|
|
124
|
+
? void 0
|
|
125
|
+
: r_product.integration_meta) === null || _a === void 0
|
|
126
|
+
? void 0
|
|
127
|
+
: _a.id) == `${nameSpace}_${sap_product.ITEMCODE}`
|
|
128
|
+
);
|
|
129
|
+
});
|
|
130
|
+
// Tax
|
|
131
|
+
const tax =
|
|
132
|
+
(_f =
|
|
133
|
+
repzo_taxes === null || repzo_taxes === void 0
|
|
134
|
+
? void 0
|
|
135
|
+
: repzo_taxes.data) === null || _f === void 0
|
|
136
|
+
? void 0
|
|
137
|
+
: _f.find((tax) => {
|
|
138
|
+
var _a;
|
|
139
|
+
return (
|
|
140
|
+
((_a =
|
|
141
|
+
tax === null || tax === void 0
|
|
142
|
+
? void 0
|
|
143
|
+
: tax.integration_meta) === null || _a === void 0
|
|
144
|
+
? void 0
|
|
145
|
+
: _a.id) == `${nameSpace}_${sap_product.ITEMTAXCODE}`
|
|
146
|
+
);
|
|
147
|
+
});
|
|
148
|
+
if (!tax) {
|
|
149
|
+
throw `Tax not found => ITEMTAXCODE: ${sap_product.ITEMTAXCODE}`;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
const product_tax =
|
|
153
|
+
(_g = tax === null || tax === void 0 ? void 0 : tax._id) === null ||
|
|
154
|
+
_g === void 0
|
|
155
|
+
? void 0
|
|
156
|
+
: _g.toString();
|
|
157
|
+
// Category
|
|
158
|
+
const category =
|
|
159
|
+
(_h =
|
|
160
|
+
repzo_categories === null || repzo_categories === void 0
|
|
161
|
+
? void 0
|
|
162
|
+
: repzo_categories.data) === null || _h === void 0
|
|
163
|
+
? void 0
|
|
164
|
+
: _h.find((category) => {
|
|
165
|
+
var _a;
|
|
166
|
+
return (
|
|
167
|
+
((_a =
|
|
168
|
+
category === null || category === void 0
|
|
169
|
+
? void 0
|
|
170
|
+
: category.integration_meta) === null || _a === void 0
|
|
171
|
+
? void 0
|
|
172
|
+
: _a.id) == `${nameSpace}_${sap_product.ITEMGROUPCODE}`
|
|
173
|
+
);
|
|
174
|
+
});
|
|
175
|
+
if (!category) {
|
|
176
|
+
throw `Category not found => ITEMGROUPCODE: ${sap_product.ITEMGROUPCODE}`;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const product_category =
|
|
180
|
+
(_j =
|
|
181
|
+
category === null || category === void 0
|
|
182
|
+
? void 0
|
|
183
|
+
: category._id) === null || _j === void 0
|
|
184
|
+
? void 0
|
|
185
|
+
: _j.toString();
|
|
186
|
+
// measureUnit family
|
|
187
|
+
const family = await repzo.measureunitFamily.find({
|
|
188
|
+
"integration_meta.id": `${nameSpace}_${sap_product.ITEMCODE}`,
|
|
189
|
+
disabled: false,
|
|
190
|
+
});
|
|
191
|
+
if (
|
|
192
|
+
!(family === null || family === void 0 ? void 0 : family.data) ||
|
|
193
|
+
((_k =
|
|
194
|
+
family === null || family === void 0 ? void 0 : family.data) ===
|
|
195
|
+
null || _k === void 0
|
|
196
|
+
? void 0
|
|
197
|
+
: _k.length) != 1
|
|
198
|
+
) {
|
|
199
|
+
throw `Family not found => ITEMCODE: ${sap_product.ITEMCODE}`;
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const product_family =
|
|
203
|
+
(_m =
|
|
204
|
+
(_l = family.data[0]) === null || _l === void 0
|
|
205
|
+
? void 0
|
|
206
|
+
: _l._id) === null || _m === void 0
|
|
207
|
+
? void 0
|
|
208
|
+
: _m.toString();
|
|
209
|
+
// measureUnit
|
|
210
|
+
const measureUnit = await repzo.measureunit.find({
|
|
211
|
+
"integration_meta.UOMGROUPENTRY": sap_product.UOMGROUPENTRY,
|
|
212
|
+
name: sap_product.DEFAULTITEMUOM,
|
|
213
|
+
company_namespace: nameSpace,
|
|
214
|
+
disabled: false,
|
|
215
|
+
});
|
|
216
|
+
if (!measureUnit.data || measureUnit.data.length != 1) {
|
|
217
|
+
throw `MeasureUnit not found => UOMGROUPENTRY: ${sap_product.UOMGROUPENTRY}, ITEMCODE: ${sap_product.ITEMCODE}, DEFAULTITEMUOM: ${sap_product.DEFAULTITEMUOM}`;
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
const product_measureUnit =
|
|
221
|
+
(_o = measureUnit.data[0]._id) === null || _o === void 0
|
|
222
|
+
? void 0
|
|
223
|
+
: _o.toString();
|
|
224
|
+
const body = {
|
|
225
|
+
active: true,
|
|
226
|
+
name: sap_product.ITEMDESC,
|
|
227
|
+
barcode: sap_product.ITEMBARCODE,
|
|
228
|
+
sku: sap_product.ITEMCODE,
|
|
229
|
+
sv_tax: product_tax,
|
|
230
|
+
category: product_category,
|
|
231
|
+
measureunit_family: product_family,
|
|
232
|
+
sv_measureUnit: product_measureUnit,
|
|
233
|
+
integration_meta: {
|
|
234
|
+
id: `${nameSpace}_${sap_product.ITEMCODE}`,
|
|
235
|
+
ITEMGROUPCODE: sap_product.ITEMGROUPCODE,
|
|
236
|
+
UOMGROUPENTRY: sap_product.UOMGROUPENTRY,
|
|
237
|
+
BRAND: sap_product.BRAND,
|
|
238
|
+
},
|
|
239
|
+
variants: [
|
|
240
|
+
{
|
|
241
|
+
disabled: false,
|
|
242
|
+
name: sap_product.ITEMCODE,
|
|
243
|
+
price: 0,
|
|
244
|
+
integration_meta: {
|
|
245
|
+
id: `${nameSpace}_${sap_product.ITEMCODE}`,
|
|
246
|
+
ITEMCODE: sap_product.ITEMCODE,
|
|
247
|
+
},
|
|
248
|
+
company_namespace: [nameSpace],
|
|
249
|
+
},
|
|
250
|
+
],
|
|
251
|
+
company_namespace: [nameSpace],
|
|
252
|
+
};
|
|
253
|
+
if (!repzo_product) {
|
|
254
|
+
// Create
|
|
255
|
+
try {
|
|
256
|
+
const created_product = await repzo.product.create(body);
|
|
257
|
+
result.created++;
|
|
258
|
+
} catch (e) {
|
|
259
|
+
console.log(
|
|
260
|
+
"Create Product Failed >> ",
|
|
261
|
+
e === null || e === void 0 ? void 0 : e.response,
|
|
262
|
+
body
|
|
263
|
+
);
|
|
264
|
+
failed_docs_report.push({
|
|
265
|
+
method: "create",
|
|
266
|
+
doc: body,
|
|
267
|
+
error_message: set_error(e),
|
|
268
|
+
});
|
|
269
|
+
result.failed++;
|
|
270
|
+
}
|
|
271
|
+
} else {
|
|
272
|
+
if (is_matched(body, repzo_product)) {
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
if (
|
|
276
|
+
(_p =
|
|
277
|
+
repzo_product === null || repzo_product === void 0
|
|
278
|
+
? void 0
|
|
279
|
+
: repzo_product.variants) === null || _p === void 0
|
|
280
|
+
? void 0
|
|
281
|
+
: _p.length
|
|
282
|
+
) {
|
|
283
|
+
(_q = body === null || body === void 0 ? void 0 : body.variants) ===
|
|
284
|
+
null || _q === void 0
|
|
285
|
+
? void 0
|
|
286
|
+
: _q.forEach((variant) => {
|
|
287
|
+
var _a;
|
|
288
|
+
const hasMatch =
|
|
289
|
+
(_a =
|
|
290
|
+
repzo_product === null || repzo_product === void 0
|
|
291
|
+
? void 0
|
|
292
|
+
: repzo_product.variants) === null || _a === void 0
|
|
293
|
+
? void 0
|
|
294
|
+
: _a.find((v) => {
|
|
295
|
+
var _a, _b;
|
|
296
|
+
return (
|
|
297
|
+
((_a = v.integration_meta) === null || _a === void 0
|
|
298
|
+
? void 0
|
|
299
|
+
: _a.id) ==
|
|
300
|
+
((_b =
|
|
301
|
+
variant === null || variant === void 0
|
|
302
|
+
? void 0
|
|
303
|
+
: variant.integration_meta) === null ||
|
|
304
|
+
_b === void 0
|
|
305
|
+
? void 0
|
|
306
|
+
: _b.id)
|
|
307
|
+
);
|
|
308
|
+
});
|
|
309
|
+
if (hasMatch) {
|
|
310
|
+
variant._id = hasMatch._id;
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
// Update
|
|
315
|
+
try {
|
|
316
|
+
const updated_product = await repzo.product.update(
|
|
317
|
+
repzo_product._id,
|
|
318
|
+
body
|
|
319
|
+
);
|
|
320
|
+
result.updated++;
|
|
321
|
+
} catch (e) {
|
|
322
|
+
console.log(
|
|
323
|
+
"Update Product Failed >> ",
|
|
324
|
+
(_r = e === null || e === void 0 ? void 0 : e.response) ===
|
|
325
|
+
null || _r === void 0
|
|
326
|
+
? void 0
|
|
327
|
+
: _r.data,
|
|
328
|
+
body
|
|
329
|
+
);
|
|
330
|
+
failed_docs_report.push({
|
|
331
|
+
method: "update",
|
|
332
|
+
doc_id:
|
|
333
|
+
repzo_product === null || repzo_product === void 0
|
|
334
|
+
? void 0
|
|
335
|
+
: repzo_product._id,
|
|
336
|
+
doc: body,
|
|
337
|
+
error_message: set_error(e),
|
|
338
|
+
});
|
|
339
|
+
result.failed++;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
} catch (e) {
|
|
343
|
+
console.log("FetchingData Product Failed >> ", e);
|
|
344
|
+
failed_docs_report.push({
|
|
345
|
+
method: "fetchingData",
|
|
346
|
+
doc_id: sap_product.ITEMBARCODE,
|
|
347
|
+
error_message: set_error(e),
|
|
348
|
+
});
|
|
349
|
+
result.failed++;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
// console.log(result);
|
|
353
|
+
await update_bench_time(
|
|
354
|
+
repzo,
|
|
355
|
+
commandEvent.app._id,
|
|
356
|
+
bench_time_key,
|
|
357
|
+
new_bench_time
|
|
358
|
+
);
|
|
359
|
+
await commandLog
|
|
360
|
+
.setStatus(
|
|
361
|
+
"success",
|
|
362
|
+
failed_docs_report.length ? failed_docs_report : null
|
|
363
|
+
)
|
|
364
|
+
.setBody(result)
|
|
365
|
+
.commit();
|
|
366
|
+
return result;
|
|
367
|
+
} catch (e) {
|
|
368
|
+
//@ts-ignore
|
|
369
|
+
console.error(
|
|
370
|
+
((_s = e === null || e === void 0 ? void 0 : e.response) === null ||
|
|
371
|
+
_s === void 0
|
|
372
|
+
? void 0
|
|
373
|
+
: _s.data) || e
|
|
374
|
+
);
|
|
375
|
+
await commandLog.setStatus("fail", e).commit();
|
|
376
|
+
throw e;
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
const get_sap_products = async (serviceEndPoint, query) => {
|
|
380
|
+
try {
|
|
381
|
+
const sap_products = await _create(serviceEndPoint, "/Items", {
|
|
382
|
+
Active: "Y",
|
|
383
|
+
UpdateAt: date_formatting(
|
|
384
|
+
query === null || query === void 0 ? void 0 : query.updateAt,
|
|
385
|
+
"YYYYMMDD:HHmmss"
|
|
386
|
+
),
|
|
387
|
+
});
|
|
388
|
+
return sap_products.Items;
|
|
389
|
+
} catch (e) {
|
|
390
|
+
throw e;
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
const is_matched = (body_1, body_2) => {
|
|
394
|
+
var _a, _b, _c, _d, _e, _f;
|
|
395
|
+
try {
|
|
396
|
+
const keys = [
|
|
397
|
+
"active",
|
|
398
|
+
"name",
|
|
399
|
+
"barcode",
|
|
400
|
+
"sku",
|
|
401
|
+
"sv_tax",
|
|
402
|
+
"category",
|
|
403
|
+
"measureunit_family",
|
|
404
|
+
"sv_measureUnit",
|
|
405
|
+
];
|
|
406
|
+
const integration_meta_keys = [
|
|
407
|
+
"id",
|
|
408
|
+
"ITEMGROUPCODE",
|
|
409
|
+
"UOMGROUPENTRY",
|
|
410
|
+
"BRAND",
|
|
411
|
+
];
|
|
412
|
+
const variant_keys = ["variants", "disabled", "name", "price"];
|
|
413
|
+
const variant_integration_meta_keys = ["integration_meta.id"];
|
|
414
|
+
for (let i = 0; i < keys.length; i++) {
|
|
415
|
+
const key = keys[i];
|
|
416
|
+
if (
|
|
417
|
+
((_a = body_1[key]) === null || _a === void 0
|
|
418
|
+
? void 0
|
|
419
|
+
: _a.toString()) !==
|
|
420
|
+
((_b = body_2[key]) === null || _b === void 0 ? void 0 : _b.toString())
|
|
421
|
+
) {
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
for (let i = 0; i < integration_meta_keys.length; i++) {
|
|
426
|
+
const key = keys[i];
|
|
427
|
+
if (
|
|
428
|
+
((_d =
|
|
429
|
+
(_c =
|
|
430
|
+
body_1 === null || body_1 === void 0
|
|
431
|
+
? void 0
|
|
432
|
+
: body_1.integration_meta) === null || _c === void 0
|
|
433
|
+
? void 0
|
|
434
|
+
: _c[key]) === null || _d === void 0
|
|
435
|
+
? void 0
|
|
436
|
+
: _d.toString()) !==
|
|
437
|
+
((_f =
|
|
438
|
+
(_e =
|
|
439
|
+
body_2 === null || body_2 === void 0
|
|
440
|
+
? void 0
|
|
441
|
+
: body_2.integration_meta) === null || _e === void 0
|
|
442
|
+
? void 0
|
|
443
|
+
: _e[key]) === null || _f === void 0
|
|
444
|
+
? void 0
|
|
445
|
+
: _f.toString())
|
|
446
|
+
) {
|
|
447
|
+
return false;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
return true;
|
|
451
|
+
} catch (e) {
|
|
452
|
+
throw e;
|
|
453
|
+
}
|
|
454
|
+
};
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import Repzo from "repzo";
|
|
2
|
+
import {
|
|
3
|
+
_create,
|
|
4
|
+
update_bench_time,
|
|
5
|
+
date_formatting,
|
|
6
|
+
set_error,
|
|
7
|
+
} from "../util.js";
|
|
8
|
+
export const sync_disabled_product = async (commandEvent) => {
|
|
9
|
+
var _a, _b, _c, _d;
|
|
10
|
+
const repzo = new Repzo(
|
|
11
|
+
(_a = commandEvent.app.formData) === null || _a === void 0
|
|
12
|
+
? void 0
|
|
13
|
+
: _a.repzoApiKey,
|
|
14
|
+
{
|
|
15
|
+
env: commandEvent.env,
|
|
16
|
+
}
|
|
17
|
+
);
|
|
18
|
+
const commandLog = new Repzo.CommandLog(
|
|
19
|
+
repzo,
|
|
20
|
+
commandEvent.app,
|
|
21
|
+
commandEvent.command
|
|
22
|
+
);
|
|
23
|
+
try {
|
|
24
|
+
console.log("sync_disabled_product");
|
|
25
|
+
const new_bench_time = new Date().toISOString();
|
|
26
|
+
const bench_time_key = "bench_time_product_disabled";
|
|
27
|
+
await commandLog.load(commandEvent.sync_id);
|
|
28
|
+
await commandLog
|
|
29
|
+
.addDetail("Repzo SAP: Started Syncing Disabled Products")
|
|
30
|
+
.commit();
|
|
31
|
+
const nameSpace = commandEvent.nameSpace.join("_");
|
|
32
|
+
const result = {
|
|
33
|
+
sap_total: 0,
|
|
34
|
+
repzo_total: 0,
|
|
35
|
+
created: 0,
|
|
36
|
+
updated: 0,
|
|
37
|
+
failed: 0,
|
|
38
|
+
};
|
|
39
|
+
const failed_docs_report = [];
|
|
40
|
+
const sap_products = await get_sap_disabled_products(
|
|
41
|
+
commandEvent.app.formData.sapHostUrl,
|
|
42
|
+
{ updateAt: commandEvent.app.options_formData[bench_time_key] }
|
|
43
|
+
);
|
|
44
|
+
result.sap_total =
|
|
45
|
+
sap_products === null || sap_products === void 0
|
|
46
|
+
? void 0
|
|
47
|
+
: sap_products.length;
|
|
48
|
+
await commandLog
|
|
49
|
+
.addDetail(
|
|
50
|
+
`${result.sap_total} Disabled Products in SAP changed since ${
|
|
51
|
+
commandEvent.app.options_formData[bench_time_key] || "ever"
|
|
52
|
+
}`
|
|
53
|
+
)
|
|
54
|
+
.commit();
|
|
55
|
+
const sap_product_query =
|
|
56
|
+
sap_products === null || sap_products === void 0
|
|
57
|
+
? void 0
|
|
58
|
+
: sap_products.map((product) => `${nameSpace}_${product.ITEMCODE}`);
|
|
59
|
+
const repzo_products = [];
|
|
60
|
+
const per_page = 200;
|
|
61
|
+
const pages = Math.ceil(sap_product_query.length / per_page);
|
|
62
|
+
for (let i = 0; i < pages; i += per_page) {
|
|
63
|
+
const repzo_product_per_page = await repzo.product.find({
|
|
64
|
+
active: true,
|
|
65
|
+
per_page: 50000,
|
|
66
|
+
"integration_meta.id": sap_product_query.slice(i, i + per_page),
|
|
67
|
+
});
|
|
68
|
+
if (
|
|
69
|
+
(_b =
|
|
70
|
+
repzo_product_per_page === null || repzo_product_per_page === void 0
|
|
71
|
+
? void 0
|
|
72
|
+
: repzo_product_per_page.data) === null || _b === void 0
|
|
73
|
+
? void 0
|
|
74
|
+
: _b.length
|
|
75
|
+
)
|
|
76
|
+
repzo_products.push(...repzo_product_per_page.data);
|
|
77
|
+
}
|
|
78
|
+
result.repzo_total =
|
|
79
|
+
repzo_products === null || repzo_products === void 0
|
|
80
|
+
? void 0
|
|
81
|
+
: repzo_products.length;
|
|
82
|
+
await commandLog
|
|
83
|
+
.addDetail(
|
|
84
|
+
`${result.repzo_total} Active Products in Repzo should be disabled`
|
|
85
|
+
)
|
|
86
|
+
.commit();
|
|
87
|
+
for (
|
|
88
|
+
let i = 0;
|
|
89
|
+
i <
|
|
90
|
+
(sap_products === null || sap_products === void 0
|
|
91
|
+
? void 0
|
|
92
|
+
: sap_products.length);
|
|
93
|
+
i++
|
|
94
|
+
) {
|
|
95
|
+
const sap_product = sap_products[i];
|
|
96
|
+
const repzo_product = repzo_products.find((r_product) => {
|
|
97
|
+
var _a;
|
|
98
|
+
return (
|
|
99
|
+
((_a =
|
|
100
|
+
r_product === null || r_product === void 0
|
|
101
|
+
? void 0
|
|
102
|
+
: r_product.integration_meta) === null || _a === void 0
|
|
103
|
+
? void 0
|
|
104
|
+
: _a.id) == `${nameSpace}_${sap_product.ITEMCODE}`
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
if (repzo_product) {
|
|
108
|
+
// Update
|
|
109
|
+
try {
|
|
110
|
+
const disabled_product = await repzo.product.remove(
|
|
111
|
+
repzo_product._id
|
|
112
|
+
);
|
|
113
|
+
result.updated++;
|
|
114
|
+
} catch (e) {
|
|
115
|
+
console.log(
|
|
116
|
+
"Disabled Product Failed >> ",
|
|
117
|
+
(_c = e === null || e === void 0 ? void 0 : e.response) === null ||
|
|
118
|
+
_c === void 0
|
|
119
|
+
? void 0
|
|
120
|
+
: _c.data,
|
|
121
|
+
{
|
|
122
|
+
ITEMCODE: sap_product.ITEMCODE,
|
|
123
|
+
}
|
|
124
|
+
);
|
|
125
|
+
failed_docs_report.push({
|
|
126
|
+
method: "delete",
|
|
127
|
+
doc_id:
|
|
128
|
+
repzo_product === null || repzo_product === void 0
|
|
129
|
+
? void 0
|
|
130
|
+
: repzo_product._id,
|
|
131
|
+
doc: { ITEMCODE: sap_product.ITEMCODE },
|
|
132
|
+
error_message: set_error(e),
|
|
133
|
+
});
|
|
134
|
+
result.failed++;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// console.log(result);
|
|
139
|
+
await update_bench_time(
|
|
140
|
+
repzo,
|
|
141
|
+
commandEvent.app._id,
|
|
142
|
+
bench_time_key,
|
|
143
|
+
new_bench_time
|
|
144
|
+
);
|
|
145
|
+
await commandLog
|
|
146
|
+
.setStatus(
|
|
147
|
+
"success",
|
|
148
|
+
failed_docs_report.length ? failed_docs_report : null
|
|
149
|
+
)
|
|
150
|
+
.setBody(result)
|
|
151
|
+
.commit();
|
|
152
|
+
return result;
|
|
153
|
+
} catch (e) {
|
|
154
|
+
//@ts-ignore
|
|
155
|
+
console.error(
|
|
156
|
+
((_d = e === null || e === void 0 ? void 0 : e.response) === null ||
|
|
157
|
+
_d === void 0
|
|
158
|
+
? void 0
|
|
159
|
+
: _d.data) || e
|
|
160
|
+
);
|
|
161
|
+
await commandLog.setStatus("fail", e).commit();
|
|
162
|
+
throw e;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
const get_sap_disabled_products = async (serviceEndPoint, query) => {
|
|
166
|
+
try {
|
|
167
|
+
const sap_products = await _create(serviceEndPoint, "/Items", {
|
|
168
|
+
Active: "N",
|
|
169
|
+
UpdateAt: date_formatting(
|
|
170
|
+
query === null || query === void 0 ? void 0 : query.updateAt,
|
|
171
|
+
"YYYYMMDD:HHmmss"
|
|
172
|
+
),
|
|
173
|
+
});
|
|
174
|
+
return sap_products.Items;
|
|
175
|
+
} catch (e) {
|
|
176
|
+
throw e;
|
|
177
|
+
}
|
|
178
|
+
};
|