wsp-ms-core 1.0.82 → 1.0.84

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/dist/index.cjs CHANGED
@@ -36,6 +36,7 @@ __export(src_exports, {
36
36
  Country: () => Country,
37
37
  Currency: () => Currency,
38
38
  DateTime: () => DateTime,
39
+ DefaultMysqlInboxRunner: () => DefaultMysqlInboxRunner,
39
40
  DefaultMysqlOutboxRunner: () => DefaultMysqlOutboxRunner,
40
41
  DomainEntity: () => DomainEntity,
41
42
  DomainError: () => DomainError,
@@ -49,6 +50,7 @@ __export(src_exports, {
49
50
  FatalError: () => FatalError,
50
51
  HttpHealthCheckController: () => HttpHealthCheckController,
51
52
  HttpNotFoundController: () => HttpNotFoundController,
53
+ InboxRecord: () => InboxRecord,
52
54
  IntegrationEvent: () => IntegrationEvent,
53
55
  InternalError: () => InternalError,
54
56
  KafkaManager: () => KafkaManager,
@@ -1161,9 +1163,9 @@ var EventManager = class {
1161
1163
  this._onCrash = null;
1162
1164
  this._onReconnect = null;
1163
1165
  }
1164
- async execRoute(topic, message) {
1166
+ async execRoute(topic, event) {
1165
1167
  if (this._callbackList[topic]) {
1166
- await this._callbackList[topic](message);
1168
+ await this._callbackList[topic](event);
1167
1169
  }
1168
1170
  }
1169
1171
  async execCallback(callback, data) {
@@ -1219,7 +1221,7 @@ var OutboxRecord = class _OutboxRecord {
1219
1221
  this._errorMessage = errorMessage;
1220
1222
  this._publishedAt = publishedAt;
1221
1223
  this._lastAttempt = lastAttempt;
1222
- this._createdAt = createdAt;
1224
+ this._createdAt = createdAt ?? DateTime.now();
1223
1225
  }
1224
1226
  get eventUuid() {
1225
1227
  return this._eventUuid;
@@ -1312,30 +1314,126 @@ var OutboxRecord = class _OutboxRecord {
1312
1314
  }
1313
1315
  };
1314
1316
 
1317
+ // src/infrastructure/contracts/InboxRecord.ts
1318
+ var InboxRecord = class _InboxRecord {
1319
+ constructor(eventUuid, tenantUuid, topic, producer, payload, status, attempts, errorMessage, lastAttempt, processedAt, createdAt) {
1320
+ this._eventUuid = eventUuid;
1321
+ this._tenantUuid = tenantUuid;
1322
+ this._topic = topic;
1323
+ this._producer = producer;
1324
+ this._payload = payload;
1325
+ this._status = status;
1326
+ this._attempts = attempts ?? 0;
1327
+ this._errorMessage = errorMessage ?? void 0;
1328
+ this._lastAttempt = lastAttempt ?? void 0;
1329
+ this._processedAt = processedAt ?? void 0;
1330
+ this._createdAt = createdAt ?? DateTime.now();
1331
+ }
1332
+ get eventUuid() {
1333
+ return this._eventUuid;
1334
+ }
1335
+ get tenantUuid() {
1336
+ return this._tenantUuid;
1337
+ }
1338
+ get topic() {
1339
+ return this._topic;
1340
+ }
1341
+ get producer() {
1342
+ return this._producer;
1343
+ }
1344
+ get payload() {
1345
+ return this._payload;
1346
+ }
1347
+ get status() {
1348
+ return this._status;
1349
+ }
1350
+ get attempts() {
1351
+ return this._attempts;
1352
+ }
1353
+ get errorMessage() {
1354
+ return this._errorMessage;
1355
+ }
1356
+ get lastAttempt() {
1357
+ return this._lastAttempt;
1358
+ }
1359
+ get processedAt() {
1360
+ return this._processedAt;
1361
+ }
1362
+ get createdAt() {
1363
+ return this._createdAt;
1364
+ }
1365
+ incrementAttempts() {
1366
+ this._attempts++;
1367
+ this._lastAttempt = DateTime.now();
1368
+ }
1369
+ markProcessed() {
1370
+ this._status = ProcessStatus.PROCESSED;
1371
+ this._processedAt = DateTime.now();
1372
+ }
1373
+ markProcessing() {
1374
+ this.incrementAttempts();
1375
+ this._status = ProcessStatus.PROCESSING;
1376
+ }
1377
+ markWithError(error) {
1378
+ this._status = this.attempts < 5 ? ProcessStatus.FAILED : ProcessStatus.DEAD;
1379
+ this._errorMessage = error;
1380
+ }
1381
+ toPrimitives() {
1382
+ return {
1383
+ eventUuid: this.eventUuid.value,
1384
+ tenantUuid: this.tenantUuid.value,
1385
+ topic: this.topic,
1386
+ producer: this.producer,
1387
+ payload: this.payload,
1388
+ status: this.status.value,
1389
+ attempts: this.attempts,
1390
+ errorMessage: this.errorMessage ?? void 0,
1391
+ lastAttempt: this.lastAttempt?.value ?? void 0,
1392
+ processedAt: this.processedAt?.value ?? void 0,
1393
+ createdAt: this.createdAt.value
1394
+ };
1395
+ }
1396
+ static reconstitute(data) {
1397
+ return new _InboxRecord(
1398
+ UUID.create(data.event_uuid),
1399
+ UUID.create(data.tenant_uuid),
1400
+ String(data.topic),
1401
+ String(data.producer),
1402
+ String(data.payload),
1403
+ ProcessStatus.create(data.status),
1404
+ Number(data.attempts),
1405
+ data.error_message ?? void 0,
1406
+ data.last_attempt ? DateTime.create(data.last_attempt) : void 0,
1407
+ data.processed_at ? DateTime.create(data.processed_at) : void 0,
1408
+ data.created_at ? DateTime.create(data.created_at) : void 0
1409
+ );
1410
+ }
1411
+ };
1412
+
1315
1413
  // src/infrastructure/event-bus/EventBusMysqlRepository.ts
1316
1414
  var EventBusMysqlRepository = class {
1317
1415
  constructor(connection) {
1318
1416
  this.connection = connection;
1319
1417
  }
1320
- eventToRowValues(e) {
1418
+ recordToRowValues(record) {
1321
1419
  return [
1322
- e.eventUuid.value,
1323
- e.eventType,
1324
- e.tenantUuid.value,
1325
- e.aggregateUuid.value,
1326
- e.aggregateType,
1327
- e.topic,
1328
- e.payload,
1329
- e.status.value,
1330
- e.attempts,
1331
- e.errorMessage,
1332
- e.publishedAt?.value,
1333
- e.lastAttempt?.value,
1334
- e.createdAt.value
1420
+ record.eventUuid.value,
1421
+ record.eventType,
1422
+ record.tenantUuid.value,
1423
+ record.aggregateUuid.value,
1424
+ record.aggregateType,
1425
+ record.topic,
1426
+ record.payload,
1427
+ record.status.value,
1428
+ record.attempts,
1429
+ record.errorMessage,
1430
+ record.publishedAt?.value,
1431
+ record.lastAttempt?.value,
1432
+ record.createdAt.value
1335
1433
  ];
1336
1434
  }
1337
- async create(event) {
1338
- const values = this.eventToRowValues(event);
1435
+ async create(record) {
1436
+ const values = this.recordToRowValues(record);
1339
1437
  await this.connection.query(
1340
1438
  `INSERT INTO events_outbox (event_uuid, event_type, tenant_uuid, aggregate_uuid, aggregate_type, topic,
1341
1439
  payload, status, attempts, error_message, published_at, last_attempt, created_at)
@@ -1343,8 +1441,8 @@ var EventBusMysqlRepository = class {
1343
1441
  values
1344
1442
  );
1345
1443
  }
1346
- async update(event) {
1347
- const values = [event.status.value, event.attempts, event.errorMessage, event.publishedAt?.value, event.lastAttempt?.value, event.eventUuid.value];
1444
+ async update(record) {
1445
+ const values = [record.status.value, record.attempts, record.errorMessage, record.publishedAt?.value, record.lastAttempt?.value, record.eventUuid.value];
1348
1446
  await this.connection.query(
1349
1447
  `UPDATE events_outbox
1350
1448
  SET status = ?,
@@ -1358,7 +1456,7 @@ var EventBusMysqlRepository = class {
1358
1456
  }
1359
1457
  async listPending(limit) {
1360
1458
  const result = await this.connection.query(
1361
- `SELECT * FROM events_outbox WHERE status IN ('PENDING','FAILED') AND published_at IS NULL LIMIT 50`,
1459
+ `SELECT * FROM events_outbox WHERE status IN ('PENDING','FAILED') AND published_at IS NULL LIMIT ${limit}`,
1362
1460
  []
1363
1461
  );
1364
1462
  return result.length > 0 ? result.map((r) => OutboxRecord.reconstitute(r)) : [];
@@ -1555,7 +1653,13 @@ var KafkaManager = class extends EventManager {
1555
1653
  eachMessage: async ({ topic, partition, message, heartbeat }) => {
1556
1654
  try {
1557
1655
  await this.execCallback(this._onMessage, `[New message detected for ${topic}]: ${message.value?.toString()}`);
1558
- await this.execRoute(topic, String(message.value?.toString()));
1656
+ const json = JSON.parse(String(message.value?.toString()));
1657
+ await this.execRoute(topic, {
1658
+ topic: String(json.topic),
1659
+ producer: String(json.producer),
1660
+ tenant: UUID.create(String(json.tenant)),
1661
+ message: json.message
1662
+ });
1559
1663
  const next = (BigInt(message.offset) + 1n).toString();
1560
1664
  await this.consumer.commitOffsets([{ topic, partition, offset: next }]);
1561
1665
  await heartbeat();
@@ -1571,11 +1675,12 @@ var KafkaManager = class extends EventManager {
1571
1675
  await this.execCallback(this._onError, new InternalError(ErrorManager.APP_ERRORS.PROCESS, error.toString()));
1572
1676
  }
1573
1677
  }
1574
- async send(topic, message) {
1678
+ async send(e) {
1575
1679
  const evt = {
1576
1680
  date: DateTime.now().value,
1577
- producer: process.env.NAME && process.env.ENVIRONMENT ? `${process.env.NAME}-${process.env.ENVIRONMENT}` : "unknown",
1578
- data: message
1681
+ tenant: e.tenant.value,
1682
+ producer: e.producer,
1683
+ data: e.message
1579
1684
  };
1580
1685
  try {
1581
1686
  if (!this.producer) {
@@ -1583,7 +1688,7 @@ var KafkaManager = class extends EventManager {
1583
1688
  }
1584
1689
  await this.producer.connect();
1585
1690
  await this.producer.send({
1586
- topic,
1691
+ topic: e.topic,
1587
1692
  messages: [{ value: JSON.stringify(evt) }]
1588
1693
  });
1589
1694
  await this.producer.disconnect();
@@ -1707,11 +1812,17 @@ var DefaultMysqlOutboxRunner = class {
1707
1812
  async sleep() {
1708
1813
  return new Promise((r) => setTimeout(r, this.maxEvents));
1709
1814
  }
1815
+ // HACER QUE PUEDA MANDAR UN OBJETO NO UN STRING POR KAFKA
1710
1816
  async processOutboxRecord(e, eventBusRepository) {
1711
1817
  try {
1712
1818
  e.markProcessing();
1713
1819
  await eventBusRepository.update(e);
1714
- await this.eventManager.send(e.topic, e.payload);
1820
+ await this.eventManager.send({
1821
+ topic: e.topic,
1822
+ producer: process.env.NAME && process.env.ENVIRONMENT ? `${process.env.NAME}-${process.env.ENVIRONMENT}` : "unknown",
1823
+ tenant: e.tenantUuid,
1824
+ message: JSON.parse(e.payload)
1825
+ });
1715
1826
  e.markProcessed();
1716
1827
  } catch (error) {
1717
1828
  const type = String(error.type);
@@ -1745,6 +1856,36 @@ var DefaultMysqlOutboxRunner = class {
1745
1856
  }
1746
1857
  };
1747
1858
 
1859
+ // src/infrastructure/runners/default-mysql-inbox-runner.ts
1860
+ var DefaultMysqlInboxRunner = class {
1861
+ constructor(uowFactory, eventManager) {
1862
+ this.errors = [];
1863
+ this.uowFactory = uowFactory;
1864
+ this.eventManager = eventManager;
1865
+ this.interval = Number(process.env.OUTBOX_RUNNER_INTERVAL_MS || 5e3);
1866
+ this.maxEvents = Number(process.env.OUTBOX_RUNNER_MAX_EVENTS || 20);
1867
+ }
1868
+ async saveEvent(e, repository) {
1869
+ let record = new InboxRecord(
1870
+ UUID.create(),
1871
+ e.tenant,
1872
+ e.topic,
1873
+ e.producer,
1874
+ JSON.stringify(e.message),
1875
+ ProcessStatus.PENDING
1876
+ );
1877
+ try {
1878
+ await repository.create(record);
1879
+ } catch (error) {
1880
+ const type = String(error.type);
1881
+ record.markWithError(type);
1882
+ throw new Error(type);
1883
+ } finally {
1884
+ await repository.update(record);
1885
+ }
1886
+ }
1887
+ };
1888
+
1748
1889
  // src/utils/ExchangeRates.ts
1749
1890
  var ExchangeRates = class _ExchangeRates extends BaseObject {
1750
1891
  constructor(props) {
@@ -1805,6 +1946,7 @@ var ExchangeRates = class _ExchangeRates extends BaseObject {
1805
1946
  Country,
1806
1947
  Currency,
1807
1948
  DateTime,
1949
+ DefaultMysqlInboxRunner,
1808
1950
  DefaultMysqlOutboxRunner,
1809
1951
  DomainEntity,
1810
1952
  DomainError,
@@ -1818,6 +1960,7 @@ var ExchangeRates = class _ExchangeRates extends BaseObject {
1818
1960
  FatalError,
1819
1961
  HttpHealthCheckController,
1820
1962
  HttpNotFoundController,
1963
+ InboxRecord,
1821
1964
  IntegrationEvent,
1822
1965
  InternalError,
1823
1966
  KafkaManager,