repzo-sap-absjo 1.0.13 → 1.0.14

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.
@@ -27,6 +27,7 @@ export const basic = async (commandEvent) => {
27
27
  "measureunit",
28
28
  "measureunit_family",
29
29
  "category",
30
+ "brand",
30
31
  "channel",
31
32
  "payment_term",
32
33
  "bank",
@@ -0,0 +1,4 @@
1
+ import { CommandEvent, Result } from "../types";
2
+ export declare const sync_brand: (
3
+ commandEvent: CommandEvent
4
+ ) => Promise<Result>;
@@ -0,0 +1,181 @@
1
+ import Repzo from "repzo";
2
+ import { _create, update_bench_time, set_error } from "../util.js";
3
+ export const sync_brand = async (commandEvent) => {
4
+ var _a, _b, _c, _d, _e, _f;
5
+ const repzo = new Repzo(
6
+ (_a = commandEvent.app.formData) === null || _a === void 0
7
+ ? void 0
8
+ : _a.repzoApiKey,
9
+ {
10
+ env: commandEvent.env,
11
+ }
12
+ );
13
+ const commandLog = new Repzo.CommandLog(
14
+ repzo,
15
+ commandEvent.app,
16
+ commandEvent.command
17
+ );
18
+ try {
19
+ // console.log("sync_brand");
20
+ const new_bench_time = new Date().toISOString();
21
+ const bench_time_key = "bench_time_brand";
22
+ await commandLog.load(commandEvent.sync_id);
23
+ await commandLog.addDetail("Repzo SAP: Started Syncing Brands").commit();
24
+ const nameSpace = commandEvent.nameSpace.join("_");
25
+ const result = {
26
+ sap_total: 0,
27
+ repzo_total: 0,
28
+ created: 0,
29
+ updated: 0,
30
+ failed: 0,
31
+ };
32
+ const failed_docs_report = [];
33
+ const sap_brands = await get_sap_brands(
34
+ commandEvent.app.formData.sapHostUrl,
35
+ {}
36
+ );
37
+ result.sap_total =
38
+ sap_brands === null || sap_brands === void 0 ? void 0 : sap_brands.length;
39
+ await commandLog.addDetail(`${result.sap_total} Brands in SAP`).commit();
40
+ const repzo_brands = await repzo.brand.find({ per_page: 50000 });
41
+ result.repzo_total =
42
+ (_b =
43
+ repzo_brands === null || repzo_brands === void 0
44
+ ? void 0
45
+ : repzo_brands.data) === null || _b === void 0
46
+ ? void 0
47
+ : _b.length;
48
+ await commandLog
49
+ .addDetail(
50
+ `${
51
+ (_c =
52
+ repzo_brands === null || repzo_brands === void 0
53
+ ? void 0
54
+ : repzo_brands.data) === null || _c === void 0
55
+ ? void 0
56
+ : _c.length
57
+ } Brand in Repzo`
58
+ )
59
+ .commit();
60
+ for (
61
+ let i = 0;
62
+ i <
63
+ (sap_brands === null || sap_brands === void 0
64
+ ? void 0
65
+ : sap_brands.length);
66
+ i++
67
+ ) {
68
+ const sap_brand = sap_brands[i];
69
+ const repzo_brand = repzo_brands.data.find((r_cat) => {
70
+ var _a;
71
+ return (
72
+ ((_a = r_cat.integration_meta) === null || _a === void 0
73
+ ? void 0
74
+ : _a.id) == `${nameSpace}_${sap_brand.U_Code}`
75
+ );
76
+ });
77
+ const body = {
78
+ name: sap_brand.Name,
79
+ disabled: false,
80
+ integration_meta: { id: `${nameSpace}_${sap_brand.U_Code}` },
81
+ company_namespace: [nameSpace],
82
+ };
83
+ if (!repzo_brand) {
84
+ // Create
85
+ try {
86
+ const created_brand = await repzo.brand.create(body);
87
+ result.created++;
88
+ } catch (e) {
89
+ // console.log("Create Brand 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
+ } else {
98
+ if (
99
+ ((_d =
100
+ repzo_brand === null || repzo_brand === void 0
101
+ ? void 0
102
+ : repzo_brand.integration_meta) === null || _d === void 0
103
+ ? void 0
104
+ : _d.id) ==
105
+ ((_e =
106
+ body === null || body === void 0
107
+ ? void 0
108
+ : body.integration_meta) === null || _e === void 0
109
+ ? void 0
110
+ : _e.id) &&
111
+ (repzo_brand === null || repzo_brand === void 0
112
+ ? void 0
113
+ : repzo_brand.name) ==
114
+ (body === null || body === void 0 ? void 0 : body.name) &&
115
+ (repzo_brand === null || repzo_brand === void 0
116
+ ? void 0
117
+ : repzo_brand.disabled) == false
118
+ ) {
119
+ continue;
120
+ }
121
+ // Update
122
+ try {
123
+ const updated_brand = await repzo.brand.update(repzo_brand._id, body);
124
+ result.updated++;
125
+ } catch (e) {
126
+ // console.log(
127
+ // "Update Brand Failed >> ",
128
+ // e?.response?.data,
129
+ // body
130
+ // );
131
+ failed_docs_report.push({
132
+ method: "update",
133
+ doc_id:
134
+ repzo_brand === null || repzo_brand === void 0
135
+ ? void 0
136
+ : repzo_brand._id,
137
+ doc: body,
138
+ error_message: set_error(e),
139
+ });
140
+ result.failed++;
141
+ }
142
+ }
143
+ }
144
+ // console.log(result);
145
+ await update_bench_time(
146
+ repzo,
147
+ commandEvent.app._id,
148
+ bench_time_key,
149
+ new_bench_time
150
+ );
151
+ await commandLog
152
+ .setStatus(
153
+ "success",
154
+ failed_docs_report.length ? failed_docs_report : null
155
+ )
156
+ .setBody(result)
157
+ .commit();
158
+ return result;
159
+ } catch (e) {
160
+ //@ts-ignore
161
+ console.error(
162
+ ((_f = e === null || e === void 0 ? void 0 : e.response) === null ||
163
+ _f === void 0
164
+ ? void 0
165
+ : _f.data) || e
166
+ );
167
+ await commandLog.setStatus("fail", e).commit();
168
+ throw e;
169
+ }
170
+ };
171
+ const get_sap_brands = async (serviceEndPoint, query) => {
172
+ try {
173
+ const sap_brands = await _create(serviceEndPoint, "/ParentCategory", {
174
+ UpdateAt: "20201230:000000",
175
+ Active: "Y",
176
+ });
177
+ return sap_brands.ItemSubGroup;
178
+ } catch (e) {
179
+ throw e;
180
+ }
181
+ };
@@ -16,6 +16,7 @@ import { sync_price_list } from "./price_list.js";
16
16
  import { sync_client } from "./client.js";
17
17
  import { sync_disabled_client } from "./client_disabled.js";
18
18
  import { adjust_inventory } from "./adjust_inventory.js";
19
+ import { sync_brand } from "./brand.js";
19
20
  export const commands = async (CommandEvent) => {
20
21
  switch (CommandEvent.command) {
21
22
  case "join":
@@ -54,6 +55,8 @@ export const commands = async (CommandEvent) => {
54
55
  return await sync_disabled_client(CommandEvent);
55
56
  case "adjust_inventory":
56
57
  return await adjust_inventory(CommandEvent);
58
+ case "brand":
59
+ return await sync_brand(CommandEvent);
57
60
  default:
58
61
  throw `Route: ${CommandEvent.command} not found`;
59
62
  }
@@ -35,5 +35,6 @@ export declare const sync_product: (commandEvent: CommandEvent) => Promise<
35
35
  Result & {
36
36
  repzo_total_taxes: number;
37
37
  repzo_total_categories: number;
38
+ repzo_total_brands: number;
38
39
  }
39
40
  >;
@@ -6,7 +6,25 @@ import {
6
6
  set_error,
7
7
  } from "../util.js";
8
8
  export const sync_product = async (commandEvent) => {
9
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
9
+ var _a,
10
+ _b,
11
+ _c,
12
+ _d,
13
+ _e,
14
+ _f,
15
+ _g,
16
+ _h,
17
+ _j,
18
+ _k,
19
+ _l,
20
+ _m,
21
+ _o,
22
+ _p,
23
+ _q,
24
+ _r,
25
+ _s,
26
+ _t,
27
+ _u;
10
28
  const repzo = new Repzo(
11
29
  (_a = commandEvent.app.formData) === null || _a === void 0
12
30
  ? void 0
@@ -35,6 +53,7 @@ export const sync_product = async (commandEvent) => {
35
53
  failed: 0,
36
54
  repzo_total_taxes: 0,
37
55
  repzo_total_categories: 0,
56
+ repzo_total_brands: 0,
38
57
  };
39
58
  const failed_docs_report = [];
40
59
  const sap_products = await get_sap_products(
@@ -106,6 +125,21 @@ export const sync_product = async (commandEvent) => {
106
125
  await commandLog
107
126
  .addDetail(`${result.repzo_total_categories} Product Categories in Repzo`)
108
127
  .commit();
128
+ // Brand
129
+ const repzo_brands = await repzo.brand.find({
130
+ per_page: 50000,
131
+ disabled: false,
132
+ });
133
+ result.repzo_total_brands =
134
+ (_f =
135
+ repzo_brands === null || repzo_brands === void 0
136
+ ? void 0
137
+ : repzo_brands.data) === null || _f === void 0
138
+ ? void 0
139
+ : _f.length;
140
+ await commandLog
141
+ .addDetail(`${result.repzo_total_brands} Brands in Repzo`)
142
+ .commit();
109
143
  for (
110
144
  let i = 0;
111
145
  i <
@@ -129,12 +163,12 @@ export const sync_product = async (commandEvent) => {
129
163
  });
130
164
  // Tax
131
165
  const tax =
132
- (_f =
166
+ (_g =
133
167
  repzo_taxes === null || repzo_taxes === void 0
134
168
  ? void 0
135
- : repzo_taxes.data) === null || _f === void 0
169
+ : repzo_taxes.data) === null || _g === void 0
136
170
  ? void 0
137
- : _f.find((tax) => {
171
+ : _g.find((tax) => {
138
172
  var _a;
139
173
  return (
140
174
  ((_a =
@@ -150,18 +184,18 @@ export const sync_product = async (commandEvent) => {
150
184
  continue;
151
185
  }
152
186
  const product_tax =
153
- (_g = tax === null || tax === void 0 ? void 0 : tax._id) === null ||
154
- _g === void 0
187
+ (_h = tax === null || tax === void 0 ? void 0 : tax._id) === null ||
188
+ _h === void 0
155
189
  ? void 0
156
- : _g.toString();
190
+ : _h.toString();
157
191
  // Category
158
192
  const category =
159
- (_h =
193
+ (_j =
160
194
  repzo_categories === null || repzo_categories === void 0
161
195
  ? void 0
162
- : repzo_categories.data) === null || _h === void 0
196
+ : repzo_categories.data) === null || _j === void 0
163
197
  ? void 0
164
- : _h.find((category) => {
198
+ : _j.find((category) => {
165
199
  var _a;
166
200
  return (
167
201
  ((_a =
@@ -177,12 +211,35 @@ export const sync_product = async (commandEvent) => {
177
211
  continue;
178
212
  }
179
213
  const product_category =
180
- (_j =
214
+ (_k =
181
215
  category === null || category === void 0
182
216
  ? void 0
183
- : category._id) === null || _j === void 0
217
+ : category._id) === null || _k === void 0
218
+ ? void 0
219
+ : _k.toString();
220
+ // Brand
221
+ const brand =
222
+ (_l =
223
+ repzo_brands === null || repzo_brands === void 0
224
+ ? void 0
225
+ : repzo_brands.data) === null || _l === void 0
184
226
  ? void 0
185
- : _j.toString();
227
+ : _l.find((brand) => {
228
+ var _a;
229
+ return (
230
+ ((_a =
231
+ brand === null || brand === void 0
232
+ ? void 0
233
+ : brand.integration_meta) === null || _a === void 0
234
+ ? void 0
235
+ : _a.id) == `${nameSpace}_${sap_product["Parent Category"]}`
236
+ );
237
+ });
238
+ const product_brand =
239
+ (_m = brand === null || brand === void 0 ? void 0 : brand._id) ===
240
+ null || _m === void 0
241
+ ? void 0
242
+ : _m.toString();
186
243
  // measureUnit family
187
244
  const family = await repzo.measureunitFamily.find({
188
245
  "integration_meta.id": `${nameSpace}_${sap_product.ITEMCODE}`,
@@ -190,22 +247,22 @@ export const sync_product = async (commandEvent) => {
190
247
  });
191
248
  if (
192
249
  !(family === null || family === void 0 ? void 0 : family.data) ||
193
- ((_k =
250
+ ((_o =
194
251
  family === null || family === void 0 ? void 0 : family.data) ===
195
- null || _k === void 0
252
+ null || _o === void 0
196
253
  ? void 0
197
- : _k.length) != 1
254
+ : _o.length) != 1
198
255
  ) {
199
256
  throw `Family not found => ITEMCODE: ${sap_product.ITEMCODE}`;
200
257
  continue;
201
258
  }
202
259
  const product_family =
203
- (_m =
204
- (_l = family.data[0]) === null || _l === void 0
260
+ (_q =
261
+ (_p = family.data[0]) === null || _p === void 0
205
262
  ? void 0
206
- : _l._id) === null || _m === void 0
263
+ : _p._id) === null || _q === void 0
207
264
  ? void 0
208
- : _m.toString();
265
+ : _q.toString();
209
266
  // measureUnit
210
267
  const measureUnit = await repzo.measureunit.find({
211
268
  "integration_meta.UOMGROUPENTRY": sap_product.UOMGROUPENTRY,
@@ -218,9 +275,9 @@ export const sync_product = async (commandEvent) => {
218
275
  continue;
219
276
  }
220
277
  const product_measureUnit =
221
- (_o = measureUnit.data[0]._id) === null || _o === void 0
278
+ (_r = measureUnit.data[0]._id) === null || _r === void 0
222
279
  ? void 0
223
- : _o.toString();
280
+ : _r.toString();
224
281
  const body = {
225
282
  active: true,
226
283
  name: sap_product.ITEMDESC,
@@ -229,6 +286,7 @@ export const sync_product = async (commandEvent) => {
229
286
  sku: sap_product.ITEMCODE,
230
287
  sv_tax: product_tax,
231
288
  category: product_category,
289
+ brand: product_brand,
232
290
  measureunit_family: product_family,
233
291
  sv_measureUnit: product_measureUnit,
234
292
  integration_meta: {
@@ -270,17 +328,17 @@ export const sync_product = async (commandEvent) => {
270
328
  continue;
271
329
  }
272
330
  if (
273
- (_p =
331
+ (_s =
274
332
  repzo_product === null || repzo_product === void 0
275
333
  ? void 0
276
- : repzo_product.variants) === null || _p === void 0
334
+ : repzo_product.variants) === null || _s === void 0
277
335
  ? void 0
278
- : _p.length
336
+ : _s.length
279
337
  ) {
280
- (_q = body === null || body === void 0 ? void 0 : body.variants) ===
281
- null || _q === void 0
338
+ (_t = body === null || body === void 0 ? void 0 : body.variants) ===
339
+ null || _t === void 0
282
340
  ? void 0
283
- : _q.forEach((variant) => {
341
+ : _t.forEach((variant) => {
284
342
  var _a;
285
343
  const hasMatch =
286
344
  (_a =
@@ -357,10 +415,10 @@ export const sync_product = async (commandEvent) => {
357
415
  } catch (e) {
358
416
  //@ts-ignore
359
417
  console.error(
360
- ((_r = e === null || e === void 0 ? void 0 : e.response) === null ||
361
- _r === void 0
418
+ ((_u = e === null || e === void 0 ? void 0 : e.response) === null ||
419
+ _u === void 0
362
420
  ? void 0
363
- : _r.data) || e
421
+ : _u.data) || e
364
422
  );
365
423
  await commandLog.setStatus("fail", e).commit();
366
424
  throw e;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,97 @@
1
+ import { Commands } from "../../index.js";
2
+ let commandEvent = {
3
+ app: {
4
+ _id: "6391a53edb71ef6435719794",
5
+ name: "SAP",
6
+ disabled: false,
7
+ available_app: {
8
+ _id: "6391a268db71ef64357195da",
9
+ name: "repzo-sap-absjo",
10
+ title: "SAP ABS JO",
11
+ logo: "https://www.e2abs.com/wp-content/uploads/2021/03/Website-Main-Logo-1.svg",
12
+ description: "",
13
+ disabled: false,
14
+ JSONSchema: {
15
+ title: "SAP Integration Settings",
16
+ type: "object",
17
+ required: ["repzoApiKey", "sapHostUrl"],
18
+ properties: {
19
+ repzoApiKey: {
20
+ type: "string",
21
+ title: "Repzo API KEY",
22
+ },
23
+ sapHostUrl: {
24
+ type: "string",
25
+ title: "SAP Host Url",
26
+ },
27
+ errorEmail: {
28
+ type: "string",
29
+ format: "email",
30
+ title: "Email in case of error",
31
+ },
32
+ warehouseDefaultUpdateDate: {
33
+ type: "string",
34
+ format: "date",
35
+ title: "Warehouse: Default Update Date",
36
+ },
37
+ },
38
+ },
39
+ options_JSONSchema: {
40
+ title: "SAP Integration Optional Settings",
41
+ type: "object",
42
+ required: [],
43
+ properties: {
44
+ bench_time_warehouse: {
45
+ title: "Bench Time: Warehouse",
46
+ type: "string",
47
+ format: "date",
48
+ },
49
+ },
50
+ },
51
+ app_settings: {
52
+ repo: "",
53
+ serviceEndPoint: "",
54
+ _id: "6391a268db71ef64357195db",
55
+ },
56
+ app_category: "6249fa8466312f76e595634a",
57
+ commands: [
58
+ {
59
+ command: "basic",
60
+ name: "Full Sync",
61
+ description: "",
62
+ _id: "6391a268db71ef64357195dc",
63
+ },
64
+ {
65
+ command: "join",
66
+ name: "Join",
67
+ description: "",
68
+ _id: "6391a268db71ef64357195dd",
69
+ },
70
+ ],
71
+ actions: [],
72
+ createdAt: "2022-12-08T08:38:00.915Z",
73
+ updatedAt: "2022-12-08T09:42:49.236Z",
74
+ __v: 0,
75
+ },
76
+ company_namespace: ["unisap"],
77
+ formData: {
78
+ repzoApiKey: "L98_Pc8qZG2R5hZIIMjxLQNUgUzT3_0aX2BuLvkyh74",
79
+ sapHostUrl: "http://unipal.b1pro.com:8083/api",
80
+ errorEmail: "maram.alshen@repzoapp.com",
81
+ serviceApiKey: "awdas",
82
+ warehouseDefaultUpdateDate: "2015-01-01",
83
+ },
84
+ options_formData: {},
85
+ createdAt: "2022-12-08T08:50:06.903Z",
86
+ updatedAt: "2022-12-08T09:43:21.620Z",
87
+ __v: 0,
88
+ },
89
+ end_of_day: "04:00",
90
+ nameSpace: ["unisap"],
91
+ timezone: "Asia/Amman",
92
+ meta: "",
93
+ env: "staging",
94
+ sync_id: "47c9c804-e136-4d54-928a-000006",
95
+ command: "brand",
96
+ };
97
+ Commands(commandEvent);
package/lib/types.d.ts CHANGED
@@ -49,6 +49,7 @@ export type CommandType =
49
49
  | "measureunit"
50
50
  | "measureunit_family"
51
51
  | "category"
52
+ | "brand"
52
53
  | "channel"
53
54
  | "payment_term"
54
55
  | "bank"
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "repzo-sap-absjo",
3
3
  "description": "repzo SAP ABS jo integration",
4
- "version": "1.0.13",
4
+ "version": "1.0.14",
5
5
  "homepage": "",
6
6
  "keywords": [],
7
7
  "author": {
@@ -21,7 +21,7 @@
21
21
  "lint": "npx prettier --write .",
22
22
  "build": "tsc && npm run lint",
23
23
  "test": "mocha",
24
- "mac": "tsc && node ./lib/test/commands/basic.js"
24
+ "mac": "npm run build && node ./lib/test/commands/basic.js"
25
25
  },
26
26
  "repository": {
27
27
  "type": "git",
@@ -27,6 +27,7 @@ export const basic = async (commandEvent: CommandEvent) => {
27
27
  "measureunit",
28
28
  "measureunit_family",
29
29
  "category",
30
+ "brand",
30
31
  "channel",
31
32
  "payment_term",
32
33
  "bank",
@@ -0,0 +1,170 @@
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 SAPBrand {
16
+ Code: string; // "ALKALINE SPECIALITY",
17
+ Name: string; // "ALKALINE SPECIALITY",
18
+ U_Code: string; // "1"
19
+ }
20
+
21
+ interface SAPBrands {
22
+ result: "Success";
23
+ ItemSubGroup: SAPBrand[];
24
+ }
25
+
26
+ export const sync_brand = 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_brand");
38
+
39
+ const new_bench_time = new Date().toISOString();
40
+ const bench_time_key = "bench_time_brand";
41
+
42
+ await commandLog.load(commandEvent.sync_id);
43
+ await commandLog.addDetail("Repzo SAP: Started Syncing Brands").commit();
44
+
45
+ const nameSpace: string = commandEvent.nameSpace.join("_");
46
+ const result: Result = {
47
+ sap_total: 0,
48
+ repzo_total: 0,
49
+ created: 0,
50
+ updated: 0,
51
+ failed: 0,
52
+ };
53
+ const failed_docs_report: FailedDocsReport = [];
54
+
55
+ const sap_brands: SAPBrand[] = await get_sap_brands(
56
+ commandEvent.app.formData.sapHostUrl,
57
+ {}
58
+ );
59
+ result.sap_total = sap_brands?.length;
60
+
61
+ await commandLog.addDetail(`${result.sap_total} Brands in SAP`).commit();
62
+
63
+ const repzo_brands = await repzo.brand.find({ per_page: 50000 });
64
+ result.repzo_total = repzo_brands?.data?.length;
65
+ await commandLog
66
+ .addDetail(`${repzo_brands?.data?.length} Brand in Repzo`)
67
+ .commit();
68
+
69
+ for (let i = 0; i < sap_brands?.length; i++) {
70
+ const sap_brand: SAPBrand = sap_brands[i];
71
+ const repzo_brand = repzo_brands.data.find(
72
+ (r_cat) =>
73
+ r_cat.integration_meta?.id == `${nameSpace}_${sap_brand.U_Code}`
74
+ );
75
+
76
+ const body: Service.Brand.Create.Body | Service.Brand.Update.Body = {
77
+ name: sap_brand.Name,
78
+ disabled: false,
79
+ integration_meta: { id: `${nameSpace}_${sap_brand.U_Code}` },
80
+ company_namespace: [nameSpace],
81
+ };
82
+
83
+ if (!repzo_brand) {
84
+ // Create
85
+ try {
86
+ const created_brand = await repzo.brand.create(
87
+ body as Service.Brand.Create.Body
88
+ );
89
+ result.created++;
90
+ } catch (e: any) {
91
+ // console.log("Create Brand Failed >> ", e?.response, body);
92
+ failed_docs_report.push({
93
+ method: "create",
94
+ doc: body,
95
+ error_message: set_error(e),
96
+ });
97
+ result.failed++;
98
+ }
99
+ } else {
100
+ if (
101
+ repzo_brand?.integration_meta?.id == body?.integration_meta?.id &&
102
+ repzo_brand?.name == body?.name &&
103
+ repzo_brand?.disabled == false
104
+ ) {
105
+ continue;
106
+ }
107
+
108
+ // Update
109
+ try {
110
+ const updated_brand = await repzo.brand.update(
111
+ repzo_brand._id,
112
+ body as Service.Brand.Update.Body
113
+ );
114
+ result.updated++;
115
+ } catch (e: any) {
116
+ // console.log(
117
+ // "Update Brand Failed >> ",
118
+ // e?.response?.data,
119
+ // body
120
+ // );
121
+ failed_docs_report.push({
122
+ method: "update",
123
+ doc_id: repzo_brand?._id,
124
+ doc: body,
125
+ error_message: set_error(e),
126
+ });
127
+ result.failed++;
128
+ }
129
+ }
130
+ }
131
+
132
+ // console.log(result);
133
+
134
+ await update_bench_time(
135
+ repzo,
136
+ commandEvent.app._id,
137
+ bench_time_key,
138
+ new_bench_time
139
+ );
140
+ await commandLog
141
+ .setStatus(
142
+ "success",
143
+ failed_docs_report.length ? failed_docs_report : null
144
+ )
145
+ .setBody(result)
146
+ .commit();
147
+ return result;
148
+ } catch (e: any) {
149
+ //@ts-ignore
150
+ console.error(e?.response?.data || e);
151
+ await commandLog.setStatus("fail", e).commit();
152
+ throw e;
153
+ }
154
+ };
155
+
156
+ const get_sap_brands = async (
157
+ serviceEndPoint: string,
158
+ query?: { updateAt?: string }
159
+ ): Promise<SAPBrand[]> => {
160
+ try {
161
+ const sap_brands: SAPBrands = await _create(
162
+ serviceEndPoint,
163
+ "/ParentCategory",
164
+ { UpdateAt: "20201230:000000", Active: "Y" }
165
+ );
166
+ return sap_brands.ItemSubGroup;
167
+ } catch (e: any) {
168
+ throw e;
169
+ }
170
+ };
@@ -19,6 +19,7 @@ import { sync_price_list } from "./price_list.js";
19
19
  import { sync_client } from "./client.js";
20
20
  import { sync_disabled_client } from "./client_disabled.js";
21
21
  import { adjust_inventory } from "./adjust_inventory.js";
22
+ import { sync_brand } from "./brand.js";
22
23
 
23
24
  export const commands = async (CommandEvent: CommandEvent) => {
24
25
  switch (CommandEvent.command) {
@@ -58,6 +59,8 @@ export const commands = async (CommandEvent: CommandEvent) => {
58
59
  return await sync_disabled_client(CommandEvent);
59
60
  case "adjust_inventory":
60
61
  return await adjust_inventory(CommandEvent);
62
+ case "brand":
63
+ return await sync_brand(CommandEvent);
61
64
  default:
62
65
  throw `Route: ${CommandEvent.command} not found`;
63
66
  }
@@ -70,6 +70,7 @@ export const sync_product = async (commandEvent: CommandEvent) => {
70
70
  const result: Result & {
71
71
  repzo_total_taxes: number;
72
72
  repzo_total_categories: number;
73
+ repzo_total_brands: number;
73
74
  } = {
74
75
  sap_total: 0,
75
76
  repzo_total: 0,
@@ -78,6 +79,7 @@ export const sync_product = async (commandEvent: CommandEvent) => {
78
79
  failed: 0,
79
80
  repzo_total_taxes: 0,
80
81
  repzo_total_categories: 0,
82
+ repzo_total_brands: 0,
81
83
  };
82
84
  const failed_docs_report: FailedDocsReport = [];
83
85
 
@@ -125,6 +127,16 @@ export const sync_product = async (commandEvent: CommandEvent) => {
125
127
  .addDetail(`${result.repzo_total_categories} Product Categories in Repzo`)
126
128
  .commit();
127
129
 
130
+ // Brand
131
+ const repzo_brands = await repzo.brand.find({
132
+ per_page: 50000,
133
+ disabled: false,
134
+ });
135
+ result.repzo_total_brands = repzo_brands?.data?.length;
136
+ await commandLog
137
+ .addDetail(`${result.repzo_total_brands} Brands in Repzo`)
138
+ .commit();
139
+
128
140
  for (let i = 0; i < sap_products?.length; i++) {
129
141
  const sap_product: SAPProduct = sap_products[i];
130
142
  try {
@@ -158,6 +170,14 @@ export const sync_product = async (commandEvent: CommandEvent) => {
158
170
  }
159
171
  const product_category = category?._id?.toString();
160
172
 
173
+ // Brand
174
+ const brand = repzo_brands?.data?.find(
175
+ (brand) =>
176
+ brand?.integration_meta?.id ==
177
+ `${nameSpace}_${sap_product["Parent Category"]}`
178
+ );
179
+ const product_brand = brand?._id?.toString();
180
+
161
181
  // measureUnit family
162
182
  const family = await repzo.measureunitFamily.find({
163
183
  "integration_meta.id": `${nameSpace}_${sap_product.ITEMCODE}`,
@@ -191,6 +211,7 @@ export const sync_product = async (commandEvent: CommandEvent) => {
191
211
  sku: sap_product.ITEMCODE,
192
212
  sv_tax: product_tax,
193
213
  category: product_category,
214
+ brand: product_brand,
194
215
  measureunit_family: product_family,
195
216
  sv_measureUnit: product_measureUnit,
196
217
  integration_meta: {
@@ -0,0 +1,100 @@
1
+ import { CommandEvent, Result } from "../../types";
2
+ import { Commands } from "../../index.js";
3
+
4
+ let commandEvent: CommandEvent | any = {
5
+ app: {
6
+ _id: "6391a53edb71ef6435719794",
7
+ name: "SAP",
8
+ disabled: false,
9
+ available_app: {
10
+ _id: "6391a268db71ef64357195da",
11
+ name: "repzo-sap-absjo",
12
+ title: "SAP ABS JO",
13
+ logo: "https://www.e2abs.com/wp-content/uploads/2021/03/Website-Main-Logo-1.svg",
14
+ description: "",
15
+ disabled: false,
16
+ JSONSchema: {
17
+ title: "SAP Integration Settings",
18
+ type: "object",
19
+ required: ["repzoApiKey", "sapHostUrl"],
20
+ properties: {
21
+ repzoApiKey: {
22
+ type: "string",
23
+ title: "Repzo API KEY",
24
+ },
25
+ sapHostUrl: {
26
+ type: "string",
27
+ title: "SAP Host Url",
28
+ },
29
+ errorEmail: {
30
+ type: "string",
31
+ format: "email",
32
+ title: "Email in case of error",
33
+ },
34
+ warehouseDefaultUpdateDate: {
35
+ type: "string",
36
+ format: "date",
37
+ title: "Warehouse: Default Update Date",
38
+ },
39
+ },
40
+ },
41
+ options_JSONSchema: {
42
+ title: "SAP Integration Optional Settings",
43
+ type: "object",
44
+ required: [],
45
+ properties: {
46
+ bench_time_warehouse: {
47
+ title: "Bench Time: Warehouse",
48
+ type: "string",
49
+ format: "date",
50
+ },
51
+ },
52
+ },
53
+ app_settings: {
54
+ repo: "",
55
+ serviceEndPoint: "",
56
+ _id: "6391a268db71ef64357195db",
57
+ },
58
+ app_category: "6249fa8466312f76e595634a",
59
+ commands: [
60
+ {
61
+ command: "basic",
62
+ name: "Full Sync",
63
+ description: "",
64
+ _id: "6391a268db71ef64357195dc",
65
+ },
66
+ {
67
+ command: "join",
68
+ name: "Join",
69
+ description: "",
70
+ _id: "6391a268db71ef64357195dd",
71
+ },
72
+ ],
73
+ actions: [],
74
+ createdAt: "2022-12-08T08:38:00.915Z",
75
+ updatedAt: "2022-12-08T09:42:49.236Z",
76
+ __v: 0,
77
+ },
78
+ company_namespace: ["unisap"],
79
+ formData: {
80
+ repzoApiKey: "L98_Pc8qZG2R5hZIIMjxLQNUgUzT3_0aX2BuLvkyh74",
81
+ sapHostUrl: "http://unipal.b1pro.com:8083/api",
82
+ errorEmail: "maram.alshen@repzoapp.com",
83
+ serviceApiKey: "awdas",
84
+ warehouseDefaultUpdateDate: "2015-01-01",
85
+ },
86
+ options_formData: {},
87
+ createdAt: "2022-12-08T08:50:06.903Z",
88
+ updatedAt: "2022-12-08T09:43:21.620Z",
89
+ __v: 0,
90
+ },
91
+ end_of_day: "04:00",
92
+ nameSpace: ["unisap"], // demosv
93
+ timezone: "Asia/Amman",
94
+ meta: "",
95
+ env: "staging",
96
+ sync_id: "47c9c804-e136-4d54-928a-000006",
97
+ command: "brand",
98
+ };
99
+
100
+ Commands(commandEvent);
package/src/types.ts CHANGED
@@ -50,6 +50,7 @@ export type CommandType =
50
50
  | "measureunit"
51
51
  | "measureunit_family"
52
52
  | "category"
53
+ | "brand"
53
54
  | "channel"
54
55
  | "payment_term"
55
56
  | "bank"