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,410 @@
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_client = async (commandEvent) => {
9
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
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_client");
25
+ const new_bench_time = new Date().toISOString();
26
+ const bench_time_key = "bench_time_client";
27
+ await commandLog.load(commandEvent.sync_id);
28
+ await commandLog.addDetail("Repzo SAP: Started Syncing Clients").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
+ };
37
+ const failed_docs_report = [];
38
+ // Get SAP CLients to be created/updated
39
+ const sap_clients = await get_sap_clients(
40
+ commandEvent.app.formData.sapHostUrl,
41
+ {
42
+ updateAt: commandEvent.app.options_formData[bench_time_key],
43
+ GroupCode: commandEvent.app.formData.GroupCode,
44
+ }
45
+ );
46
+ result.sap_total =
47
+ sap_clients === null || sap_clients === void 0
48
+ ? void 0
49
+ : sap_clients.length;
50
+ await commandLog
51
+ .addDetail(
52
+ `${result.sap_total} Customers in SAP changed since ${
53
+ commandEvent.app.options_formData[bench_time_key] || "ever"
54
+ }`
55
+ )
56
+ .commit();
57
+ // Get SAP CLients to be created/updated
58
+ const sap_all_clients = await get_sap_clients(
59
+ commandEvent.app.formData.sapHostUrl,
60
+ {}
61
+ );
62
+ result.sap_total =
63
+ sap_clients === null || sap_clients === void 0
64
+ ? void 0
65
+ : sap_clients.length;
66
+ // Get Repzo Tags
67
+ const repzo_tags = await repzo.tag.find({
68
+ disabled: true,
69
+ per_page: 50000,
70
+ });
71
+ // Get Repzo Channels
72
+ const repzo_channels = await repzo.channel.find({
73
+ disabled: true,
74
+ per_page: 50000,
75
+ });
76
+ // Get Repzo PaymentTerms
77
+ const repzo_payment_terms = await repzo.paymentTerm.find({
78
+ disabled: false,
79
+ per_page: 50000,
80
+ });
81
+ // Get Repzo PriceLists
82
+ const repzo_price_lists = await repzo.priceList.find({
83
+ disabled: false,
84
+ per_page: 50000,
85
+ });
86
+ // Get Repzo Clients
87
+ const repzo_clients = await repzo.client.find({
88
+ per_page: 50000,
89
+ project: ["_id", "integration_meta"],
90
+ });
91
+ result.repzo_total =
92
+ (_b =
93
+ repzo_clients === null || repzo_clients === void 0
94
+ ? void 0
95
+ : repzo_clients.data) === null || _b === void 0
96
+ ? void 0
97
+ : _b.length;
98
+ await commandLog
99
+ .addDetail(
100
+ `${
101
+ (_c =
102
+ repzo_clients === null || repzo_clients === void 0
103
+ ? void 0
104
+ : repzo_clients.data) === null || _c === void 0
105
+ ? void 0
106
+ : _c.length
107
+ } Clients in Repzo`
108
+ )
109
+ .commit();
110
+ for (
111
+ let i = 0;
112
+ i <
113
+ (sap_clients === null || sap_clients === void 0
114
+ ? void 0
115
+ : sap_clients.length);
116
+ i++
117
+ ) {
118
+ const sap_client = sap_clients[i];
119
+ const repzo_client = repzo_clients.data.find((r_client) => {
120
+ var _a;
121
+ return (
122
+ ((_a = r_client.integration_meta) === null || _a === void 0
123
+ ? void 0
124
+ : _a.id) == `${nameSpace}_${sap_client.CLIENTID}`
125
+ );
126
+ });
127
+ const tag =
128
+ (_d =
129
+ repzo_tags === null || repzo_tags === void 0
130
+ ? void 0
131
+ : repzo_tags.data) === null || _d === void 0
132
+ ? void 0
133
+ : _d.find((tag) => {
134
+ var _a;
135
+ return (
136
+ ((_a = tag.integration_meta) === null || _a === void 0
137
+ ? void 0
138
+ : _a.id) == `${nameSpace}_${sap_client.TERRITORYID}`
139
+ );
140
+ });
141
+ const channel =
142
+ (_e =
143
+ repzo_channels === null || repzo_channels === void 0
144
+ ? void 0
145
+ : repzo_channels.data) === null || _e === void 0
146
+ ? void 0
147
+ : _e.find((channel) => {
148
+ var _a;
149
+ return (
150
+ ((_a = channel.integration_meta) === null || _a === void 0
151
+ ? void 0
152
+ : _a.id) == `${nameSpace}_${sap_client.CLIENTGROUP}`
153
+ );
154
+ });
155
+ const paymentTerm =
156
+ (_f =
157
+ repzo_payment_terms === null || repzo_payment_terms === void 0
158
+ ? void 0
159
+ : repzo_payment_terms.data) === null || _f === void 0
160
+ ? void 0
161
+ : _f.find((paymentTerm) => {
162
+ var _a;
163
+ return (
164
+ ((_a = paymentTerm.integration_meta) === null || _a === void 0
165
+ ? void 0
166
+ : _a.id) == `${nameSpace}_${sap_client.PAYMENTTERM}`
167
+ );
168
+ });
169
+ const priceList =
170
+ (_g =
171
+ repzo_price_lists === null || repzo_price_lists === void 0
172
+ ? void 0
173
+ : repzo_price_lists.data) === null || _g === void 0
174
+ ? void 0
175
+ : _g.find((pricelist) => {
176
+ var _a;
177
+ return (
178
+ ((_a = pricelist.integration_meta) === null || _a === void 0
179
+ ? void 0
180
+ : _a.id) == `${nameSpace}_${sap_client.CLIENTPRICELISTID}`
181
+ );
182
+ });
183
+ let parent;
184
+ if (sap_client.PARENTCODE) {
185
+ parent = sap_all_clients.find(
186
+ (c) => c.CLIENTID == sap_client.PARENTCODE
187
+ );
188
+ }
189
+ const credit_limit = parent
190
+ ? parent.CLIENTCREDITLIMIT
191
+ : sap_client.CLIENTCREDITLIMIT;
192
+ const client_credit_consumed = parent
193
+ ? parent.CLIENTCREDITCONSUMED
194
+ : sap_client.CLIENTCREDITCONSUMED;
195
+ const body = {
196
+ integration_meta: {
197
+ id: `${nameSpace}_${sap_client.CLIENTID}`,
198
+ PAYMENTTERM: sap_client.PAYMENTTERM,
199
+ CLIENTCREDITCONSUMED: parent
200
+ ? parent.CLIENTCREDITCONSUMED
201
+ : sap_client.CLIENTCREDITCONSUMED,
202
+ CLIENTMAXCHEQUEVALUE: parent
203
+ ? parent.CLIENTMAXCHEQUEVALUE
204
+ : sap_client.CLIENTMAXCHEQUEVALUE,
205
+ CLIENTCREDITLIMIT: parent
206
+ ? parent.CLIENTCREDITLIMIT
207
+ : sap_client.CLIENTCREDITLIMIT,
208
+ CLIENTPRICELISTID: sap_client.CLIENTPRICELISTID,
209
+ },
210
+ client_code: sap_client.CLIENTID,
211
+ name: sap_client.CLIENTDESC,
212
+ local_name: sap_client.CLIENTDESCF ? sap_client.CLIENTDESCF : undefined,
213
+ city: sap_client.CLIENTCITY,
214
+ state: sap_client.CLIENTCOUNTY,
215
+ country: sap_client.CLIENTCOUNTRY,
216
+ contact_name: sap_client.CLIENTCONTACTPERSON,
217
+ phone: sap_client.CLIENTPHONE1,
218
+ cell_phone: sap_client.CLIENTPHONE2,
219
+ comment: sap_client.CLIENTNOTE,
220
+ formatted_address: sap_client.CLIENTADDRESSID,
221
+ tags: tag ? [tag._id] : [],
222
+ credit_limit:
223
+ sap_client.CLIENTGROUP == "Cash Van"
224
+ ? 1000000000
225
+ : credit_limit && Math.round(credit_limit * 1000),
226
+ financials: {
227
+ credit_limit:
228
+ sap_client.CLIENTGROUP == "Cash Van"
229
+ ? 1000000000
230
+ : credit_limit && Math.round(credit_limit * 1000),
231
+ },
232
+ channel: channel ? channel._id : undefined,
233
+ paymentTerm: paymentTerm ? paymentTerm._id : undefined,
234
+ sv_priceList: priceList ? priceList._id : undefined,
235
+ disabled: sap_client.ACTIVE == "Y" ? false : true,
236
+ payment_type: sap_client.CLIENTGROUP == "Cash Van" ? "cash" : "credit",
237
+ integrated_client_balance:
238
+ client_credit_consumed && Math.round(client_credit_consumed * 1000),
239
+ };
240
+ if (!repzo_client) {
241
+ // Create
242
+ try {
243
+ const created_client = await repzo.client.create(body);
244
+ result.created++;
245
+ } catch (e) {
246
+ console.log(
247
+ "Create Client Failed >> ",
248
+ e === null || e === void 0 ? void 0 : e.response,
249
+ body
250
+ );
251
+ failed_docs_report.push({
252
+ method: "create",
253
+ doc: body,
254
+ error_message: set_error(e),
255
+ });
256
+ result.failed++;
257
+ }
258
+ } else {
259
+ const repzo_original_doc = await repzo.client.get(repzo_client._id);
260
+ // console.log("match", is_matched(body, repzo_original_doc));
261
+ if (is_matched(body, repzo_original_doc)) {
262
+ continue;
263
+ }
264
+ // Update
265
+ try {
266
+ const updated_client = await repzo.client.update(
267
+ repzo_client._id,
268
+ body
269
+ );
270
+ result.updated++;
271
+ } catch (e) {
272
+ console.log(
273
+ "Update Client Failed >> ",
274
+ (_h = e === null || e === void 0 ? void 0 : e.response) === null ||
275
+ _h === void 0
276
+ ? void 0
277
+ : _h.data,
278
+ body
279
+ );
280
+ failed_docs_report.push({
281
+ method: "update",
282
+ doc_id:
283
+ repzo_client === null || repzo_client === void 0
284
+ ? void 0
285
+ : repzo_client._id,
286
+ doc: body,
287
+ error_message: set_error(e),
288
+ });
289
+ result.failed++;
290
+ }
291
+ }
292
+ }
293
+ // console.log(result);
294
+ await update_bench_time(
295
+ repzo,
296
+ commandEvent.app._id,
297
+ bench_time_key,
298
+ new_bench_time
299
+ );
300
+ await commandLog
301
+ .setStatus(
302
+ "success",
303
+ failed_docs_report.length ? failed_docs_report : null
304
+ )
305
+ .setBody(result)
306
+ .commit();
307
+ return result;
308
+ } catch (e) {
309
+ //@ts-ignore
310
+ console.error(
311
+ ((_j = e === null || e === void 0 ? void 0 : e.response) === null ||
312
+ _j === void 0
313
+ ? void 0
314
+ : _j.data) || e
315
+ );
316
+ await commandLog.setStatus("fail", e).commit();
317
+ throw e;
318
+ }
319
+ };
320
+ const get_sap_clients = async (serviceEndPoint, query) => {
321
+ try {
322
+ const sap_clients = await _create(serviceEndPoint, "/Customers", {
323
+ Active: "Y",
324
+ Frozen: "N",
325
+ UpdateAt: date_formatting(
326
+ query === null || query === void 0 ? void 0 : query.updateAt,
327
+ "YYYYMMDD:HHmmss"
328
+ ),
329
+ GroupCode:
330
+ (query === null || query === void 0 ? void 0 : query.GroupCode) || "",
331
+ });
332
+ return sap_clients.Customers;
333
+ } catch (e) {
334
+ throw e;
335
+ }
336
+ };
337
+ const is_matched = (body_1, body_2) => {
338
+ var _a, _b, _c, _d, _e, _f;
339
+ try {
340
+ const keys = [
341
+ "client_code",
342
+ "name",
343
+ "local_name",
344
+ "city",
345
+ "state",
346
+ "country",
347
+ "contact_name",
348
+ "phone",
349
+ "cell_phone",
350
+ "comment",
351
+ "formatted_address",
352
+ "tags",
353
+ "credit_limit",
354
+ "financials",
355
+ "channel",
356
+ "paymentTerm",
357
+ "sv_priceList",
358
+ "disabled",
359
+ "payment_type",
360
+ "integrated_client_balance",
361
+ ];
362
+ const integration_meta_keys = [
363
+ "id",
364
+ "PAYMENTTERM",
365
+ "CLIENTCREDITCONSUMED",
366
+ "CLIENTMAXCHEQUEVALUE",
367
+ "CLIENTCREDITLIMIT",
368
+ "CLIENTPRICELISTID",
369
+ ];
370
+ for (let i = 0; i < keys.length; i++) {
371
+ const key = keys[i];
372
+ if (
373
+ ((_a = body_1[key]) === null || _a === void 0
374
+ ? void 0
375
+ : _a.toString()) !==
376
+ ((_b = body_2[key]) === null || _b === void 0 ? void 0 : _b.toString())
377
+ ) {
378
+ return false;
379
+ }
380
+ }
381
+ for (let i = 0; i < integration_meta_keys.length; i++) {
382
+ const key = keys[i];
383
+ if (
384
+ ((_d =
385
+ (_c =
386
+ body_1 === null || body_1 === void 0
387
+ ? void 0
388
+ : body_1.integration_meta) === null || _c === void 0
389
+ ? void 0
390
+ : _c[key]) === null || _d === void 0
391
+ ? void 0
392
+ : _d.toString()) !==
393
+ ((_f =
394
+ (_e =
395
+ body_2 === null || body_2 === void 0
396
+ ? void 0
397
+ : body_2.integration_meta) === null || _e === void 0
398
+ ? void 0
399
+ : _e[key]) === null || _f === void 0
400
+ ? void 0
401
+ : _f.toString())
402
+ ) {
403
+ return false;
404
+ }
405
+ }
406
+ return true;
407
+ } catch (e) {
408
+ throw e;
409
+ }
410
+ };
@@ -0,0 +1,4 @@
1
+ import { CommandEvent, Result } from "../types";
2
+ export declare const sync_disabled_client: (
3
+ commandEvent: CommandEvent
4
+ ) => Promise<Result>;
@@ -0,0 +1,183 @@
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_client = 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_client");
25
+ const new_bench_time = new Date().toISOString();
26
+ const bench_time_key = "bench_time_disabled_client";
27
+ await commandLog.load(commandEvent.sync_id);
28
+ await commandLog
29
+ .addDetail("Repzo SAP: Started Syncing Disabled Clients")
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_disabled_clients = await get_sap_clients(
41
+ commandEvent.app.formData.sapHostUrl,
42
+ {
43
+ updateAt: commandEvent.app.options_formData[bench_time_key],
44
+ GroupCode: commandEvent.app.formData.GroupCode,
45
+ }
46
+ );
47
+ result.sap_total =
48
+ sap_disabled_clients === null || sap_disabled_clients === void 0
49
+ ? void 0
50
+ : sap_disabled_clients.length;
51
+ await commandLog
52
+ .addDetail(
53
+ `${result.sap_total} Disabled Clients in SAP changed since ${
54
+ commandEvent.app.options_formData[bench_time_key] || "ever"
55
+ }`
56
+ )
57
+ .commit();
58
+ const sap_client_query =
59
+ sap_disabled_clients === null || sap_disabled_clients === void 0
60
+ ? void 0
61
+ : sap_disabled_clients.map(
62
+ (client) => `${nameSpace}_${client.CLIENTID}`
63
+ );
64
+ const repzo_disabled_clients = [];
65
+ const per_page = 200;
66
+ const pages = Math.ceil(sap_client_query.length / per_page);
67
+ for (let i = 0; i < pages; i += per_page) {
68
+ const repzo_clients_per_page = await repzo.client.find({
69
+ disabled: false,
70
+ project: ["_id", "integration_meta"],
71
+ per_page: 50000,
72
+ "integration_meta.id": sap_client_query.slice(i, i + per_page),
73
+ });
74
+ if (
75
+ (_b =
76
+ repzo_clients_per_page === null || repzo_clients_per_page === void 0
77
+ ? void 0
78
+ : repzo_clients_per_page.data) === null || _b === void 0
79
+ ? void 0
80
+ : _b.length
81
+ )
82
+ repzo_disabled_clients.push(...repzo_clients_per_page.data);
83
+ }
84
+ result.repzo_total =
85
+ repzo_disabled_clients === null || repzo_disabled_clients === void 0
86
+ ? void 0
87
+ : repzo_disabled_clients.length;
88
+ await commandLog
89
+ .addDetail(`${result.repzo_total} Matched Active Clients in Repzo`)
90
+ .commit();
91
+ for (
92
+ let i = 0;
93
+ i <
94
+ (sap_disabled_clients === null || sap_disabled_clients === void 0
95
+ ? void 0
96
+ : sap_disabled_clients.length);
97
+ i++
98
+ ) {
99
+ const sap_client = sap_disabled_clients[i];
100
+ const repzo_client =
101
+ repzo_disabled_clients === null || repzo_disabled_clients === void 0
102
+ ? void 0
103
+ : repzo_disabled_clients.find((r_client) => {
104
+ var _a;
105
+ return (
106
+ ((_a = r_client.integration_meta) === null || _a === void 0
107
+ ? void 0
108
+ : _a.id) == `${nameSpace}_${sap_client.CLIENTID}`
109
+ );
110
+ });
111
+ if (repzo_client) {
112
+ // Update
113
+ try {
114
+ const disabled_client = await repzo.client.remove(repzo_client._id);
115
+ result.updated++;
116
+ } catch (e) {
117
+ console.log(
118
+ "Disable Client Failed >> ",
119
+ (_c = e === null || e === void 0 ? void 0 : e.response) === null ||
120
+ _c === void 0
121
+ ? void 0
122
+ : _c.data,
123
+ {
124
+ CLIENTID: sap_client.CLIENTID,
125
+ }
126
+ );
127
+ failed_docs_report.push({
128
+ method: "delete",
129
+ doc_id:
130
+ repzo_client === null || repzo_client === void 0
131
+ ? void 0
132
+ : repzo_client._id,
133
+ doc: { CLIENTID: sap_client.CLIENTID },
134
+ error_message: set_error(e),
135
+ });
136
+ result.failed++;
137
+ }
138
+ }
139
+ }
140
+ // console.log(result);
141
+ await update_bench_time(
142
+ repzo,
143
+ commandEvent.app._id,
144
+ bench_time_key,
145
+ new_bench_time
146
+ );
147
+ await commandLog
148
+ .setStatus(
149
+ "success",
150
+ failed_docs_report.length ? failed_docs_report : null
151
+ )
152
+ .setBody(result)
153
+ .commit();
154
+ return result;
155
+ } catch (e) {
156
+ //@ts-ignore
157
+ console.error(
158
+ ((_d = e === null || e === void 0 ? void 0 : e.response) === null ||
159
+ _d === void 0
160
+ ? void 0
161
+ : _d.data) || e
162
+ );
163
+ await commandLog.setStatus("fail", e).commit();
164
+ throw e;
165
+ }
166
+ };
167
+ const get_sap_clients = async (serviceEndPoint, query) => {
168
+ try {
169
+ const sap_clients = await _create(serviceEndPoint, "/Customers", {
170
+ Active: "N",
171
+ Frozen: "N",
172
+ UpdateAt: date_formatting(
173
+ query === null || query === void 0 ? void 0 : query.updateAt,
174
+ "YYYYMMDD:HHmmss"
175
+ ),
176
+ GroupCode:
177
+ (query === null || query === void 0 ? void 0 : query.GroupCode) || "",
178
+ });
179
+ return sap_clients.Customers;
180
+ } catch (e) {
181
+ throw e;
182
+ }
183
+ };
@@ -0,0 +1,23 @@
1
+ import { Command, CommandEvent, Result } from "./../types";
2
+ export declare const commands: (CommandEvent: CommandEvent) => Promise<
3
+ | void
4
+ | Result
5
+ | {
6
+ PL: {
7
+ created: number;
8
+ updated: number;
9
+ failed: number;
10
+ };
11
+ PL_items: {
12
+ created: number;
13
+ updated: number;
14
+ failed: number;
15
+ };
16
+ sap_total: number;
17
+ repzo_total: number;
18
+ repzo_PL_items: number;
19
+ sap_UoMs_total: number;
20
+ repzo_products_total: number;
21
+ }
22
+ >;
23
+ export declare const commandsList: Command[];
@@ -0,0 +1,82 @@
1
+ import { join } from "./join.js";
2
+ import { basic } from "./basic.js";
3
+ import { sync_warehouse } from "./warehouse.js";
4
+ import { sync_rep } from "./rep.js";
5
+ import { sync_tax } from "./tax.js";
6
+ import { sync_tag } from "./tag.js";
7
+ import { sync_measureunit } from "./measureunit.js";
8
+ import { sync_measureunit_family } from "./measureunit_family.js";
9
+ import { sync_category } from "./category.js";
10
+ import { sync_channel } from "./channel.js";
11
+ import { sync_payment_term } from "./payment_term.js";
12
+ import { sync_bank } from "./bank.js";
13
+ import { sync_product } from "./product.js";
14
+ import { sync_disabled_product } from "./product_disabled.js";
15
+ import { sync_price_list } from "./price_list.js";
16
+ import { sync_client } from "./client.js";
17
+ import { sync_disabled_client } from "./client_disabled.js";
18
+ import { adjust_inventory } from "./adjust_inventory.js";
19
+ export const commands = async (CommandEvent) => {
20
+ switch (CommandEvent.command) {
21
+ case "join":
22
+ return await join(CommandEvent);
23
+ case "basic":
24
+ return await basic(CommandEvent);
25
+ case "warehouse":
26
+ return await sync_warehouse(CommandEvent);
27
+ case "rep":
28
+ return await sync_rep(CommandEvent);
29
+ case "tax":
30
+ return await sync_tax(CommandEvent);
31
+ case "tag":
32
+ return await sync_tag(CommandEvent);
33
+ case "measureunit":
34
+ return await sync_measureunit(CommandEvent);
35
+ case "measureunit_family":
36
+ return await sync_measureunit_family(CommandEvent);
37
+ case "category":
38
+ return await sync_category(CommandEvent);
39
+ case "channel":
40
+ return await sync_channel(CommandEvent);
41
+ case "payment_term":
42
+ return await sync_payment_term(CommandEvent);
43
+ case "bank":
44
+ return await sync_bank(CommandEvent);
45
+ case "product":
46
+ return await sync_product(CommandEvent);
47
+ case "disabled_product":
48
+ return await sync_disabled_product(CommandEvent);
49
+ case "price_list":
50
+ return await sync_price_list(CommandEvent);
51
+ case "client":
52
+ return await sync_client(CommandEvent);
53
+ case "disabled_client":
54
+ return await sync_disabled_client(CommandEvent);
55
+ case "adjust_inventory":
56
+ return await adjust_inventory(CommandEvent);
57
+ default:
58
+ throw `Route: ${CommandEvent.command} not found`;
59
+ }
60
+ };
61
+ export const commandsList = [
62
+ {
63
+ command: "basic",
64
+ name: "Full Sync",
65
+ description: "",
66
+ },
67
+ {
68
+ command: "join",
69
+ name: "Join",
70
+ description: "",
71
+ },
72
+ {
73
+ command: "warehouse",
74
+ name: "Sync Warehouse",
75
+ description: "Sync warehouses From SAP to Repzo",
76
+ },
77
+ {
78
+ command: "rep",
79
+ name: "Sync Reps",
80
+ description: "Sync Reps From SAP to Repzo",
81
+ },
82
+ ];
@@ -0,0 +1,2 @@
1
+ import { CommandEvent } from "../types";
2
+ export declare const join: (commandEvent: CommandEvent) => Promise<void>;