wsp-ms-core 1.0.57 → 1.0.59

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
@@ -37,8 +37,11 @@ __export(src_exports, {
37
37
  DomainEntity: () => DomainEntity,
38
38
  DomainError: () => DomainError,
39
39
  DomainEvent: () => DomainEvent,
40
+ DomainEventStatus: () => DomainEventStatus,
40
41
  Email: () => Email,
41
42
  ErrorManager: () => ErrorManager,
43
+ EventBus: () => EventBus,
44
+ EventBusMysqlRepository: () => EventBusMysqlRepository,
42
45
  ExchangeRates: () => ExchangeRates,
43
46
  FatalError: () => FatalError,
44
47
  HttpHealthCheckController: () => HttpHealthCheckController,
@@ -223,17 +226,186 @@ var DomainError = class extends Error {
223
226
  }
224
227
  };
225
228
 
229
+ // src/domain/errors/InternalError.ts
230
+ var InternalError = class extends DomainError {
231
+ constructor(type, message = "") {
232
+ super(type, message);
233
+ }
234
+ };
235
+
236
+ // src/domain/value-objects/UUID.ts
237
+ var crypto = __toESM(require("crypto"));
238
+ var _UUID = class _UUID extends ValueObject {
239
+ constructor(value) {
240
+ super(value);
241
+ }
242
+ validate(uuid) {
243
+ if (!_UUID.isValid(uuid)) {
244
+ throw new InternalError(`Invalid uuid <${uuid}>`);
245
+ }
246
+ }
247
+ toPrimitives() {
248
+ return { value: this.value };
249
+ }
250
+ static create(uuid) {
251
+ return new _UUID(uuid != null ? uuid : crypto.randomUUID());
252
+ }
253
+ static version(uuid) {
254
+ const m = /^[0-9a-f]{8}-[0-9a-f]{4}-([1-8])[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.exec(uuid);
255
+ return m ? Number(m[1]) : void 0;
256
+ }
257
+ static isNil(uuid) {
258
+ return /^0{8}-0{4}-0{4}-0{4}-0{12}$/i.test(uuid);
259
+ }
260
+ static isRFCStyle(uuid) {
261
+ return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(uuid);
262
+ }
263
+ static isValid(uuid, opts = {}) {
264
+ var _a, _b;
265
+ const allowed = (_a = opts.allowedVersions) != null ? _a : [1, 2, 3, 4, 5, 6, 7, 8];
266
+ const allowNil = (_b = opts.allowNil) != null ? _b : false;
267
+ if (allowNil && _UUID.isNil(uuid))
268
+ return true;
269
+ if (!_UUID.isRFCStyle(uuid))
270
+ return false;
271
+ const v = _UUID.version(uuid);
272
+ return !!v && allowed.includes(v);
273
+ }
274
+ };
275
+ _UUID.NIL = "00000000-0000-0000-0000-000000000000";
276
+ var UUID = _UUID;
277
+
278
+ // src/domain/value-objects/DomainEventStatus.ts
279
+ var _DomainEventStatus = class _DomainEventStatus extends ValueObject {
280
+ constructor(status) {
281
+ super(status.trim().toUpperCase());
282
+ }
283
+ validate(value) {
284
+ if (!_DomainEventStatus.SUPPORTED.includes(value)) {
285
+ throw new InternalError(`Domain event status <${value}> is not supported`);
286
+ }
287
+ }
288
+ toPrimitives() {
289
+ return void 0;
290
+ }
291
+ static create(status) {
292
+ return new _DomainEventStatus(status);
293
+ }
294
+ };
295
+ _DomainEventStatus.SUPPORTED = ["PENDING", "PROCESSING", "PROCESSED", "FAILED", "DEAD"];
296
+ _DomainEventStatus.PENDING = new _DomainEventStatus("PENDING");
297
+ _DomainEventStatus.PROCESSING = new _DomainEventStatus("PROCESSING");
298
+ _DomainEventStatus.PROCESSED = new _DomainEventStatus("PROCESSED");
299
+ _DomainEventStatus.FAILED = new _DomainEventStatus("FAILED");
300
+ _DomainEventStatus.DEAD = new _DomainEventStatus("DEAD");
301
+ var DomainEventStatus = _DomainEventStatus;
302
+
226
303
  // src/domain/contracts/DomainEvent.ts
227
- var DomainEvent = class {
228
- constructor(payload) {
304
+ var DomainEvent = class _DomainEvent {
305
+ constructor(eventUuid, tenantUuid, aggregateUuid, aggregateType, eventType, topic, payload, status, attempts, errorMessage, publishedAt, lastAttempt, createdAt) {
306
+ this._eventUuid = eventUuid;
307
+ this._tenantUuid = tenantUuid;
308
+ this._aggregateUuid = aggregateUuid;
309
+ this._aggregateType = aggregateType;
310
+ this._eventType = eventType;
311
+ this._topic = topic;
229
312
  this._payload = payload;
230
- this._occurredAt = DateTime.create();
313
+ this._status = status;
314
+ this._attempts = 0;
315
+ this._errorMessage = errorMessage;
316
+ this._publishedAt = publishedAt;
317
+ this._lastAttempt = lastAttempt;
318
+ this._createdAt = createdAt;
319
+ }
320
+ get eventUuid() {
321
+ return this._eventUuid;
322
+ }
323
+ get tenantUuid() {
324
+ return this._tenantUuid;
325
+ }
326
+ get aggregateUuid() {
327
+ return this._aggregateUuid;
328
+ }
329
+ get aggregateType() {
330
+ return this._aggregateType;
331
+ }
332
+ get eventType() {
333
+ return this._eventType;
334
+ }
335
+ get topic() {
336
+ return this._topic;
231
337
  }
232
338
  get payload() {
233
339
  return this._payload;
234
340
  }
235
- get occurredAt() {
236
- return this._occurredAt;
341
+ get status() {
342
+ return this._status;
343
+ }
344
+ get attempts() {
345
+ return this._attempts;
346
+ }
347
+ get errorMessage() {
348
+ return this._errorMessage;
349
+ }
350
+ get publishedAt() {
351
+ return this._publishedAt;
352
+ }
353
+ get lastAttempt() {
354
+ return this._lastAttempt;
355
+ }
356
+ get createdAt() {
357
+ return this._createdAt;
358
+ }
359
+ incrementAttempts() {
360
+ this._attempts++;
361
+ this._lastAttempt = DateTime.now();
362
+ }
363
+ markProcessed() {
364
+ this._status = DomainEventStatus.PROCESSED;
365
+ this._publishedAt = DateTime.now();
366
+ }
367
+ markProcessing() {
368
+ this._status = DomainEventStatus.PROCESSING;
369
+ }
370
+ markWithError(error) {
371
+ this._status = DomainEventStatus.FAILED;
372
+ this._errorMessage = error;
373
+ }
374
+ toPrimitives() {
375
+ var _a, _b, _c, _d, _e;
376
+ return {
377
+ eventUuid: this.eventUuid.value,
378
+ tenantUuid: this.tenantUuid.value,
379
+ aggregateUuid: this.aggregateUuid.value,
380
+ aggregateType: this.aggregateType,
381
+ eventType: this.eventType,
382
+ topic: this.topic,
383
+ payload: this.payload,
384
+ status: this.status.value,
385
+ attempts: this.attempts,
386
+ errorMessage: (_a = this.errorMessage) != null ? _a : void 0,
387
+ publishedAt: (_c = (_b = this.publishedAt) == null ? void 0 : _b.value) != null ? _c : void 0,
388
+ lastAttempt: (_e = (_d = this.lastAttempt) == null ? void 0 : _d.value) != null ? _e : void 0,
389
+ createdAt: this.createdAt.value
390
+ };
391
+ }
392
+ static reconstitute(data) {
393
+ var _a;
394
+ return new _DomainEvent(
395
+ UUID.create(data.event_uuid),
396
+ UUID.create(data.tenant_uuid),
397
+ UUID.create(data.aggregate_uuid),
398
+ String(data.aggregate_type),
399
+ String(data.event_type),
400
+ String(data.topic),
401
+ String(data.payload),
402
+ DomainEventStatus.create(data.status),
403
+ Number(data.attempts),
404
+ (_a = data.error_message) != null ? _a : void 0,
405
+ data.published_at ? DateTime.create(data.published_at) : void 0,
406
+ data.last_attempt ? DateTime.create(data.last_attempt) : void 0,
407
+ data.created_at ? DateTime.create(data.created_at) : void 0
408
+ );
237
409
  }
238
410
  };
239
411
 
@@ -251,13 +423,6 @@ var FatalError = class extends DomainError {
251
423
  }
252
424
  };
253
425
 
254
- // src/domain/errors/InternalError.ts
255
- var InternalError = class extends DomainError {
256
- constructor(type, message = "") {
257
- super(type, message);
258
- }
259
- };
260
-
261
426
  // src/domain/errors/UsageError.ts
262
427
  var UsageError = class extends DomainError {
263
428
  constructor(type, vars = {}) {
@@ -452,93 +617,6 @@ _Language.SPANISH_NICARAGUA = new _Language("es-ni");
452
617
  _Language.SPANISH_PUERTO_RICO = new _Language("es-pr");
453
618
  var Language = _Language;
454
619
 
455
- // src/domain/value-objects/payments/PaymentGateway.ts
456
- var _PaymentGateway = class _PaymentGateway extends ValueObject {
457
- constructor(gateway) {
458
- super(gateway);
459
- }
460
- validate(value) {
461
- if (!_PaymentGateway.SUPPORTED.includes(value)) {
462
- throw new InternalError(`Payment gateway <${value}> is not supported`);
463
- }
464
- }
465
- isExternal() {
466
- return _PaymentGateway.EXTERNALS.includes(this.value);
467
- }
468
- toPrimitives() {
469
- return { value: this.value };
470
- }
471
- static create(gateway) {
472
- return new _PaymentGateway(gateway.trim().toUpperCase());
473
- }
474
- };
475
- _PaymentGateway.SUPPORTED = [
476
- "MERCADOPAGO",
477
- "HANDY",
478
- "WONA_DEBIT",
479
- "WONA_CARD",
480
- "WONA_CASH",
481
- "WONA_TRANSFER",
482
- "WONA_MERCADOPAGO"
483
- ];
484
- _PaymentGateway.EXTERNALS = ["MERCADOPAGO", "STRIPE"];
485
- _PaymentGateway.MERCADOPAGO = new _PaymentGateway("MERCADOPAGO");
486
- _PaymentGateway.HANDY = new _PaymentGateway("HANDY");
487
- _PaymentGateway.WONA_DEBIT = new _PaymentGateway("WONA_DEBIT");
488
- _PaymentGateway.WONA_CARD = new _PaymentGateway("WONA_CARD");
489
- _PaymentGateway.WONA_CASH = new _PaymentGateway("WONA_CASH");
490
- _PaymentGateway.WONA_TRANSFER = new _PaymentGateway("WONA_TRANSFER");
491
- _PaymentGateway.WONA_MERCADOPAGO = new _PaymentGateway("WONA_MERCADOPAGO");
492
- var PaymentGateway = _PaymentGateway;
493
-
494
- // src/domain/value-objects/payments/PaymentStatus.ts
495
- var _PaymentStatus = class _PaymentStatus extends ValueObject {
496
- constructor(status) {
497
- super(status);
498
- }
499
- validate(status) {
500
- if (!_PaymentStatus.SUPPORTED.includes(status)) {
501
- throw new InternalError(`Payment status <${status}> is not supported`);
502
- }
503
- }
504
- get isDone() {
505
- return this.value === "DONE";
506
- }
507
- get isPending() {
508
- return this.value === "PENDING";
509
- }
510
- get isInProgress() {
511
- return this.value === "IN_PROGRESS";
512
- }
513
- get isFailed() {
514
- return this.value === "FAILED";
515
- }
516
- get isCanceled() {
517
- return this.value === "CANCELED";
518
- }
519
- get isHold() {
520
- return this.value === "HOLD";
521
- }
522
- get isRefunded() {
523
- return this.value === "REFUNDED";
524
- }
525
- toPrimitives() {
526
- return { value: this.value };
527
- }
528
- static create(gateway) {
529
- return new _PaymentStatus(gateway.trim().toUpperCase());
530
- }
531
- };
532
- _PaymentStatus.SUPPORTED = ["DONE", "PENDING", "FAILED", "CANCELED", "HOLD", "REFUNDED", "IN_PROGRESS"];
533
- _PaymentStatus.DONE = new _PaymentStatus("DONE");
534
- _PaymentStatus.PENDING = new _PaymentStatus("PENDING");
535
- _PaymentStatus.IN_PROGRESS = new _PaymentStatus("IN_PROGRESS");
536
- _PaymentStatus.FAILED = new _PaymentStatus("FAILED");
537
- _PaymentStatus.CANCELED = new _PaymentStatus("CANCELED");
538
- _PaymentStatus.HOLD = new _PaymentStatus("HOLD");
539
- _PaymentStatus.REFUNDED = new _PaymentStatus("REFUNDED");
540
- var PaymentStatus = _PaymentStatus;
541
-
542
620
  // src/utils/StringVars.ts
543
621
  var StringVars = class {
544
622
  static parse(str, ob) {
@@ -694,47 +772,107 @@ _Price.MIN_AMOUNT = -1e6;
694
772
  _Price.MAX_AMOUNT = 1e9;
695
773
  var Price = _Price;
696
774
 
697
- // src/domain/value-objects/UUID.ts
698
- var crypto = __toESM(require("crypto"));
699
- var _UUID = class _UUID extends ValueObject {
700
- constructor(value) {
701
- super(value);
775
+ // src/domain/value-objects/payments/PaymentGateway.ts
776
+ var _PaymentGateway = class _PaymentGateway extends ValueObject {
777
+ constructor(gateway) {
778
+ super(gateway);
702
779
  }
703
- validate(uuid) {
704
- if (!_UUID.isValid(uuid)) {
705
- throw new InternalError(`Invalid uuid <${uuid}>`);
780
+ validate(value) {
781
+ if (!_PaymentGateway.SUPPORTED.includes(value)) {
782
+ throw new InternalError(`Payment gateway <${value}> is not supported`);
706
783
  }
707
784
  }
785
+ isExternal() {
786
+ return _PaymentGateway.EXTERNALS.includes(this.value);
787
+ }
708
788
  toPrimitives() {
709
789
  return { value: this.value };
710
790
  }
711
- static create(uuid) {
712
- return new _UUID(uuid != null ? uuid : crypto.randomUUID());
791
+ static create(gateway) {
792
+ return new _PaymentGateway(gateway.trim().toUpperCase());
713
793
  }
714
- static version(uuid) {
715
- const m = /^[0-9a-f]{8}-[0-9a-f]{4}-([1-8])[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.exec(uuid);
716
- return m ? Number(m[1]) : void 0;
794
+ };
795
+ _PaymentGateway.SUPPORTED = [
796
+ "MERCADOPAGO",
797
+ "HANDY",
798
+ "WONA_DEBIT",
799
+ "WONA_CARD",
800
+ "WONA_CASH",
801
+ "WONA_TRANSFER",
802
+ "WONA_MERCADOPAGO"
803
+ ];
804
+ _PaymentGateway.EXTERNALS = ["MERCADOPAGO", "STRIPE"];
805
+ _PaymentGateway.MERCADOPAGO = new _PaymentGateway("MERCADOPAGO");
806
+ _PaymentGateway.HANDY = new _PaymentGateway("HANDY");
807
+ _PaymentGateway.WONA_DEBIT = new _PaymentGateway("WONA_DEBIT");
808
+ _PaymentGateway.WONA_CARD = new _PaymentGateway("WONA_CARD");
809
+ _PaymentGateway.WONA_CASH = new _PaymentGateway("WONA_CASH");
810
+ _PaymentGateway.WONA_TRANSFER = new _PaymentGateway("WONA_TRANSFER");
811
+ _PaymentGateway.WONA_MERCADOPAGO = new _PaymentGateway("WONA_MERCADOPAGO");
812
+ var PaymentGateway = _PaymentGateway;
813
+
814
+ // src/domain/value-objects/payments/PaymentStatus.ts
815
+ var _PaymentStatus = class _PaymentStatus extends ValueObject {
816
+ constructor(status) {
817
+ super(status);
717
818
  }
718
- static isNil(uuid) {
719
- return /^0{8}-0{4}-0{4}-0{4}-0{12}$/i.test(uuid);
819
+ validate(status) {
820
+ if (!_PaymentStatus.SUPPORTED.includes(status)) {
821
+ throw new InternalError(`Payment status <${status}> is not supported`);
822
+ }
720
823
  }
721
- static isRFCStyle(uuid) {
722
- return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(uuid);
824
+ get isDone() {
825
+ return this.value === "DONE";
723
826
  }
724
- static isValid(uuid, opts = {}) {
725
- var _a, _b;
726
- const allowed = (_a = opts.allowedVersions) != null ? _a : [1, 2, 3, 4, 5, 6, 7, 8];
727
- const allowNil = (_b = opts.allowNil) != null ? _b : false;
728
- if (allowNil && _UUID.isNil(uuid))
729
- return true;
730
- if (!_UUID.isRFCStyle(uuid))
731
- return false;
732
- const v = _UUID.version(uuid);
733
- return !!v && allowed.includes(v);
827
+ get isPending() {
828
+ return this.value === "PENDING";
829
+ }
830
+ get isInProgress() {
831
+ return this.value === "IN_PROGRESS";
832
+ }
833
+ get isFailed() {
834
+ return this.value === "FAILED";
835
+ }
836
+ get isCanceled() {
837
+ return this.value === "CANCELED";
838
+ }
839
+ get isHold() {
840
+ return this.value === "HOLD";
841
+ }
842
+ get isRefunded() {
843
+ return this.value === "REFUNDED";
844
+ }
845
+ toPrimitives() {
846
+ return { value: this.value };
847
+ }
848
+ static create(gateway) {
849
+ return new _PaymentStatus(gateway.trim().toUpperCase());
850
+ }
851
+ };
852
+ _PaymentStatus.SUPPORTED = ["DONE", "PENDING", "FAILED", "CANCELED", "HOLD", "REFUNDED", "IN_PROGRESS"];
853
+ _PaymentStatus.DONE = new _PaymentStatus("DONE");
854
+ _PaymentStatus.PENDING = new _PaymentStatus("PENDING");
855
+ _PaymentStatus.IN_PROGRESS = new _PaymentStatus("IN_PROGRESS");
856
+ _PaymentStatus.FAILED = new _PaymentStatus("FAILED");
857
+ _PaymentStatus.CANCELED = new _PaymentStatus("CANCELED");
858
+ _PaymentStatus.HOLD = new _PaymentStatus("HOLD");
859
+ _PaymentStatus.REFUNDED = new _PaymentStatus("REFUNDED");
860
+ var PaymentStatus = _PaymentStatus;
861
+
862
+ // src/application/event-bus/EventBus.ts
863
+ var EventBus = class {
864
+ constructor(repository) {
865
+ this.repository = repository;
866
+ }
867
+ async publish(event) {
868
+ await this.repository.create(event);
869
+ }
870
+ async publishMany(events) {
871
+ for (let event of events) {
872
+ await this.publish(event);
873
+ }
734
874
  }
735
875
  };
736
- _UUID.NIL = "00000000-0000-0000-0000-000000000000";
737
- var UUID = _UUID;
738
876
 
739
877
  // src/application/unit-of-work/BasicUnitOfWork.ts
740
878
  var BasicUnitOfWork = class {
@@ -786,6 +924,10 @@ var _MysqlConnector = class _MysqlConnector {
786
924
  async wrap(conn) {
787
925
  return new MysqlConnection(conn);
788
926
  }
927
+ async query(statement, params = []) {
928
+ const [rows] = await this._pool.query(statement, params);
929
+ return rows;
930
+ }
789
931
  async getConnection() {
790
932
  const conn = await this._pool.getConnection();
791
933
  return this.wrap(conn);
@@ -807,7 +949,7 @@ var _MysqlConnector = class _MysqlConnector {
807
949
  }
808
950
  }
809
951
  };
810
- _MysqlConnector.DEFAULT_POOL_SIZE = 10;
952
+ _MysqlConnector.DEFAULT_POOL_SIZE = 20;
811
953
  var MysqlConnector = _MysqlConnector;
812
954
  var MysqlConnection = class {
813
955
  constructor(conn) {
@@ -998,6 +1140,61 @@ function adaptExpressErrorHandler(errorManager) {
998
1140
  };
999
1141
  }
1000
1142
 
1143
+ // src/infrastructure/event-bus/EventBusMysqlRepository.ts
1144
+ var EventBusMysqlRepository = class {
1145
+ constructor(connection) {
1146
+ this.connection = connection;
1147
+ }
1148
+ eventToRowValues(e) {
1149
+ var _a, _b;
1150
+ return [
1151
+ e.eventUuid.value,
1152
+ e.tenantUuid.value,
1153
+ e.aggregateUuid.value,
1154
+ e.aggregateType,
1155
+ e.eventType,
1156
+ e.topic,
1157
+ e.payload,
1158
+ e.status.value,
1159
+ e.attempts,
1160
+ e.errorMessage,
1161
+ (_a = e.publishedAt) == null ? void 0 : _a.value,
1162
+ (_b = e.lastAttempt) == null ? void 0 : _b.value,
1163
+ e.createdAt.value
1164
+ ];
1165
+ }
1166
+ async create(event) {
1167
+ const values = this.eventToRowValues(event);
1168
+ await this.connection.query(
1169
+ `INSERT INTO events_outbox (event_uuid, tenant_uuid, aggregate_uuid, aggregate_type, event_type, topic,
1170
+ payload, status, attempts, error_message, published_at, last_attempt, created_at)
1171
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
1172
+ values
1173
+ );
1174
+ }
1175
+ async update(event) {
1176
+ var _a, _b;
1177
+ const values = [event.status.value, event.attempts, event.errorMessage, (_a = event.publishedAt) == null ? void 0 : _a.value, (_b = event.lastAttempt) == null ? void 0 : _b.value, event.eventUuid.value];
1178
+ await this.connection.query(
1179
+ `UPDATE events_outbox
1180
+ SET status = ?,
1181
+ attempts = ?,
1182
+ error_message = ?,
1183
+ published_at = ?,
1184
+ last_attempt = ?
1185
+ WHERE event_uuid = ?`,
1186
+ values
1187
+ );
1188
+ }
1189
+ async listPending(limit) {
1190
+ const result = await this.connection.query(
1191
+ `SELECT * FROM events_outbox WHERE published_at IS NULL LIMIT 50`,
1192
+ []
1193
+ );
1194
+ return result.length > 0 ? result.map((r) => DomainEvent.reconstitute(r)) : [];
1195
+ }
1196
+ };
1197
+
1001
1198
  // src/utils/ExchangeRates.ts
1002
1199
  var ExchangeRates = class _ExchangeRates extends BaseObject {
1003
1200
  constructor(props) {
@@ -1060,8 +1257,11 @@ var ExchangeRates = class _ExchangeRates extends BaseObject {
1060
1257
  DomainEntity,
1061
1258
  DomainError,
1062
1259
  DomainEvent,
1260
+ DomainEventStatus,
1063
1261
  Email,
1064
1262
  ErrorManager,
1263
+ EventBus,
1264
+ EventBusMysqlRepository,
1065
1265
  ExchangeRates,
1066
1266
  FatalError,
1067
1267
  HttpHealthCheckController,