@begapp/sdk 1.1.2 → 2.0.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.
package/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { configurarSDKBegaApp } from './src/client.js';
2
- import { crearNotificacionBegaApp } from './src/notificaciones.js';
2
+ import { crearNotificacionBegaApp, crearNotificacionFileBegaApp } from './src/notificaciones.js';
3
3
 
4
4
  export {
5
5
  configurarSDKBegaApp,
6
6
  crearNotificacionBegaApp,
7
+ crearNotificacionFileBegaApp
7
8
  };
package/package.json CHANGED
@@ -1,20 +1,20 @@
1
- {
2
- "name": "@begapp/sdk",
3
- "version": "1.1.2",
4
- "description": "SDK para interactuar con el servicio de notificaciones de Bega App",
5
- "main": "index.js",
6
- "type": "module",
7
- "scripts": {
8
- "test": "echo \"Error: no test specified\" && exit 1"
9
- },
10
- "keywords": [],
11
- "author": "Bega App",
12
- "license": "MIT",
13
- "dependencies": {
14
- "axios": "^1.9.0",
15
- "crypto-js": "^4.2.0"
16
- },
17
- "publishConfig": {
18
- "access": "public"
19
- }
20
- }
1
+ {
2
+ "name": "@begapp/sdk",
3
+ "version": "2.0.0",
4
+ "description": "SDK para interactuar con el servicio de notificaciones de Bega App",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "keywords": [],
11
+ "author": "Bega App",
12
+ "license": "MIT",
13
+ "dependencies": {
14
+ "axios": "^1.9.0",
15
+ "crypto-js": "^4.2.0"
16
+ },
17
+ "publishConfig": {
18
+ "access": "public"
19
+ }
20
+ }
package/src/client.js CHANGED
@@ -6,9 +6,9 @@ var claveSecreta = '';
6
6
  export function configurarSDKBegaApp({ apiKey, urlApi, secretKey }) {
7
7
  if (!apiKey) throw new Error("Debes proporcionar una API key");
8
8
  if (!urlApi) throw new Error("Debes proporcionar la URL de la API Bega");
9
- if (!secretKey) throw new Error("Debes proporcionar la URL de la API Bega");
9
+ // if (!secretKey) throw new Error("Debes proporcionar la Secret Key de la API Bega");
10
10
 
11
- claveSecreta = secretKey;
11
+ // claveSecreta = secretKey;
12
12
 
13
13
  axiosInstance = axios.create({
14
14
  baseURL: urlApi,
@@ -25,12 +25,4 @@ export function getClient() {
25
25
  }
26
26
 
27
27
  return axiosInstance;
28
- }
29
-
30
- export function getClaveSecreta() {
31
- if (secretKey === '') {
32
- throw new Error("No se ha configurado la Clave secreta para encriptar los mensajes");
33
- }
34
-
35
- return claveSecreta;
36
28
  }
@@ -1,18 +1,76 @@
1
- import { getClaveSecreta, getClient } from './client.js';
2
- import { encryptMensaje } from './crypMensaje.js';
1
+ import { getClient } from './client.js';
3
2
 
4
- export async function crearNotificacionBegaApp(id, dataFull) {
5
- if (!id || typeof dataFull !== 'object') {
6
- throw new Error("crearNotificacion requiere un ID y un objeto data.");
3
+ export async function crearNotificacionBegaApp(id, data) {
4
+ try {
5
+ if (!id || typeof data !== 'object') {
6
+ throw new Error("crearNotificacion requiere un ID y un objeto data.");
7
+ }
8
+
9
+ const client = getClient();
10
+ const res = await client.post("/notification/create-notification", {
11
+ id,
12
+ data,
13
+ });
14
+
15
+ return { status: "success", data: res.data };
16
+ } catch (error) {
17
+ return { status: "error", message: error.message }
7
18
  }
19
+ }
20
+
21
+ export async function crearNotificacionFileBegaApp(id, data, files) {
22
+ try {
23
+ if (!id || typeof data !== 'object') {
24
+ throw new Error("crearNotificacionFileBegaApp requiere un ID y un objeto data.");
25
+ }
26
+
27
+ if (!files || files.length <= 0) {
28
+ throw new Error("crearNotificacionFileBegaApp requiere al menos un archivo");
29
+ }
30
+
31
+ const client = getClient();
32
+
33
+ const attachments = await Promise.all(
34
+ files.map(async ({ name, file }) => {
8
35
 
9
- const data = encryptMensaje(dataFull, getClaveSecreta());
36
+ const form = new FormData();
37
+ form.append('file', file, `${name}.pdf`);
38
+ form.append('meta', JSON.stringify({ generatedAt: new Date().toISOString() }));
39
+ form.append('id', id);
10
40
 
11
- const client = getClient();
12
- const res = await client.post("/notification/create-notification", {
13
- id,
14
- data,
15
- });
41
+ console.log("ASÍ ESTOY ARMANDO EL FORM: ", form);
16
42
 
17
- return res.data;
43
+ const respFile = await client.post("uploads/document-notification", form, {
44
+ headers: {
45
+ // No pongas application/json. Deja que el browser ponga el Content-Type con boundary.
46
+ // En axios está bien poner 'multipart/form-data' — el browser añadirá el boundary.
47
+ 'Content-Type': 'multipart/form-data'
48
+ },
49
+ onUploadProgress: (e) => {
50
+ if (!e.total) return;
51
+ const pct = Math.round((e.loaded * 100) / e.total);
52
+ console.log(`${name}: Progreso de subida: ${pct}%`);
53
+ },
54
+ });
55
+
56
+ if (respFile?.data?.status !== 'success') {
57
+ throw new Error(`Upload fallido para ${name}: ${respFile?.data?.message ?? 'sin mensaje'}`);
58
+ }
59
+
60
+ return respFile.data.attachment;
61
+ })
62
+ );
63
+
64
+ console.log("ESTE ES MI ATTACHMENTS: ", attachments);
65
+
66
+ const res = await client.post("/notification/create-notification", {
67
+ id,
68
+ attachments,
69
+ data,
70
+ });
71
+
72
+ return { status: "success", data: res.data };
73
+ } catch (error) {
74
+ return { status: "error", message: error.message }
75
+ }
18
76
  }
@@ -1,24 +0,0 @@
1
- import CryptoJS from "crypto-js";
2
-
3
- /**
4
- * Cifra un objeto JSON como string
5
- * @param {Object} data - El objeto con los datos (por ejemplo { cliente, estado })
6
- * @param {string} claveSecreta - Clave AES
7
- * @returns {string} - Texto cifrado
8
- */
9
- export function encryptMensaje(data, claveSecreta) {
10
- const stringData = JSON.stringify(data);
11
- return CryptoJS.AES.encrypt(stringData, claveSecreta).toString();
12
- }
13
-
14
- /**
15
- * Descifra un string cifrado a objeto JSON
16
- * @param {string} mensajeCifrado - Texto cifrado
17
- * @param {string} claveSecreta - Clave AES
18
- * @returns {Object} - Objeto original
19
- */
20
- export function decryptMensaje(mensajeCifrado, claveSecreta) {
21
- const bytes = CryptoJS.AES.decrypt(mensajeCifrado, claveSecreta);
22
- const textoPlano = bytes.toString(CryptoJS.enc.Utf8);
23
- return JSON.parse(textoPlano);
24
- }