@miguelmorales13/nestkit 0.31.0 → 0.31.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.
package/README.md CHANGED
@@ -1197,6 +1197,8 @@ Qué resuelve por vos, y por qué cada cosa existe:
1197
1197
 
1198
1198
  - **Secretos cifrados** (AES-256-GCM, `CredentialsCipher`); la API nunca devuelve una llave (`ConnectionView`
1199
1199
  no las tiene). Sin llave de cifrado, `available === false` y nada intenta cobrar.
1200
+ - **Reconectar no duplica**: autorizar otra vez una cuenta de Mercado Pago ya conectada renueva su fila (llaves,
1201
+ activa); solo agrega otra si la cuenta es distinta o tiene terminales sin tomar.
1200
1202
  - **Renovación del token de Mercado Pago** 5 min antes de caducar, guardando el `refresh_token` nuevo (rota).
1201
1203
  - **Aprovisionamiento**: reutiliza la Sucursal/Caja que la terminal ya trae antes de crear otra, y rechaza con
1202
1204
  `MERCADOPAGO_TERMINAL_NO_SOPORTADA` una Point Air (solo Smart 1 y 2 operan por API). Si algo falla no bloquea la elección.
@@ -216,6 +216,13 @@ export declare class TerminalConnections {
216
216
  id: string;
217
217
  terminals: number;
218
218
  }>;
219
+ /**
220
+ * Re-authorizing an account the tenant already connected renews that row (credentials, active) instead of piling up
221
+ * copies of it; alias, primary flag and chosen terminal stay. Returns null — a new row is due — when the account is
222
+ * new, or when it holds terminals that no row has taken yet (that is how a second terminal is added). A row still
223
+ * waiting for its terminal is reused first.
224
+ */
225
+ private renewMercadoPago;
219
226
  /** Inserts a new row (never overwrites); the tenant's first terminal becomes primary on its own. */
220
227
  private save;
221
228
  terminals(scopeId: string, id: string): Promise<TerminalSummary[]>;
@@ -1408,9 +1408,37 @@ var TerminalConnections = class {
1408
1408
  expira: tokens.expiresAt.toISOString()
1409
1409
  };
1410
1410
  const terminals = await this.adapter("mercadopago", credentials, signed.scopeId).listTerminals();
1411
- const id = await this.save(signed.scopeId, "mercadopago", credentials, terminals);
1411
+ const id = await _asyncNullishCoalesce(await this.renewMercadoPago(signed.scopeId, credentials, terminals), async () => ( await this.save(signed.scopeId, "mercadopago", credentials, terminals)));
1412
1412
  return { scopeId: signed.scopeId, actorId: signed.actorId, id, terminals: terminals.length };
1413
1413
  }
1414
+ /**
1415
+ * Re-authorizing an account the tenant already connected renews that row (credentials, active) instead of piling up
1416
+ * copies of it; alias, primary flag and chosen terminal stay. Returns null — a new row is due — when the account is
1417
+ * new, or when it holds terminals that no row has taken yet (that is how a second terminal is added). A row still
1418
+ * waiting for its terminal is reused first.
1419
+ */
1420
+ async renewMercadoPago(scopeId, credentials, terminals) {
1421
+ if (!credentials.mpUserId) return null;
1422
+ const cipher = this.requireCipher();
1423
+ const same = this.sorted(await this.o.store.list(scopeId)).filter((r) => {
1424
+ if (r.provider !== "mercadopago") return false;
1425
+ try {
1426
+ return cipher.decryptJson(r.encryptedCredentials).mpUserId === credentials.mpUserId;
1427
+ } catch (e4) {
1428
+ return false;
1429
+ }
1430
+ });
1431
+ const only = terminals.length === 1 ? terminals[0] : null;
1432
+ const allTaken = terminals.every((t) => same.some((r) => r.terminalId === t.id));
1433
+ const row = _nullishCoalesce(_nullishCoalesce((only && same.find((r) => r.terminalId === only.id)), () => ( same.find((r) => !r.terminalId))), () => ( (allTaken ? same[0] : void 0)));
1434
+ if (!row) return null;
1435
+ await this.o.store.update(row.id, {
1436
+ encryptedCredentials: cipher.encryptJson(credentials),
1437
+ active: true,
1438
+ ...only && !row.terminalId && { terminalId: only.id, terminalName: terminalName(only) }
1439
+ });
1440
+ return row.id;
1441
+ }
1414
1442
  /** Inserts a new row (never overwrites); the tenant's first terminal becomes primary on its own. */
1415
1443
  async save(scopeId, provider, credentials, terminals) {
1416
1444
  const cipher = this.requireCipher();
@@ -1616,7 +1644,7 @@ var TerminalConnections = class {
1616
1644
  scopes.add(r.scopeId);
1617
1645
  seen.set(u, scopes);
1618
1646
  byUser.set(u, (_nullishCoalesce(byUser.get(u), () => ( /* @__PURE__ */ new Set()))).add(r.id));
1619
- } catch (e4) {
1647
+ } catch (e5) {
1620
1648
  }
1621
1649
  }
1622
1650
  this.mpCache = { at: Date.now(), byUser };
@@ -1408,9 +1408,37 @@ var TerminalConnections = class {
1408
1408
  expira: tokens.expiresAt.toISOString()
1409
1409
  };
1410
1410
  const terminals = await this.adapter("mercadopago", credentials, signed.scopeId).listTerminals();
1411
- const id = await this.save(signed.scopeId, "mercadopago", credentials, terminals);
1411
+ const id = await this.renewMercadoPago(signed.scopeId, credentials, terminals) ?? await this.save(signed.scopeId, "mercadopago", credentials, terminals);
1412
1412
  return { scopeId: signed.scopeId, actorId: signed.actorId, id, terminals: terminals.length };
1413
1413
  }
1414
+ /**
1415
+ * Re-authorizing an account the tenant already connected renews that row (credentials, active) instead of piling up
1416
+ * copies of it; alias, primary flag and chosen terminal stay. Returns null — a new row is due — when the account is
1417
+ * new, or when it holds terminals that no row has taken yet (that is how a second terminal is added). A row still
1418
+ * waiting for its terminal is reused first.
1419
+ */
1420
+ async renewMercadoPago(scopeId, credentials, terminals) {
1421
+ if (!credentials.mpUserId) return null;
1422
+ const cipher = this.requireCipher();
1423
+ const same = this.sorted(await this.o.store.list(scopeId)).filter((r) => {
1424
+ if (r.provider !== "mercadopago") return false;
1425
+ try {
1426
+ return cipher.decryptJson(r.encryptedCredentials).mpUserId === credentials.mpUserId;
1427
+ } catch {
1428
+ return false;
1429
+ }
1430
+ });
1431
+ const only = terminals.length === 1 ? terminals[0] : null;
1432
+ const allTaken = terminals.every((t) => same.some((r) => r.terminalId === t.id));
1433
+ const row = (only && same.find((r) => r.terminalId === only.id)) ?? same.find((r) => !r.terminalId) ?? (allTaken ? same[0] : void 0);
1434
+ if (!row) return null;
1435
+ await this.o.store.update(row.id, {
1436
+ encryptedCredentials: cipher.encryptJson(credentials),
1437
+ active: true,
1438
+ ...only && !row.terminalId && { terminalId: only.id, terminalName: terminalName(only) }
1439
+ });
1440
+ return row.id;
1441
+ }
1414
1442
  /** Inserts a new row (never overwrites); the tenant's first terminal becomes primary on its own. */
1415
1443
  async save(scopeId, provider, credentials, terminals) {
1416
1444
  const cipher = this.requireCipher();
package/package.json CHANGED
@@ -1,8 +1,12 @@
1
1
  {
2
2
  "name": "@miguelmorales13/nestkit",
3
- "version": "0.31.0",
3
+ "version": "0.31.4",
4
4
  "private": false,
5
5
  "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/miguelmoralesr13/nestkit.git"
9
+ },
6
10
  "type": "module",
7
11
  "description": "Reusable NestJS building blocks: i18n, error handling, standardized responses, request tracking, base entities, generic CRUD, Postgres/Supabase connection helpers, outbound Telegram/WhatsApp/Stripe/Meta clients, short-lived media hosting, batteries-included auth, Stripe webhooks, Resend/Nodemailer email, card-present terminal payments (Mercado Pago Point, Clip) with marketplace OAuth, Store/POS provisioning and a multi-tenant connections service (encrypted credentials, token refresh, webhooks, refunds, online checkout), signed stateless OAuth connect state, and CFDI 4.0 invoicing (Facturapi, Facturama).",
8
12
  "publishConfig": {
@@ -284,28 +288,28 @@
284
288
  "test": "bun test"
285
289
  },
286
290
  "peerDependencies": {
291
+ "@aws-sdk/client-s3": "^3.0.0",
292
+ "@aws-sdk/lib-storage": "^3.0.0",
293
+ "@aws-sdk/s3-request-presigner": "^3.0.0",
287
294
  "@nestjs/common": "^11.0.0",
288
295
  "@nestjs/core": "^11.0.0",
296
+ "@nestjs/schedule": "",
289
297
  "@nestjs/swagger": "^11.0.0",
298
+ "@simplewebauthn/server": "^13.0.0 || ^14.0.0",
290
299
  "@supabase/supabase-js": "^2.45.0",
300
+ "class-transformer": "^0.5.1",
301
+ "class-validator": "^0.15.0",
291
302
  "helmet": "^8.0.0",
303
+ "nodemailer": "^9.0.5",
292
304
  "pg": "^8.11.0",
293
305
  "reflect-metadata": "^0.2.0",
306
+ "resend": "^6.20.0",
294
307
  "rxjs": "^7.8.0",
295
- "telegraf": "^4.16.0",
296
308
  "stripe": "^22.0.0",
297
- "class-validator": "^0.15.0",
298
- "class-transformer": "^0.5.1",
299
- "resend": "^6.20.0",
300
- "nodemailer": "^9.0.5",
301
- "@aws-sdk/client-s3": "^3.0.0",
302
- "@aws-sdk/s3-request-presigner": "^3.0.0",
303
- "@aws-sdk/lib-storage": "^3.0.0",
309
+ "telegraf": "^4.16.0",
304
310
  "typeorm": "^0.3.0 || ^1.0.0",
305
- "@simplewebauthn/server": "^13.0.0 || ^14.0.0",
306
- "web-push": "^3.6.0",
307
- "@nestjs/schedule": "",
308
- "undici": ""
311
+ "undici": "",
312
+ "web-push": "^3.6.0"
309
313
  },
310
314
  "peerDependenciesMeta": {
311
315
  "web-push": {
@@ -364,9 +368,9 @@
364
368
  }
365
369
  },
366
370
  "dependencies": {
367
- "nestjs-i18n": "^10.4.5",
368
371
  "bcryptjs": "^3.0.3",
369
- "jsonwebtoken": "^9.0.3"
372
+ "jsonwebtoken": "^9.0.3",
373
+ "nestjs-i18n": "^10.4.5"
370
374
  },
371
375
  "devDependencies": {
372
376
  "@aws-sdk/client-s3": "^3.1121.0",
@@ -401,9 +405,5 @@
401
405
  "typescript": "^5.6.0",
402
406
  "undici": "^7.0.0",
403
407
  "web-push": "^3.6.7"
404
- },
405
- "overrides": {
406
- "@nestjs/common": "11.2.3",
407
- "@nestjs/core": "11.2.3"
408
408
  }
409
409
  }