repzo-sap-absjo 1.0.7 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changelog.md +1 -0
- package/lib/actions/create_invoice.d.ts +13 -19
- package/lib/actions/create_invoice.js +214 -371
- package/lib/actions/create_payment.d.ts +1 -4
- package/lib/actions/create_payment.js +125 -208
- package/lib/actions/create_proforma.d.ts +1 -4
- package/lib/actions/create_proforma.js +165 -305
- package/lib/actions/create_return_invoice.d.ts +1 -4
- package/lib/actions/create_return_invoice.js +177 -332
- package/lib/actions/create_transfer.d.ts +1 -4
- package/lib/actions/create_transfer.js +142 -234
- package/lib/actions/index.js +15 -23
- package/lib/commands/adjust_inventory.d.ts +2 -4
- package/lib/commands/adjust_inventory.js +160 -290
- package/lib/commands/bank.d.ts +2 -4
- package/lib/commands/bank.js +157 -279
- package/lib/commands/basic.js +47 -66
- package/lib/commands/category.d.ts +1 -3
- package/lib/commands/category.js +104 -178
- package/lib/commands/channel.d.ts +1 -3
- package/lib/commands/channel.js +117 -198
- package/lib/commands/client.d.ts +32 -34
- package/lib/commands/client.js +244 -404
- package/lib/commands/client_disabled.d.ts +1 -3
- package/lib/commands/client_disabled.js +94 -163
- package/lib/commands/index.d.ts +11 -15
- package/lib/commands/index.js +60 -60
- package/lib/commands/join.js +66 -192
- package/lib/commands/measureunit.d.ts +14 -19
- package/lib/commands/measureunit.js +191 -289
- package/lib/commands/measureunit_family.d.ts +8 -10
- package/lib/commands/measureunit_family.js +138 -250
- package/lib/commands/payment_term.d.ts +1 -3
- package/lib/commands/payment_term.js +123 -202
- package/lib/commands/price_list.d.ts +15 -15
- package/lib/commands/price_list.js +326 -612
- package/lib/commands/product.d.ts +30 -32
- package/lib/commands/product.js +243 -425
- package/lib/commands/product_disabled.d.ts +1 -3
- package/lib/commands/product_disabled.js +94 -164
- package/lib/commands/rep.js +141 -221
- package/lib/commands/tag.js +109 -174
- package/lib/commands/tax.js +112 -177
- package/lib/commands/warehouse.d.ts +1 -3
- package/lib/commands/warehouse.js +119 -207
- package/lib/index.d.ts +12 -21
- package/lib/test/actions/create_invoice.js +188 -193
- package/lib/test/actions/create_payment.js +107 -112
- package/lib/test/actions/create_proforma.js +216 -220
- package/lib/test/actions/create_return_invoice.js +200 -205
- package/lib/test/actions/create_transfer.js +115 -120
- package/lib/test/commands/adjust_inventory.js +90 -90
- package/lib/test/commands/bank.js +90 -90
- package/lib/test/commands/basic.js +327 -328
- package/lib/test/commands/category.js +90 -90
- package/lib/test/commands/channel.js +90 -90
- package/lib/test/commands/client.js +353 -355
- package/lib/test/commands/client_disabled.js +90 -90
- package/lib/test/commands/join.js +327 -328
- package/lib/test/commands/measureunit.js +90 -90
- package/lib/test/commands/measureunit_family.js +90 -90
- package/lib/test/commands/payment_term.js +90 -90
- package/lib/test/commands/price_list.js +334 -337
- package/lib/test/commands/product.js +90 -90
- package/lib/test/commands/product_disabled.js +90 -90
- package/lib/test/commands/rep.js +344 -346
- package/lib/test/commands/tag.js +90 -90
- package/lib/test/commands/tax.js +90 -90
- package/lib/test/commands/warehouse.js +90 -90
- package/lib/types.d.ts +60 -83
- package/lib/util.d.ts +19 -70
- package/lib/util.js +144 -170
- package/package.json +1 -1
- package/src/actions/create_invoice.ts +2 -1
- package/src/actions/create_payment.ts +2 -1
- package/src/actions/create_proforma.ts +2 -1
- package/src/actions/create_return_invoice.ts +2 -1
- package/src/actions/create_transfer.ts +2 -1
|
@@ -1,171 +1,101 @@
|
|
|
1
1
|
import Repzo from "repzo";
|
|
2
|
-
import {
|
|
3
|
-
_create,
|
|
4
|
-
update_bench_time,
|
|
5
|
-
date_formatting,
|
|
6
|
-
set_error,
|
|
7
|
-
} from "../util.js";
|
|
2
|
+
import { _create, update_bench_time, date_formatting, set_error, } from "../util.js";
|
|
8
3
|
export const sync_disabled_product = async (commandEvent) => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
{
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
repzo_products === null || repzo_products === void 0
|
|
80
|
-
? void 0
|
|
81
|
-
: repzo_products.length;
|
|
82
|
-
await commandLog
|
|
83
|
-
.addDetail(
|
|
84
|
-
`${result.repzo_total} Active Products in Repzo should be disabled`
|
|
85
|
-
)
|
|
86
|
-
.commit();
|
|
87
|
-
for (
|
|
88
|
-
let i = 0;
|
|
89
|
-
i <
|
|
90
|
-
(sap_products === null || sap_products === void 0
|
|
91
|
-
? void 0
|
|
92
|
-
: sap_products.length);
|
|
93
|
-
i++
|
|
94
|
-
) {
|
|
95
|
-
const sap_product = sap_products[i];
|
|
96
|
-
const repzo_product = repzo_products.find((r_product) => {
|
|
97
|
-
var _a;
|
|
98
|
-
return (
|
|
99
|
-
((_a =
|
|
100
|
-
r_product === null || r_product === void 0
|
|
101
|
-
? void 0
|
|
102
|
-
: r_product.integration_meta) === null || _a === void 0
|
|
103
|
-
? void 0
|
|
104
|
-
: _a.id) == `${nameSpace}_${sap_product.ITEMCODE}`
|
|
105
|
-
);
|
|
106
|
-
});
|
|
107
|
-
if (repzo_product) {
|
|
108
|
-
// Update
|
|
109
|
-
try {
|
|
110
|
-
const disabled_product = await repzo.product.remove(
|
|
111
|
-
repzo_product._id
|
|
112
|
-
);
|
|
113
|
-
result.updated++;
|
|
114
|
-
} catch (e) {
|
|
115
|
-
// console.log("Disabled Product Failed >> ", e?.response?.data, {
|
|
116
|
-
// ITEMCODE: sap_product.ITEMCODE,
|
|
117
|
-
// });
|
|
118
|
-
failed_docs_report.push({
|
|
119
|
-
method: "delete",
|
|
120
|
-
doc_id:
|
|
121
|
-
repzo_product === null || repzo_product === void 0
|
|
122
|
-
? void 0
|
|
123
|
-
: repzo_product._id,
|
|
124
|
-
doc: { ITEMCODE: sap_product.ITEMCODE },
|
|
125
|
-
error_message: set_error(e),
|
|
126
|
-
});
|
|
127
|
-
result.failed++;
|
|
4
|
+
var _a, _b, _c;
|
|
5
|
+
const repzo = new Repzo((_a = commandEvent.app.formData) === null || _a === void 0 ? void 0 : _a.repzoApiKey, {
|
|
6
|
+
env: commandEvent.env,
|
|
7
|
+
});
|
|
8
|
+
const commandLog = new Repzo.CommandLog(repzo, commandEvent.app, commandEvent.command);
|
|
9
|
+
try {
|
|
10
|
+
// console.log("sync_disabled_product");
|
|
11
|
+
const new_bench_time = new Date().toISOString();
|
|
12
|
+
const bench_time_key = "bench_time_product_disabled";
|
|
13
|
+
await commandLog.load(commandEvent.sync_id);
|
|
14
|
+
await commandLog
|
|
15
|
+
.addDetail("Repzo SAP: Started Syncing Disabled Products")
|
|
16
|
+
.commit();
|
|
17
|
+
const nameSpace = commandEvent.nameSpace.join("_");
|
|
18
|
+
const result = {
|
|
19
|
+
sap_total: 0,
|
|
20
|
+
repzo_total: 0,
|
|
21
|
+
created: 0,
|
|
22
|
+
updated: 0,
|
|
23
|
+
failed: 0,
|
|
24
|
+
};
|
|
25
|
+
const failed_docs_report = [];
|
|
26
|
+
const sap_products = await get_sap_disabled_products(commandEvent.app.formData.sapHostUrl, { updateAt: commandEvent.app.options_formData[bench_time_key] });
|
|
27
|
+
result.sap_total = sap_products === null || sap_products === void 0 ? void 0 : sap_products.length;
|
|
28
|
+
await commandLog
|
|
29
|
+
.addDetail(`${result.sap_total} Disabled Products in SAP changed since ${commandEvent.app.options_formData[bench_time_key] || "ever"}`)
|
|
30
|
+
.commit();
|
|
31
|
+
const sap_product_query = sap_products === null || sap_products === void 0 ? void 0 : sap_products.map((product) => `${nameSpace}_${product.ITEMCODE}`);
|
|
32
|
+
const repzo_products = [];
|
|
33
|
+
const per_page = 200;
|
|
34
|
+
const pages = Math.ceil(sap_product_query.length / per_page);
|
|
35
|
+
for (let i = 0; i < pages; i += per_page) {
|
|
36
|
+
const repzo_product_per_page = await repzo.product.find({
|
|
37
|
+
active: true,
|
|
38
|
+
per_page: 50000,
|
|
39
|
+
"integration_meta.id": sap_product_query.slice(i, i + per_page),
|
|
40
|
+
});
|
|
41
|
+
if ((_b = repzo_product_per_page === null || repzo_product_per_page === void 0 ? void 0 : repzo_product_per_page.data) === null || _b === void 0 ? void 0 : _b.length)
|
|
42
|
+
repzo_products.push(...repzo_product_per_page.data);
|
|
43
|
+
}
|
|
44
|
+
result.repzo_total = repzo_products === null || repzo_products === void 0 ? void 0 : repzo_products.length;
|
|
45
|
+
await commandLog
|
|
46
|
+
.addDetail(`${result.repzo_total} Active Products in Repzo should be disabled`)
|
|
47
|
+
.commit();
|
|
48
|
+
for (let i = 0; i < (sap_products === null || sap_products === void 0 ? void 0 : sap_products.length); i++) {
|
|
49
|
+
const sap_product = sap_products[i];
|
|
50
|
+
const repzo_product = repzo_products.find((r_product) => {
|
|
51
|
+
var _a;
|
|
52
|
+
return ((_a = r_product === null || r_product === void 0 ? void 0 : r_product.integration_meta) === null || _a === void 0 ? void 0 : _a.id) ==
|
|
53
|
+
`${nameSpace}_${sap_product.ITEMCODE}`;
|
|
54
|
+
});
|
|
55
|
+
if (repzo_product) {
|
|
56
|
+
// Update
|
|
57
|
+
try {
|
|
58
|
+
const disabled_product = await repzo.product.remove(repzo_product._id);
|
|
59
|
+
result.updated++;
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
// console.log("Disabled Product Failed >> ", e?.response?.data, {
|
|
63
|
+
// ITEMCODE: sap_product.ITEMCODE,
|
|
64
|
+
// });
|
|
65
|
+
failed_docs_report.push({
|
|
66
|
+
method: "delete",
|
|
67
|
+
doc_id: repzo_product === null || repzo_product === void 0 ? void 0 : repzo_product._id,
|
|
68
|
+
doc: { ITEMCODE: sap_product.ITEMCODE },
|
|
69
|
+
error_message: set_error(e),
|
|
70
|
+
});
|
|
71
|
+
result.failed++;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
128
74
|
}
|
|
129
|
-
|
|
75
|
+
// console.log(result);
|
|
76
|
+
await update_bench_time(repzo, commandEvent.app._id, bench_time_key, new_bench_time);
|
|
77
|
+
await commandLog
|
|
78
|
+
.setStatus("success", failed_docs_report.length ? failed_docs_report : null)
|
|
79
|
+
.setBody(result)
|
|
80
|
+
.commit();
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
//@ts-ignore
|
|
85
|
+
console.error(((_c = e === null || e === void 0 ? void 0 : e.response) === null || _c === void 0 ? void 0 : _c.data) || e);
|
|
86
|
+
await commandLog.setStatus("fail", e).commit();
|
|
87
|
+
throw e;
|
|
130
88
|
}
|
|
131
|
-
// console.log(result);
|
|
132
|
-
await update_bench_time(
|
|
133
|
-
repzo,
|
|
134
|
-
commandEvent.app._id,
|
|
135
|
-
bench_time_key,
|
|
136
|
-
new_bench_time
|
|
137
|
-
);
|
|
138
|
-
await commandLog
|
|
139
|
-
.setStatus(
|
|
140
|
-
"success",
|
|
141
|
-
failed_docs_report.length ? failed_docs_report : null
|
|
142
|
-
)
|
|
143
|
-
.setBody(result)
|
|
144
|
-
.commit();
|
|
145
|
-
return result;
|
|
146
|
-
} catch (e) {
|
|
147
|
-
//@ts-ignore
|
|
148
|
-
console.error(
|
|
149
|
-
((_c = e === null || e === void 0 ? void 0 : e.response) === null ||
|
|
150
|
-
_c === void 0
|
|
151
|
-
? void 0
|
|
152
|
-
: _c.data) || e
|
|
153
|
-
);
|
|
154
|
-
await commandLog.setStatus("fail", e).commit();
|
|
155
|
-
throw e;
|
|
156
|
-
}
|
|
157
89
|
};
|
|
158
90
|
const get_sap_disabled_products = async (serviceEndPoint, query) => {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
throw e;
|
|
170
|
-
}
|
|
91
|
+
try {
|
|
92
|
+
const sap_products = await _create(serviceEndPoint, "/Items", {
|
|
93
|
+
Active: "N",
|
|
94
|
+
UpdateAt: date_formatting(query === null || query === void 0 ? void 0 : query.updateAt, "YYYYMMDD:HHmmss"),
|
|
95
|
+
});
|
|
96
|
+
return sap_products.Items;
|
|
97
|
+
}
|
|
98
|
+
catch (e) {
|
|
99
|
+
throw e;
|
|
100
|
+
}
|
|
171
101
|
};
|
package/lib/commands/rep.js
CHANGED
|
@@ -1,231 +1,151 @@
|
|
|
1
1
|
import Repzo from "repzo";
|
|
2
2
|
import DataSet from "data-set-query";
|
|
3
|
-
import { _create, update_bench_time, set_error } from "../util.js";
|
|
3
|
+
import { _create, update_bench_time, set_error, } from "../util.js";
|
|
4
4
|
export const sync_rep = async (commandEvent) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
? void 0
|
|
9
|
-
: _a.repzoApiKey,
|
|
10
|
-
{
|
|
11
|
-
env: commandEvent.env,
|
|
12
|
-
}
|
|
13
|
-
);
|
|
14
|
-
const commandLog = new Repzo.CommandLog(
|
|
15
|
-
repzo,
|
|
16
|
-
commandEvent.app,
|
|
17
|
-
commandEvent.command
|
|
18
|
-
);
|
|
19
|
-
try {
|
|
20
|
-
// console.log("sync_rep");
|
|
21
|
-
const new_bench_time = new Date().toISOString();
|
|
22
|
-
const bench_time_key = "bench_time_rep";
|
|
23
|
-
await commandLog.load(commandEvent.sync_id);
|
|
24
|
-
await commandLog.addDetail("Repzo SAP: Started Syncing Reps").commit();
|
|
25
|
-
const nameSpace = commandEvent.nameSpace.join("_");
|
|
26
|
-
const result = {
|
|
27
|
-
sap_total: 0,
|
|
28
|
-
repzo_total: 0,
|
|
29
|
-
created: 0,
|
|
30
|
-
updated: 0,
|
|
31
|
-
failed: 0,
|
|
32
|
-
};
|
|
33
|
-
const failed_docs_report = [];
|
|
34
|
-
const sap_reps = await get_sap_reps(
|
|
35
|
-
commandEvent.app.formData.sapHostUrl,
|
|
36
|
-
{}
|
|
37
|
-
);
|
|
38
|
-
result.sap_total =
|
|
39
|
-
(_b =
|
|
40
|
-
sap_reps === null || sap_reps === void 0 ? void 0 : sap_reps.Users) ===
|
|
41
|
-
null || _b === void 0
|
|
42
|
-
? void 0
|
|
43
|
-
: _b.length;
|
|
44
|
-
await commandLog.addDetail(`${result.sap_total} reps in SAP`).commit();
|
|
45
|
-
// hard code ************************************
|
|
46
|
-
// sap_reps?.Users?.forEach((sap_rep) => {
|
|
47
|
-
// if (
|
|
48
|
-
// sap_rep.USERDESC.startsWith("WS ") ||
|
|
49
|
-
// sap_rep.USERDESC.startsWith("RET ")
|
|
50
|
-
// ) {
|
|
51
|
-
// sap_rep.USERWHSCODE = "1";
|
|
52
|
-
// }
|
|
53
|
-
// if (sap_rep.USERDESC.startsWith("MT ")) {
|
|
54
|
-
// sap_rep.USERWHSCODE = "K.A";
|
|
55
|
-
// }
|
|
56
|
-
// });
|
|
57
|
-
// **********************************************
|
|
58
|
-
const db = new DataSet([], { autoIndex: false });
|
|
59
|
-
db.createIndex({
|
|
60
|
-
USERID: true,
|
|
61
|
-
USERDESC: true,
|
|
62
|
-
DEPARTMENTCODE: true,
|
|
63
|
-
USERCASHACCOUNT: true,
|
|
64
|
-
USERCHECKACCTCODE: true,
|
|
65
|
-
USERWHSCODE: true,
|
|
5
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
6
|
+
const repzo = new Repzo((_a = commandEvent.app.formData) === null || _a === void 0 ? void 0 : _a.repzoApiKey, {
|
|
7
|
+
env: commandEvent.env,
|
|
66
8
|
});
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
} reps in
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
:
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
let warehouse;
|
|
108
|
-
if (sap_rep.USERWHSCODE && sap_rep.USERWHSCODE != "") {
|
|
109
|
-
const warehouse_res = await repzo.warehouse.find({
|
|
110
|
-
code: sap_rep.USERWHSCODE,
|
|
111
|
-
});
|
|
112
|
-
if (
|
|
113
|
-
(_f =
|
|
114
|
-
warehouse_res === null || warehouse_res === void 0
|
|
115
|
-
? void 0
|
|
116
|
-
: warehouse_res.data) === null || _f === void 0
|
|
117
|
-
? void 0
|
|
118
|
-
: _f.length
|
|
119
|
-
)
|
|
120
|
-
warehouse = warehouse_res.data[0]._id;
|
|
121
|
-
}
|
|
122
|
-
const body = {
|
|
123
|
-
name: sap_rep.USERDESC,
|
|
124
|
-
password: Math.round(Math.random() * (9999 - 1000) + 1000).toString(),
|
|
125
|
-
username: nameSpace + sap_rep.USERID,
|
|
126
|
-
integration_id:
|
|
127
|
-
(_g = sap_rep.USERID) === null || _g === void 0
|
|
128
|
-
? void 0
|
|
129
|
-
: _g.toString(),
|
|
130
|
-
integration_meta: {
|
|
131
|
-
DEPARTMENTCODE: sap_rep.DEPARTMENTCODE,
|
|
132
|
-
USERCASHACCOUNT: sap_rep.USERCASHACCOUNT,
|
|
133
|
-
USERCHECKACCTCODE: sap_rep.USERCHECKACCTCODE,
|
|
134
|
-
USERWHSCODE: sap_rep.USERWHSCODE,
|
|
135
|
-
id: `${nameSpace}_${sap_rep.USERID}`,
|
|
136
|
-
},
|
|
137
|
-
assigned_warehouse: warehouse,
|
|
138
|
-
company_namespace: [nameSpace],
|
|
139
|
-
};
|
|
140
|
-
if (!repzo_rep) {
|
|
141
|
-
// Create
|
|
142
|
-
try {
|
|
143
|
-
const created_rep = await repzo.rep.create(body);
|
|
144
|
-
result.created++;
|
|
145
|
-
} catch (e) {
|
|
146
|
-
// console.log("Create Rep Failed >> ", e?.response, body);
|
|
147
|
-
failed_docs_report.push({
|
|
148
|
-
method: "create",
|
|
149
|
-
doc: body,
|
|
150
|
-
error_message: set_error(e),
|
|
151
|
-
});
|
|
152
|
-
result.failed++;
|
|
153
|
-
}
|
|
154
|
-
} else {
|
|
155
|
-
const found_identical_docs = db.search({
|
|
156
|
-
USERID: repzo_rep.integration_id,
|
|
157
|
-
USERDESC: repzo_rep.name,
|
|
158
|
-
DEPARTMENTCODE:
|
|
159
|
-
(_h = repzo_rep.integration_meta) === null || _h === void 0
|
|
160
|
-
? void 0
|
|
161
|
-
: _h.DEPARTMENTCODE,
|
|
162
|
-
USERCASHACCOUNT:
|
|
163
|
-
(_j = repzo_rep.integration_meta) === null || _j === void 0
|
|
164
|
-
? void 0
|
|
165
|
-
: _j.USERCASHACCOUNT,
|
|
166
|
-
USERCHECKACCTCODE:
|
|
167
|
-
(_k = repzo_rep.integration_meta) === null || _k === void 0
|
|
168
|
-
? void 0
|
|
169
|
-
: _k.USERCHECKACCTCODE,
|
|
170
|
-
USERWHSCODE:
|
|
171
|
-
(_l = repzo_rep.integration_meta) === null || _l === void 0
|
|
172
|
-
? void 0
|
|
173
|
-
: _l.USERWHSCODE,
|
|
9
|
+
const commandLog = new Repzo.CommandLog(repzo, commandEvent.app, commandEvent.command);
|
|
10
|
+
try {
|
|
11
|
+
// console.log("sync_rep");
|
|
12
|
+
const new_bench_time = new Date().toISOString();
|
|
13
|
+
const bench_time_key = "bench_time_rep";
|
|
14
|
+
await commandLog.load(commandEvent.sync_id);
|
|
15
|
+
await commandLog.addDetail("Repzo SAP: Started Syncing Reps").commit();
|
|
16
|
+
const nameSpace = commandEvent.nameSpace.join("_");
|
|
17
|
+
const result = {
|
|
18
|
+
sap_total: 0,
|
|
19
|
+
repzo_total: 0,
|
|
20
|
+
created: 0,
|
|
21
|
+
updated: 0,
|
|
22
|
+
failed: 0,
|
|
23
|
+
};
|
|
24
|
+
const failed_docs_report = [];
|
|
25
|
+
const sap_reps = await get_sap_reps(commandEvent.app.formData.sapHostUrl, {});
|
|
26
|
+
result.sap_total = (_b = sap_reps === null || sap_reps === void 0 ? void 0 : sap_reps.Users) === null || _b === void 0 ? void 0 : _b.length;
|
|
27
|
+
await commandLog.addDetail(`${result.sap_total} reps in SAP`).commit();
|
|
28
|
+
// hard code ************************************
|
|
29
|
+
// sap_reps?.Users?.forEach((sap_rep) => {
|
|
30
|
+
// if (
|
|
31
|
+
// sap_rep.USERDESC.startsWith("WS ") ||
|
|
32
|
+
// sap_rep.USERDESC.startsWith("RET ")
|
|
33
|
+
// ) {
|
|
34
|
+
// sap_rep.USERWHSCODE = "1";
|
|
35
|
+
// }
|
|
36
|
+
// if (sap_rep.USERDESC.startsWith("MT ")) {
|
|
37
|
+
// sap_rep.USERWHSCODE = "K.A";
|
|
38
|
+
// }
|
|
39
|
+
// });
|
|
40
|
+
// **********************************************
|
|
41
|
+
const db = new DataSet([], { autoIndex: false });
|
|
42
|
+
db.createIndex({
|
|
43
|
+
USERID: true,
|
|
44
|
+
USERDESC: true,
|
|
45
|
+
DEPARTMENTCODE: true,
|
|
46
|
+
USERCASHACCOUNT: true,
|
|
47
|
+
USERCHECKACCTCODE: true,
|
|
48
|
+
USERWHSCODE: true,
|
|
174
49
|
});
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
:
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
50
|
+
db.load(sap_reps === null || sap_reps === void 0 ? void 0 : sap_reps.Users);
|
|
51
|
+
const repzo_reps = await repzo.rep.find({ per_page: 50000 });
|
|
52
|
+
result.repzo_total = (_c = repzo_reps === null || repzo_reps === void 0 ? void 0 : repzo_reps.data) === null || _c === void 0 ? void 0 : _c.length;
|
|
53
|
+
await commandLog
|
|
54
|
+
.addDetail(`${(_d = repzo_reps === null || repzo_reps === void 0 ? void 0 : repzo_reps.data) === null || _d === void 0 ? void 0 : _d.length} reps in Repzo`)
|
|
55
|
+
.commit();
|
|
56
|
+
for (let i = 0; i < ((_e = sap_reps === null || sap_reps === void 0 ? void 0 : sap_reps.Users) === null || _e === void 0 ? void 0 : _e.length); i++) {
|
|
57
|
+
const sap_rep = sap_reps.Users[i];
|
|
58
|
+
const repzo_rep = repzo_reps.data.find((r_rep) => { var _a; return ((_a = r_rep.integration_meta) === null || _a === void 0 ? void 0 : _a.id) == `${nameSpace}_${sap_rep.USERID}`; });
|
|
59
|
+
let warehouse;
|
|
60
|
+
if (sap_rep.USERWHSCODE && sap_rep.USERWHSCODE != "") {
|
|
61
|
+
const warehouse_res = await repzo.warehouse.find({
|
|
62
|
+
code: sap_rep.USERWHSCODE,
|
|
63
|
+
});
|
|
64
|
+
if ((_f = warehouse_res === null || warehouse_res === void 0 ? void 0 : warehouse_res.data) === null || _f === void 0 ? void 0 : _f.length)
|
|
65
|
+
warehouse = warehouse_res.data[0]._id;
|
|
66
|
+
}
|
|
67
|
+
const body = {
|
|
68
|
+
name: sap_rep.USERDESC,
|
|
69
|
+
password: Math.round(Math.random() * (9999 - 1000) + 1000).toString(),
|
|
70
|
+
username: nameSpace + sap_rep.USERID,
|
|
71
|
+
integration_id: (_g = sap_rep.USERID) === null || _g === void 0 ? void 0 : _g.toString(),
|
|
72
|
+
integration_meta: {
|
|
73
|
+
DEPARTMENTCODE: sap_rep.DEPARTMENTCODE,
|
|
74
|
+
USERCASHACCOUNT: sap_rep.USERCASHACCOUNT,
|
|
75
|
+
USERCHECKACCTCODE: sap_rep.USERCHECKACCTCODE,
|
|
76
|
+
USERWHSCODE: sap_rep.USERWHSCODE,
|
|
77
|
+
id: `${nameSpace}_${sap_rep.USERID}`,
|
|
78
|
+
},
|
|
79
|
+
assigned_warehouse: warehouse,
|
|
80
|
+
company_namespace: [nameSpace],
|
|
81
|
+
};
|
|
82
|
+
if (!repzo_rep) {
|
|
83
|
+
// Create
|
|
84
|
+
try {
|
|
85
|
+
const created_rep = await repzo.rep.create(body);
|
|
86
|
+
result.created++;
|
|
87
|
+
}
|
|
88
|
+
catch (e) {
|
|
89
|
+
// console.log("Create Rep Failed >> ", e?.response, body);
|
|
90
|
+
failed_docs_report.push({
|
|
91
|
+
method: "create",
|
|
92
|
+
doc: body,
|
|
93
|
+
error_message: set_error(e),
|
|
94
|
+
});
|
|
95
|
+
result.failed++;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
const found_identical_docs = db.search({
|
|
100
|
+
USERID: repzo_rep.integration_id,
|
|
101
|
+
USERDESC: repzo_rep.name,
|
|
102
|
+
DEPARTMENTCODE: (_h = repzo_rep.integration_meta) === null || _h === void 0 ? void 0 : _h.DEPARTMENTCODE,
|
|
103
|
+
USERCASHACCOUNT: (_j = repzo_rep.integration_meta) === null || _j === void 0 ? void 0 : _j.USERCASHACCOUNT,
|
|
104
|
+
USERCHECKACCTCODE: (_k = repzo_rep.integration_meta) === null || _k === void 0 ? void 0 : _k.USERCHECKACCTCODE,
|
|
105
|
+
USERWHSCODE: (_l = repzo_rep.integration_meta) === null || _l === void 0 ? void 0 : _l.USERWHSCODE,
|
|
106
|
+
});
|
|
107
|
+
if (found_identical_docs.length)
|
|
108
|
+
continue;
|
|
109
|
+
// Update
|
|
110
|
+
try {
|
|
111
|
+
// Delete Rep.Password
|
|
112
|
+
delete body.password;
|
|
113
|
+
const updated_rep = await repzo.rep.update(repzo_rep._id, body);
|
|
114
|
+
result.updated++;
|
|
115
|
+
}
|
|
116
|
+
catch (e) {
|
|
117
|
+
// console.log("Update Rep Failed >> ", e?.response?.data, body);
|
|
118
|
+
failed_docs_report.push({
|
|
119
|
+
method: "update",
|
|
120
|
+
doc_id: repzo_rep === null || repzo_rep === void 0 ? void 0 : repzo_rep._id,
|
|
121
|
+
doc: body,
|
|
122
|
+
error_message: set_error(e),
|
|
123
|
+
});
|
|
124
|
+
result.failed++;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
194
127
|
}
|
|
195
|
-
|
|
128
|
+
// console.log(result);
|
|
129
|
+
await update_bench_time(repzo, commandEvent.app._id, bench_time_key, new_bench_time);
|
|
130
|
+
await commandLog
|
|
131
|
+
.setStatus("success", failed_docs_report.length ? failed_docs_report : null)
|
|
132
|
+
.setBody(result)
|
|
133
|
+
.commit();
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
136
|
+
catch (e) {
|
|
137
|
+
//@ts-ignore
|
|
138
|
+
console.error(((_m = e === null || e === void 0 ? void 0 : e.response) === null || _m === void 0 ? void 0 : _m.data) || e);
|
|
139
|
+
await commandLog.setStatus("fail", e).commit();
|
|
140
|
+
throw e;
|
|
196
141
|
}
|
|
197
|
-
// console.log(result);
|
|
198
|
-
await update_bench_time(
|
|
199
|
-
repzo,
|
|
200
|
-
commandEvent.app._id,
|
|
201
|
-
bench_time_key,
|
|
202
|
-
new_bench_time
|
|
203
|
-
);
|
|
204
|
-
await commandLog
|
|
205
|
-
.setStatus(
|
|
206
|
-
"success",
|
|
207
|
-
failed_docs_report.length ? failed_docs_report : null
|
|
208
|
-
)
|
|
209
|
-
.setBody(result)
|
|
210
|
-
.commit();
|
|
211
|
-
return result;
|
|
212
|
-
} catch (e) {
|
|
213
|
-
//@ts-ignore
|
|
214
|
-
console.error(
|
|
215
|
-
((_m = e === null || e === void 0 ? void 0 : e.response) === null ||
|
|
216
|
-
_m === void 0
|
|
217
|
-
? void 0
|
|
218
|
-
: _m.data) || e
|
|
219
|
-
);
|
|
220
|
-
await commandLog.setStatus("fail", e).commit();
|
|
221
|
-
throw e;
|
|
222
|
-
}
|
|
223
142
|
};
|
|
224
143
|
const get_sap_reps = async (serviceEndPoint, query) => {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
144
|
+
try {
|
|
145
|
+
const sap_reps = await _create(serviceEndPoint, "/Users", {});
|
|
146
|
+
return sap_reps;
|
|
147
|
+
}
|
|
148
|
+
catch (e) {
|
|
149
|
+
throw e;
|
|
150
|
+
}
|
|
231
151
|
};
|