@zauru-sdk/services 1.0.5 → 1.0.6

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 CHANGED
@@ -3,6 +3,14 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [1.0.6](https://github.com/intuitiva/zauru-typescript-sdk/compare/v1.0.5...v1.0.6) (2024-03-16)
7
+
8
+ **Note:** Version bump only for package @zauru-sdk/services
9
+
10
+
11
+
12
+
13
+
6
14
  ## [1.0.5](https://github.com/intuitiva/zauru-typescript-sdk/compare/v1.0.4...v1.0.5) (2024-03-16)
7
15
 
8
16
  **Note:** Version bump only for package @zauru-sdk/services
@@ -1,7 +1,6 @@
1
1
  import { Session } from "@remix-run/node";
2
2
  import { AxiosRequestHeaders } from "axios";
3
- import { AxiosUtilsResponse } from "./zauru/httpZauru.server.js";
4
- import { EmployeeGraphQL, OauthProfile, ProfileResponse, AgencyGraphQL } from "@zauru-sdk/types";
3
+ import { EmployeeGraphQL, OauthProfile, ProfileResponse, AgencyGraphQL, AxiosUtilsResponse } from "@zauru-sdk/types";
5
4
  /**
6
5
  * loginWebApp
7
6
  * @param session
@@ -16,14 +15,6 @@ export declare const loginWebApp: (session: Session, codeValue: string, cookie:
16
15
  userProfile: ProfileResponse;
17
16
  agencyProfile: AgencyGraphQL;
18
17
  }>>;
19
- export type GraphQLToken = {
20
- status: number;
21
- message: string;
22
- token: string;
23
- expires: string;
24
- expiresMsg: string;
25
- graphqlUrl: string;
26
- };
27
18
  /**
28
19
  * Obtiene los headers que se usan en todos los endpoints de zauru
29
20
  * @param cookie
@@ -52,3 +43,13 @@ export type SessionMessage = {
52
43
  export declare const saveSessionMessage: (session: Session, info: SessionMessage) => Promise<void>;
53
44
  export declare const deleteSessionMessage: (session: Session, id: string) => Promise<boolean>;
54
45
  export declare function generateDistinctCode(prefix: string): string;
46
+ /**
47
+ *
48
+ * @param headers
49
+ * @param session
50
+ * @param names
51
+ * @returns
52
+ */
53
+ export declare function getVariablesByName(headers: any, session: Session, names: Array<string>): Promise<{
54
+ [key: string]: string;
55
+ }>;
@@ -1,8 +1,10 @@
1
1
  import { redirect } from "@remix-run/node";
2
2
  import { commitSession, getSession } from "./sessions/sessions.server.js";
3
3
  import chalk from "chalk";
4
- import httpZauru, { handlePossibleAxiosErrors, } from "./zauru/httpZauru.server.js";
4
+ import httpZauru from "./zauru/httpZauru.server.js";
5
5
  import { getAgencyInfo, getEmployeeInfo, getOauthUserInfo, getProfileInformation, } from "./zauru/zauru-profiles.server.js";
6
+ import { handlePossibleAxiosErrors } from "@zauru-sdk/common";
7
+ import { getVariables } from "./zauru/zauru-variables.server.js";
6
8
  /**
7
9
  * loginWebApp
8
10
  * @param session
@@ -177,3 +179,44 @@ export function generateDistinctCode(prefix) {
177
179
  const codigoProducto = `${prefix}-${uuid}`;
178
180
  return codigoProducto;
179
181
  }
182
+ /**
183
+ *
184
+ * @param headers
185
+ * @param session
186
+ * @param names
187
+ * @returns
188
+ */
189
+ export async function getVariablesByName(headers, session, names) {
190
+ //variables
191
+ let variables = [];
192
+ //consulto si ya están guardadas en la sesión
193
+ const tempVars = session.get("variables");
194
+ if (Array.isArray(tempVars) && tempVars.length) {
195
+ //si ya están guardadas, uso esas
196
+ variables = tempVars;
197
+ }
198
+ else {
199
+ //si no están en la sesión, las obtengo de zauru y luego las guardo en la sesión
200
+ //Obtengo mis variables, para tener los tags solicitados
201
+ const response = await getVariables(headers);
202
+ if (response.error) {
203
+ throw new Error(`${response.userMsg} - ${response.msg}`);
204
+ }
205
+ session.set("variables", response.data);
206
+ await commitSession(session);
207
+ variables = response.data ?? [];
208
+ }
209
+ const filtrados = variables.filter((value) => names.includes(value.name));
210
+ const returnObject = {};
211
+ filtrados.forEach((variable) => {
212
+ returnObject[`${variable.name}`] = variable.value;
213
+ });
214
+ //Pregunto si todas las variables fueron encontradas o no
215
+ if (!names.every((variable) => Object.keys(returnObject).includes(variable))) {
216
+ const noEncontradas = names
217
+ .filter((variable) => !Object.keys(returnObject).includes(variable))
218
+ .join(",");
219
+ throw new Error(`No se encontraron las variables: ${noEncontradas} pruebe cerrar e iniciar sesión nuevamente para continuar.`);
220
+ }
221
+ return returnObject;
222
+ }
package/dist/index.d.ts CHANGED
@@ -5,4 +5,6 @@ export * from "./zauru/httpGraphQL.server.js";
5
5
  export * from "./zauru/httpOauth.server.js";
6
6
  export * from "./zauru/httpZauru.server.js";
7
7
  export * from "./zauru/zauru-agencies.server.js";
8
+ export * from "./zauru/zauru-automatic-numbers.server.js";
8
9
  export * from "./zauru/zauru-profiles.server.js";
10
+ export * from "./zauru/zauru-variables.server.js";
package/dist/index.js CHANGED
@@ -5,4 +5,6 @@ export * from "./zauru/httpGraphQL.server.js";
5
5
  export * from "./zauru/httpOauth.server.js";
6
6
  export * from "./zauru/httpZauru.server.js";
7
7
  export * from "./zauru/zauru-agencies.server.js";
8
+ export * from "./zauru/zauru-automatic-numbers.server.js";
8
9
  export * from "./zauru/zauru-profiles.server.js";
10
+ export * from "./zauru/zauru-variables.server.js";
@@ -1,14 +1,2 @@
1
- export type AxiosUtilsResponse<T> = {
2
- error?: boolean;
3
- msg?: string;
4
- userMsg?: string;
5
- data?: T;
6
- };
7
- /**
8
- * Handle web app table actions and return a response with a consistent format.
9
- * @param action A function that returns a Promise of type T.
10
- * @returns A Promise of AxiosUtilsResponse<T>.
11
- */
12
- export declare function handlePossibleAxiosErrors<T>(action: () => Promise<T>): Promise<AxiosUtilsResponse<T>>;
13
1
  declare const httpZauru: import("axios").AxiosInstance;
14
2
  export default httpZauru;
@@ -1,20 +1,6 @@
1
1
  import axios from "axios";
2
2
  import chalk from "chalk";
3
3
  import { config } from "@zauru-sdk/config";
4
- /**
5
- * Handle web app table actions and return a response with a consistent format.
6
- * @param action A function that returns a Promise of type T.
7
- * @returns A Promise of AxiosUtilsResponse<T>.
8
- */
9
- export async function handlePossibleAxiosErrors(action) {
10
- try {
11
- const result = await action();
12
- return { error: false, data: result };
13
- }
14
- catch (error) {
15
- return { error: true, userMsg: error?.toString() };
16
- }
17
- }
18
4
  const axiosInstance = axios.create({
19
5
  baseURL: `${config.zauruBaseURL}`,
20
6
  });
@@ -1,6 +1,5 @@
1
1
  import type { Session } from "@remix-run/node";
2
- import type { AxiosUtilsResponse } from "./httpZauru.server.js";
3
- import type { AgencyGraphQL } from "@zauru-sdk/types";
2
+ import type { AgencyGraphQL, AxiosUtilsResponse } from "@zauru-sdk/types";
4
3
  /**
5
4
  * getAgencies
6
5
  * @param headers
@@ -1,4 +1,4 @@
1
- import { handlePossibleAxiosErrors } from "./httpZauru.server.js";
1
+ import { handlePossibleAxiosErrors } from "@zauru-sdk/common";
2
2
  import httpGraphQLAPI from "./httpGraphQL.server.js";
3
3
  import { getGraphQLAPIHeaders } from "~/common.server.js";
4
4
  import { getAgenciesStringQuery } from "@zauru-sdk/graphql";
@@ -0,0 +1,8 @@
1
+ import { DocumentAutomaticNumberGraphQL, AxiosUtilsResponse } from "@zauru-sdk/types";
2
+ /**
3
+ * updateAutomaticNumber
4
+ * @param headers
5
+ * @param body
6
+ * @returns
7
+ */
8
+ export declare const updateAutomaticNumber: (headers: any, body: Partial<DocumentAutomaticNumberGraphQL>) => Promise<AxiosUtilsResponse<DocumentAutomaticNumberGraphQL>>;
@@ -0,0 +1,21 @@
1
+ import { handlePossibleAxiosErrors } from "@zauru-sdk/common";
2
+ import httpZauru from "./httpZauru.server.js";
3
+ /**
4
+ * updateAutomaticNumber
5
+ * @param headers
6
+ * @param body
7
+ * @returns
8
+ */
9
+ export const updateAutomaticNumber = async (headers, body) => {
10
+ return handlePossibleAxiosErrors(async () => {
11
+ const sendBody = {
12
+ document_automatic_number: {
13
+ ...body,
14
+ },
15
+ };
16
+ const response = await httpZauru.put(`/settings/templates/document_automatic_numbers/${body.id}.json`, sendBody, {
17
+ headers,
18
+ });
19
+ return response.data;
20
+ });
21
+ };
@@ -1,6 +1,5 @@
1
1
  import type { Session } from "@remix-run/node";
2
- import type { AxiosUtilsResponse } from "./httpZauru.server.js";
3
- import { AgencyGraphQL, EmployeeGraphQL, OauthProfile, ProfileResponse } from "@zauru-sdk/types";
2
+ import { AgencyGraphQL, EmployeeGraphQL, OauthProfile, ProfileResponse, AxiosUtilsResponse } from "@zauru-sdk/types";
4
3
  /**
5
4
  * getOauthUserInfo
6
5
  * @param codeValue
@@ -1,5 +1,6 @@
1
- import httpZauru, { handlePossibleAxiosErrors } from "./httpZauru.server.js";
1
+ import httpZauru from "./httpZauru.server.js";
2
2
  import httpOauth from "./httpOauth.server.js";
3
+ import { handlePossibleAxiosErrors } from "@zauru-sdk/common";
3
4
  import { config } from "@zauru-sdk/config";
4
5
  /**
5
6
  * getOauthUserInfo
@@ -0,0 +1,14 @@
1
+ import { VariableGraphQL, AxiosUtilsResponse } from "@zauru-sdk/types";
2
+ /**
3
+ * getVariables Function for get all zauru variables
4
+ * @param headers
5
+ * @returns
6
+ */
7
+ export declare function getVariables(headers: any): Promise<AxiosUtilsResponse<VariableGraphQL[]>>;
8
+ /**
9
+ *
10
+ * @param headers
11
+ * @param body
12
+ * @returns
13
+ */
14
+ export declare function createVariable(headers: any, body: Partial<VariableGraphQL>): Promise<AxiosUtilsResponse<VariableGraphQL>>;
@@ -0,0 +1,48 @@
1
+ import chalk from "chalk";
2
+ import httpZauru from "./httpZauru.server.js";
3
+ /**
4
+ * getVariables Function for get all zauru variables
5
+ * @param headers
6
+ * @returns
7
+ */
8
+ export async function getVariables(headers) {
9
+ try {
10
+ const response = await httpZauru(`/apps/webapp_vars.json`, {
11
+ method: "GET",
12
+ headers: headers,
13
+ });
14
+ return { data: response.data, error: false };
15
+ }
16
+ catch (error) {
17
+ console.log(chalk.red(`OCURRIÓ UN ERROR AL CARGAR LAS VARIABLES: ${error}`));
18
+ return {
19
+ msg: error,
20
+ error: true,
21
+ userMsg: "Ocurrió un error al intentar obtener las variables",
22
+ };
23
+ }
24
+ }
25
+ /**
26
+ *
27
+ * @param headers
28
+ * @param body
29
+ * @returns
30
+ */
31
+ export async function createVariable(headers, body) {
32
+ try {
33
+ const response = await httpZauru.post(`/apps/webapp_vars.json`, { variable: body }, {
34
+ headers,
35
+ });
36
+ return {
37
+ data: response.data,
38
+ error: false,
39
+ };
40
+ }
41
+ catch (error) {
42
+ return {
43
+ msg: error,
44
+ error: true,
45
+ userMsg: `Ocurrió un error al intentar crear la variable ${body.name}`,
46
+ };
47
+ }
48
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zauru-sdk/services",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "Configuración centralizada para aplicaciones Zauru SDK",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -19,14 +19,15 @@
19
19
  },
20
20
  "dependencies": {
21
21
  "@remix-run/node": "^2.8.1",
22
+ "@zauru-sdk/common": "^1.0.6",
22
23
  "@zauru-sdk/config": "^1.0.4",
23
24
  "@zauru-sdk/graphql": "^1.0.5",
24
- "@zauru-sdk/types": "^1.0.5",
25
+ "@zauru-sdk/types": "^1.0.6",
25
26
  "axios": "^1.6.7",
26
27
  "chalk": "5.3.0"
27
28
  },
28
29
  "engines": {
29
30
  "node": ">=18.0.0"
30
31
  },
31
- "gitHead": "0802e24e0ee8234741e4b50c68067dd025acde49"
32
+ "gitHead": "d49d7b1cec4ab5685b34983c46d97db129115aaf"
32
33
  }
@@ -2,10 +2,7 @@ import { Session, redirect } from "@remix-run/node";
2
2
  import { commitSession, getSession } from "./sessions/sessions.server.js";
3
3
  import { AxiosRequestHeaders } from "axios";
4
4
  import chalk from "chalk";
5
- import httpZauru, {
6
- AxiosUtilsResponse,
7
- handlePossibleAxiosErrors,
8
- } from "./zauru/httpZauru.server.js";
5
+ import httpZauru from "./zauru/httpZauru.server.js";
9
6
  import {
10
7
  getAgencyInfo,
11
8
  getEmployeeInfo,
@@ -17,7 +14,12 @@ import {
17
14
  OauthProfile,
18
15
  ProfileResponse,
19
16
  AgencyGraphQL,
17
+ VariableGraphQL,
18
+ GraphQLToken,
19
+ AxiosUtilsResponse,
20
20
  } from "@zauru-sdk/types";
21
+ import { handlePossibleAxiosErrors } from "@zauru-sdk/common";
22
+ import { getVariables } from "./zauru/zauru-variables.server.js";
21
23
 
22
24
  /**
23
25
  * loginWebApp
@@ -107,15 +109,6 @@ export const loginWebApp = async (
107
109
  });
108
110
  };
109
111
 
110
- export type GraphQLToken = {
111
- status: number;
112
- message: string;
113
- token: string;
114
- expires: string;
115
- expiresMsg: string;
116
- graphqlUrl: string;
117
- };
118
-
119
112
  /**
120
113
  * Obtiene los headers que se usan en todos los endpoints de zauru
121
114
  * @param cookie
@@ -293,3 +286,58 @@ export function generateDistinctCode(prefix: string) {
293
286
  const codigoProducto = `${prefix}-${uuid}`;
294
287
  return codigoProducto;
295
288
  }
289
+ /**
290
+ *
291
+ * @param headers
292
+ * @param session
293
+ * @param names
294
+ * @returns
295
+ */
296
+ export async function getVariablesByName(
297
+ headers: any,
298
+ session: Session,
299
+ names: Array<string>
300
+ ): Promise<{ [key: string]: string }> {
301
+ //variables
302
+ let variables: VariableGraphQL[] = [];
303
+
304
+ //consulto si ya están guardadas en la sesión
305
+ const tempVars: VariableGraphQL[] = session.get("variables");
306
+ if (Array.isArray(tempVars) && tempVars.length) {
307
+ //si ya están guardadas, uso esas
308
+ variables = tempVars;
309
+ } else {
310
+ //si no están en la sesión, las obtengo de zauru y luego las guardo en la sesión
311
+ //Obtengo mis variables, para tener los tags solicitados
312
+ const response = await getVariables(headers);
313
+ if (response.error) {
314
+ throw new Error(`${response.userMsg} - ${response.msg}`);
315
+ }
316
+ session.set("variables", response.data);
317
+ await commitSession(session);
318
+ variables = response.data ?? [];
319
+ }
320
+
321
+ const filtrados = variables.filter((value: VariableGraphQL) =>
322
+ names.includes(value.name)
323
+ );
324
+
325
+ const returnObject: { [key: string]: string } = {};
326
+ filtrados.forEach((variable) => {
327
+ returnObject[`${variable.name}`] = variable.value;
328
+ });
329
+
330
+ //Pregunto si todas las variables fueron encontradas o no
331
+ if (
332
+ !names.every((variable) => Object.keys(returnObject).includes(variable))
333
+ ) {
334
+ const noEncontradas = names
335
+ .filter((variable) => !Object.keys(returnObject).includes(variable))
336
+ .join(",");
337
+ throw new Error(
338
+ `No se encontraron las variables: ${noEncontradas} pruebe cerrar e iniciar sesión nuevamente para continuar.`
339
+ );
340
+ }
341
+
342
+ return returnObject;
343
+ }
package/src/index.ts CHANGED
@@ -8,4 +8,6 @@ export * from "./zauru/httpOauth.server.js";
8
8
  export * from "./zauru/httpZauru.server.js";
9
9
 
10
10
  export * from "./zauru/zauru-agencies.server.js";
11
+ export * from "./zauru/zauru-automatic-numbers.server.js";
11
12
  export * from "./zauru/zauru-profiles.server.js";
13
+ export * from "./zauru/zauru-variables.server.js";
@@ -1,29 +1,7 @@
1
1
  import axios from "axios";
2
2
  import chalk from "chalk";
3
3
  import { config } from "@zauru-sdk/config";
4
-
5
- export type AxiosUtilsResponse<T> = {
6
- error?: boolean;
7
- msg?: string;
8
- userMsg?: string;
9
- data?: T;
10
- };
11
-
12
- /**
13
- * Handle web app table actions and return a response with a consistent format.
14
- * @param action A function that returns a Promise of type T.
15
- * @returns A Promise of AxiosUtilsResponse<T>.
16
- */
17
- export async function handlePossibleAxiosErrors<T>(
18
- action: () => Promise<T>
19
- ): Promise<AxiosUtilsResponse<T>> {
20
- try {
21
- const result = await action();
22
- return { error: false, data: result } as AxiosUtilsResponse<T>;
23
- } catch (error) {
24
- return { error: true, userMsg: error?.toString() } as AxiosUtilsResponse<T>;
25
- }
26
- }
4
+ import { AxiosUtilsResponse } from "@zauru-sdk/types";
27
5
 
28
6
  const axiosInstance = axios.create({
29
7
  baseURL: `${config.zauruBaseURL}`,
@@ -1,10 +1,9 @@
1
1
  import type { Session } from "@remix-run/node";
2
- import type { AxiosUtilsResponse } from "./httpZauru.server.js";
3
- import { handlePossibleAxiosErrors } from "./httpZauru.server.js";
2
+ import { handlePossibleAxiosErrors } from "@zauru-sdk/common";
4
3
  import httpGraphQLAPI from "./httpGraphQL.server.js";
5
4
  import { getGraphQLAPIHeaders } from "~/common.server.js";
6
5
  import { getAgenciesStringQuery } from "@zauru-sdk/graphql";
7
- import type { AgencyGraphQL } from "@zauru-sdk/types";
6
+ import type { AgencyGraphQL, AxiosUtilsResponse } from "@zauru-sdk/types";
8
7
 
9
8
  /**
10
9
  * getAgencies
@@ -0,0 +1,35 @@
1
+ import {
2
+ DocumentAutomaticNumberGraphQL,
3
+ AxiosUtilsResponse,
4
+ } from "@zauru-sdk/types";
5
+ import { handlePossibleAxiosErrors } from "@zauru-sdk/common";
6
+ import httpZauru from "./httpZauru.server.js";
7
+
8
+ /**
9
+ * updateAutomaticNumber
10
+ * @param headers
11
+ * @param body
12
+ * @returns
13
+ */
14
+ export const updateAutomaticNumber = async (
15
+ headers: any,
16
+ body: Partial<DocumentAutomaticNumberGraphQL>
17
+ ): Promise<AxiosUtilsResponse<DocumentAutomaticNumberGraphQL>> => {
18
+ return handlePossibleAxiosErrors(async () => {
19
+ const sendBody: any = {
20
+ document_automatic_number: {
21
+ ...body,
22
+ },
23
+ };
24
+
25
+ const response = await httpZauru.put<DocumentAutomaticNumberGraphQL>(
26
+ `/settings/templates/document_automatic_numbers/${body.id}.json`,
27
+ sendBody,
28
+ {
29
+ headers,
30
+ }
31
+ );
32
+
33
+ return response.data;
34
+ });
35
+ };
@@ -1,13 +1,14 @@
1
1
  import type { Session } from "@remix-run/node";
2
- import type { AxiosUtilsResponse } from "./httpZauru.server.js";
3
- import httpZauru, { handlePossibleAxiosErrors } from "./httpZauru.server.js";
2
+ import httpZauru from "./httpZauru.server.js";
4
3
  import httpOauth from "./httpOauth.server.js";
5
4
  import {
6
5
  AgencyGraphQL,
7
6
  EmployeeGraphQL,
8
7
  OauthProfile,
9
8
  ProfileResponse,
9
+ AxiosUtilsResponse,
10
10
  } from "@zauru-sdk/types";
11
+ import { handlePossibleAxiosErrors } from "@zauru-sdk/common";
11
12
  import { config } from "@zauru-sdk/config";
12
13
 
13
14
  /**
@@ -0,0 +1,67 @@
1
+ import chalk from "chalk";
2
+ import httpZauru from "./httpZauru.server.js";
3
+ import { VariableGraphQL, AxiosUtilsResponse } from "@zauru-sdk/types";
4
+
5
+ /**
6
+ * getVariables Function for get all zauru variables
7
+ * @param headers
8
+ * @returns
9
+ */
10
+ export async function getVariables(
11
+ headers: any
12
+ ): Promise<AxiosUtilsResponse<VariableGraphQL[]>> {
13
+ try {
14
+ const response = await httpZauru<VariableGraphQL[]>(
15
+ `/apps/webapp_vars.json`,
16
+ {
17
+ method: "GET",
18
+ headers: headers,
19
+ }
20
+ );
21
+
22
+ return { data: response.data, error: false } as AxiosUtilsResponse<
23
+ VariableGraphQL[]
24
+ >;
25
+ } catch (error) {
26
+ console.log(
27
+ chalk.red(`OCURRIÓ UN ERROR AL CARGAR LAS VARIABLES: ${error}`)
28
+ );
29
+ return {
30
+ msg: error,
31
+ error: true,
32
+ userMsg: "Ocurrió un error al intentar obtener las variables",
33
+ } as AxiosUtilsResponse<VariableGraphQL[]>;
34
+ }
35
+ }
36
+
37
+ /**
38
+ *
39
+ * @param headers
40
+ * @param body
41
+ * @returns
42
+ */
43
+ export async function createVariable(
44
+ headers: any,
45
+ body: Partial<VariableGraphQL>
46
+ ) {
47
+ try {
48
+ const response = await httpZauru.post<VariableGraphQL>(
49
+ `/apps/webapp_vars.json`,
50
+ { variable: body },
51
+ {
52
+ headers,
53
+ }
54
+ );
55
+
56
+ return {
57
+ data: response.data,
58
+ error: false,
59
+ } as AxiosUtilsResponse<VariableGraphQL>;
60
+ } catch (error) {
61
+ return {
62
+ msg: error,
63
+ error: true,
64
+ userMsg: `Ocurrió un error al intentar crear la variable ${body.name}`,
65
+ } as AxiosUtilsResponse<VariableGraphQL>;
66
+ }
67
+ }