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.
Files changed (157) hide show
  1. package/README.md +3 -0
  2. package/changelog.md +17 -0
  3. package/lib/actions/create_invoice.d.ts +26 -0
  4. package/lib/actions/create_invoice.js +311 -0
  5. package/lib/actions/create_payment.d.ts +5 -0
  6. package/lib/actions/create_payment.js +177 -0
  7. package/lib/actions/create_proforma.d.ts +5 -0
  8. package/lib/actions/create_proforma.js +241 -0
  9. package/lib/actions/create_return_invoice.d.ts +5 -0
  10. package/lib/actions/create_return_invoice.js +268 -0
  11. package/lib/actions/create_transfer.d.ts +5 -0
  12. package/lib/actions/create_transfer.js +171 -0
  13. package/lib/actions/index.d.ts +3 -0
  14. package/lib/actions/index.js +31 -0
  15. package/lib/commands/adjust_inventory.d.ts +6 -0
  16. package/lib/commands/adjust_inventory.js +303 -0
  17. package/lib/commands/bank.d.ts +7 -0
  18. package/lib/commands/bank.js +301 -0
  19. package/lib/commands/basic.d.ts +2 -0
  20. package/lib/commands/basic.js +73 -0
  21. package/lib/commands/category.d.ts +4 -0
  22. package/lib/commands/category.js +193 -0
  23. package/lib/commands/channel.d.ts +4 -0
  24. package/lib/commands/channel.js +213 -0
  25. package/lib/commands/client.d.ts +39 -0
  26. package/lib/commands/client.js +410 -0
  27. package/lib/commands/client_disabled.d.ts +4 -0
  28. package/lib/commands/client_disabled.js +183 -0
  29. package/lib/commands/index.d.ts +23 -0
  30. package/lib/commands/index.js +82 -0
  31. package/lib/commands/join.d.ts +2 -0
  32. package/lib/commands/join.js +134 -0
  33. package/lib/commands/measureunit.d.ts +23 -0
  34. package/lib/commands/measureunit.js +314 -0
  35. package/lib/commands/measureunit_family.d.ts +12 -0
  36. package/lib/commands/measureunit_family.js +263 -0
  37. package/lib/commands/payment_term.d.ts +4 -0
  38. package/lib/commands/payment_term.js +217 -0
  39. package/lib/commands/price_list.d.ts +18 -0
  40. package/lib/commands/price_list.js +608 -0
  41. package/lib/commands/product.d.ts +39 -0
  42. package/lib/commands/product.js +454 -0
  43. package/lib/commands/product_disabled.d.ts +4 -0
  44. package/lib/commands/product_disabled.js +178 -0
  45. package/lib/commands/rep.d.ts +2 -0
  46. package/lib/commands/rep.js +246 -0
  47. package/lib/commands/tag.d.ts +2 -0
  48. package/lib/commands/tag.js +195 -0
  49. package/lib/commands/tax.d.ts +2 -0
  50. package/lib/commands/tax.js +198 -0
  51. package/lib/commands/warehouse.d.ts +4 -0
  52. package/lib/commands/warehouse.js +221 -0
  53. package/lib/index.d.ts +29 -0
  54. package/lib/index.js +6 -0
  55. package/lib/test/actions/create_invoice.d.ts +1 -0
  56. package/lib/test/actions/create_invoice.js +214 -0
  57. package/lib/test/actions/create_payment.d.ts +1 -0
  58. package/lib/test/actions/create_payment.js +131 -0
  59. package/lib/test/actions/create_proforma.d.ts +1 -0
  60. package/lib/test/actions/create_proforma.js +242 -0
  61. package/lib/test/actions/create_return_invoice.d.ts +1 -0
  62. package/lib/test/actions/create_return_invoice.js +226 -0
  63. package/lib/test/actions/create_transfer.d.ts +1 -0
  64. package/lib/test/actions/create_transfer.js +142 -0
  65. package/lib/test/commands/adjust_inventory.d.ts +1 -0
  66. package/lib/test/commands/adjust_inventory.js +97 -0
  67. package/lib/test/commands/bank.d.ts +1 -0
  68. package/lib/test/commands/bank.js +97 -0
  69. package/lib/test/commands/basic.d.ts +1 -0
  70. package/lib/test/commands/basic.js +339 -0
  71. package/lib/test/commands/category.d.ts +1 -0
  72. package/lib/test/commands/category.js +97 -0
  73. package/lib/test/commands/channel.d.ts +1 -0
  74. package/lib/test/commands/channel.js +97 -0
  75. package/lib/test/commands/client.d.ts +1 -0
  76. package/lib/test/commands/client.js +97 -0
  77. package/lib/test/commands/client_disabled.d.ts +1 -0
  78. package/lib/test/commands/client_disabled.js +97 -0
  79. package/lib/test/commands/join.d.ts +1 -0
  80. package/lib/test/commands/join.js +339 -0
  81. package/lib/test/commands/measureunit.d.ts +1 -0
  82. package/lib/test/commands/measureunit.js +97 -0
  83. package/lib/test/commands/measureunit_family.d.ts +1 -0
  84. package/lib/test/commands/measureunit_family.js +97 -0
  85. package/lib/test/commands/payment_term.d.ts +1 -0
  86. package/lib/test/commands/payment_term.js +97 -0
  87. package/lib/test/commands/price_list.d.ts +1 -0
  88. package/lib/test/commands/price_list.js +97 -0
  89. package/lib/test/commands/product.d.ts +1 -0
  90. package/lib/test/commands/product.js +97 -0
  91. package/lib/test/commands/product_disabled.d.ts +1 -0
  92. package/lib/test/commands/product_disabled.js +97 -0
  93. package/lib/test/commands/rep.d.ts +1 -0
  94. package/lib/test/commands/rep.js +97 -0
  95. package/lib/test/commands/tag.d.ts +1 -0
  96. package/lib/test/commands/tag.js +97 -0
  97. package/lib/test/commands/tax.d.ts +1 -0
  98. package/lib/test/commands/tax.js +97 -0
  99. package/lib/test/commands/warehouse.d.ts +1 -0
  100. package/lib/test/commands/warehouse.js +97 -0
  101. package/lib/types.d.ts +77 -0
  102. package/lib/types.js +1 -0
  103. package/lib/util.d.ts +70 -0
  104. package/lib/util.js +164 -0
  105. package/package.json +51 -0
  106. package/src/actions/create_invoice.ts +275 -0
  107. package/src/actions/create_payment.ts +138 -0
  108. package/src/actions/create_proforma.ts +187 -0
  109. package/src/actions/create_return_invoice.ts +217 -0
  110. package/src/actions/create_transfer.ts +160 -0
  111. package/src/actions/index.ts +27 -0
  112. package/src/commands/adjust_inventory.ts +250 -0
  113. package/src/commands/bank.ts +243 -0
  114. package/src/commands/basic.ts +64 -0
  115. package/src/commands/category.ts +176 -0
  116. package/src/commands/channel.ts +219 -0
  117. package/src/commands/client.ts +368 -0
  118. package/src/commands/client_disabled.ts +158 -0
  119. package/src/commands/index.ts +87 -0
  120. package/src/commands/join.ts +80 -0
  121. package/src/commands/measureunit.ts +276 -0
  122. package/src/commands/measureunit_family.ts +209 -0
  123. package/src/commands/payment_term.ts +224 -0
  124. package/src/commands/price_list.ts +434 -0
  125. package/src/commands/product.ts +359 -0
  126. package/src/commands/product_disabled.ts +152 -0
  127. package/src/commands/rep.ts +209 -0
  128. package/src/commands/tag.ts +173 -0
  129. package/src/commands/tax.ts +178 -0
  130. package/src/commands/warehouse.ts +204 -0
  131. package/src/index.ts +7 -0
  132. package/src/test/actions/create_invoice.ts +214 -0
  133. package/src/test/actions/create_payment.ts +131 -0
  134. package/src/test/actions/create_proforma.ts +242 -0
  135. package/src/test/actions/create_return_invoice.ts +226 -0
  136. package/src/test/actions/create_transfer.ts +142 -0
  137. package/src/test/commands/adjust_inventory.ts +100 -0
  138. package/src/test/commands/bank.ts +100 -0
  139. package/src/test/commands/basic.ts +342 -0
  140. package/src/test/commands/category.ts +100 -0
  141. package/src/test/commands/channel.ts +100 -0
  142. package/src/test/commands/client.ts +100 -0
  143. package/src/test/commands/client_disabled.ts +100 -0
  144. package/src/test/commands/join.ts +342 -0
  145. package/src/test/commands/measureunit.ts +100 -0
  146. package/src/test/commands/measureunit_family.ts +100 -0
  147. package/src/test/commands/payment_term.ts +100 -0
  148. package/src/test/commands/price_list.ts +100 -0
  149. package/src/test/commands/product.ts +100 -0
  150. package/src/test/commands/product_disabled.ts +100 -0
  151. package/src/test/commands/rep.ts +100 -0
  152. package/src/test/commands/tag.ts +100 -0
  153. package/src/test/commands/tax.ts +100 -0
  154. package/src/test/commands/warehouse.ts +100 -0
  155. package/src/types.ts +76 -0
  156. package/src/util.ts +205 -0
  157. package/tsconfig.json +15 -0
@@ -0,0 +1,359 @@
1
+ import Repzo from "repzo";
2
+ import { Service } from "repzo/src/types";
3
+ import DataSet from "data-set-query";
4
+ import _ from "lodash";
5
+ import { CommandEvent, Result, FailedDocsReport } from "../types";
6
+ import {
7
+ _fetch,
8
+ _create,
9
+ _update,
10
+ _delete,
11
+ update_bench_time,
12
+ date_formatting,
13
+ set_error,
14
+ } from "../util.js";
15
+
16
+ export interface SAPProduct {
17
+ ITEMBARCODE: string; // "10900035835";
18
+ ITEMDESC: string; // "Diamond Cling Wrap 24X300ftX30cm Regular";
19
+ ITEMAVALIABLEQTY: number; // 67.54987;
20
+ ITEMTAX: number; // 16.0;
21
+ ITEMTAXCODE: string; // "S16";
22
+ ITEMCODE: string; // "020-REY-DI0018";
23
+ Division: string; // "Non-Food";
24
+ "Parent Category": string; // "Cling Wrap";
25
+ "Sub-Category": string; // "Cling Wrap";
26
+ "Item Type": string; // "ORIGINAL";
27
+ DEFAULTITEMUOM: string; // "CRTN";
28
+ DEFAULTSALEUOMID: number; // 3;
29
+ INVUOMID: number; // 3;
30
+ ITEMSALESUOMS: string; // "PC, CRTN";
31
+ ITEMQTY: number; // 1.0;
32
+ ITEMGROUPCODE: number; // 122;
33
+ PRICE: number; // 46.156662;
34
+ ISSERIAL: "Y" | "N";
35
+ ITEMDESCF: string; // "داياموند نايلون تغليف الأطعمه 24*300قدم*30سم";
36
+ UOMGROUPENTRY: number; // 76;
37
+ MILCODE: string; // "020-REY-DI0018";
38
+ MODELNO: string; // "1090003583";
39
+ BRAND: string; // "Reynolds";
40
+ ITEMSUBCATEGORY: string; // "Non-Food";
41
+ CREATEDATE: string; // "2021-12-07T21:00:00Z";
42
+ UPDATEDATE: string; // "2022-11-29T21:00:00Z";
43
+ }
44
+
45
+ export interface SAPProducts {
46
+ result: "Success";
47
+ Items: SAPProduct[];
48
+ }
49
+
50
+ export const sync_product = async (commandEvent: CommandEvent) => {
51
+ const repzo = new Repzo(commandEvent.app.formData?.repzoApiKey, {
52
+ env: commandEvent.env,
53
+ });
54
+
55
+ const commandLog = new Repzo.CommandLog(
56
+ repzo,
57
+ commandEvent.app,
58
+ commandEvent.command
59
+ );
60
+ try {
61
+ console.log("sync_product");
62
+
63
+ const new_bench_time = new Date().toISOString();
64
+ const bench_time_key = "bench_time_product";
65
+
66
+ await commandLog.load(commandEvent.sync_id);
67
+ await commandLog.addDetail("Repzo SAP: Started Syncing Products").commit();
68
+
69
+ const nameSpace: string = commandEvent.nameSpace.join("_");
70
+ const result: Result & {
71
+ repzo_total_taxes: number;
72
+ repzo_total_categories: number;
73
+ } = {
74
+ sap_total: 0,
75
+ repzo_total: 0,
76
+ created: 0,
77
+ updated: 0,
78
+ failed: 0,
79
+ repzo_total_taxes: 0,
80
+ repzo_total_categories: 0,
81
+ };
82
+ const failed_docs_report: FailedDocsReport = [];
83
+
84
+ const sap_products: SAPProduct[] = await get_sap_products(
85
+ commandEvent.app.formData.sapHostUrl,
86
+ { updateAt: commandEvent.app.options_formData[bench_time_key] }
87
+ );
88
+ result.sap_total = sap_products?.length;
89
+
90
+ await commandLog
91
+ .addDetail(
92
+ `${result.sap_total} Items in SAP changed since ${
93
+ commandEvent.app.options_formData[bench_time_key] || "ever"
94
+ }`
95
+ )
96
+ .commit();
97
+
98
+ // Product Repzo
99
+ const repzo_products = await repzo.product.find({
100
+ per_page: 50000,
101
+ withVariants: true,
102
+ });
103
+ result.repzo_total = repzo_products?.data?.length;
104
+ await commandLog
105
+ .addDetail(`${repzo_products?.data?.length} Products in Repzo`)
106
+ .commit();
107
+
108
+ // Tax
109
+ const repzo_taxes = await repzo.tax.find({
110
+ per_page: 50000,
111
+ disabled: false,
112
+ });
113
+ result.repzo_total_taxes = repzo_taxes?.data?.length;
114
+ await commandLog
115
+ .addDetail(`${result.repzo_total_taxes} Taxes in Repzo`)
116
+ .commit();
117
+
118
+ // Category
119
+ const repzo_categories = await repzo.category.find({
120
+ per_page: 50000,
121
+ disabled: false,
122
+ });
123
+ result.repzo_total_categories = repzo_categories?.data?.length;
124
+ await commandLog
125
+ .addDetail(`${result.repzo_total_categories} Product Categories in Repzo`)
126
+ .commit();
127
+
128
+ for (let i = 0; i < sap_products?.length; i++) {
129
+ const sap_product: SAPProduct = sap_products[i];
130
+ try {
131
+ const repzo_product = repzo_products.data.find(
132
+ (r_product) =>
133
+ r_product?.integration_meta?.id ==
134
+ `${nameSpace}_${sap_product.ITEMCODE}`
135
+ );
136
+
137
+ // Tax
138
+ const tax = repzo_taxes?.data?.find(
139
+ (tax) =>
140
+ tax?.integration_meta?.id ==
141
+ `${nameSpace}_${sap_product.ITEMTAXCODE}`
142
+ );
143
+ if (!tax) {
144
+ throw `Tax not found => ITEMTAXCODE: ${sap_product.ITEMTAXCODE}`;
145
+ continue;
146
+ }
147
+ const product_tax = tax?._id?.toString();
148
+
149
+ // Category
150
+ const category = repzo_categories?.data?.find(
151
+ (category) =>
152
+ category?.integration_meta?.id ==
153
+ `${nameSpace}_${sap_product.ITEMGROUPCODE}`
154
+ );
155
+ if (!category) {
156
+ throw `Category not found => ITEMGROUPCODE: ${sap_product.ITEMGROUPCODE}`;
157
+ continue;
158
+ }
159
+ const product_category = category?._id?.toString();
160
+
161
+ // measureUnit family
162
+ const family = await repzo.measureunitFamily.find({
163
+ "integration_meta.id": `${nameSpace}_${sap_product.ITEMCODE}`,
164
+ disabled: false,
165
+ });
166
+ if (!family?.data || family?.data?.length != 1) {
167
+ throw `Family not found => ITEMCODE: ${sap_product.ITEMCODE}`;
168
+ continue;
169
+ }
170
+ const product_family = family.data[0]?._id?.toString();
171
+
172
+ // measureUnit
173
+ const measureUnit = await repzo.measureunit.find({
174
+ "integration_meta.UOMGROUPENTRY": sap_product.UOMGROUPENTRY,
175
+ name: sap_product.DEFAULTITEMUOM,
176
+ company_namespace: nameSpace,
177
+ disabled: false,
178
+ });
179
+ if (!measureUnit.data || measureUnit.data.length != 1) {
180
+ throw `MeasureUnit not found => UOMGROUPENTRY: ${sap_product.UOMGROUPENTRY}, ITEMCODE: ${sap_product.ITEMCODE}, DEFAULTITEMUOM: ${sap_product.DEFAULTITEMUOM}`;
181
+ continue;
182
+ }
183
+ const product_measureUnit = measureUnit.data[0]._id?.toString();
184
+
185
+ const body: Service.Product.Create.Body | Service.Product.Update.Body =
186
+ {
187
+ active: true,
188
+ name: sap_product.ITEMDESC,
189
+ barcode: sap_product.ITEMBARCODE,
190
+ sku: sap_product.ITEMCODE,
191
+ sv_tax: product_tax,
192
+ category: product_category,
193
+ measureunit_family: product_family,
194
+ sv_measureUnit: product_measureUnit,
195
+ integration_meta: {
196
+ id: `${nameSpace}_${sap_product.ITEMCODE}`,
197
+ ITEMGROUPCODE: sap_product.ITEMGROUPCODE,
198
+ UOMGROUPENTRY: sap_product.UOMGROUPENTRY,
199
+ BRAND: sap_product.BRAND,
200
+ },
201
+ variants: [
202
+ {
203
+ disabled: false,
204
+ name: sap_product.ITEMCODE,
205
+ price: 0, // Math.round(sap_product.PRICE * 1000),
206
+ integration_meta: {
207
+ id: `${nameSpace}_${sap_product.ITEMCODE}`,
208
+ ITEMCODE: sap_product.ITEMCODE,
209
+ },
210
+ company_namespace: [nameSpace],
211
+ },
212
+ ],
213
+ company_namespace: [nameSpace],
214
+ };
215
+
216
+ if (!repzo_product) {
217
+ // Create
218
+ try {
219
+ const created_product = await repzo.product.create(
220
+ body as Service.Product.Create.Body
221
+ );
222
+ result.created++;
223
+ } catch (e: any) {
224
+ console.log("Create Product Failed >> ", e?.response, body);
225
+ failed_docs_report.push({
226
+ method: "create",
227
+ doc: body,
228
+ error_message: set_error(e),
229
+ });
230
+ result.failed++;
231
+ }
232
+ } else {
233
+ if (is_matched(body, repzo_product)) {
234
+ continue;
235
+ }
236
+
237
+ if (repzo_product?.variants?.length) {
238
+ body?.variants?.forEach((variant) => {
239
+ const hasMatch = repzo_product?.variants?.find(
240
+ (v) => v.integration_meta?.id == variant?.integration_meta?.id
241
+ );
242
+ if (hasMatch) {
243
+ variant._id = hasMatch._id;
244
+ }
245
+ });
246
+ }
247
+
248
+ // Update
249
+ try {
250
+ const updated_product = await repzo.product.update(
251
+ repzo_product._id,
252
+ body as Service.Product.Update.Body
253
+ );
254
+ result.updated++;
255
+ } catch (e: any) {
256
+ console.log("Update Product Failed >> ", e?.response?.data, body);
257
+ failed_docs_report.push({
258
+ method: "update",
259
+ doc_id: repzo_product?._id,
260
+ doc: body,
261
+ error_message: set_error(e),
262
+ });
263
+ result.failed++;
264
+ }
265
+ }
266
+ } catch (e) {
267
+ console.log("FetchingData Product Failed >> ", e);
268
+ failed_docs_report.push({
269
+ method: "fetchingData",
270
+ doc_id: sap_product.ITEMBARCODE,
271
+ error_message: set_error(e),
272
+ });
273
+ result.failed++;
274
+ }
275
+ }
276
+
277
+ // console.log(result);
278
+
279
+ await update_bench_time(
280
+ repzo,
281
+ commandEvent.app._id,
282
+ bench_time_key,
283
+ new_bench_time
284
+ );
285
+ await commandLog
286
+ .setStatus(
287
+ "success",
288
+ failed_docs_report.length ? failed_docs_report : null
289
+ )
290
+ .setBody(result)
291
+ .commit();
292
+ return result;
293
+ } catch (e: any) {
294
+ //@ts-ignore
295
+ console.error(e?.response?.data || e);
296
+ await commandLog.setStatus("fail", e).commit();
297
+ throw e;
298
+ }
299
+ };
300
+
301
+ const get_sap_products = async (
302
+ serviceEndPoint: string,
303
+ query?: { updateAt?: string }
304
+ ): Promise<SAPProduct[]> => {
305
+ try {
306
+ const sap_products: SAPProducts = await _create(serviceEndPoint, "/Items", {
307
+ Active: "Y",
308
+ UpdateAt: date_formatting(query?.updateAt, "YYYYMMDD:HHmmss"),
309
+ });
310
+ return sap_products.Items;
311
+ } catch (e: any) {
312
+ throw e;
313
+ }
314
+ };
315
+
316
+ const is_matched = (
317
+ body_1: { [keys: string]: any },
318
+ body_2: { [keys: string]: any }
319
+ ) => {
320
+ try {
321
+ const keys = [
322
+ "active",
323
+ "name",
324
+ "barcode",
325
+ "sku",
326
+ "sv_tax",
327
+ "category",
328
+ "measureunit_family",
329
+ "sv_measureUnit",
330
+ ];
331
+ const integration_meta_keys = [
332
+ "id",
333
+ "ITEMGROUPCODE",
334
+ "UOMGROUPENTRY",
335
+ "BRAND",
336
+ ];
337
+ const variant_keys = ["variants", "disabled", "name", "price"];
338
+ const variant_integration_meta_keys = ["integration_meta.id"];
339
+
340
+ for (let i = 0; i < keys.length; i++) {
341
+ const key = keys[i];
342
+ if (body_1[key]?.toString() !== body_2[key]?.toString()) {
343
+ return false;
344
+ }
345
+ }
346
+ for (let i = 0; i < integration_meta_keys.length; i++) {
347
+ const key = keys[i];
348
+ if (
349
+ body_1?.integration_meta?.[key]?.toString() !==
350
+ body_2?.integration_meta?.[key]?.toString()
351
+ ) {
352
+ return false;
353
+ }
354
+ }
355
+ return true;
356
+ } catch (e) {
357
+ throw e;
358
+ }
359
+ };
@@ -0,0 +1,152 @@
1
+ import Repzo from "repzo";
2
+ import { Service } from "repzo/src/types";
3
+ import { CommandEvent, Result, FailedDocsReport } from "../types";
4
+ import {
5
+ _fetch,
6
+ _create,
7
+ _update,
8
+ _delete,
9
+ update_bench_time,
10
+ date_formatting,
11
+ set_error,
12
+ } from "../util.js";
13
+
14
+ import { SAPProduct, SAPProducts } from "./product.js";
15
+
16
+ export const sync_disabled_product = async (commandEvent: CommandEvent) => {
17
+ const repzo = new Repzo(commandEvent.app.formData?.repzoApiKey, {
18
+ env: commandEvent.env,
19
+ });
20
+
21
+ const commandLog = new Repzo.CommandLog(
22
+ repzo,
23
+ commandEvent.app,
24
+ commandEvent.command
25
+ );
26
+ try {
27
+ console.log("sync_disabled_product");
28
+
29
+ const new_bench_time = new Date().toISOString();
30
+ const bench_time_key = "bench_time_product_disabled";
31
+
32
+ await commandLog.load(commandEvent.sync_id);
33
+ await commandLog
34
+ .addDetail("Repzo SAP: Started Syncing Disabled Products")
35
+ .commit();
36
+
37
+ const nameSpace: string = commandEvent.nameSpace.join("_");
38
+ const result: Result = {
39
+ sap_total: 0,
40
+ repzo_total: 0,
41
+ created: 0,
42
+ updated: 0,
43
+ failed: 0,
44
+ };
45
+ const failed_docs_report: FailedDocsReport = [];
46
+
47
+ const sap_products: SAPProduct[] = await get_sap_disabled_products(
48
+ commandEvent.app.formData.sapHostUrl,
49
+ { updateAt: commandEvent.app.options_formData[bench_time_key] }
50
+ );
51
+ result.sap_total = sap_products?.length;
52
+
53
+ await commandLog
54
+ .addDetail(
55
+ `${result.sap_total} Disabled Products in SAP changed since ${
56
+ commandEvent.app.options_formData[bench_time_key] || "ever"
57
+ }`
58
+ )
59
+ .commit();
60
+
61
+ const sap_product_query = sap_products?.map(
62
+ (product) => `${nameSpace}_${product.ITEMCODE}`
63
+ );
64
+
65
+ const repzo_products = [];
66
+ const per_page = 200;
67
+ const pages = Math.ceil(sap_product_query.length / per_page);
68
+ for (let i = 0; i < pages; i += per_page) {
69
+ const repzo_product_per_page = await repzo.product.find({
70
+ active: true,
71
+ per_page: 50000,
72
+ "integration_meta.id": sap_product_query.slice(i, i + per_page),
73
+ });
74
+ if (repzo_product_per_page?.data?.length)
75
+ repzo_products.push(...repzo_product_per_page.data);
76
+ }
77
+
78
+ result.repzo_total = repzo_products?.length;
79
+ await commandLog
80
+ .addDetail(
81
+ `${result.repzo_total} Active Products in Repzo should be disabled`
82
+ )
83
+ .commit();
84
+
85
+ for (let i = 0; i < sap_products?.length; i++) {
86
+ const sap_product: SAPProduct = sap_products[i];
87
+ const repzo_product = repzo_products.find(
88
+ (r_product) =>
89
+ r_product?.integration_meta?.id ==
90
+ `${nameSpace}_${sap_product.ITEMCODE}`
91
+ );
92
+
93
+ if (repzo_product) {
94
+ // Update
95
+ try {
96
+ const disabled_product = await repzo.product.remove(
97
+ repzo_product._id
98
+ );
99
+ result.updated++;
100
+ } catch (e: any) {
101
+ console.log("Disabled Product Failed >> ", e?.response?.data, {
102
+ ITEMCODE: sap_product.ITEMCODE,
103
+ });
104
+ failed_docs_report.push({
105
+ method: "delete",
106
+ doc_id: repzo_product?._id,
107
+ doc: { ITEMCODE: sap_product.ITEMCODE },
108
+ error_message: set_error(e),
109
+ });
110
+ result.failed++;
111
+ }
112
+ }
113
+ }
114
+
115
+ // console.log(result);
116
+
117
+ await update_bench_time(
118
+ repzo,
119
+ commandEvent.app._id,
120
+ bench_time_key,
121
+ new_bench_time
122
+ );
123
+ await commandLog
124
+ .setStatus(
125
+ "success",
126
+ failed_docs_report.length ? failed_docs_report : null
127
+ )
128
+ .setBody(result)
129
+ .commit();
130
+ return result;
131
+ } catch (e: any) {
132
+ //@ts-ignore
133
+ console.error(e?.response?.data || e);
134
+ await commandLog.setStatus("fail", e).commit();
135
+ throw e;
136
+ }
137
+ };
138
+
139
+ const get_sap_disabled_products = async (
140
+ serviceEndPoint: string,
141
+ query?: { updateAt?: string }
142
+ ): Promise<SAPProduct[]> => {
143
+ try {
144
+ const sap_products: SAPProducts = await _create(serviceEndPoint, "/Items", {
145
+ Active: "N",
146
+ UpdateAt: date_formatting(query?.updateAt, "YYYYMMDD:HHmmss"),
147
+ });
148
+ return sap_products.Items;
149
+ } catch (e: any) {
150
+ throw e;
151
+ }
152
+ };
@@ -0,0 +1,209 @@
1
+ import Repzo from "repzo";
2
+ import { Service } from "repzo/src/types";
3
+ import DataSet from "data-set-query";
4
+ import { CommandEvent, Result, FailedDocsReport } from "../types";
5
+ import {
6
+ _fetch,
7
+ _create,
8
+ _update,
9
+ _delete,
10
+ update_bench_time,
11
+ date_formatting,
12
+ set_error,
13
+ } from "../util.js";
14
+
15
+ interface SAPRep {
16
+ USERID: number; // 135;
17
+ USERDESC: string; // "MT ZIYAD AL SALAH";
18
+ DEPARTMENTCODE: string; // "D3";
19
+ USERCASHACCOUNT: string; // "124020002";
20
+ USERCHECKACCTCODE: string; // "124020003";
21
+ USERWHSCODE: string; // "MToffers";
22
+ }
23
+
24
+ interface SAPReps {
25
+ Users: SAPRep[];
26
+ }
27
+
28
+ export const sync_rep = async (commandEvent: CommandEvent) => {
29
+ const repzo = new Repzo(commandEvent.app.formData?.repzoApiKey, {
30
+ env: commandEvent.env,
31
+ });
32
+
33
+ const commandLog = new Repzo.CommandLog(
34
+ repzo,
35
+ commandEvent.app,
36
+ commandEvent.command
37
+ );
38
+ try {
39
+ console.log("sync_rep");
40
+
41
+ const new_bench_time = new Date().toISOString();
42
+ const bench_time_key = "bench_time_rep";
43
+
44
+ await commandLog.load(commandEvent.sync_id);
45
+ await commandLog.addDetail("Repzo SAP: Started Syncing Reps").commit();
46
+
47
+ const nameSpace: string = commandEvent.nameSpace.join("_");
48
+ const result: Result = {
49
+ sap_total: 0,
50
+ repzo_total: 0,
51
+ created: 0,
52
+ updated: 0,
53
+ failed: 0,
54
+ };
55
+ const failed_docs_report: FailedDocsReport = [];
56
+
57
+ const sap_reps: SAPReps = await get_sap_reps(
58
+ commandEvent.app.formData.sapHostUrl,
59
+ {}
60
+ );
61
+ result.sap_total = sap_reps?.Users?.length;
62
+
63
+ await commandLog.addDetail(`${result.sap_total} reps in SAP`).commit();
64
+
65
+ // hard code ************************************
66
+ sap_reps?.Users?.forEach((sap_rep) => {
67
+ if (
68
+ sap_rep.USERDESC.startsWith("WS ") ||
69
+ sap_rep.USERDESC.startsWith("RET ")
70
+ ) {
71
+ sap_rep.USERWHSCODE = "1";
72
+ }
73
+ if (sap_rep.USERDESC.startsWith("MT ")) {
74
+ sap_rep.USERWHSCODE = "K.A";
75
+ }
76
+ });
77
+ // **********************************************
78
+
79
+ const db = new DataSet([], { autoIndex: false });
80
+ db.createIndex({
81
+ USERID: true,
82
+ USERDESC: true,
83
+ DEPARTMENTCODE: true,
84
+ USERCASHACCOUNT: true,
85
+ USERCHECKACCTCODE: true,
86
+ USERWHSCODE: true,
87
+ });
88
+ db.load(sap_reps?.Users);
89
+
90
+ const repzo_reps = await repzo.rep.find({ per_page: 50000 });
91
+ result.repzo_total = repzo_reps?.data?.length;
92
+ await commandLog
93
+ .addDetail(`${repzo_reps?.data?.length} reps in Repzo`)
94
+ .commit();
95
+
96
+ for (let i = 0; i < sap_reps?.Users?.length; i++) {
97
+ const sap_rep: SAPRep = sap_reps.Users[i];
98
+ const repzo_rep = repzo_reps.data.find(
99
+ (r_rep) =>
100
+ r_rep.integration_meta?.id == `${nameSpace}_${sap_rep.USERID}`
101
+ );
102
+
103
+ let warehouse;
104
+ if (sap_rep.USERWHSCODE && sap_rep.USERWHSCODE != "") {
105
+ const warehouse_res = await repzo.warehouse.find({
106
+ code: sap_rep.USERWHSCODE,
107
+ });
108
+ if (warehouse_res?.data?.length) warehouse = warehouse_res.data[0]._id;
109
+ }
110
+
111
+ const body: Service.Rep.Create.Body | Service.Rep.Update.Body = {
112
+ name: sap_rep.USERDESC,
113
+ password: Math.round(Math.random() * (9999 - 1000) + 1000).toString(),
114
+ username: nameSpace + sap_rep.USERID,
115
+ integration_id: sap_rep.USERID?.toString(),
116
+ integration_meta: {
117
+ DEPARTMENTCODE: sap_rep.DEPARTMENTCODE,
118
+ USERCASHACCOUNT: sap_rep.USERCASHACCOUNT,
119
+ USERCHECKACCTCODE: sap_rep.USERCHECKACCTCODE,
120
+ USERWHSCODE: sap_rep.USERWHSCODE,
121
+ id: `${nameSpace}_${sap_rep.USERID}`,
122
+ },
123
+ assigned_warehouse: warehouse,
124
+ company_namespace: [nameSpace],
125
+ };
126
+
127
+ if (!repzo_rep) {
128
+ // Create
129
+ try {
130
+ const created_rep = await repzo.rep.create(
131
+ body as Service.Rep.Create.Body
132
+ );
133
+ result.created++;
134
+ } catch (e: any) {
135
+ console.log("Create Rep Failed >> ", e?.response, body);
136
+ failed_docs_report.push({
137
+ method: "create",
138
+ doc: body,
139
+ error_message: set_error(e),
140
+ });
141
+ result.failed++;
142
+ }
143
+ } else {
144
+ const found_identical_docs = db.search({
145
+ USERID: repzo_rep.integration_id,
146
+ USERDESC: repzo_rep.name,
147
+ DEPARTMENTCODE: repzo_rep.integration_meta?.DEPARTMENTCODE,
148
+ USERCASHACCOUNT: repzo_rep.integration_meta?.USERCASHACCOUNT,
149
+ USERCHECKACCTCODE: repzo_rep.integration_meta?.USERCHECKACCTCODE,
150
+ USERWHSCODE: repzo_rep.integration_meta?.USERWHSCODE,
151
+ });
152
+ if (found_identical_docs.length) continue;
153
+ // Update
154
+ try {
155
+ // Delete Rep.Password
156
+ delete body.password;
157
+ const updated_rep = await repzo.rep.update(
158
+ repzo_rep._id,
159
+ body as Service.Rep.Update.Body
160
+ );
161
+ result.updated++;
162
+ } catch (e: any) {
163
+ console.log("Update Rep Failed >> ", e?.response?.data, body);
164
+ failed_docs_report.push({
165
+ method: "update",
166
+ doc_id: repzo_rep?._id,
167
+ doc: body,
168
+ error_message: set_error(e),
169
+ });
170
+ result.failed++;
171
+ }
172
+ }
173
+ }
174
+
175
+ // console.log(result);
176
+
177
+ await update_bench_time(
178
+ repzo,
179
+ commandEvent.app._id,
180
+ bench_time_key,
181
+ new_bench_time
182
+ );
183
+ await commandLog
184
+ .setStatus(
185
+ "success",
186
+ failed_docs_report.length ? failed_docs_report : null
187
+ )
188
+ .setBody(result)
189
+ .commit();
190
+ return result;
191
+ } catch (e: any) {
192
+ //@ts-ignore
193
+ console.error(e?.response?.data || e);
194
+ await commandLog.setStatus("fail", e).commit();
195
+ throw e;
196
+ }
197
+ };
198
+
199
+ const get_sap_reps = async (
200
+ serviceEndPoint: string,
201
+ query?: { updateAt?: string }
202
+ ): Promise<SAPReps> => {
203
+ try {
204
+ const sap_reps: SAPReps = await _create(serviceEndPoint, "/Users", {});
205
+ return sap_reps;
206
+ } catch (e: any) {
207
+ throw e;
208
+ }
209
+ };