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