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.
- package/README.md +3 -0
- package/changelog.md +17 -0
- package/lib/actions/create_invoice.d.ts +26 -0
- package/lib/actions/create_invoice.js +311 -0
- package/lib/actions/create_payment.d.ts +5 -0
- package/lib/actions/create_payment.js +177 -0
- package/lib/actions/create_proforma.d.ts +5 -0
- package/lib/actions/create_proforma.js +241 -0
- package/lib/actions/create_return_invoice.d.ts +5 -0
- package/lib/actions/create_return_invoice.js +268 -0
- package/lib/actions/create_transfer.d.ts +5 -0
- package/lib/actions/create_transfer.js +171 -0
- package/lib/actions/index.d.ts +3 -0
- package/lib/actions/index.js +31 -0
- package/lib/commands/adjust_inventory.d.ts +6 -0
- package/lib/commands/adjust_inventory.js +303 -0
- package/lib/commands/bank.d.ts +7 -0
- package/lib/commands/bank.js +301 -0
- package/lib/commands/basic.d.ts +2 -0
- package/lib/commands/basic.js +73 -0
- package/lib/commands/category.d.ts +4 -0
- package/lib/commands/category.js +193 -0
- package/lib/commands/channel.d.ts +4 -0
- package/lib/commands/channel.js +213 -0
- package/lib/commands/client.d.ts +39 -0
- package/lib/commands/client.js +410 -0
- package/lib/commands/client_disabled.d.ts +4 -0
- package/lib/commands/client_disabled.js +183 -0
- package/lib/commands/index.d.ts +23 -0
- package/lib/commands/index.js +82 -0
- package/lib/commands/join.d.ts +2 -0
- package/lib/commands/join.js +134 -0
- package/lib/commands/measureunit.d.ts +23 -0
- package/lib/commands/measureunit.js +314 -0
- package/lib/commands/measureunit_family.d.ts +12 -0
- package/lib/commands/measureunit_family.js +263 -0
- package/lib/commands/payment_term.d.ts +4 -0
- package/lib/commands/payment_term.js +217 -0
- package/lib/commands/price_list.d.ts +18 -0
- package/lib/commands/price_list.js +608 -0
- package/lib/commands/product.d.ts +39 -0
- package/lib/commands/product.js +454 -0
- package/lib/commands/product_disabled.d.ts +4 -0
- package/lib/commands/product_disabled.js +178 -0
- package/lib/commands/rep.d.ts +2 -0
- package/lib/commands/rep.js +246 -0
- package/lib/commands/tag.d.ts +2 -0
- package/lib/commands/tag.js +195 -0
- package/lib/commands/tax.d.ts +2 -0
- package/lib/commands/tax.js +198 -0
- package/lib/commands/warehouse.d.ts +4 -0
- package/lib/commands/warehouse.js +221 -0
- package/lib/index.d.ts +29 -0
- package/lib/index.js +6 -0
- package/lib/test/actions/create_invoice.d.ts +1 -0
- package/lib/test/actions/create_invoice.js +214 -0
- package/lib/test/actions/create_payment.d.ts +1 -0
- package/lib/test/actions/create_payment.js +131 -0
- package/lib/test/actions/create_proforma.d.ts +1 -0
- package/lib/test/actions/create_proforma.js +242 -0
- package/lib/test/actions/create_return_invoice.d.ts +1 -0
- package/lib/test/actions/create_return_invoice.js +226 -0
- package/lib/test/actions/create_transfer.d.ts +1 -0
- package/lib/test/actions/create_transfer.js +142 -0
- package/lib/test/commands/adjust_inventory.d.ts +1 -0
- package/lib/test/commands/adjust_inventory.js +97 -0
- package/lib/test/commands/bank.d.ts +1 -0
- package/lib/test/commands/bank.js +97 -0
- package/lib/test/commands/basic.d.ts +1 -0
- package/lib/test/commands/basic.js +339 -0
- package/lib/test/commands/category.d.ts +1 -0
- package/lib/test/commands/category.js +97 -0
- package/lib/test/commands/channel.d.ts +1 -0
- package/lib/test/commands/channel.js +97 -0
- package/lib/test/commands/client.d.ts +1 -0
- package/lib/test/commands/client.js +97 -0
- package/lib/test/commands/client_disabled.d.ts +1 -0
- package/lib/test/commands/client_disabled.js +97 -0
- package/lib/test/commands/join.d.ts +1 -0
- package/lib/test/commands/join.js +339 -0
- package/lib/test/commands/measureunit.d.ts +1 -0
- package/lib/test/commands/measureunit.js +97 -0
- package/lib/test/commands/measureunit_family.d.ts +1 -0
- package/lib/test/commands/measureunit_family.js +97 -0
- package/lib/test/commands/payment_term.d.ts +1 -0
- package/lib/test/commands/payment_term.js +97 -0
- package/lib/test/commands/price_list.d.ts +1 -0
- package/lib/test/commands/price_list.js +97 -0
- package/lib/test/commands/product.d.ts +1 -0
- package/lib/test/commands/product.js +97 -0
- package/lib/test/commands/product_disabled.d.ts +1 -0
- package/lib/test/commands/product_disabled.js +97 -0
- package/lib/test/commands/rep.d.ts +1 -0
- package/lib/test/commands/rep.js +97 -0
- package/lib/test/commands/tag.d.ts +1 -0
- package/lib/test/commands/tag.js +97 -0
- package/lib/test/commands/tax.d.ts +1 -0
- package/lib/test/commands/tax.js +97 -0
- package/lib/test/commands/warehouse.d.ts +1 -0
- package/lib/test/commands/warehouse.js +97 -0
- package/lib/types.d.ts +77 -0
- package/lib/types.js +1 -0
- package/lib/util.d.ts +70 -0
- package/lib/util.js +164 -0
- package/package.json +51 -0
- package/src/actions/create_invoice.ts +275 -0
- package/src/actions/create_payment.ts +138 -0
- package/src/actions/create_proforma.ts +187 -0
- package/src/actions/create_return_invoice.ts +217 -0
- package/src/actions/create_transfer.ts +160 -0
- package/src/actions/index.ts +27 -0
- package/src/commands/adjust_inventory.ts +250 -0
- package/src/commands/bank.ts +243 -0
- package/src/commands/basic.ts +64 -0
- package/src/commands/category.ts +176 -0
- package/src/commands/channel.ts +219 -0
- package/src/commands/client.ts +368 -0
- package/src/commands/client_disabled.ts +158 -0
- package/src/commands/index.ts +87 -0
- package/src/commands/join.ts +80 -0
- package/src/commands/measureunit.ts +276 -0
- package/src/commands/measureunit_family.ts +209 -0
- package/src/commands/payment_term.ts +224 -0
- package/src/commands/price_list.ts +434 -0
- package/src/commands/product.ts +359 -0
- package/src/commands/product_disabled.ts +152 -0
- package/src/commands/rep.ts +209 -0
- package/src/commands/tag.ts +173 -0
- package/src/commands/tax.ts +178 -0
- package/src/commands/warehouse.ts +204 -0
- package/src/index.ts +7 -0
- package/src/test/actions/create_invoice.ts +214 -0
- package/src/test/actions/create_payment.ts +131 -0
- package/src/test/actions/create_proforma.ts +242 -0
- package/src/test/actions/create_return_invoice.ts +226 -0
- package/src/test/actions/create_transfer.ts +142 -0
- package/src/test/commands/adjust_inventory.ts +100 -0
- package/src/test/commands/bank.ts +100 -0
- package/src/test/commands/basic.ts +342 -0
- package/src/test/commands/category.ts +100 -0
- package/src/test/commands/channel.ts +100 -0
- package/src/test/commands/client.ts +100 -0
- package/src/test/commands/client_disabled.ts +100 -0
- package/src/test/commands/join.ts +342 -0
- package/src/test/commands/measureunit.ts +100 -0
- package/src/test/commands/measureunit_family.ts +100 -0
- package/src/test/commands/payment_term.ts +100 -0
- package/src/test/commands/price_list.ts +100 -0
- package/src/test/commands/product.ts +100 -0
- package/src/test/commands/product_disabled.ts +100 -0
- package/src/test/commands/rep.ts +100 -0
- package/src/test/commands/tag.ts +100 -0
- package/src/test/commands/tax.ts +100 -0
- package/src/test/commands/warehouse.ts +100 -0
- package/src/types.ts +76 -0
- package/src/util.ts +205 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,243 @@
|
|
|
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 SAPBank {
|
|
16
|
+
BANKNAME: string; // "Jordan Kuwait Bank - DABUK BRANCH";
|
|
17
|
+
BANKCODE: string; // "20590";
|
|
18
|
+
BRANCH?: string; // "";
|
|
19
|
+
COUNTRY: string; // "JO";
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface SAPBanks {
|
|
23
|
+
result: "Success";
|
|
24
|
+
Banks: SAPBank[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const sync_bank = 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_bank");
|
|
39
|
+
|
|
40
|
+
const new_bench_time = new Date().toISOString();
|
|
41
|
+
const bench_time_key = "bench_time_bank";
|
|
42
|
+
|
|
43
|
+
await commandLog.load(commandEvent.sync_id);
|
|
44
|
+
await commandLog.addDetail("Repzo SAP: Started Syncing Banks").commit();
|
|
45
|
+
|
|
46
|
+
const nameSpace: string = commandEvent.nameSpace.join("_");
|
|
47
|
+
const result: Result & {
|
|
48
|
+
repzo_bank_list_total: number;
|
|
49
|
+
repzo_bank_list_updated: boolean;
|
|
50
|
+
} = {
|
|
51
|
+
sap_total: 0,
|
|
52
|
+
repzo_total: 0,
|
|
53
|
+
created: 0,
|
|
54
|
+
updated: 0,
|
|
55
|
+
failed: 0,
|
|
56
|
+
repzo_bank_list_total: 0,
|
|
57
|
+
repzo_bank_list_updated: false,
|
|
58
|
+
};
|
|
59
|
+
const failed_docs_report: FailedDocsReport = [];
|
|
60
|
+
|
|
61
|
+
const country_translator: { [key: string]: string } = {
|
|
62
|
+
JO: "Jordan",
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// Bank **************************************************
|
|
66
|
+
|
|
67
|
+
const sap_banks: SAPBank[] = await get_sap_banks(
|
|
68
|
+
commandEvent.app.formData.sapHostUrl,
|
|
69
|
+
{}
|
|
70
|
+
);
|
|
71
|
+
result.sap_total = sap_banks?.length;
|
|
72
|
+
|
|
73
|
+
await commandLog.addDetail(`${result.sap_total} Banks in SAP`).commit();
|
|
74
|
+
|
|
75
|
+
const db = new DataSet([], { autoIndex: false });
|
|
76
|
+
db.createIndex({
|
|
77
|
+
BANKNAME: true,
|
|
78
|
+
BANKCODE: true,
|
|
79
|
+
COUNTRY: true,
|
|
80
|
+
});
|
|
81
|
+
db.load(sap_banks);
|
|
82
|
+
|
|
83
|
+
const sap_bank_query = sap_banks?.map(
|
|
84
|
+
(bank: SAPBank) => `${nameSpace}_${bank.BANKCODE}`
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
const repzo_banks = await repzo.bank.find({
|
|
88
|
+
"integration_meta.company_namespace": nameSpace,
|
|
89
|
+
per_page: 50000,
|
|
90
|
+
});
|
|
91
|
+
result.repzo_total = repzo_banks?.data?.length;
|
|
92
|
+
await commandLog
|
|
93
|
+
.addDetail(`${repzo_banks?.data?.length} Banks in Repzo`)
|
|
94
|
+
.commit();
|
|
95
|
+
|
|
96
|
+
for (let i = 0; i < sap_banks?.length; i++) {
|
|
97
|
+
const sap_bank: SAPBank = sap_banks[i];
|
|
98
|
+
const repzo_bank = repzo_banks.data.find(
|
|
99
|
+
(r_bank) =>
|
|
100
|
+
r_bank.integration_meta?.id == `${nameSpace}_${sap_bank.BANKCODE}`
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
const country = country_translator[sap_bank.COUNTRY];
|
|
104
|
+
if (!country && sap_bank.COUNTRY) {
|
|
105
|
+
throw `COUNTRY of COUNTRY Code: ${sap_bank.COUNTRY} was not found on the translator`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const body: Service.Bank.Create.Body | Service.Bank.Update.Body = {
|
|
109
|
+
name: sap_bank.BANKNAME,
|
|
110
|
+
country: [country],
|
|
111
|
+
integration_meta: {
|
|
112
|
+
id: `${nameSpace}_${sap_bank.BANKCODE}`,
|
|
113
|
+
BANKCODE: sap_bank.BANKCODE,
|
|
114
|
+
COUNTRY: sap_bank.COUNTRY,
|
|
115
|
+
company_namespace: nameSpace,
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
if (!repzo_bank) {
|
|
120
|
+
// Create
|
|
121
|
+
try {
|
|
122
|
+
const created_bank = await repzo.bank.create(
|
|
123
|
+
body as Service.Bank.Create.Body
|
|
124
|
+
);
|
|
125
|
+
result.created++;
|
|
126
|
+
} catch (e: any) {
|
|
127
|
+
console.log("Create Bank Failed >> ", e?.response, body);
|
|
128
|
+
failed_docs_report.push({
|
|
129
|
+
method: "create",
|
|
130
|
+
doc: body,
|
|
131
|
+
error_message: set_error(e),
|
|
132
|
+
});
|
|
133
|
+
result.failed++;
|
|
134
|
+
}
|
|
135
|
+
} else {
|
|
136
|
+
if (
|
|
137
|
+
repzo_bank?.name == body?.name &&
|
|
138
|
+
repzo_bank?.integration_meta?.COUNTRY ==
|
|
139
|
+
body?.integration_meta?.COUNTRY &&
|
|
140
|
+
repzo_bank?.integration_meta?.BANKCODE ==
|
|
141
|
+
body?.integration_meta?.BANKCODE
|
|
142
|
+
)
|
|
143
|
+
continue;
|
|
144
|
+
// Update
|
|
145
|
+
try {
|
|
146
|
+
const updated_bank = await repzo.bank.update(
|
|
147
|
+
repzo_bank._id,
|
|
148
|
+
body as Service.Bank.Update.Body
|
|
149
|
+
);
|
|
150
|
+
result.updated++;
|
|
151
|
+
} catch (e: any) {
|
|
152
|
+
console.log("Update Bank Failed >> ", e?.response?.data, body);
|
|
153
|
+
failed_docs_report.push({
|
|
154
|
+
method: "update",
|
|
155
|
+
doc_id: repzo_bank?._id,
|
|
156
|
+
doc: body,
|
|
157
|
+
error_message: set_error(e),
|
|
158
|
+
});
|
|
159
|
+
result.failed++;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// console.log(result);
|
|
165
|
+
// *******************************************************
|
|
166
|
+
|
|
167
|
+
// Bank List *********************************************
|
|
168
|
+
|
|
169
|
+
const all_nameSpace_banks = await repzo.bank.find({
|
|
170
|
+
"integration_meta.company_namespace": nameSpace,
|
|
171
|
+
per_page: 50000,
|
|
172
|
+
});
|
|
173
|
+
const repzo_bankLists = await repzo.bank_list.find({
|
|
174
|
+
"integration_meta.id": nameSpace,
|
|
175
|
+
per_page: 50000,
|
|
176
|
+
});
|
|
177
|
+
result.repzo_bank_list_total = repzo_banks?.data?.length;
|
|
178
|
+
|
|
179
|
+
if (repzo_bankLists.data?.length > 1) {
|
|
180
|
+
throw `Each nameSpace should have One Bank List but nameSpace: ${nameSpace} more than one Bank List`;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const repzo_bank_list = repzo_bankLists?.data?.[0];
|
|
184
|
+
if (repzo_bank_list) {
|
|
185
|
+
const ids: { [key: string]: true } = {};
|
|
186
|
+
repzo_bank_list?.banks?.forEach((bank) => {
|
|
187
|
+
ids[bank?.bank?.toString()] = true;
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
all_nameSpace_banks?.data?.forEach((bank) => {
|
|
191
|
+
ids[bank?._id?.toString()] = true;
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
const new_banks = Object.keys(ids).map((bank_id) => {
|
|
195
|
+
return { bank: bank_id };
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
const repzo_bankList_updated = await repzo.bank_list.update(
|
|
199
|
+
repzo_bank_list?._id?.toString(),
|
|
200
|
+
{
|
|
201
|
+
...repzo_bank_list,
|
|
202
|
+
banks: new_banks,
|
|
203
|
+
} as Service.BankList.Update.Body
|
|
204
|
+
);
|
|
205
|
+
result.repzo_bank_list_updated = true;
|
|
206
|
+
} else {
|
|
207
|
+
throw `Please Check with IT teams to create BankList for Your nameSpace`;
|
|
208
|
+
}
|
|
209
|
+
// *******************************************************
|
|
210
|
+
|
|
211
|
+
await update_bench_time(
|
|
212
|
+
repzo,
|
|
213
|
+
commandEvent.app._id,
|
|
214
|
+
bench_time_key,
|
|
215
|
+
new_bench_time
|
|
216
|
+
);
|
|
217
|
+
await commandLog
|
|
218
|
+
.setStatus(
|
|
219
|
+
"success",
|
|
220
|
+
failed_docs_report.length ? failed_docs_report : null
|
|
221
|
+
)
|
|
222
|
+
.setBody(result)
|
|
223
|
+
.commit();
|
|
224
|
+
return result;
|
|
225
|
+
} catch (e: any) {
|
|
226
|
+
//@ts-ignore
|
|
227
|
+
console.error(e?.response?.data || e);
|
|
228
|
+
await commandLog.setStatus("fail", e).commit();
|
|
229
|
+
throw e;
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const get_sap_banks = async (
|
|
234
|
+
serviceEndPoint: string,
|
|
235
|
+
query?: { updateAt?: string }
|
|
236
|
+
): Promise<SAPBank[]> => {
|
|
237
|
+
try {
|
|
238
|
+
const sap_banks: SAPBanks = await _create(serviceEndPoint, "/Banks", {});
|
|
239
|
+
return sap_banks.Banks;
|
|
240
|
+
} catch (e: any) {
|
|
241
|
+
throw e;
|
|
242
|
+
}
|
|
243
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import Repzo from "repzo";
|
|
2
|
+
import { EVENT, Config, CommandEvent } from "../types";
|
|
3
|
+
import { _fetch, _create, _update, _delete } from "../util.js";
|
|
4
|
+
|
|
5
|
+
import { commands, commandsList } from "./index.js";
|
|
6
|
+
export const basic = 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("basic sync");
|
|
18
|
+
|
|
19
|
+
await commandLog.load(commandEvent.sync_id);
|
|
20
|
+
await commandLog.addDetail("Repzo SAP: Basic Sync").commit();
|
|
21
|
+
|
|
22
|
+
const required_syncing_commands: string[] = [
|
|
23
|
+
"join",
|
|
24
|
+
"warehouse",
|
|
25
|
+
"rep",
|
|
26
|
+
"tax",
|
|
27
|
+
"tag",
|
|
28
|
+
"measureunit",
|
|
29
|
+
"measureunit_family",
|
|
30
|
+
"category",
|
|
31
|
+
"channel",
|
|
32
|
+
"payment_term",
|
|
33
|
+
"bank",
|
|
34
|
+
"product",
|
|
35
|
+
"disabled_product",
|
|
36
|
+
"price_list",
|
|
37
|
+
"client",
|
|
38
|
+
"disabled_client",
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
for (let i = 0; i < required_syncing_commands.length; i++) {
|
|
42
|
+
const command = required_syncing_commands[i];
|
|
43
|
+
const commandDes = commandsList.find((c) => c.command == command);
|
|
44
|
+
const event: CommandEvent = JSON.parse(JSON.stringify(commandEvent));
|
|
45
|
+
event.command = command;
|
|
46
|
+
await commandLog
|
|
47
|
+
.addDetail(`Start Syncing: ${commandDes?.name || command}`)
|
|
48
|
+
.commit();
|
|
49
|
+
await commands(event);
|
|
50
|
+
await commandLog.load(commandEvent.sync_id);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
await commandLog
|
|
54
|
+
.setStatus("success")
|
|
55
|
+
.setBody({ message: "Basic Sync: Completed" })
|
|
56
|
+
.commit();
|
|
57
|
+
} catch (e: any) {
|
|
58
|
+
//@ts-ignore
|
|
59
|
+
console.error(e?.response?.data || e);
|
|
60
|
+
await commandLog.load(commandEvent.sync_id);
|
|
61
|
+
await commandLog.setStatus("fail", e).commit();
|
|
62
|
+
throw e;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
@@ -0,0 +1,176 @@
|
|
|
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 SAPCategory {
|
|
16
|
+
GROUPDESC: string; // "Items",
|
|
17
|
+
GROUPCODE: string; // "100",
|
|
18
|
+
BRAND: null | string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface SAPCategories {
|
|
22
|
+
result: "Success";
|
|
23
|
+
ItemGroup: SAPCategory[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const sync_category = async (commandEvent: CommandEvent) => {
|
|
27
|
+
const repzo = new Repzo(commandEvent.app.formData?.repzoApiKey, {
|
|
28
|
+
env: commandEvent.env,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const commandLog = new Repzo.CommandLog(
|
|
32
|
+
repzo,
|
|
33
|
+
commandEvent.app,
|
|
34
|
+
commandEvent.command
|
|
35
|
+
);
|
|
36
|
+
try {
|
|
37
|
+
console.log("sync_category");
|
|
38
|
+
|
|
39
|
+
const new_bench_time = new Date().toISOString();
|
|
40
|
+
const bench_time_key = "bench_time_category";
|
|
41
|
+
|
|
42
|
+
await commandLog.load(commandEvent.sync_id);
|
|
43
|
+
await commandLog
|
|
44
|
+
.addDetail("Repzo SAP: Started Syncing Product Categories")
|
|
45
|
+
.commit();
|
|
46
|
+
|
|
47
|
+
const nameSpace: string = commandEvent.nameSpace.join("_");
|
|
48
|
+
const result: Result = {
|
|
49
|
+
sap_total: 0,
|
|
50
|
+
repzo_total: 0,
|
|
51
|
+
created: 0,
|
|
52
|
+
updated: 0,
|
|
53
|
+
failed: 0,
|
|
54
|
+
};
|
|
55
|
+
const failed_docs_report: FailedDocsReport = [];
|
|
56
|
+
|
|
57
|
+
const sap_categories: SAPCategory[] = await get_sap_categories(
|
|
58
|
+
commandEvent.app.formData.sapHostUrl,
|
|
59
|
+
{}
|
|
60
|
+
);
|
|
61
|
+
result.sap_total = sap_categories?.length;
|
|
62
|
+
|
|
63
|
+
await commandLog
|
|
64
|
+
.addDetail(`${result.sap_total} Item Groups in SAP`)
|
|
65
|
+
.commit();
|
|
66
|
+
|
|
67
|
+
const repzo_categories = await repzo.category.find({
|
|
68
|
+
per_page: 50000,
|
|
69
|
+
});
|
|
70
|
+
result.repzo_total = repzo_categories?.data?.length;
|
|
71
|
+
await commandLog
|
|
72
|
+
.addDetail(`${repzo_categories?.data?.length} Product Category in Repzo`)
|
|
73
|
+
.commit();
|
|
74
|
+
|
|
75
|
+
for (let i = 0; i < sap_categories?.length; i++) {
|
|
76
|
+
const sap_category: SAPCategory = sap_categories[i];
|
|
77
|
+
const repzo_category = repzo_categories.data.find(
|
|
78
|
+
(r_cat) =>
|
|
79
|
+
r_cat.integration_meta?.id == `${nameSpace}_${sap_category.GROUPCODE}`
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const body: Service.Category.Create.Body | Service.Category.Update.Body =
|
|
83
|
+
{
|
|
84
|
+
name: sap_category.GROUPDESC,
|
|
85
|
+
disabled: false,
|
|
86
|
+
integration_meta: { id: `${nameSpace}_${sap_category.GROUPCODE}` },
|
|
87
|
+
company_namespace: [nameSpace],
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
if (!repzo_category) {
|
|
91
|
+
// Create
|
|
92
|
+
try {
|
|
93
|
+
const created_category = await repzo.category.create(
|
|
94
|
+
body as Service.Category.Create.Body
|
|
95
|
+
);
|
|
96
|
+
result.created++;
|
|
97
|
+
} catch (e: any) {
|
|
98
|
+
console.log("Create Product Category Failed >> ", e?.response, body);
|
|
99
|
+
failed_docs_report.push({
|
|
100
|
+
method: "create",
|
|
101
|
+
doc: body,
|
|
102
|
+
error_message: set_error(e),
|
|
103
|
+
});
|
|
104
|
+
result.failed++;
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
if (
|
|
108
|
+
repzo_category?.integration_meta?.id == body?.integration_meta?.id &&
|
|
109
|
+
repzo_category?.name == body?.name
|
|
110
|
+
) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Update
|
|
115
|
+
try {
|
|
116
|
+
const updated_category = await repzo.category.update(
|
|
117
|
+
repzo_category._id,
|
|
118
|
+
body as Service.Category.Update.Body
|
|
119
|
+
);
|
|
120
|
+
result.updated++;
|
|
121
|
+
} catch (e: any) {
|
|
122
|
+
console.log(
|
|
123
|
+
"Update Product Category Failed >> ",
|
|
124
|
+
e?.response?.data,
|
|
125
|
+
body
|
|
126
|
+
);
|
|
127
|
+
failed_docs_report.push({
|
|
128
|
+
method: "update",
|
|
129
|
+
doc_id: repzo_category?._id,
|
|
130
|
+
doc: body,
|
|
131
|
+
error_message: set_error(e),
|
|
132
|
+
});
|
|
133
|
+
result.failed++;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// console.log(result);
|
|
139
|
+
|
|
140
|
+
await update_bench_time(
|
|
141
|
+
repzo,
|
|
142
|
+
commandEvent.app._id,
|
|
143
|
+
bench_time_key,
|
|
144
|
+
new_bench_time
|
|
145
|
+
);
|
|
146
|
+
await commandLog
|
|
147
|
+
.setStatus(
|
|
148
|
+
"success",
|
|
149
|
+
failed_docs_report.length ? failed_docs_report : null
|
|
150
|
+
)
|
|
151
|
+
.setBody(result)
|
|
152
|
+
.commit();
|
|
153
|
+
return result;
|
|
154
|
+
} catch (e: any) {
|
|
155
|
+
//@ts-ignore
|
|
156
|
+
console.error(e?.response?.data || e);
|
|
157
|
+
await commandLog.setStatus("fail", e).commit();
|
|
158
|
+
throw e;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const get_sap_categories = async (
|
|
163
|
+
serviceEndPoint: string,
|
|
164
|
+
query?: { updateAt?: string }
|
|
165
|
+
): Promise<SAPCategory[]> => {
|
|
166
|
+
try {
|
|
167
|
+
const sap_categories: SAPCategories = await _create(
|
|
168
|
+
serviceEndPoint,
|
|
169
|
+
"/ItemGroup",
|
|
170
|
+
{}
|
|
171
|
+
);
|
|
172
|
+
return sap_categories.ItemGroup;
|
|
173
|
+
} catch (e: any) {
|
|
174
|
+
throw e;
|
|
175
|
+
}
|
|
176
|
+
};
|
|
@@ -0,0 +1,219 @@
|
|
|
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 SAPClient {
|
|
16
|
+
CLIENTID: string; // "C01077";
|
|
17
|
+
CLIENTDESC: string; // "س م عايش";
|
|
18
|
+
CLIENTADDRESS?: string; // null;
|
|
19
|
+
CLIENTCITY?: string; // null;
|
|
20
|
+
CLIENTCOUNTRY?: string; // null;
|
|
21
|
+
CLIENTCOUNTY?: string; // null;
|
|
22
|
+
CLIENTGROUPCODE: number; // 123;
|
|
23
|
+
CLIENTGROUP: string; // "Cash Van";
|
|
24
|
+
PAYMENTTERM: number; // 0;
|
|
25
|
+
CLIENTCONTACTPERSON?: string; // null;
|
|
26
|
+
CLIENTPHONE1?: string; // null;
|
|
27
|
+
CLIENTPHONE2?: string; // null;
|
|
28
|
+
CLIENTNOTE?: string; // null;
|
|
29
|
+
CLIENTSTATUS: "N" | "Y";
|
|
30
|
+
CLIENTCREDITCONSUMED: number; // 0.0;
|
|
31
|
+
CLIENTMAXCHEQUEVALUE: number; // 0.0;
|
|
32
|
+
CLIENTCREDITLIMIT: number; // 0.0;
|
|
33
|
+
CLIENTPRICELISTID: number; // 2;
|
|
34
|
+
CLIENTADDRESSID?: string; // null;
|
|
35
|
+
CLIENTDESCF?: string; // null;
|
|
36
|
+
SALESPERSONCODE: number; // -1;
|
|
37
|
+
DISCOUNTPERCENT: number; // 0.0;
|
|
38
|
+
ACTIVE: "N" | "Y";
|
|
39
|
+
FROZEN: "N" | "Y";
|
|
40
|
+
TERRITORYID?: string; // null;
|
|
41
|
+
TERRITORYNAME?: string; // null;
|
|
42
|
+
CREATEDATE: string; // "2021-12-06T21:00:00Z";
|
|
43
|
+
UPDATEDATE: string; // "2022-11-29T21:00:00Z";
|
|
44
|
+
PARENTCODE?: string; // null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface SAPClients {
|
|
48
|
+
result: "Success";
|
|
49
|
+
Customers: SAPClient[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const sync_channel = async (commandEvent: CommandEvent) => {
|
|
53
|
+
const repzo = new Repzo(commandEvent.app.formData?.repzoApiKey, {
|
|
54
|
+
env: commandEvent.env,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const commandLog = new Repzo.CommandLog(
|
|
58
|
+
repzo,
|
|
59
|
+
commandEvent.app,
|
|
60
|
+
commandEvent.command
|
|
61
|
+
);
|
|
62
|
+
try {
|
|
63
|
+
console.log("sync_channel");
|
|
64
|
+
|
|
65
|
+
const new_bench_time = new Date().toISOString();
|
|
66
|
+
const bench_time_key = "bench_time_channel";
|
|
67
|
+
|
|
68
|
+
await commandLog.load(commandEvent.sync_id);
|
|
69
|
+
await commandLog
|
|
70
|
+
.addDetail("Repzo SAP: Started Syncing Client Channels")
|
|
71
|
+
.commit();
|
|
72
|
+
|
|
73
|
+
const nameSpace: string = commandEvent.nameSpace.join("_");
|
|
74
|
+
const result: Result = {
|
|
75
|
+
sap_total: 0,
|
|
76
|
+
repzo_total: 0,
|
|
77
|
+
created: 0,
|
|
78
|
+
updated: 0,
|
|
79
|
+
failed: 0,
|
|
80
|
+
};
|
|
81
|
+
const failed_docs_report: FailedDocsReport = [];
|
|
82
|
+
|
|
83
|
+
const sap_clients: SAPClient[] = await get_sap_clients(
|
|
84
|
+
commandEvent.app.formData.sapHostUrl,
|
|
85
|
+
{
|
|
86
|
+
updateAt: commandEvent.app.options_formData[bench_time_key],
|
|
87
|
+
GroupCode: commandEvent.app.formData.GroupCode,
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
const sap_unique_channels: { [key: string]: true } = {};
|
|
92
|
+
sap_clients?.forEach((client: SAPClient) => {
|
|
93
|
+
const channel = client.CLIENTGROUP;
|
|
94
|
+
sap_unique_channels[channel] = true;
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const sap_channels = Object.keys(sap_unique_channels);
|
|
98
|
+
|
|
99
|
+
result.sap_total = sap_channels?.length;
|
|
100
|
+
await commandLog
|
|
101
|
+
.addDetail(
|
|
102
|
+
`${result.sap_total} Client Channels in SAP changed since ${
|
|
103
|
+
commandEvent.app.options_formData[bench_time_key] || "ever"
|
|
104
|
+
}`
|
|
105
|
+
)
|
|
106
|
+
.commit();
|
|
107
|
+
|
|
108
|
+
const repzo_channels = await repzo.channel.find({ per_page: 50000 });
|
|
109
|
+
result.repzo_total = repzo_channels?.data?.length;
|
|
110
|
+
await commandLog
|
|
111
|
+
.addDetail(`${repzo_channels?.data?.length} Client Channels in Repzo`)
|
|
112
|
+
.commit();
|
|
113
|
+
|
|
114
|
+
for (let i = 0; i < sap_channels?.length; i++) {
|
|
115
|
+
const sap_channel: string = sap_channels[i];
|
|
116
|
+
const repzo_channel = repzo_channels.data.find(
|
|
117
|
+
(r_channel) =>
|
|
118
|
+
r_channel.integration_meta?.id == `${nameSpace}_${sap_channel}`
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
const body: Service.Channel.Create.Body | Service.Channel.Update.Body = {
|
|
122
|
+
name: sap_channel,
|
|
123
|
+
disabled: false,
|
|
124
|
+
integration_meta: {
|
|
125
|
+
id: `${nameSpace}_${sap_channel}`,
|
|
126
|
+
},
|
|
127
|
+
company_namespace: [nameSpace],
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
if (!repzo_channel) {
|
|
131
|
+
// Create
|
|
132
|
+
try {
|
|
133
|
+
const created_channel = await repzo.channel.create(
|
|
134
|
+
body as Service.Channel.Create.Body
|
|
135
|
+
);
|
|
136
|
+
result.created++;
|
|
137
|
+
} catch (e: any) {
|
|
138
|
+
console.log("Create Client Channel Failed >> ", e?.response, body);
|
|
139
|
+
failed_docs_report.push({
|
|
140
|
+
method: "create",
|
|
141
|
+
doc: body,
|
|
142
|
+
error_message: set_error(e),
|
|
143
|
+
});
|
|
144
|
+
result.failed++;
|
|
145
|
+
}
|
|
146
|
+
} else {
|
|
147
|
+
if (
|
|
148
|
+
repzo_channel?.name == body?.name &&
|
|
149
|
+
repzo_channel?.disabled == body?.disabled
|
|
150
|
+
)
|
|
151
|
+
continue;
|
|
152
|
+
// Update
|
|
153
|
+
try {
|
|
154
|
+
const updated_channel = await repzo.channel.update(
|
|
155
|
+
repzo_channel._id,
|
|
156
|
+
body as Service.Channel.Update.Body
|
|
157
|
+
);
|
|
158
|
+
result.updated++;
|
|
159
|
+
} catch (e: any) {
|
|
160
|
+
console.log(
|
|
161
|
+
"Update Client Channel Failed >> ",
|
|
162
|
+
e?.response?.data,
|
|
163
|
+
body
|
|
164
|
+
);
|
|
165
|
+
failed_docs_report.push({
|
|
166
|
+
method: "update",
|
|
167
|
+
doc_id: repzo_channel?._id,
|
|
168
|
+
doc: body,
|
|
169
|
+
error_message: set_error(e),
|
|
170
|
+
});
|
|
171
|
+
result.failed++;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// console.log(result);
|
|
177
|
+
|
|
178
|
+
await update_bench_time(
|
|
179
|
+
repzo,
|
|
180
|
+
commandEvent.app._id,
|
|
181
|
+
bench_time_key,
|
|
182
|
+
new_bench_time
|
|
183
|
+
);
|
|
184
|
+
await commandLog
|
|
185
|
+
.setStatus(
|
|
186
|
+
"success",
|
|
187
|
+
failed_docs_report.length ? failed_docs_report : null
|
|
188
|
+
)
|
|
189
|
+
.setBody(result)
|
|
190
|
+
.commit();
|
|
191
|
+
return result;
|
|
192
|
+
} catch (e: any) {
|
|
193
|
+
//@ts-ignore
|
|
194
|
+
console.error(e?.response?.data || e);
|
|
195
|
+
await commandLog.setStatus("fail", e).commit();
|
|
196
|
+
throw e;
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
const get_sap_clients = async (
|
|
201
|
+
serviceEndPoint: string,
|
|
202
|
+
query?: { updateAt?: string; GroupCode?: string }
|
|
203
|
+
): Promise<SAPClient[]> => {
|
|
204
|
+
try {
|
|
205
|
+
const sap_clients: SAPClients = await _create(
|
|
206
|
+
serviceEndPoint,
|
|
207
|
+
"/Customers",
|
|
208
|
+
{
|
|
209
|
+
Active: "Y",
|
|
210
|
+
Frozen: "N",
|
|
211
|
+
UpdateAt: date_formatting(query?.updateAt, "YYYYMMDD:HHmmss"),
|
|
212
|
+
GroupCode: query?.GroupCode || "",
|
|
213
|
+
}
|
|
214
|
+
);
|
|
215
|
+
return sap_clients.Customers;
|
|
216
|
+
} catch (e: any) {
|
|
217
|
+
throw e;
|
|
218
|
+
}
|
|
219
|
+
};
|