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,173 @@
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 SAPTag {
16
+ TerritoryID: number; // 754;
17
+ Description: string; // "الاردن";
18
+ }
19
+
20
+ interface SAPTags {
21
+ result: "Success";
22
+ Territories: SAPTag[];
23
+ }
24
+
25
+ export const sync_tag = async (commandEvent: CommandEvent) => {
26
+ const repzo = new Repzo(commandEvent.app.formData?.repzoApiKey, {
27
+ env: commandEvent.env,
28
+ });
29
+
30
+ const commandLog = new Repzo.CommandLog(
31
+ repzo,
32
+ commandEvent.app,
33
+ commandEvent.command
34
+ );
35
+ try {
36
+ console.log("sync_tag");
37
+
38
+ const new_bench_time = new Date().toISOString();
39
+ const bench_time_key = "bench_time_tag";
40
+
41
+ await commandLog.load(commandEvent.sync_id);
42
+ await commandLog.addDetail("Repzo SAP: Started Syncing Tags").commit();
43
+
44
+ const nameSpace: string = commandEvent.nameSpace.join("_");
45
+ const result: Result = {
46
+ sap_total: 0,
47
+ repzo_total: 0,
48
+ created: 0,
49
+ updated: 0,
50
+ failed: 0,
51
+ };
52
+ const failed_docs_report: FailedDocsReport = [];
53
+
54
+ const sap_tags: SAPTags = await get_sap_tags(
55
+ commandEvent.app.formData.sapHostUrl,
56
+ {}
57
+ );
58
+ result.sap_total = sap_tags?.Territories?.length;
59
+
60
+ await commandLog
61
+ .addDetail(`${result.sap_total} Territories in SAP`)
62
+ .commit();
63
+
64
+ const db = new DataSet([], { autoIndex: false });
65
+ db.createIndex({
66
+ TerritoryID: true,
67
+ Description: true,
68
+ });
69
+ db.load(sap_tags?.Territories);
70
+
71
+ const repzo_tags = await repzo.tag.find({ type: "area", per_page: 50000 });
72
+ result.repzo_total = repzo_tags?.data?.length;
73
+ await commandLog
74
+ .addDetail(`${repzo_tags?.data?.length} Area Tags in Repzo`)
75
+ .commit();
76
+
77
+ for (let i = 0; i < sap_tags?.Territories?.length; i++) {
78
+ const sap_tag: SAPTag = sap_tags.Territories[i];
79
+ const repzo_tag = repzo_tags.data.find(
80
+ (r_tag) =>
81
+ r_tag.integration_meta?.id == `${nameSpace}_${sap_tag.TerritoryID}`
82
+ );
83
+
84
+ const body: Service.Tag.Create.Body | Service.Tag.Update.Body = {
85
+ tag: sap_tag.Description,
86
+ type: "area",
87
+ disabled: false,
88
+ integration_meta: {
89
+ id: `${nameSpace}_${sap_tag.TerritoryID}`,
90
+ TerritoryID: sap_tag.TerritoryID,
91
+ },
92
+ company_namespace: [nameSpace],
93
+ };
94
+
95
+ if (!repzo_tag) {
96
+ // Create
97
+ try {
98
+ const created_tag = await repzo.tag.create(
99
+ body as Service.Tag.Create.Body
100
+ );
101
+ result.created++;
102
+ } catch (e: any) {
103
+ console.log("Create Tag Failed >> ", e?.response, body);
104
+ failed_docs_report.push({
105
+ method: "create",
106
+ doc: body,
107
+ error_message: set_error(e),
108
+ });
109
+ result.failed++;
110
+ }
111
+ } else {
112
+ const found_identical_docs = db.search({
113
+ TerritoryID: repzo_tag.integration_meta?.TerritoryID,
114
+ Description: repzo_tag.tag,
115
+ });
116
+ if (found_identical_docs.length) continue;
117
+ // Update
118
+ try {
119
+ const updated_tag = await repzo.tag.update(
120
+ repzo_tag._id,
121
+ body as Service.Tag.Update.Body
122
+ );
123
+ result.updated++;
124
+ } catch (e: any) {
125
+ console.log("Update Tag Failed >> ", e?.response?.data, body);
126
+ failed_docs_report.push({
127
+ method: "update",
128
+ doc_id: repzo_tag?._id,
129
+ doc: body,
130
+ error_message: set_error(e),
131
+ });
132
+ result.failed++;
133
+ }
134
+ }
135
+ }
136
+
137
+ // console.log(result);
138
+
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: any) {
154
+ //@ts-ignore
155
+ console.error(e?.response?.data || e);
156
+ await commandLog.setStatus("fail", e).commit();
157
+ throw e;
158
+ }
159
+ };
160
+
161
+ const get_sap_tags = async (
162
+ serviceEndPoint: string,
163
+ query?: { updateAt?: string }
164
+ ): Promise<SAPTags> => {
165
+ try {
166
+ const sap_tags: SAPTags = await _create(serviceEndPoint, "/Territories", {
167
+ Inactive: "N",
168
+ });
169
+ return sap_tags;
170
+ } catch (e: any) {
171
+ throw e;
172
+ }
173
+ };
@@ -0,0 +1,178 @@
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 SAPTax {
16
+ TaxCode: string; // "P4",
17
+ TaxName: string; // "امانات ضريبة مشتريات محلية% 4",
18
+ TaxRate: number; // 4.0,
19
+ Inactive: string; // "N"
20
+ }
21
+
22
+ interface SAPTaxes {
23
+ result: "Success";
24
+ Taxes: SAPTax[];
25
+ }
26
+
27
+ export const sync_tax = async (commandEvent: CommandEvent) => {
28
+ const repzo = new Repzo(commandEvent.app.formData?.repzoApiKey, {
29
+ env: commandEvent.env,
30
+ });
31
+
32
+ const commandLog = new Repzo.CommandLog(
33
+ repzo,
34
+ commandEvent.app,
35
+ commandEvent.command
36
+ );
37
+ try {
38
+ console.log("sync_tax");
39
+
40
+ const new_bench_time = new Date().toISOString();
41
+ const bench_time_key = "bench_time_tax";
42
+
43
+ await commandLog.load(commandEvent.sync_id);
44
+ await commandLog.addDetail("Repzo SAP: Started Syncing Taxes").commit();
45
+
46
+ const nameSpace: string = commandEvent.nameSpace.join("_");
47
+ const result: Result = {
48
+ sap_total: 0,
49
+ repzo_total: 0,
50
+ created: 0,
51
+ updated: 0,
52
+ failed: 0,
53
+ };
54
+ const failed_docs_report: FailedDocsReport = [];
55
+
56
+ const sap_taxes: SAPTaxes = await get_sap_taxes(
57
+ commandEvent.app.formData.sapHostUrl,
58
+ {}
59
+ );
60
+ result.sap_total = sap_taxes?.Taxes?.length;
61
+
62
+ await commandLog.addDetail(`${result.sap_total} taxes in SAP`).commit();
63
+
64
+ const db = new DataSet([], { autoIndex: false });
65
+ db.createIndex({
66
+ TaxCode: true,
67
+ TaxName: true,
68
+ TaxRate: true,
69
+ type: "additive",
70
+ });
71
+ db.load(sap_taxes?.Taxes);
72
+
73
+ const repzo_taxes = await repzo.tax.find({ per_page: 50000 });
74
+ result.repzo_total = repzo_taxes?.data?.length;
75
+ await commandLog
76
+ .addDetail(`${repzo_taxes?.data?.length} taxes in Repzo`)
77
+ .commit();
78
+
79
+ for (let i = 0; i < sap_taxes?.Taxes?.length; i++) {
80
+ const sap_tax: SAPTax = sap_taxes.Taxes[i];
81
+ const repzo_tax = repzo_taxes.data.find(
82
+ (r_tax) =>
83
+ r_tax.integration_meta?.id == `${nameSpace}_${sap_tax.TaxCode}`
84
+ );
85
+
86
+ const body: Service.Tax.Create.Body | Service.Tax.Update.Body = {
87
+ name: sap_tax.TaxName,
88
+ rate: Number(sap_tax.TaxRate / 100),
89
+ type: "additive", // hardcode
90
+ disabled: sap_tax.Inactive === "N" ? false : true,
91
+ integration_meta: {
92
+ id: `${nameSpace}_${sap_tax.TaxCode}`,
93
+ TaxCode: sap_tax.TaxCode,
94
+ },
95
+ company_namespace: [nameSpace],
96
+ };
97
+
98
+ if (!repzo_tax) {
99
+ // Create
100
+ try {
101
+ const created_tax = await repzo.tax.create(
102
+ body as Service.Tax.Create.Body
103
+ );
104
+ result.created++;
105
+ } catch (e: any) {
106
+ console.log("Create Tax Failed >> ", e?.response, body);
107
+ failed_docs_report.push({
108
+ method: "create",
109
+ doc: body,
110
+ error_message: set_error(e),
111
+ });
112
+ result.failed++;
113
+ }
114
+ } else {
115
+ const found_identical_docs = db.search({
116
+ TaxCode: repzo_tax.integration_meta?.TaxCode,
117
+ TaxName: repzo_tax.name,
118
+ TaxRate: repzo_tax.rate * 100,
119
+ type: repzo_tax.type,
120
+ });
121
+ if (found_identical_docs.length) continue;
122
+ // Update
123
+ try {
124
+ const updated_tax = await repzo.tax.update(
125
+ repzo_tax._id,
126
+ body as Service.Tax.Update.Body
127
+ );
128
+ result.updated++;
129
+ } catch (e: any) {
130
+ console.log("Update Tax Failed >> ", e?.response?.data, body);
131
+ failed_docs_report.push({
132
+ method: "update",
133
+ doc_id: repzo_tax?._id,
134
+ doc: body,
135
+ error_message: set_error(e),
136
+ });
137
+ result.failed++;
138
+ }
139
+ }
140
+ }
141
+
142
+ // console.log(result);
143
+
144
+ await update_bench_time(
145
+ repzo,
146
+ commandEvent.app._id,
147
+ bench_time_key,
148
+ new_bench_time
149
+ );
150
+ await commandLog
151
+ .setStatus(
152
+ "success",
153
+ failed_docs_report.length ? failed_docs_report : null
154
+ )
155
+ .setBody(result)
156
+ .commit();
157
+ return result;
158
+ } catch (e: any) {
159
+ //@ts-ignore
160
+ console.error(e?.response?.data || e);
161
+ await commandLog.setStatus("fail", e).commit();
162
+ throw e;
163
+ }
164
+ };
165
+
166
+ const get_sap_taxes = async (
167
+ serviceEndPoint: string,
168
+ query?: { updateAt?: string }
169
+ ): Promise<SAPTaxes> => {
170
+ try {
171
+ const sap_taxes: SAPTaxes = await _create(serviceEndPoint, "/Taxes", {
172
+ Inactive: "N",
173
+ });
174
+ return sap_taxes;
175
+ } catch (e: any) {
176
+ throw e;
177
+ }
178
+ };
@@ -0,0 +1,204 @@
1
+ import Repzo from "repzo";
2
+ import DataSet from "data-set-query";
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
+ interface WarehouseBody {
15
+ _id?: string;
16
+ name: string;
17
+ type: "van" | "main";
18
+ disabled?: boolean;
19
+ code?: string;
20
+ integration_meta?: { id: string };
21
+ }
22
+
23
+ interface SAPWarehouse {
24
+ WAREHOUSECODE: string;
25
+ WAREHOUSENAME: string;
26
+ LOCKED: string; // "N";
27
+ INACTIVE: string; // "N";
28
+ CREATEDATE: string; // "2021-12-20T21:00:00Z";
29
+ UPDATEDATE: string; // "2021-12-29T21:00:00Z";
30
+ }
31
+
32
+ interface SAPWarehouses {
33
+ result: "Success";
34
+ Warehouses: SAPWarehouse[];
35
+ }
36
+
37
+ export const sync_warehouse = async (commandEvent: CommandEvent) => {
38
+ const repzo = new Repzo(commandEvent.app.formData?.repzoApiKey, {
39
+ env: commandEvent.env,
40
+ });
41
+ const commandLog = new Repzo.CommandLog(
42
+ repzo,
43
+ commandEvent.app,
44
+ commandEvent.command
45
+ );
46
+ try {
47
+ console.log("sync_warehouse");
48
+ const new_bench_time = new Date().toISOString();
49
+ const bench_time_key = "bench_time_warehouse";
50
+
51
+ await commandLog.load(commandEvent.sync_id);
52
+ await commandLog
53
+ .addDetail("Repzo SAP: Started Syncing Warehouses")
54
+ .commit();
55
+
56
+ const nameSpace = commandEvent.nameSpace.join("_");
57
+ const result: Result = {
58
+ sap_total: 0,
59
+ repzo_total: 0,
60
+ created: 0,
61
+ updated: 0,
62
+ failed: 0,
63
+ };
64
+ const failed_docs_report: FailedDocsReport = [];
65
+
66
+ const sap_warehouses: SAPWarehouses = await get_sap_warehouses(
67
+ commandEvent.app.formData.sapHostUrl,
68
+ {
69
+ updateAt:
70
+ commandEvent.app.formData.warehouseDefaultUpdateDate ||
71
+ commandEvent.app.options_formData[bench_time_key],
72
+ }
73
+ );
74
+ result.sap_total = sap_warehouses?.Warehouses?.length;
75
+
76
+ await commandLog
77
+ .addDetail(
78
+ `${sap_warehouses?.Warehouses?.length} warehouses changed since ${
79
+ commandEvent.app.formData.warehouseDefaultUpdateDate ||
80
+ commandEvent.app.options_formData[bench_time_key] ||
81
+ "ever"
82
+ }`
83
+ )
84
+ .commit();
85
+
86
+ const db = new DataSet([], { autoIndex: false });
87
+ db.createIndex({
88
+ WAREHOUSECODE: true,
89
+ WAREHOUSENAME: true,
90
+ });
91
+ db.load(sap_warehouses?.Warehouses);
92
+
93
+ const repzo_warehouses = await repzo.warehouse.find({ per_page: 50000 });
94
+ result.repzo_total = repzo_warehouses?.data?.length;
95
+ await commandLog
96
+ .addDetail(`${repzo_warehouses?.data?.length} warehouses in Repzo`)
97
+ .commit();
98
+
99
+ for (let i = 0; i < sap_warehouses?.Warehouses?.length; i++) {
100
+ const sap_warehouse: SAPWarehouse = sap_warehouses.Warehouses[i];
101
+ const repzo_warehouse = repzo_warehouses.data.find(
102
+ (r_warehouse) =>
103
+ r_warehouse.code == sap_warehouse.WAREHOUSECODE ||
104
+ r_warehouse.name == sap_warehouse.WAREHOUSENAME
105
+ );
106
+
107
+ const body: WarehouseBody = {
108
+ _id: repzo_warehouse?._id,
109
+ name: sap_warehouse.WAREHOUSENAME,
110
+ code: sap_warehouse.WAREHOUSECODE,
111
+ type:
112
+ sap_warehouse.WAREHOUSECODE.indexOf("VS") == 0 ||
113
+ sap_warehouse.WAREHOUSECODE.indexOf("COOPS1") == 0
114
+ ? "van"
115
+ : "main",
116
+ disabled: sap_warehouse.INACTIVE == "N" ? false : true,
117
+ };
118
+
119
+ if (!repzo_warehouse) {
120
+ // Create
121
+ try {
122
+ const created_warehouse = await repzo.warehouse.create(body);
123
+ result.created++;
124
+ } catch (e: any) {
125
+ console.log("Create warehouse Failed >> ", e?.response, body);
126
+ failed_docs_report.push({
127
+ method: "create",
128
+ doc: body,
129
+ error_message: set_error(e),
130
+ });
131
+ result.failed++;
132
+ }
133
+ } else {
134
+ const found_identical_docs = db.search({
135
+ WAREHOUSECODE: repzo_warehouse.code,
136
+ WAREHOUSENAME: repzo_warehouse.name,
137
+ });
138
+ if (found_identical_docs.length) continue; // Nothing has changed so no need for updates
139
+
140
+ // Update
141
+ try {
142
+ const updated_warehouse = await repzo.warehouse.update(
143
+ repzo_warehouse._id,
144
+ body
145
+ );
146
+ result.updated++;
147
+ } catch (e) {
148
+ console.log("Update warehouse Failed >> ", e, body);
149
+ failed_docs_report.push({
150
+ method: "update",
151
+ doc_id: repzo_warehouse?._id,
152
+ doc: body,
153
+ error_message: set_error(e),
154
+ });
155
+ result.failed++;
156
+ }
157
+ }
158
+ }
159
+
160
+ // console.log(result);
161
+
162
+ await update_bench_time(
163
+ repzo,
164
+ commandEvent.app._id,
165
+ bench_time_key,
166
+ new_bench_time,
167
+ "YYYY-MM-DD"
168
+ );
169
+ await commandLog
170
+ .setStatus(
171
+ "success",
172
+ failed_docs_report.length ? failed_docs_report : null
173
+ )
174
+ .setBody(result)
175
+ .commit();
176
+
177
+ return result;
178
+ } catch (e: any) {
179
+ //@ts-ignore
180
+ console.error(e?.response?.data || e);
181
+ await commandLog.setStatus("fail", e).commit();
182
+ throw e?.response;
183
+ }
184
+ };
185
+
186
+ const get_sap_warehouses = async (
187
+ serviceEndPoint: string,
188
+ query?: { updateAt?: string }
189
+ ): Promise<SAPWarehouses> => {
190
+ try {
191
+ const sap_warehouses: SAPWarehouses = await _create(
192
+ serviceEndPoint,
193
+ "/Warehouses",
194
+ {
195
+ UpdateAt: date_formatting(query?.updateAt, "YYYYMMDD"),
196
+ Inactive: "N",
197
+ Locked: "N",
198
+ }
199
+ );
200
+ return sap_warehouses;
201
+ } catch (e: any) {
202
+ throw e;
203
+ }
204
+ };
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { actions, actionsList } from "./actions/index.js";
2
+ export const Actions = actions;
3
+ export const ActionsList = actionsList;
4
+
5
+ import { commands, commandsList } from "./commands/index.js";
6
+ export const Commands = commands;
7
+ export const CommandsList = commandsList;