@zauru-sdk/services 6.9.0 → 6.10.0

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.
@@ -1,7 +1,8 @@
1
- import { arrayToObject, convertToFormData, formatDateToUTC, getBasketsSchema, handlePossibleAxiosErrors, reduceAdd, } from "@zauru-sdk/common";
1
+ import { arrayToObject, applyRejectionPercentageRulesToFinancials, convertToFormData, filterActiveRejectionPercentageAdjustmentRules, formatDateToUTC, getBasketsSchema, getRejectionPercentage, handlePossibleAxiosErrors, parseJsonMemo, reduceAdd, } from "@zauru-sdk/common";
2
2
  import { httpZauru } from "./httpZauru.js";
3
3
  import { getGraphQLAPIHeaders, getVariablesByName } from "../common.js";
4
4
  import { httpGraphQLAPI } from "./httpGraphQL.js";
5
+ import { getRejectionPercentageAdjustmentRulesByHeaders } from "./zauru-web-app-tables.js";
5
6
  import { getLast100ReceptionsStringQuery, getPurchaseOrderByIdNumberStringQuery, getPurchaseOrderStringQuery, getPurchaseOrdersBetweenDatesStringQuery, } from "@zauru-sdk/graphql";
6
7
  /**
7
8
  * markAsReceivePurchaseOrder
@@ -23,14 +24,47 @@ export const markAsReceivePurchaseOrder = (headers, body) => {
23
24
  return true;
24
25
  });
25
26
  };
27
+ const applyRejectionRulesToPurchaseOrderBody = async (headers, body) => {
28
+ const originPercentage = getRejectionPercentage(body.memo);
29
+ const details = body.purchase_order_details ?? [];
30
+ const parsedMemo = parseJsonMemo(body.memo);
31
+ if (parsedMemo.rejectionPercentage == null || details.length === 0) {
32
+ return body;
33
+ }
34
+ const rulesResponse = await getRejectionPercentageAdjustmentRulesByHeaders(headers);
35
+ const rules = filterActiveRejectionPercentageAdjustmentRules(rulesResponse.error ? [] : rulesResponse.data);
36
+ if (rules.length === 0) {
37
+ return body;
38
+ }
39
+ const payee = body.payee;
40
+ const adjusted = applyRejectionPercentageRulesToFinancials({
41
+ memo: body.memo,
42
+ originPercentage,
43
+ details,
44
+ rules,
45
+ ctx: {
46
+ itemIds: details
47
+ .map((detail) => Number(detail.item_id))
48
+ .filter((itemId) => Number.isFinite(itemId)),
49
+ tipo: body.reference ?? undefined,
50
+ providerCategoryId: payee?.payee_category_id,
51
+ },
52
+ });
53
+ return {
54
+ ...body,
55
+ memo: adjusted.memo,
56
+ discount: adjusted.discount,
57
+ };
58
+ };
26
59
  export const createNewPurchaseOrder = (headers, body) => {
27
60
  return handlePossibleAxiosErrors(async () => {
61
+ const adjustedBody = await applyRejectionRulesToPurchaseOrderBody(headers, body);
28
62
  let sendBody = {
29
- ...body,
30
- purchase_order_details_attributes: arrayToObject(body?.purchase_order_details),
63
+ ...adjustedBody,
64
+ purchase_order_details_attributes: arrayToObject(adjustedBody?.purchase_order_details),
31
65
  tag_ids: [
32
66
  "",
33
- ...(body?.taggings?.map((x) => x?.tag_id?.toString()) ?? []),
67
+ ...(adjustedBody?.taggings?.map((x) => x?.tag_id?.toString()) ?? []),
34
68
  ],
35
69
  };
36
70
  delete sendBody.__rvfInternalFormId;
@@ -1,8 +1,9 @@
1
- import { handlePossibleAxiosErrors } from "@zauru-sdk/common";
1
+ import { handlePossibleAxiosErrors, REJECTION_PERCENTAGE_ADJUSTMENT_RULES_TABLE_VAR, } from "@zauru-sdk/common";
2
2
  import { getGraphQLAPIHeaders, getVariablesByName } from "../common.js";
3
3
  import { httpGraphQLAPI } from "./httpGraphQL.js";
4
4
  import { getWebAppRowStringQuery, getWebAppRowsByWebAppTableIdStringQuery, } from "@zauru-sdk/graphql";
5
5
  import { httpZauru } from "./httpZauru.js";
6
+ import { getVariables } from "./zauru-variables.js";
6
7
  /**
7
8
  * getWebAppRow
8
9
  * @param headers
@@ -56,6 +57,33 @@ export async function deleteWebAppTableRegister(headers, id_web_app_table, id_re
56
57
  });
57
58
  return response.data;
58
59
  }
60
+ /**
61
+ * REST listing of webapp table rows (no GraphQL session token required).
62
+ */
63
+ export async function getWebAppTableRegistersRest(headers, id_web_app_table) {
64
+ return handlePossibleAxiosErrors(async () => {
65
+ const response = await httpZauru.get(`/apps/webapp_tables/${id_web_app_table}/webapp_rows.json`, { headers });
66
+ return Array.isArray(response.data) ? response.data : [];
67
+ });
68
+ }
69
+ export const getRejectionPercentageAdjustmentRulesByHeaders = (headers) => {
70
+ return handlePossibleAxiosErrors(async () => {
71
+ const varsResponse = await getVariables(headers);
72
+ if (varsResponse.error || !varsResponse.data) {
73
+ return [];
74
+ }
75
+ const tableId = varsResponse.data.find((variable) => variable.name === REJECTION_PERCENTAGE_ADJUSTMENT_RULES_TABLE_VAR)?.value;
76
+ if (!tableId) {
77
+ return [];
78
+ }
79
+ const rowsResponse = await getWebAppTableRegistersRest(headers, tableId);
80
+ if (rowsResponse.error) {
81
+ throw new Error(rowsResponse.userMsg ??
82
+ "No se pudieron leer las reglas de porcentaje de rechazo");
83
+ }
84
+ return rowsResponse.data ?? [];
85
+ });
86
+ };
59
87
  /**
60
88
  * createWebAppTableRegister function for create a new web app table register
61
89
  * @param headers
@@ -1,5 +1,5 @@
1
1
  import type { Session } from "@remix-run/node";
2
- import { AxiosUtilsResponse, RejectionWebAppTableObject, WebAppRowAssociateBody, WebAppRowAssociateResponse, WebAppRowDissociateBody, WebAppRowDissociateResponse, WebAppRowGraphQL, WebAppTableCreateBody, WebAppTableGraphQL, WebAppTableUpdateResponse } from "@zauru-sdk/types";
2
+ import { AxiosUtilsResponse, RejectionPercentageAdjustmentRule, RejectionWebAppTableObject, WebAppRowAssociateBody, WebAppRowAssociateResponse, WebAppRowDissociateBody, WebAppRowDissociateResponse, WebAppRowGraphQL, WebAppTableCreateBody, WebAppTableGraphQL, WebAppTableUpdateResponse } from "@zauru-sdk/types";
3
3
  import { type GetWebAppRowsByTableIdOptions } from "@zauru-sdk/graphql";
4
4
  /**
5
5
  * getWebAppRow
@@ -23,6 +23,11 @@ export declare function getWebAppTableRegisters<T>(session: Session, webapp_tabl
23
23
  * @returns
24
24
  */
25
25
  export declare function deleteWebAppTableRegister(headers: any, id_web_app_table: string, id_register: number): Promise<WebAppTableUpdateResponse>;
26
+ /**
27
+ * REST listing of webapp table rows (no GraphQL session token required).
28
+ */
29
+ export declare function getWebAppTableRegistersRest<T>(headers: any, id_web_app_table: string): Promise<AxiosUtilsResponse<WebAppRowGraphQL<T>[]>>;
30
+ export declare const getRejectionPercentageAdjustmentRulesByHeaders: (headers: any) => Promise<AxiosUtilsResponse<WebAppRowGraphQL<RejectionPercentageAdjustmentRule>[]>>;
26
31
  /**
27
32
  * createWebAppTableRegister function for create a new web app table register
28
33
  * @param headers
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zauru-sdk/services",
3
- "version": "6.9.0",
3
+ "version": "6.10.0",
4
4
  "description": "Servicios de consulta a Zauru",
5
5
  "main": "./dist/esm/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -26,13 +26,13 @@
26
26
  "dependencies": {
27
27
  "@remix-run/node": "^2.8.1",
28
28
  "@upstash/redis": "^1.34.5",
29
- "@zauru-sdk/common": "^6.4.0",
29
+ "@zauru-sdk/common": "^6.5.0",
30
30
  "@zauru-sdk/config": "^6.0.0",
31
31
  "@zauru-sdk/graphql": "^6.7.0",
32
- "@zauru-sdk/types": "^6.14.0",
32
+ "@zauru-sdk/types": "^6.15.0",
33
33
  "axios": "^1.6.7",
34
34
  "chalk": "5.3.0",
35
35
  "moment": "^2.30.1"
36
36
  },
37
- "gitHead": "add1b36bf68bad8debe05c25fd826d68acce2810"
37
+ "gitHead": "2dceb781cae3f4c45ceecb915977c6d142bca6b9"
38
38
  }