@zauru-sdk/services 1.0.49 → 1.0.52

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 (108) hide show
  1. package/dist/cjs/common.js +235 -0
  2. package/dist/cjs/index.js +50 -0
  3. package/dist/cjs/sessions/sessions.js +18 -0
  4. package/dist/cjs/sessions/upstash.js +59 -0
  5. package/dist/cjs/zauru/httpGraphQL.js +42 -0
  6. package/dist/cjs/zauru/httpOauth.js +42 -0
  7. package/dist/cjs/zauru/httpZauru.js +42 -0
  8. package/dist/cjs/zauru/zauru-agencies.js +27 -0
  9. package/dist/cjs/zauru/zauru-automatic-numbers.js +28 -0
  10. package/dist/cjs/zauru/zauru-bookings.js +90 -0
  11. package/dist/cjs/zauru/zauru-bundles.js +104 -0
  12. package/dist/cjs/zauru/zauru-cases.js +30 -0
  13. package/dist/cjs/zauru/zauru-consolidated.js +81 -0
  14. package/dist/cjs/zauru/zauru-currencies.js +27 -0
  15. package/dist/cjs/zauru/zauru-deliveries.js +53 -0
  16. package/dist/cjs/zauru/zauru-discharges.js +55 -0
  17. package/dist/cjs/zauru/zauru-employees.js +30 -0
  18. package/dist/cjs/zauru/zauru-forms.js +363 -0
  19. package/dist/cjs/zauru/zauru-invoices.js +104 -0
  20. package/dist/cjs/zauru/zauru-items.js +293 -0
  21. package/dist/cjs/zauru/zauru-lote-record.js +33 -0
  22. package/dist/cjs/zauru/zauru-lotes.js +312 -0
  23. package/dist/cjs/zauru/zauru-motivos-rechazo.js +57 -0
  24. package/dist/cjs/zauru/zauru-payees.js +247 -0
  25. package/dist/cjs/zauru/zauru-payment-terms.js +75 -0
  26. package/dist/cjs/zauru/zauru-price-lists.js +49 -0
  27. package/dist/cjs/zauru/zauru-profiles.js +80 -0
  28. package/dist/cjs/zauru/zauru-purchase-orders.js +438 -0
  29. package/dist/cjs/zauru/zauru-receptions.js +104 -0
  30. package/dist/cjs/zauru/zauru-shipments.js +62 -0
  31. package/dist/cjs/zauru/zauru-suggested-prices.js +94 -0
  32. package/dist/cjs/zauru/zauru-tags.js +20 -0
  33. package/dist/cjs/zauru/zauru-templates.js +22 -0
  34. package/dist/cjs/zauru/zauru-variables.js +56 -0
  35. package/dist/cjs/zauru/zauru-web-app-tables.js +167 -0
  36. package/package.json +12 -9
  37. package/CHANGELOG.md +0 -232
  38. package/src/common.ts +0 -343
  39. package/src/index.ts +0 -37
  40. package/src/sessions/sessions.ts +0 -17
  41. package/src/sessions/upstash.ts +0 -55
  42. package/src/zauru/httpGraphQL.ts +0 -55
  43. package/src/zauru/httpOauth.ts +0 -55
  44. package/src/zauru/httpZauru.ts +0 -56
  45. package/src/zauru/zauru-agencies.ts +0 -32
  46. package/src/zauru/zauru-automatic-numbers.ts +0 -35
  47. package/src/zauru/zauru-bookings.ts +0 -134
  48. package/src/zauru/zauru-bundles.ts +0 -157
  49. package/src/zauru/zauru-cases.ts +0 -44
  50. package/src/zauru/zauru-consolidated.ts +0 -129
  51. package/src/zauru/zauru-currencies.ts +0 -39
  52. package/src/zauru/zauru-deliveries.ts +0 -70
  53. package/src/zauru/zauru-discharges.ts +0 -90
  54. package/src/zauru/zauru-employees.ts +0 -43
  55. package/src/zauru/zauru-forms.ts +0 -618
  56. package/src/zauru/zauru-invoices.ts +0 -142
  57. package/src/zauru/zauru-items.ts +0 -481
  58. package/src/zauru/zauru-lote-record.ts +0 -77
  59. package/src/zauru/zauru-lotes.ts +0 -484
  60. package/src/zauru/zauru-motivos-rechazo.ts +0 -121
  61. package/src/zauru/zauru-payees.ts +0 -402
  62. package/src/zauru/zauru-payment-terms.ts +0 -126
  63. package/src/zauru/zauru-price-lists.ts +0 -68
  64. package/src/zauru/zauru-profiles.ts +0 -106
  65. package/src/zauru/zauru-purchase-orders.ts +0 -701
  66. package/src/zauru/zauru-receptions.ts +0 -148
  67. package/src/zauru/zauru-shipments.ts +0 -88
  68. package/src/zauru/zauru-suggested-prices.ts +0 -137
  69. package/src/zauru/zauru-tags.ts +0 -22
  70. package/src/zauru/zauru-templates.ts +0 -24
  71. package/src/zauru/zauru-variables.ts +0 -67
  72. package/src/zauru/zauru-web-app-tables.ts +0 -256
  73. package/tsconfig.json +0 -24
  74. /package/dist/{common.js → esm/common.js} +0 -0
  75. /package/dist/{index.js → esm/index.js} +0 -0
  76. /package/dist/{sessions → esm/sessions}/sessions.js +0 -0
  77. /package/dist/{sessions → esm/sessions}/upstash.js +0 -0
  78. /package/dist/{zauru → esm/zauru}/httpGraphQL.js +0 -0
  79. /package/dist/{zauru → esm/zauru}/httpOauth.js +0 -0
  80. /package/dist/{zauru → esm/zauru}/httpZauru.js +0 -0
  81. /package/dist/{zauru → esm/zauru}/zauru-agencies.js +0 -0
  82. /package/dist/{zauru → esm/zauru}/zauru-automatic-numbers.js +0 -0
  83. /package/dist/{zauru → esm/zauru}/zauru-bookings.js +0 -0
  84. /package/dist/{zauru → esm/zauru}/zauru-bundles.js +0 -0
  85. /package/dist/{zauru → esm/zauru}/zauru-cases.js +0 -0
  86. /package/dist/{zauru → esm/zauru}/zauru-consolidated.js +0 -0
  87. /package/dist/{zauru → esm/zauru}/zauru-currencies.js +0 -0
  88. /package/dist/{zauru → esm/zauru}/zauru-deliveries.js +0 -0
  89. /package/dist/{zauru → esm/zauru}/zauru-discharges.js +0 -0
  90. /package/dist/{zauru → esm/zauru}/zauru-employees.js +0 -0
  91. /package/dist/{zauru → esm/zauru}/zauru-forms.js +0 -0
  92. /package/dist/{zauru → esm/zauru}/zauru-invoices.js +0 -0
  93. /package/dist/{zauru → esm/zauru}/zauru-items.js +0 -0
  94. /package/dist/{zauru → esm/zauru}/zauru-lote-record.js +0 -0
  95. /package/dist/{zauru → esm/zauru}/zauru-lotes.js +0 -0
  96. /package/dist/{zauru → esm/zauru}/zauru-motivos-rechazo.js +0 -0
  97. /package/dist/{zauru → esm/zauru}/zauru-payees.js +0 -0
  98. /package/dist/{zauru → esm/zauru}/zauru-payment-terms.js +0 -0
  99. /package/dist/{zauru → esm/zauru}/zauru-price-lists.js +0 -0
  100. /package/dist/{zauru → esm/zauru}/zauru-profiles.js +0 -0
  101. /package/dist/{zauru → esm/zauru}/zauru-purchase-orders.js +0 -0
  102. /package/dist/{zauru → esm/zauru}/zauru-receptions.js +0 -0
  103. /package/dist/{zauru → esm/zauru}/zauru-shipments.js +0 -0
  104. /package/dist/{zauru → esm/zauru}/zauru-suggested-prices.js +0 -0
  105. /package/dist/{zauru → esm/zauru}/zauru-tags.js +0 -0
  106. /package/dist/{zauru → esm/zauru}/zauru-templates.js +0 -0
  107. /package/dist/{zauru → esm/zauru}/zauru-variables.js +0 -0
  108. /package/dist/{zauru → esm/zauru}/zauru-web-app-tables.js +0 -0
@@ -0,0 +1,363 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.deleteFormSubmission = exports.updateSubmissionInvoiceFormSubmission = exports.createFormSubmission = exports.updateForm = exports.deleteForm = exports.createForm = exports.getInvoiceFormSubmissionsByInvoiceId = exports.getLastInvoiceFormSubmission = exports.getMyCaseFormSubmissions = exports.getInvoiceFormSubmissionsByAgencyId = exports.getFormSubmissionById = exports.getFormsByDocumentType = exports.getFormByName = exports.getForms = void 0;
7
+ const common_1 = require("@zauru-sdk/common");
8
+ const common_js_1 = require("../common.js");
9
+ const httpGraphQL_js_1 = __importDefault(require("./httpGraphQL.js"));
10
+ const graphql_1 = require("@zauru-sdk/graphql");
11
+ const httpZauru_js_1 = __importDefault(require("./httpZauru.js"));
12
+ /**
13
+ * getForms
14
+ */
15
+ async function getForms(session) {
16
+ return (0, common_1.handlePossibleAxiosErrors)(async () => {
17
+ const headers = await (0, common_js_1.getGraphQLAPIHeaders)(session);
18
+ const response = await httpGraphQL_js_1.default.post("", {
19
+ query: graphql_1.getFormsStringQuery,
20
+ }, { headers });
21
+ if (response.data.errors) {
22
+ throw new Error(response.data.errors.map((x) => x.message).join(";"));
23
+ }
24
+ const registers = response?.data?.data?.settings_forms;
25
+ return registers;
26
+ });
27
+ }
28
+ exports.getForms = getForms;
29
+ /**
30
+ * getFormByName
31
+ */
32
+ async function getFormByName(session, name) {
33
+ return (0, common_1.handlePossibleAxiosErrors)(async () => {
34
+ const headers = await (0, common_js_1.getGraphQLAPIHeaders)(session);
35
+ const response = await httpGraphQL_js_1.default.post("", {
36
+ query: graphql_1.getFormByNameStringQuery,
37
+ variables: {
38
+ name,
39
+ },
40
+ }, { headers });
41
+ if (response.data.errors) {
42
+ throw new Error(response.data.errors.map((x) => x.message).join(";"));
43
+ }
44
+ if (!response?.data?.data?.settings_forms[0]) {
45
+ throw new Error(`No se encontró ningún formulario con el nombre: ${name} asociado`);
46
+ }
47
+ const register = response?.data?.data?.settings_forms[0];
48
+ return register;
49
+ });
50
+ }
51
+ exports.getFormByName = getFormByName;
52
+ /**
53
+ * getFormsByDocumentType
54
+ */
55
+ async function getFormsByDocumentType(session, document_type, filters = {}) {
56
+ return (0, common_1.handlePossibleAxiosErrors)(async () => {
57
+ const headers = await (0, common_js_1.getGraphQLAPIHeaders)(session);
58
+ const response = await httpGraphQL_js_1.default.post("", {
59
+ query: (0, graphql_1.getFormsByDocumentTypeStringQuery)(filters),
60
+ variables: {
61
+ document_type,
62
+ },
63
+ }, { headers });
64
+ if (response.data.errors) {
65
+ throw new Error(response.data.errors.map((x) => x.message).join(";"));
66
+ }
67
+ const registers = response?.data?.data?.settings_forms;
68
+ // Filtrar los registros para obtener sólo los de la versión más alta.
69
+ const groupedByVersion = registers.reduce((acc, record) => {
70
+ const zid = record.zid;
71
+ if (!acc[zid]) {
72
+ acc[zid] = record;
73
+ }
74
+ return acc;
75
+ }, {});
76
+ const latestVersionRecords = Object.values(groupedByVersion).reverse();
77
+ return latestVersionRecords;
78
+ });
79
+ }
80
+ exports.getFormsByDocumentType = getFormsByDocumentType;
81
+ /**
82
+ * getFormSubmissionById
83
+ */
84
+ async function getFormSubmissionById(headersZauru, session, id, config = { withFiles: false }) {
85
+ return (0, common_1.handlePossibleAxiosErrors)(async () => {
86
+ const headers = await (0, common_js_1.getGraphQLAPIHeaders)(session);
87
+ const response = await httpGraphQL_js_1.default.post("", {
88
+ query: graphql_1.getFormSubmissionByIdStringQuery,
89
+ variables: {
90
+ formId: id,
91
+ },
92
+ }, { headers });
93
+ if (response.data.errors) {
94
+ throw new Error(response.data.errors.map((x) => x.message).join(";"));
95
+ }
96
+ if (!response?.data?.data?.settings_form_submissions.length) {
97
+ throw new Error("No se encontró el form submission indicado.");
98
+ }
99
+ const register = response?.data?.data?.settings_form_submissions[0];
100
+ if (config.withFiles) {
101
+ const responseZauru = await httpZauru_js_1.default.get(`/settings/forms/form_submissions/${id}.json`, {
102
+ headers: headersZauru,
103
+ });
104
+ register.settings_form_submission_values =
105
+ register.settings_form_submission_values.map((x) => {
106
+ if (x.settings_form_field.field_type === "image" ||
107
+ x.settings_form_field.field_type === "file" ||
108
+ x.settings_form_field.field_type === "pdf") {
109
+ x.value = responseZauru.data[x.settings_form_field.print_var_name]
110
+ ?.toString()
111
+ .replace(/\\u0026/g, "&");
112
+ }
113
+ return x;
114
+ });
115
+ }
116
+ return register;
117
+ });
118
+ }
119
+ exports.getFormSubmissionById = getFormSubmissionById;
120
+ /**
121
+ * getInvoiceFormSubmissionsByAgencyId
122
+ */
123
+ async function getInvoiceFormSubmissionsByAgencyId(session, agency_id, filters) {
124
+ return (0, common_1.handlePossibleAxiosErrors)(async () => {
125
+ const headers = await (0, common_js_1.getGraphQLAPIHeaders)(session);
126
+ const queryBuilded = (0, graphql_1.getInvoiceFormSubmissionsByAgencyIdStringQuery)({
127
+ seller_id: filters?.seller_id,
128
+ payee_id_number_search: filters?.payee_id_number_search,
129
+ //some_field_value: filters?.some_field_value, //Este filtro ahora lo hago abajo, con código, porque al hacerlo así antes,
130
+ //no funcionaba para cuando se cambiaba un campo, por ejemplo, tengo campo1 con "blabla", si lo cambiaba a campo1 = "bleble",
131
+ //ese campo1 con "blabla" ya iba a ser la versión vieja, pero si buscaba por ese blabla, si me iba a seguir apareciendo.
132
+ bundle_ids: filters?.bundle_ids,
133
+ item_ids: filters?.item_ids,
134
+ startDate: filters?.startDate,
135
+ endDate: filters?.endDate,
136
+ });
137
+ const response = await httpGraphQL_js_1.default.post("", {
138
+ query: queryBuilded,
139
+ variables: {
140
+ agency_id,
141
+ },
142
+ }, { headers });
143
+ if (response.data.errors) {
144
+ throw new Error(response.data.errors.map((x) => x.message).join(";"));
145
+ }
146
+ const registers = response?.data?.data?.submission_invoices;
147
+ // Filtrar los registros para obtener sólo los de la versión más alta.
148
+ const groupedByVersion = registers.reduce((acc, record) => {
149
+ const zid = record.settings_form_submission.zid;
150
+ if (!acc[zid]) {
151
+ acc[zid] = record;
152
+ }
153
+ return acc;
154
+ }, {});
155
+ let latestVersionRecords = Object.values(groupedByVersion).reverse();
156
+ //aplico el filtro de somevalue
157
+ if (filters?.some_field_value) {
158
+ latestVersionRecords = latestVersionRecords.filter((x) => x.settings_form_submission.settings_form_submission_values.some((y) => y.value === filters?.some_field_value));
159
+ }
160
+ return latestVersionRecords;
161
+ });
162
+ }
163
+ exports.getInvoiceFormSubmissionsByAgencyId = getInvoiceFormSubmissionsByAgencyId;
164
+ /**
165
+ * getMyCaseFormSubmissions
166
+ */
167
+ async function getMyCaseFormSubmissions(headersZauru, session, filters = {}, config = { withFiles: false }) {
168
+ return (0, common_1.handlePossibleAxiosErrors)(async () => {
169
+ const headers = await (0, common_js_1.getGraphQLAPIHeaders)(session);
170
+ const response = await httpGraphQL_js_1.default.post("", {
171
+ query: (0, graphql_1.getMyCaseFormSubmissionsStringQuery)({
172
+ formZid: filters?.formZid,
173
+ caseId: filters?.caseId,
174
+ }),
175
+ variables: {
176
+ responsible_id: session.get("employee_id"),
177
+ },
178
+ }, { headers });
179
+ if (response.data.errors) {
180
+ throw new Error(response.data.errors.map((x) => x.message).join(";"));
181
+ }
182
+ let registers = response?.data?.data?.submission_cases;
183
+ if (config.withFiles) {
184
+ registers = await Promise.all(registers.map(async (register) => {
185
+ const responseZauru = await httpZauru_js_1.default.get(`/settings/forms/form_submissions/${register.settings_form_submission.id}.json`, {
186
+ headers: headersZauru,
187
+ });
188
+ register.settings_form_submission.settings_form_submission_values =
189
+ register.settings_form_submission.settings_form_submission_values.map((x) => {
190
+ if (x.settings_form_field.field_type === "image" ||
191
+ x.settings_form_field.field_type === "file" ||
192
+ x.settings_form_field.field_type === "pdf") {
193
+ x.value = responseZauru.data[x.settings_form_field.print_var_name]
194
+ ?.toString()
195
+ .replace(/\\u0026/g, "&");
196
+ }
197
+ return x;
198
+ });
199
+ return register;
200
+ }));
201
+ }
202
+ return registers;
203
+ });
204
+ }
205
+ exports.getMyCaseFormSubmissions = getMyCaseFormSubmissions;
206
+ /**
207
+ * getLastInvoiceFormSubmission
208
+ * @param session
209
+ * @param filters
210
+ * @returns
211
+ */
212
+ async function getLastInvoiceFormSubmission(session, filters = {}) {
213
+ return (0, common_1.handlePossibleAxiosErrors)(async () => {
214
+ const headers = await (0, common_js_1.getGraphQLAPIHeaders)(session);
215
+ const response = await httpGraphQL_js_1.default.post("", {
216
+ query: (0, graphql_1.getLastInvoiceFormSubmissionStringQuery)({
217
+ formZid: filters?.formZid,
218
+ }),
219
+ }, { headers });
220
+ if (response.data.errors) {
221
+ throw new Error(response.data.errors.map((x) => x.message).join(";"));
222
+ }
223
+ const register = response?.data?.data?.submission_invoices[0];
224
+ return register;
225
+ });
226
+ }
227
+ exports.getLastInvoiceFormSubmission = getLastInvoiceFormSubmission;
228
+ /**
229
+ * getInvoiceFormSubmissionsByInvoiceId
230
+ */
231
+ async function getInvoiceFormSubmissionsByInvoiceId(session, invoice_id, filters = {}) {
232
+ return (0, common_1.handlePossibleAxiosErrors)(async () => {
233
+ const headers = await (0, common_js_1.getGraphQLAPIHeaders)(session);
234
+ const response = await httpGraphQL_js_1.default.post("", {
235
+ query: (0, graphql_1.getInvoiceFormSubmissionsByInvoiceIdStringQuery)({
236
+ formZid: filters?.formZid,
237
+ }),
238
+ variables: {
239
+ invoice_id,
240
+ },
241
+ }, { headers });
242
+ if (response.data.errors) {
243
+ throw new Error(response.data.errors.map((x) => x.message).join(";"));
244
+ }
245
+ const registers = response?.data?.data?.submission_invoices;
246
+ // Filtrar los registros para obtener sólo los de la versión más alta.
247
+ const groupedByVersion = registers.reduce((acc, record) => {
248
+ const zid = record.settings_form_submission.zid;
249
+ if (!acc[zid]) {
250
+ acc[zid] = record;
251
+ }
252
+ return acc;
253
+ }, {});
254
+ const latestVersionRecords = Object.values(groupedByVersion).reverse();
255
+ return latestVersionRecords;
256
+ });
257
+ }
258
+ exports.getInvoiceFormSubmissionsByInvoiceId = getInvoiceFormSubmissionsByInvoiceId;
259
+ /**
260
+ * createForm
261
+ * @param headers
262
+ * @param body
263
+ */
264
+ async function createForm(headers, body) {
265
+ return (0, common_1.handlePossibleAxiosErrors)(async () => {
266
+ const sendBody = {
267
+ ...body,
268
+ form_fields_attributes: (0, common_1.arrayToObject)(body.settings_form_fields),
269
+ };
270
+ delete sendBody.settings_form_fields;
271
+ const response = await httpZauru_js_1.default.post(`/settings/forms.json`, { settings_form: sendBody }, { headers });
272
+ return response.data;
273
+ });
274
+ }
275
+ exports.createForm = createForm;
276
+ /**
277
+ * deleteForm
278
+ * @param headers
279
+ * @param body
280
+ */
281
+ async function deleteForm(headers, id) {
282
+ return (0, common_1.handlePossibleAxiosErrors)(async () => {
283
+ await httpZauru_js_1.default.delete(`/settings/forms/${id}?destroy=true`, {
284
+ headers,
285
+ });
286
+ return true;
287
+ });
288
+ }
289
+ exports.deleteForm = deleteForm;
290
+ /**
291
+ * createForm
292
+ * @param headers
293
+ * @param body
294
+ */
295
+ async function updateForm(headers, body) {
296
+ return (0, common_1.handlePossibleAxiosErrors)(async () => {
297
+ const sendBody = {
298
+ ...body,
299
+ form_fields_attributes: (0, common_1.arrayToObject)(body.settings_form_fields),
300
+ };
301
+ delete sendBody.settings_form_fields;
302
+ const response = await httpZauru_js_1.default.post(`/settings/forms.json`, { settings_form: sendBody }, { headers });
303
+ return response.data;
304
+ });
305
+ }
306
+ exports.updateForm = updateForm;
307
+ /**
308
+ * createFormSubmission
309
+ * @param headers
310
+ * @param body
311
+ * @returns
312
+ */
313
+ async function createFormSubmission(headers, body) {
314
+ return (0, common_1.handlePossibleAxiosErrors)(async () => {
315
+ delete headers["Content-type"];
316
+ const sendBody = (0, common_1.convertToFormData)({
317
+ settings_form_submission: body,
318
+ });
319
+ const response = await httpZauru_js_1.default.post(`/settings/forms/form_submissions.json`, sendBody, { headers });
320
+ return response.data;
321
+ });
322
+ }
323
+ exports.createFormSubmission = createFormSubmission;
324
+ /**
325
+ * updateSubmissionInvoiceFormSubmission
326
+ * @param headers
327
+ * @param body
328
+ * @returns
329
+ */
330
+ async function updateSubmissionInvoiceFormSubmission(headers, body) {
331
+ return (0, common_1.handlePossibleAxiosErrors)(async () => {
332
+ const settings_form_submission = {
333
+ form_id: body.settings_form_submission?.settings_form?.id,
334
+ reference: body.settings_form_submission?.reference,
335
+ document_type: "invoice",
336
+ document_id: body.invoice_id,
337
+ zid: body.settings_form_submission?.zid,
338
+ form_submission_values_attributes: (0, common_1.arrayToObject)(body.settings_form_submission?.settings_form_submission_values?.map((x) => {
339
+ delete x.settings_form_field;
340
+ return x;
341
+ })),
342
+ };
343
+ const response = await httpZauru_js_1.default.post(`/settings/forms/form_submissions.json`, {
344
+ settings_form_submission,
345
+ }, { headers });
346
+ return response.data;
347
+ });
348
+ }
349
+ exports.updateSubmissionInvoiceFormSubmission = updateSubmissionInvoiceFormSubmission;
350
+ /**
351
+ * deleteFormSubmission
352
+ * @param headers
353
+ * @param body
354
+ */
355
+ async function deleteFormSubmission(headers, id) {
356
+ return (0, common_1.handlePossibleAxiosErrors)(async () => {
357
+ await httpZauru_js_1.default.delete(`/settings/forms/form_submissions/${id}.json`, {
358
+ headers,
359
+ });
360
+ return true;
361
+ });
362
+ }
363
+ exports.deleteFormSubmission = deleteFormSubmission;
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.deleteInvoiceOrder = exports.updateInvoiceOrder = exports.createInvoiceOrder = exports.getInvoicesByAgencyId = void 0;
7
+ const common_1 = require("@zauru-sdk/common");
8
+ const common_js_1 = require("../common.js");
9
+ const httpGraphQL_js_1 = __importDefault(require("./httpGraphQL.js"));
10
+ const graphql_1 = require("@zauru-sdk/graphql");
11
+ const httpZauru_js_1 = __importDefault(require("./httpZauru.js"));
12
+ /**
13
+ * getInvoicesByAgencyId
14
+ */
15
+ async function getInvoicesByAgencyId(session, id) {
16
+ return (0, common_1.handlePossibleAxiosErrors)(async () => {
17
+ const headers = await (0, common_js_1.getGraphQLAPIHeaders)(session);
18
+ const response = await httpGraphQL_js_1.default.post("", {
19
+ query: graphql_1.getInvoicesByAgencyIdStringQuery,
20
+ variables: {
21
+ id,
22
+ },
23
+ }, { headers });
24
+ if (response.data.errors) {
25
+ throw new Error(response.data.errors.map((x) => x.message).join(";"));
26
+ }
27
+ // Filtrar los registros para obtener sólo los de la versión más alta.
28
+ const registers = response?.data?.data?.invoices.map((x) => {
29
+ const groupedByVersion = x.submission_invoices?.reduce((acc, record) => {
30
+ const zid = record.settings_form_submission?.zid;
31
+ if (!acc[zid]) {
32
+ acc[zid] = record;
33
+ }
34
+ return acc;
35
+ }, {});
36
+ x.submission_invoices = Object.values(groupedByVersion)?.reverse() ?? [];
37
+ return x;
38
+ });
39
+ return registers;
40
+ });
41
+ }
42
+ exports.getInvoicesByAgencyId = getInvoicesByAgencyId;
43
+ /**
44
+ * createInvoiceOrder
45
+ * @param headers
46
+ * @param body
47
+ * @returns
48
+ */
49
+ async function createInvoiceOrder(headers, body) {
50
+ return (0, common_1.handlePossibleAxiosErrors)(async () => {
51
+ const sendBody = {
52
+ ...body,
53
+ issued: false, // Esto lo hace una órden y no una factura
54
+ invoice_details_attributes: (0, common_1.arrayToObject)(body.invoice_details),
55
+ tag_ids: body.tagging_invoices?.map((x) => x.tag_id) ?? [],
56
+ };
57
+ if (sendBody.deleted_invoice_details)
58
+ delete sendBody.deleted_invoice_details;
59
+ if (sendBody.__rvfInternalFormId)
60
+ delete sendBody.__rvfInternalFormId;
61
+ if (sendBody.invoice_details)
62
+ delete sendBody.invoice_details;
63
+ const response = await httpZauru_js_1.default.post(`/sales/orders.json`, { invoice: sendBody }, { headers });
64
+ return response.data;
65
+ });
66
+ }
67
+ exports.createInvoiceOrder = createInvoiceOrder;
68
+ /**
69
+ * updateInvoiceOrder
70
+ * @param headers
71
+ * @param body
72
+ * @returns
73
+ */
74
+ async function updateInvoiceOrder(headers, body) {
75
+ return (0, common_1.handlePossibleAxiosErrors)(async () => {
76
+ const sendBody = {
77
+ ...body,
78
+ invoice_details_attributes: (0, common_1.arrayToObject)(body.invoice_details),
79
+ };
80
+ if (sendBody.deleted_invoice_details)
81
+ delete sendBody.deleted_invoice_details;
82
+ if (sendBody.__rvfInternalFormId)
83
+ delete sendBody.__rvfInternalFormId;
84
+ if (sendBody.invoice_details)
85
+ delete sendBody.invoice_details;
86
+ const response = await httpZauru_js_1.default.patch(`/sales/orders/${body.id}.json`, { invoice: sendBody }, { headers });
87
+ return response.data;
88
+ });
89
+ }
90
+ exports.updateInvoiceOrder = updateInvoiceOrder;
91
+ /**
92
+ * deleteInvoice
93
+ * @param headers
94
+ * @param body
95
+ */
96
+ async function deleteInvoiceOrder(headers, id) {
97
+ return (0, common_1.handlePossibleAxiosErrors)(async () => {
98
+ await httpZauru_js_1.default.get(`/sales/orders/${id}/void`, {
99
+ headers,
100
+ });
101
+ return true;
102
+ });
103
+ }
104
+ exports.deleteInvoiceOrder = deleteInvoiceOrder;