@miguelmorales13/nestkit 0.31.3 → 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.
package/dist/index.cjs CHANGED
@@ -1,9 +1,9 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});require('./chunk-KVBQBT3D.cjs');
2
+
2
3
 
3
4
 
4
5
 
5
6
  var _chunk47IYXZHWcjs = require('./chunk-47IYXZHW.cjs');
6
- require('./chunk-KVBQBT3D.cjs');
7
7
 
8
8
 
9
9
 
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
+ import "./chunk-EPVKCBPT.js";
1
2
  import {
2
3
  PG_POOL,
3
4
  PgModule,
4
5
  withTenantScope
5
6
  } from "./chunk-ENZ7LQRE.js";
6
- import "./chunk-EPVKCBPT.js";
7
7
  import {
8
8
  SUPABASE_ANON_CLIENT,
9
9
  SUPABASE_SERVICE_ROLE_CLIENT,
@@ -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,6 +1,6 @@
1
1
  {
2
2
  "name": "@miguelmorales13/nestkit",
3
- "version": "0.31.3",
3
+ "version": "0.31.4",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "repository": {