@zauru-sdk/services 1.0.4

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.
@@ -0,0 +1,55 @@
1
+ import axios from "axios";
2
+ import chalk from "chalk";
3
+ import { config } from "@zauru-sdk/config";
4
+
5
+ const axiosInstance = axios.create({
6
+ baseURL: `${config.oauthBaseURL}`,
7
+ });
8
+
9
+ axiosInstance.interceptors.request.use(
10
+ function (request) {
11
+ // Do something before request is sent
12
+ console.log("---------------- EJECUTANDO REQUEST ----------------");
13
+ //console.time(`${request.baseURL}${request.url}`);
14
+ console.log(chalk.green(`${request.baseURL}${request.url}`));
15
+ request.timeout = 120000;
16
+
17
+ return request;
18
+ },
19
+ function (error) {
20
+ console.log(
21
+ chalk.red("---------------- ERROR CON REQUEST ----------------")
22
+ );
23
+ console.log(`${error}`);
24
+ // Do something with request error
25
+ return Promise.reject(error);
26
+ }
27
+ );
28
+
29
+ // Add a response interceptor
30
+ axiosInstance.interceptors.response.use(
31
+ function (response) {
32
+ // Do something with response data
33
+ //console.timeEnd(`${response.config.baseURL}${response.config.url}`);
34
+ return response;
35
+ },
36
+ function (error) {
37
+ console.log(
38
+ chalk.red("---------------- ERROR CON REQUEST ----------------")
39
+ );
40
+ console.log(`${error}`);
41
+ // Do something with response error
42
+ const { response } = error;
43
+ const msgError = response
44
+ ? `HTTP ${response.status} ${response.statusText} - URL: ${
45
+ response?.config?.baseURL
46
+ }${response?.config?.url} - ${JSON.stringify(response?.data)}`
47
+ : error;
48
+ console.log(chalk.red(`${msgError}`));
49
+ throw new Error(msgError);
50
+ }
51
+ );
52
+
53
+ const httpOauth = axiosInstance;
54
+
55
+ export default httpOauth;
@@ -0,0 +1,78 @@
1
+ import axios from "axios";
2
+ import chalk from "chalk";
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
+ }
27
+
28
+ const axiosInstance = axios.create({
29
+ baseURL: `${config.zauruBaseURL}`,
30
+ });
31
+
32
+ axiosInstance.interceptors.request.use(
33
+ function (request) {
34
+ // Do something before request is sent
35
+ console.log(`---------------- EJECUTANDO REQUEST ----------------`);
36
+ //console.time(`${request.baseURL}${request.url}`);
37
+ console.log(chalk.green(`${request.baseURL}${request.url}`));
38
+ request.timeout = 120000;
39
+
40
+ return request;
41
+ },
42
+ function (error) {
43
+ console.log(
44
+ chalk.red("---------------- ERROR CON REQUEST ----------------")
45
+ );
46
+ console.log(`${error}`);
47
+ // Do something with request error
48
+ return Promise.reject(error);
49
+ }
50
+ );
51
+
52
+ // Add a response interceptor
53
+ axiosInstance.interceptors.response.use(
54
+ function (response) {
55
+ // Do something with response data
56
+ //console.timeEnd(`${response.config.baseURL}${response.config.url}`);
57
+ return response;
58
+ },
59
+ function (error) {
60
+ console.log(
61
+ chalk.red("---------------- ERROR CON REQUEST ----------------")
62
+ );
63
+ console.log(`${error}`);
64
+ // Do something with response error
65
+ const { response } = error;
66
+ const msgError = response
67
+ ? `HTTP ${response.status} ${response.statusText} - URL: ${
68
+ response?.config?.baseURL
69
+ }${response?.config?.url} - ${JSON.stringify(response?.data)}`
70
+ : error;
71
+ console.log(chalk.red(`${msgError}`));
72
+ throw new Error(msgError);
73
+ }
74
+ );
75
+
76
+ const httpZauru = axiosInstance;
77
+
78
+ export default httpZauru;
@@ -0,0 +1,35 @@
1
+ import type { Session } from "@remix-run/node";
2
+ import type { AgencyGraphQL } from "@zauru-sdk/graphql";
3
+ import type { AxiosUtilsResponse } from "./httpZauru.server.js";
4
+ import { handlePossibleAxiosErrors } from "./httpZauru.server.js";
5
+ import httpGraphQLAPI from "./httpGraphQL.server.js";
6
+ import { getGraphQLAPIHeaders } from "~/common.server.js";
7
+ import graphql from "@zauru-sdk/graphql";
8
+
9
+ const { default: queries } = graphql;
10
+
11
+ /**
12
+ * getAgencies
13
+ * @param headers
14
+ * @returns
15
+ */
16
+ export async function getAgencies(
17
+ session: Session
18
+ ): Promise<AxiosUtilsResponse<AgencyGraphQL[]>> {
19
+ return handlePossibleAxiosErrors(async () => {
20
+ const headers = await getGraphQLAPIHeaders(session);
21
+ const response = await httpGraphQLAPI.post<{
22
+ data: { agencies: AgencyGraphQL[] };
23
+ }>(
24
+ ``,
25
+ {
26
+ query: queries.getAgencies,
27
+ },
28
+ {
29
+ headers,
30
+ }
31
+ );
32
+
33
+ return response.data?.data.agencies;
34
+ });
35
+ }
@@ -0,0 +1,243 @@
1
+ import type { Session } from "@remix-run/node";
2
+ import type { AxiosUtilsResponse } from "./httpZauru.server.js";
3
+ import httpZauru, { handlePossibleAxiosErrors } from "./httpZauru.server.js";
4
+ import httpOauth from "./httpOauth.server.js";
5
+ import { EmployeeGraphQL } from "@zauru-sdk/graphql";
6
+ import { config } from "@zauru-sdk/config";
7
+
8
+ interface Logo {
9
+ url: null | string;
10
+ thumb: {
11
+ url: null | string;
12
+ };
13
+ standard: {
14
+ url: null | string;
15
+ };
16
+ header: {
17
+ url: null | string;
18
+ };
19
+ }
20
+
21
+ export type OauthProfile = {
22
+ id: number;
23
+ uid: number;
24
+ username: string;
25
+ name: string;
26
+ time_zone: string;
27
+ mobile_phone: string;
28
+ admin: boolean;
29
+ selected_entity: number;
30
+ selected_entity_name: string;
31
+ selected_entity_logo: string;
32
+ selected_entity_role: number;
33
+ employee_id: number;
34
+ crm_admin: boolean;
35
+ crm_supervisor: boolean;
36
+ crm_user: boolean;
37
+ currency_id: number;
38
+ currency_name: string;
39
+ currency_prefix: string;
40
+ api_key: string;
41
+ scope: string;
42
+ email: string;
43
+ };
44
+ interface Entity {
45
+ id: number;
46
+ name: string;
47
+ tin: string;
48
+ country: string;
49
+ currency_id: number;
50
+ entity_type_id: number;
51
+ industry: string;
52
+ web: string;
53
+ notes: string;
54
+ vat: number;
55
+ vat_included: boolean;
56
+ logo: Logo;
57
+ logo_2: Logo;
58
+ created_at: string;
59
+ updated_at: string;
60
+ distributor: number;
61
+ costing_method: string;
62
+ address: string;
63
+ state: string;
64
+ city: string;
65
+ income_tax: number;
66
+ report_logo: Logo;
67
+ distributor_contract_id: number | null;
68
+ producer_contract_id: number | null;
69
+ legal_representative_name: string | null;
70
+ legal_representative_identification: string | null;
71
+ legal_representative_birthday: string | null;
72
+ legal_representative_gender: boolean;
73
+ legal_representative_marital_status: string | null;
74
+ legal_representative_occupation: string | null;
75
+ legal_representative_nationality: string | null;
76
+ exporter_code: string | null;
77
+ }
78
+
79
+ export type Agency = {
80
+ id: number;
81
+ zid: number;
82
+ active: boolean;
83
+ ean13: string;
84
+ name: string;
85
+ employee_id: number | null;
86
+ updater_id: number;
87
+ entity_id: number;
88
+ virtual: boolean;
89
+ virtual_type: string | null;
90
+ warehouse: boolean;
91
+ point_of_sale: boolean;
92
+ workshop: boolean;
93
+ factory: boolean;
94
+ contact: string;
95
+ city: string;
96
+ address_line_1: string;
97
+ address_line_2: string;
98
+ phone: string;
99
+ notes: string;
100
+ created_at: string;
101
+ updated_at: string;
102
+ price_list_id: number | null;
103
+ quote: boolean;
104
+ ecommerce: boolean;
105
+ external_storage_service_name: string;
106
+ agency_category_id: number | null;
107
+ };
108
+
109
+ export type Membership = {
110
+ id: number;
111
+ active: boolean;
112
+ current: boolean;
113
+ reference: string;
114
+ starts: string;
115
+ expires: string;
116
+ eternal: boolean;
117
+ entity_id: number;
118
+ notes: string;
119
+ created_at: string;
120
+ updated_at: string;
121
+ licenses_count: number;
122
+ entity: Entity;
123
+ };
124
+
125
+ type Profile = {
126
+ id: number;
127
+ email: string;
128
+ active: boolean;
129
+ admin: boolean;
130
+ name: string;
131
+ address: null | string;
132
+ mobile_phone: string;
133
+ birthday: null | string;
134
+ gender: boolean;
135
+ notes: null | string;
136
+ time_zone: string;
137
+ language: string;
138
+ selected_entity_id: number;
139
+ created_at: string;
140
+ updated_at: string;
141
+ authentication_token: string;
142
+ provider: string;
143
+ provider_token: string;
144
+ };
145
+
146
+ export type ProfileResponse = {
147
+ profile: Profile;
148
+ memberships: Membership[];
149
+ };
150
+
151
+ /**
152
+ * getOauthUserInfo
153
+ * @param codeValue
154
+ * @returns
155
+ */
156
+ export const getOauthUserInfo = async (
157
+ codeValue: string
158
+ ): Promise<AxiosUtilsResponse<OauthProfile>> => {
159
+ return handlePossibleAxiosErrors(async () => {
160
+ const response = await httpOauth.get<OauthProfile>(`/api/userinfo`, {
161
+ headers: {
162
+ Authorization: `Bearer ${codeValue}`,
163
+ },
164
+ });
165
+ return response.data;
166
+ });
167
+ };
168
+
169
+ /**
170
+ *
171
+ * @param employeeId
172
+ * @param headers
173
+ * @returns
174
+ */
175
+ export const getEmployeeInfo = async (
176
+ id: number,
177
+ headers: any
178
+ ): Promise<AxiosUtilsResponse<EmployeeGraphQL>> => {
179
+ return handlePossibleAxiosErrors(async () => {
180
+ const employeeInfo = await httpZauru.get<EmployeeGraphQL>(
181
+ `/settings/employees/${id}.json`,
182
+ { headers }
183
+ );
184
+ return employeeInfo.data;
185
+ });
186
+ };
187
+
188
+ /**
189
+ * getProfileInformation
190
+ * @param headers
191
+ * @returns
192
+ */
193
+ export const getProfileInformation = async (
194
+ headers: any
195
+ ): Promise<AxiosUtilsResponse<ProfileResponse>> => {
196
+ return handlePossibleAxiosErrors(async () => {
197
+ const response = await httpZauru.get<ProfileResponse>(`/profile.json`, {
198
+ headers,
199
+ });
200
+
201
+ return response.data;
202
+ });
203
+ };
204
+
205
+ export const changeEntity = async (headers: any, entityId: string) => {
206
+ const changeEntityFetch = await httpZauru.patch<any>(
207
+ `${config.zauruBaseURL}/company.json`,
208
+ JSON.stringify({
209
+ selected_entity_id: entityId,
210
+ }),
211
+ {
212
+ headers,
213
+ }
214
+ );
215
+ return changeEntityFetch.data;
216
+ };
217
+
218
+ /**
219
+ *
220
+ * @param headers
221
+ * @returns
222
+ */
223
+ export const getAgencyInfo = async (
224
+ headers: any,
225
+ session: Session
226
+ ): Promise<AxiosUtilsResponse<Agency>> => {
227
+ return handlePossibleAxiosErrors(async () => {
228
+ const agency_id = session.get("agency_id");
229
+ if (!agency_id) {
230
+ throw new Error(
231
+ "No hay una agencia asignada para este usuario... Contacte con su administrador."
232
+ );
233
+ }
234
+ const response = await httpZauru.get<Agency>(
235
+ `/settings/agencies/${agency_id}.json`,
236
+ {
237
+ headers,
238
+ }
239
+ );
240
+
241
+ return response?.data;
242
+ });
243
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "include": ["env.d.ts", "**/*.ts", "**/*.tsx"],
3
+ "compilerOptions": {
4
+ "allowSyntheticDefaultImports": true,
5
+ "lib": ["ES2022"],
6
+ "isolatedModules": true,
7
+ "moduleResolution": "NodeNext",
8
+ "module": "NodeNext",
9
+ "esModuleInterop": true,
10
+ "target": "ES2022",
11
+ "strict": true,
12
+ "allowJs": true,
13
+ "skipLibCheck": true,
14
+ "forceConsistentCasingInFileNames": true,
15
+ "baseUrl": ".",
16
+ "outDir": "./dist",
17
+ "rootDir": "./src",
18
+ "declaration": true,
19
+ "declarationDir": "./dist",
20
+ "paths": {
21
+ "~/*": ["./src/*"]
22
+ }
23
+ }
24
+ }