wsp-ms-core 1.0.77 → 1.0.78-beta

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.js CHANGED
@@ -166,6 +166,28 @@ _DateTime.FORMATS = {
166
166
  };
167
167
  var DateTime = _DateTime;
168
168
 
169
+ // src/domain/contracts/BaseEvent.ts
170
+ var BaseEvent = class {
171
+ constructor(version, type, payload) {
172
+ this._version = version;
173
+ this._type = type;
174
+ this._payload = payload;
175
+ this._occurredAt = DateTime.now();
176
+ }
177
+ get version() {
178
+ return this._version;
179
+ }
180
+ get type() {
181
+ return this._type;
182
+ }
183
+ get payload() {
184
+ return this._payload;
185
+ }
186
+ get ocurredAt() {
187
+ return this._occurredAt;
188
+ }
189
+ };
190
+
169
191
  // src/domain/contracts/DomainEntity.ts
170
192
  var DomainEntity = class {
171
193
  constructor(props) {
@@ -200,6 +222,13 @@ var DomainEntity = class {
200
222
  }
201
223
  };
202
224
 
225
+ // src/domain/contracts/BaseObject.ts
226
+ var BaseObject = class {
227
+ constructor(props) {
228
+ this.props = props;
229
+ }
230
+ };
231
+
203
232
  // src/domain/contracts/DomainError.ts
204
233
  var DomainError = class extends Error {
205
234
  constructor(type, message = "") {
@@ -208,101 +237,34 @@ var DomainError = class extends Error {
208
237
  }
209
238
  };
210
239
 
211
- // src/domain/errors/InternalError.ts
212
- var InternalError = class extends DomainError {
240
+ // src/domain/errors/FatalError.ts
241
+ var FatalError = class extends DomainError {
213
242
  constructor(type, message = "") {
214
243
  super(type, message);
215
244
  }
216
245
  };
217
246
 
218
- // src/domain/value-objects/UUID.ts
219
- import * as crypto from "crypto";
220
- var _UUID = class _UUID extends ValueObject {
221
- constructor(value) {
222
- super(value);
223
- }
224
- validate(uuid) {
225
- if (!_UUID.isValid(uuid)) {
226
- throw new InternalError(`Invalid uuid <${uuid}>`);
227
- }
228
- }
229
- toPrimitives() {
230
- return { value: this.value };
231
- }
232
- static create(uuid) {
233
- return new _UUID(uuid ?? crypto.randomUUID());
234
- }
235
- static version(uuid) {
236
- 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);
237
- return m ? Number(m[1]) : void 0;
238
- }
239
- static isNil(uuid) {
240
- return /^0{8}-0{4}-0{4}-0{4}-0{12}$/i.test(uuid);
241
- }
242
- static isRFCStyle(uuid) {
243
- 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);
244
- }
245
- static isValid(uuid, opts = {}) {
246
- const allowed = opts.allowedVersions ?? [1, 2, 3, 4, 5, 6, 7, 8];
247
- const allowNil = opts.allowNil ?? false;
248
- if (allowNil && _UUID.isNil(uuid))
249
- return true;
250
- if (!_UUID.isRFCStyle(uuid))
251
- return false;
252
- const v = _UUID.version(uuid);
253
- return !!v && allowed.includes(v);
247
+ // src/domain/errors/InternalError.ts
248
+ var InternalError = class extends DomainError {
249
+ constructor(type, message = "") {
250
+ super(type, message);
254
251
  }
255
252
  };
256
- _UUID.NIL = "00000000-0000-0000-0000-000000000000";
257
- var UUID = _UUID;
258
253
 
259
- // src/domain/value-objects/DomainEventStatus.ts
260
- var _DomainEventStatus = class _DomainEventStatus extends ValueObject {
261
- constructor(status) {
262
- super(status.trim().toUpperCase());
263
- }
264
- validate(value) {
265
- if (!_DomainEventStatus.SUPPORTED.includes(value)) {
266
- throw new InternalError(`Domain event status <${value}> is not supported`);
267
- }
268
- }
269
- toPrimitives() {
270
- return void 0;
271
- }
272
- static create(status) {
273
- return new _DomainEventStatus(status);
254
+ // src/domain/errors/UsageError.ts
255
+ var UsageError = class extends DomainError {
256
+ constructor(type, vars = {}) {
257
+ super(type);
258
+ this.vars = vars;
274
259
  }
275
260
  };
276
- _DomainEventStatus.SUPPORTED = ["PENDING", "PROCESSING", "PROCESSED", "FAILED", "DEAD"];
277
- _DomainEventStatus.PENDING = new _DomainEventStatus("PENDING");
278
- _DomainEventStatus.PROCESSING = new _DomainEventStatus("PROCESSING");
279
- _DomainEventStatus.PROCESSED = new _DomainEventStatus("PROCESSED");
280
- _DomainEventStatus.FAILED = new _DomainEventStatus("FAILED");
281
- _DomainEventStatus.DEAD = new _DomainEventStatus("DEAD");
282
- var DomainEventStatus = _DomainEventStatus;
283
261
 
284
- // src/domain/contracts/DomainEvent.ts
285
- var DomainEvent = class _DomainEvent {
286
- constructor(eventUuid, eventType, tenantUuid, aggregateUuid, aggregateType, topic, payload, status, attempts, errorMessage, publishedAt, lastAttempt, createdAt) {
287
- this._eventUuid = eventUuid;
288
- this._tenantUuid = tenantUuid;
262
+ // src/domain/events/DomainEvent.ts
263
+ var DomainEvent = class extends BaseEvent {
264
+ constructor(version, type, payload, aggregateUuid, aggregateType) {
265
+ super(version, type, payload);
289
266
  this._aggregateUuid = aggregateUuid;
290
267
  this._aggregateType = aggregateType;
291
- this._eventType = eventType;
292
- this._topic = topic;
293
- this._payload = payload;
294
- this._status = status;
295
- this._attempts = attempts;
296
- this._errorMessage = errorMessage;
297
- this._publishedAt = publishedAt;
298
- this._lastAttempt = lastAttempt;
299
- this._createdAt = createdAt;
300
- }
301
- get eventUuid() {
302
- return this._eventUuid;
303
- }
304
- get tenantUuid() {
305
- return this._tenantUuid;
306
268
  }
307
269
  get aggregateUuid() {
308
270
  return this._aggregateUuid;
@@ -310,169 +272,98 @@ var DomainEvent = class _DomainEvent {
310
272
  get aggregateType() {
311
273
  return this._aggregateType;
312
274
  }
313
- get eventType() {
314
- return this._eventType;
315
- }
316
- get topic() {
317
- return this._topic;
318
- }
319
- get payload() {
320
- return this._payload;
321
- }
322
- get status() {
323
- return this._status;
324
- }
325
- get attempts() {
326
- return this._attempts;
327
- }
328
- get errorMessage() {
329
- return this._errorMessage;
275
+ };
276
+
277
+ // src/domain/value-objects/payments/PaymentGateway.ts
278
+ var _PaymentGateway = class _PaymentGateway extends ValueObject {
279
+ constructor(gateway) {
280
+ super(gateway);
330
281
  }
331
- get publishedAt() {
332
- return this._publishedAt;
282
+ validate(value) {
283
+ if (!_PaymentGateway.SUPPORTED.includes(value)) {
284
+ throw new InternalError(`Payment gateway <${value}> is not supported`);
285
+ }
333
286
  }
334
- get lastAttempt() {
335
- return this._lastAttempt;
287
+ isExternal() {
288
+ return _PaymentGateway.EXTERNALS.includes(this.value);
336
289
  }
337
- get createdAt() {
338
- return this._createdAt;
290
+ toPrimitives() {
291
+ return { value: this.value };
339
292
  }
340
- incrementAttempts() {
341
- this._attempts++;
342
- this._lastAttempt = DateTime.now();
293
+ static create(gateway) {
294
+ return new _PaymentGateway(gateway.trim().toUpperCase());
343
295
  }
344
- markProcessed() {
345
- this._status = DomainEventStatus.PROCESSED;
346
- this._publishedAt = DateTime.now();
296
+ };
297
+ _PaymentGateway.SUPPORTED = [
298
+ "MERCADOPAGO",
299
+ "HANDY",
300
+ "WONA_DEBIT",
301
+ "WONA_CARD",
302
+ "WONA_CASH",
303
+ "WONA_TRANSFER",
304
+ "WONA_MERCADOPAGO"
305
+ ];
306
+ _PaymentGateway.EXTERNALS = ["MERCADOPAGO", "STRIPE"];
307
+ _PaymentGateway.MERCADOPAGO = new _PaymentGateway("MERCADOPAGO");
308
+ _PaymentGateway.HANDY = new _PaymentGateway("HANDY");
309
+ _PaymentGateway.WONA_DEBIT = new _PaymentGateway("WONA_DEBIT");
310
+ _PaymentGateway.WONA_CARD = new _PaymentGateway("WONA_CARD");
311
+ _PaymentGateway.WONA_CASH = new _PaymentGateway("WONA_CASH");
312
+ _PaymentGateway.WONA_TRANSFER = new _PaymentGateway("WONA_TRANSFER");
313
+ _PaymentGateway.WONA_MERCADOPAGO = new _PaymentGateway("WONA_MERCADOPAGO");
314
+ var PaymentGateway = _PaymentGateway;
315
+
316
+ // src/domain/value-objects/payments/PaymentStatus.ts
317
+ var _PaymentStatus = class _PaymentStatus extends ValueObject {
318
+ constructor(status) {
319
+ super(status);
347
320
  }
348
- markProcessing() {
349
- this.incrementAttempts();
350
- this._status = DomainEventStatus.PROCESSING;
321
+ validate(status) {
322
+ if (!_PaymentStatus.SUPPORTED.includes(status)) {
323
+ throw new InternalError(`Payment status <${status}> is not supported`);
324
+ }
351
325
  }
352
- markWithError(error) {
353
- this._status = this.attempts < 5 ? DomainEventStatus.FAILED : DomainEventStatus.DEAD;
354
- this._errorMessage = error;
326
+ get isDone() {
327
+ return this.value === "DONE";
355
328
  }
356
- toPrimitives() {
357
- return {
358
- eventUuid: this.eventUuid.value,
359
- eventType: this.eventType,
360
- tenantUuid: this.tenantUuid.value,
361
- aggregateUuid: this.aggregateUuid.value,
362
- aggregateType: this.aggregateType,
363
- topic: this.topic,
364
- payload: this.payload,
365
- status: this.status.value,
366
- attempts: this.attempts,
367
- errorMessage: this.errorMessage ?? void 0,
368
- publishedAt: this.publishedAt?.value ?? void 0,
369
- lastAttempt: this.lastAttempt?.value ?? void 0,
370
- createdAt: this.createdAt.value
371
- };
329
+ get isPending() {
330
+ return this.value === "PENDING";
372
331
  }
373
- static reconstitute(data) {
374
- return new _DomainEvent(
375
- UUID.create(data.event_uuid),
376
- String(data.event_type),
377
- UUID.create(data.tenant_uuid),
378
- UUID.create(data.aggregate_uuid),
379
- String(data.aggregate_type),
380
- String(data.topic),
381
- String(data.payload),
382
- DomainEventStatus.create(data.status),
383
- Number(data.attempts),
384
- data.error_message ?? void 0,
385
- data.published_at ? DateTime.create(data.published_at) : void 0,
386
- data.last_attempt ? DateTime.create(data.last_attempt) : void 0,
387
- data.created_at ? DateTime.create(data.created_at) : void 0
388
- );
332
+ get isInProgress() {
333
+ return this.value === "IN_PROGRESS";
389
334
  }
390
- };
391
-
392
- // src/domain/contracts/BaseObject.ts
393
- var BaseObject = class {
394
- constructor(props) {
395
- this.props = props;
335
+ get isFailed() {
336
+ return this.value === "FAILED";
396
337
  }
397
- };
398
-
399
- // src/domain/errors/FatalError.ts
400
- var FatalError = class extends DomainError {
401
- constructor(type, message = "") {
402
- super(type, message);
338
+ get isCanceled() {
339
+ return this.value === "CANCELED";
403
340
  }
404
- };
405
-
406
- // src/domain/errors/UsageError.ts
407
- var UsageError = class extends DomainError {
408
- constructor(type, vars = {}) {
409
- super(type);
410
- this.vars = vars;
341
+ get isHold() {
342
+ return this.value === "HOLD";
411
343
  }
412
- };
413
-
414
- // src/domain/value-objects/Currency.ts
415
- var _Currency = class _Currency extends ValueObject {
416
- constructor(alpha) {
417
- super(alpha.toUpperCase().trim());
418
- this.numeric = _Currency.ALPHA_TO_NUM[this.value];
344
+ get isPendingRefund() {
345
+ return this.value === "PENDING_REFUND";
419
346
  }
420
- validate(alpha) {
421
- const code = alpha.toUpperCase().trim();
422
- if (!_Currency.ALPHA_REGEX.test(code)) {
423
- throw new Error(`Currency code <${alpha}> is not a valid ISO\u20114217 alpha value`);
424
- }
425
- if (!(code in _Currency.ALPHA_TO_NUM)) {
426
- throw new Error(`Currency <${code}> is not supported`);
427
- }
347
+ get isRefunded() {
348
+ return this.value === "REFUNDED";
428
349
  }
429
350
  toPrimitives() {
430
- return {
431
- value: this.value
432
- };
433
- }
434
- static create(raw) {
435
- if (typeof raw === "number" || _Currency.NUM_REGEX.test(raw)) {
436
- const num = Number(raw);
437
- const alpha = _Currency.NUM_TO_ALPHA[num];
438
- if (!alpha) {
439
- throw new Error(`Numeric currency <${raw}> is not supported`);
440
- }
441
- return new _Currency(alpha);
442
- }
443
- return new _Currency(String(raw));
351
+ return { value: this.value };
444
352
  }
445
- static isValid(raw) {
446
- try {
447
- _Currency.create(raw);
448
- return true;
449
- } catch {
450
- return false;
451
- }
353
+ static create(gateway) {
354
+ return new _PaymentStatus(gateway.trim().toUpperCase());
452
355
  }
453
356
  };
454
- _Currency.ALPHA_REGEX = /^[A-Z]{3}$/u;
455
- _Currency.NUM_REGEX = /^\d{3}$/u;
456
- _Currency.ALPHA_TO_NUM = {
457
- USD: 840,
458
- EUR: 978,
459
- UYU: 858,
460
- ARS: 32,
461
- BRL: 986
462
- };
463
- _Currency.NUM_TO_ALPHA = Object.entries(_Currency.ALPHA_TO_NUM).reduce(
464
- (acc, [alpha, num]) => {
465
- acc[num] = alpha;
466
- return acc;
467
- },
468
- {}
469
- );
470
- _Currency.USD = new _Currency("USD");
471
- _Currency.EUR = new _Currency("EUR");
472
- _Currency.UYU = new _Currency("UYU");
473
- _Currency.ARS = new _Currency("ARS");
474
- _Currency.BRL = new _Currency("BRL");
475
- var Currency = _Currency;
357
+ _PaymentStatus.SUPPORTED = ["DONE", "PENDING", "FAILED", "CANCELED", "HOLD", "PENDING_REFUND", "REFUNDED", "IN_PROGRESS"];
358
+ _PaymentStatus.DONE = new _PaymentStatus("DONE");
359
+ _PaymentStatus.PENDING = new _PaymentStatus("PENDING");
360
+ _PaymentStatus.IN_PROGRESS = new _PaymentStatus("IN_PROGRESS");
361
+ _PaymentStatus.FAILED = new _PaymentStatus("FAILED");
362
+ _PaymentStatus.CANCELED = new _PaymentStatus("CANCELED");
363
+ _PaymentStatus.HOLD = new _PaymentStatus("HOLD");
364
+ _PaymentStatus.PENDING_REFUND = new _PaymentStatus("PENDING_REFUND");
365
+ _PaymentStatus.REFUNDED = new _PaymentStatus("REFUNDED");
366
+ var PaymentStatus = _PaymentStatus;
476
367
 
477
368
  // src/utils/StringVars.ts
478
369
  var StringVars = class {
@@ -765,6 +656,69 @@ _Country.PARAGUAY = new _Country("PARAGUAY");
765
656
  _Country.USA = new _Country("USA");
766
657
  var Country = _Country;
767
658
 
659
+ // src/domain/value-objects/Currency.ts
660
+ var _Currency = class _Currency extends ValueObject {
661
+ constructor(alpha) {
662
+ super(alpha.toUpperCase().trim());
663
+ this.numeric = _Currency.ALPHA_TO_NUM[this.value];
664
+ }
665
+ validate(alpha) {
666
+ const code = alpha.toUpperCase().trim();
667
+ if (!_Currency.ALPHA_REGEX.test(code)) {
668
+ throw new Error(`Currency code <${alpha}> is not a valid ISO\u20114217 alpha value`);
669
+ }
670
+ if (!(code in _Currency.ALPHA_TO_NUM)) {
671
+ throw new Error(`Currency <${code}> is not supported`);
672
+ }
673
+ }
674
+ toPrimitives() {
675
+ return {
676
+ value: this.value
677
+ };
678
+ }
679
+ static create(raw) {
680
+ if (typeof raw === "number" || _Currency.NUM_REGEX.test(raw)) {
681
+ const num = Number(raw);
682
+ const alpha = _Currency.NUM_TO_ALPHA[num];
683
+ if (!alpha) {
684
+ throw new Error(`Numeric currency <${raw}> is not supported`);
685
+ }
686
+ return new _Currency(alpha);
687
+ }
688
+ return new _Currency(String(raw));
689
+ }
690
+ static isValid(raw) {
691
+ try {
692
+ _Currency.create(raw);
693
+ return true;
694
+ } catch {
695
+ return false;
696
+ }
697
+ }
698
+ };
699
+ _Currency.ALPHA_REGEX = /^[A-Z]{3}$/u;
700
+ _Currency.NUM_REGEX = /^\d{3}$/u;
701
+ _Currency.ALPHA_TO_NUM = {
702
+ USD: 840,
703
+ EUR: 978,
704
+ UYU: 858,
705
+ ARS: 32,
706
+ BRL: 986
707
+ };
708
+ _Currency.NUM_TO_ALPHA = Object.entries(_Currency.ALPHA_TO_NUM).reduce(
709
+ (acc, [alpha, num]) => {
710
+ acc[num] = alpha;
711
+ return acc;
712
+ },
713
+ {}
714
+ );
715
+ _Currency.USD = new _Currency("USD");
716
+ _Currency.EUR = new _Currency("EUR");
717
+ _Currency.UYU = new _Currency("UYU");
718
+ _Currency.ARS = new _Currency("ARS");
719
+ _Currency.BRL = new _Currency("BRL");
720
+ var Currency = _Currency;
721
+
768
722
  // src/domain/value-objects/Email.ts
769
723
  var _Email = class _Email extends ValueObject {
770
724
  constructor(email) {
@@ -958,111 +912,110 @@ _Price.MIN_AMOUNT = -1e6;
958
912
  _Price.MAX_AMOUNT = 1e9;
959
913
  var Price = _Price;
960
914
 
961
- // src/domain/value-objects/payments/PaymentGateway.ts
962
- var _PaymentGateway = class _PaymentGateway extends ValueObject {
963
- constructor(gateway) {
964
- super(gateway);
915
+ // src/domain/value-objects/ProcessStatus.ts
916
+ var _ProcessStatus = class _ProcessStatus extends ValueObject {
917
+ constructor(status) {
918
+ super(status.trim().toUpperCase());
965
919
  }
966
920
  validate(value) {
967
- if (!_PaymentGateway.SUPPORTED.includes(value)) {
968
- throw new InternalError(`Payment gateway <${value}> is not supported`);
921
+ if (!_ProcessStatus.SUPPORTED.includes(value)) {
922
+ throw new InternalError(`Domain event status <${value}> is not supported`);
969
923
  }
970
924
  }
971
- isExternal() {
972
- return _PaymentGateway.EXTERNALS.includes(this.value);
973
- }
974
925
  toPrimitives() {
975
- return { value: this.value };
926
+ return void 0;
976
927
  }
977
- static create(gateway) {
978
- return new _PaymentGateway(gateway.trim().toUpperCase());
928
+ static create(status) {
929
+ return new _ProcessStatus(status);
979
930
  }
980
931
  };
981
- _PaymentGateway.SUPPORTED = [
982
- "MERCADOPAGO",
983
- "HANDY",
984
- "WONA_DEBIT",
985
- "WONA_CARD",
986
- "WONA_CASH",
987
- "WONA_TRANSFER",
988
- "WONA_MERCADOPAGO"
989
- ];
990
- _PaymentGateway.EXTERNALS = ["MERCADOPAGO", "STRIPE"];
991
- _PaymentGateway.MERCADOPAGO = new _PaymentGateway("MERCADOPAGO");
992
- _PaymentGateway.HANDY = new _PaymentGateway("HANDY");
993
- _PaymentGateway.WONA_DEBIT = new _PaymentGateway("WONA_DEBIT");
994
- _PaymentGateway.WONA_CARD = new _PaymentGateway("WONA_CARD");
995
- _PaymentGateway.WONA_CASH = new _PaymentGateway("WONA_CASH");
996
- _PaymentGateway.WONA_TRANSFER = new _PaymentGateway("WONA_TRANSFER");
997
- _PaymentGateway.WONA_MERCADOPAGO = new _PaymentGateway("WONA_MERCADOPAGO");
998
- var PaymentGateway = _PaymentGateway;
932
+ _ProcessStatus.SUPPORTED = ["PENDING", "PROCESSING", "PROCESSED", "FAILED", "DEAD"];
933
+ _ProcessStatus.PENDING = new _ProcessStatus("PENDING");
934
+ _ProcessStatus.PROCESSING = new _ProcessStatus("PROCESSING");
935
+ _ProcessStatus.PROCESSED = new _ProcessStatus("PROCESSED");
936
+ _ProcessStatus.FAILED = new _ProcessStatus("FAILED");
937
+ _ProcessStatus.DEAD = new _ProcessStatus("DEAD");
938
+ var ProcessStatus = _ProcessStatus;
999
939
 
1000
- // src/domain/value-objects/payments/PaymentStatus.ts
1001
- var _PaymentStatus = class _PaymentStatus extends ValueObject {
1002
- constructor(status) {
1003
- super(status);
940
+ // src/domain/value-objects/UUID.ts
941
+ import * as crypto from "crypto";
942
+ var _UUID = class _UUID extends ValueObject {
943
+ constructor(value) {
944
+ super(value);
1004
945
  }
1005
- validate(status) {
1006
- if (!_PaymentStatus.SUPPORTED.includes(status)) {
1007
- throw new InternalError(`Payment status <${status}> is not supported`);
946
+ validate(uuid) {
947
+ if (!_UUID.isValid(uuid)) {
948
+ throw new InternalError(`Invalid uuid <${uuid}>`);
1008
949
  }
1009
950
  }
1010
- get isDone() {
1011
- return this.value === "DONE";
1012
- }
1013
- get isPending() {
1014
- return this.value === "PENDING";
951
+ toPrimitives() {
952
+ return { value: this.value };
1015
953
  }
1016
- get isInProgress() {
1017
- return this.value === "IN_PROGRESS";
954
+ static create(uuid) {
955
+ return new _UUID(uuid ?? crypto.randomUUID());
1018
956
  }
1019
- get isFailed() {
1020
- return this.value === "FAILED";
957
+ static version(uuid) {
958
+ 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);
959
+ return m ? Number(m[1]) : void 0;
1021
960
  }
1022
- get isCanceled() {
1023
- return this.value === "CANCELED";
961
+ static isNil(uuid) {
962
+ return /^0{8}-0{4}-0{4}-0{4}-0{12}$/i.test(uuid);
1024
963
  }
1025
- get isHold() {
1026
- return this.value === "HOLD";
964
+ static isRFCStyle(uuid) {
965
+ 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);
1027
966
  }
1028
- get isPendingRefund() {
1029
- return this.value === "PENDING_REFUND";
967
+ static isValid(uuid, opts = {}) {
968
+ const allowed = opts.allowedVersions ?? [1, 2, 3, 4, 5, 6, 7, 8];
969
+ const allowNil = opts.allowNil ?? false;
970
+ if (allowNil && _UUID.isNil(uuid))
971
+ return true;
972
+ if (!_UUID.isRFCStyle(uuid))
973
+ return false;
974
+ const v = _UUID.version(uuid);
975
+ return !!v && allowed.includes(v);
1030
976
  }
1031
- get isRefunded() {
1032
- return this.value === "REFUNDED";
977
+ };
978
+ _UUID.NIL = "00000000-0000-0000-0000-000000000000";
979
+ var UUID = _UUID;
980
+
981
+ // src/application/contracts/IntegrationEvent.ts
982
+ var IntegrationEvent = class extends BaseEvent {
983
+ constructor(version, type, payload, aggregateUuid, aggregateType) {
984
+ super(version, type, payload);
985
+ this._aggregateUuid = aggregateUuid;
986
+ this._aggregateType = aggregateType;
1033
987
  }
1034
- toPrimitives() {
1035
- return { value: this.value };
988
+ get aggregateUuid() {
989
+ return this._aggregateUuid;
1036
990
  }
1037
- static create(gateway) {
1038
- return new _PaymentStatus(gateway.trim().toUpperCase());
991
+ get aggregateType() {
992
+ return this._aggregateType;
1039
993
  }
1040
994
  };
1041
- _PaymentStatus.SUPPORTED = ["DONE", "PENDING", "FAILED", "CANCELED", "HOLD", "PENDING_REFUND", "REFUNDED", "IN_PROGRESS"];
1042
- _PaymentStatus.DONE = new _PaymentStatus("DONE");
1043
- _PaymentStatus.PENDING = new _PaymentStatus("PENDING");
1044
- _PaymentStatus.IN_PROGRESS = new _PaymentStatus("IN_PROGRESS");
1045
- _PaymentStatus.FAILED = new _PaymentStatus("FAILED");
1046
- _PaymentStatus.CANCELED = new _PaymentStatus("CANCELED");
1047
- _PaymentStatus.HOLD = new _PaymentStatus("HOLD");
1048
- _PaymentStatus.PENDING_REFUND = new _PaymentStatus("PENDING_REFUND");
1049
- _PaymentStatus.REFUNDED = new _PaymentStatus("REFUNDED");
1050
- var PaymentStatus = _PaymentStatus;
1051
995
 
1052
996
  // src/application/event-bus/EventBus.ts
1053
- var EventBus = class {
997
+ var _EventBus = class _EventBus {
1054
998
  constructor(repository) {
1055
999
  this.repository = repository;
1056
1000
  }
1057
1001
  async publish(event) {
1058
- await this.repository.create(event);
1002
+ const mapper = _EventBus.MAPPERS.get(event.type);
1003
+ if (!mapper) {
1004
+ throw new InternalError(ErrorManager.APP_ERRORS.PROCESS, `Eventbus mapper not found for <${event.type}>`);
1005
+ }
1006
+ await this.repository.create(mapper(event));
1059
1007
  }
1060
1008
  async publishMany(events) {
1061
1009
  for (let event of events) {
1062
1010
  await this.publish(event);
1063
1011
  }
1064
1012
  }
1013
+ static addMapper(eventType, mapper) {
1014
+ _EventBus.MAPPERS.set(eventType, mapper);
1015
+ }
1065
1016
  };
1017
+ _EventBus.MAPPERS = /* @__PURE__ */ new Map();
1018
+ var EventBus = _EventBus;
1066
1019
 
1067
1020
  // src/application/unit-of-work/BasicUnitOfWork.ts
1068
1021
  var BasicUnitOfWork = class {
@@ -1152,104 +1105,164 @@ var EventManager = class {
1152
1105
  }
1153
1106
  };
1154
1107
 
1155
- // src/infrastructure/mysql/Mysql.ts
1156
- import { createPool } from "mysql2/promise";
1157
- var _MysqlConnector = class _MysqlConnector {
1158
- constructor(pool) {
1159
- this._pool = pool ?? createPool({
1160
- host: process.env.DB_HOST,
1161
- port: Number(process.env.DB_PORT ?? 3306),
1162
- user: process.env.DB_USER,
1163
- password: process.env.DB_PASSWORD,
1164
- database: process.env.DB_DATABASE,
1165
- dateStrings: true,
1166
- connectionLimit: Number(process.env.DB_POOL_SIZE) || _MysqlConnector.DEFAULT_POOL_SIZE,
1167
- decimalNumbers: true
1168
- });
1108
+ // src/infrastructure/contracts/OutboxRecord.ts
1109
+ var OutboxRecord = class _OutboxRecord {
1110
+ constructor(eventUuid, eventType, tenantUuid, aggregateUuid, aggregateType, topic, payload, status, attempts, errorMessage, publishedAt, lastAttempt, createdAt) {
1111
+ this._eventUuid = eventUuid;
1112
+ this._tenantUuid = tenantUuid;
1113
+ this._aggregateUuid = aggregateUuid;
1114
+ this._aggregateType = aggregateType;
1115
+ this._eventType = eventType;
1116
+ this._topic = topic;
1117
+ this._payload = payload;
1118
+ this._status = status;
1119
+ this._attempts = attempts;
1120
+ this._errorMessage = errorMessage;
1121
+ this._publishedAt = publishedAt;
1122
+ this._lastAttempt = lastAttempt;
1123
+ this._createdAt = createdAt;
1169
1124
  }
1170
- async wrap(conn) {
1171
- return new MysqlConnection(conn);
1125
+ get eventUuid() {
1126
+ return this._eventUuid;
1172
1127
  }
1173
- async query(sql, params = []) {
1174
- const [rows] = await this._pool.query(sql, params);
1175
- return rows;
1128
+ get tenantUuid() {
1129
+ return this._tenantUuid;
1176
1130
  }
1177
- async getConnection() {
1178
- const conn = await this._pool.getConnection();
1179
- return this.wrap(conn);
1131
+ get aggregateUuid() {
1132
+ return this._aggregateUuid;
1180
1133
  }
1181
- async closePool() {
1182
- await this._pool.end();
1134
+ get aggregateType() {
1135
+ return this._aggregateType;
1183
1136
  }
1184
- static async ping() {
1185
- const connector = new _MysqlConnector();
1186
- try {
1187
- const conn = await connector._pool.getConnection();
1188
- await conn.ping();
1189
- conn.release();
1190
- return true;
1191
- } catch {
1192
- return false;
1193
- } finally {
1194
- await connector.closePool();
1195
- }
1137
+ get eventType() {
1138
+ return this._eventType;
1196
1139
  }
1197
- };
1198
- _MysqlConnector.DEFAULT_POOL_SIZE = 20;
1199
- var MysqlConnector = _MysqlConnector;
1200
- var MysqlConnection = class {
1201
- constructor(conn) {
1202
- this._conn = conn;
1140
+ get topic() {
1141
+ return this._topic;
1203
1142
  }
1204
- async query(statement, params = []) {
1205
- const [rows] = await this._conn.query(statement, params);
1206
- return rows;
1143
+ get payload() {
1144
+ return this._payload;
1207
1145
  }
1208
- async begin() {
1209
- await this._conn.beginTransaction();
1146
+ get status() {
1147
+ return this._status;
1210
1148
  }
1211
- async commit() {
1212
- await this._conn.commit();
1149
+ get attempts() {
1150
+ return this._attempts;
1213
1151
  }
1214
- async rollback() {
1215
- await this._conn.rollback();
1152
+ get errorMessage() {
1153
+ return this._errorMessage;
1216
1154
  }
1217
- async transaction(fn) {
1218
- await this.begin();
1219
- try {
1220
- const result = await fn(this);
1221
- await this.commit();
1222
- return result;
1223
- } catch (err) {
1224
- await this.rollback();
1225
- throw err;
1226
- }
1155
+ get publishedAt() {
1156
+ return this._publishedAt;
1157
+ }
1158
+ get lastAttempt() {
1159
+ return this._lastAttempt;
1160
+ }
1161
+ get createdAt() {
1162
+ return this._createdAt;
1163
+ }
1164
+ incrementAttempts() {
1165
+ this._attempts++;
1166
+ this._lastAttempt = DateTime.now();
1167
+ }
1168
+ markProcessed() {
1169
+ this._status = ProcessStatus.PROCESSED;
1170
+ this._publishedAt = DateTime.now();
1171
+ }
1172
+ markProcessing() {
1173
+ this.incrementAttempts();
1174
+ this._status = ProcessStatus.PROCESSING;
1175
+ }
1176
+ markWithError(error) {
1177
+ this._status = this.attempts < 5 ? ProcessStatus.FAILED : ProcessStatus.DEAD;
1178
+ this._errorMessage = error;
1179
+ }
1180
+ toPrimitives() {
1181
+ return {
1182
+ eventUuid: this.eventUuid.value,
1183
+ eventType: this.eventType,
1184
+ tenantUuid: this.tenantUuid.value,
1185
+ aggregateUuid: this.aggregateUuid.value,
1186
+ aggregateType: this.aggregateType,
1187
+ topic: this.topic,
1188
+ payload: this.payload,
1189
+ status: this.status.value,
1190
+ attempts: this.attempts,
1191
+ errorMessage: this.errorMessage ?? void 0,
1192
+ publishedAt: this.publishedAt?.value ?? void 0,
1193
+ lastAttempt: this.lastAttempt?.value ?? void 0,
1194
+ createdAt: this.createdAt.value
1195
+ };
1227
1196
  }
1228
- async close() {
1229
- await this._conn.release();
1197
+ static reconstitute(data) {
1198
+ return new _OutboxRecord(
1199
+ UUID.create(data.event_uuid),
1200
+ String(data.event_type),
1201
+ UUID.create(data.tenant_uuid),
1202
+ UUID.create(data.aggregate_uuid),
1203
+ String(data.aggregate_type),
1204
+ String(data.topic),
1205
+ String(data.payload),
1206
+ ProcessStatus.create(data.status),
1207
+ Number(data.attempts),
1208
+ data.error_message ?? void 0,
1209
+ data.published_at ? DateTime.create(data.published_at) : void 0,
1210
+ data.last_attempt ? DateTime.create(data.last_attempt) : void 0,
1211
+ data.created_at ? DateTime.create(data.created_at) : void 0
1212
+ );
1230
1213
  }
1231
1214
  };
1232
1215
 
1233
- // src/infrastructure/http/DefaultController.ts
1234
- var HttpHealthCheckController = class {
1235
- async handle(request) {
1236
- return {
1237
- statusCode: 200,
1238
- body: {
1239
- date: DateTime.create().value,
1240
- code: 200
1241
- }
1242
- };
1216
+ // src/infrastructure/event-bus/EventBusMysqlRepository.ts
1217
+ var EventBusMysqlRepository = class {
1218
+ constructor(connection) {
1219
+ this.connection = connection;
1243
1220
  }
1244
- };
1245
- var HttpNotFoundController = class {
1246
- async handle(request) {
1247
- return {
1248
- statusCode: 404,
1249
- body: {
1250
- message: `Route ${request.headers.location} not found`
1251
- }
1252
- };
1221
+ eventToRowValues(e) {
1222
+ return [
1223
+ e.eventUuid.value,
1224
+ e.eventType,
1225
+ e.tenantUuid.value,
1226
+ e.aggregateUuid.value,
1227
+ e.aggregateType,
1228
+ e.topic,
1229
+ e.payload,
1230
+ e.status.value,
1231
+ e.attempts,
1232
+ e.errorMessage,
1233
+ e.publishedAt?.value,
1234
+ e.lastAttempt?.value,
1235
+ e.createdAt.value
1236
+ ];
1237
+ }
1238
+ async create(event) {
1239
+ const values = this.eventToRowValues(event);
1240
+ await this.connection.query(
1241
+ `INSERT INTO events_outbox (event_uuid, event_type, tenant_uuid, aggregate_uuid, aggregate_type, topic,
1242
+ payload, status, attempts, error_message, published_at, last_attempt, created_at)
1243
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
1244
+ values
1245
+ );
1246
+ }
1247
+ async update(event) {
1248
+ const values = [event.status.value, event.attempts, event.errorMessage, event.publishedAt?.value, event.lastAttempt?.value, event.eventUuid.value];
1249
+ await this.connection.query(
1250
+ `UPDATE events_outbox
1251
+ SET status = ?,
1252
+ attempts = ?,
1253
+ error_message = ?,
1254
+ published_at = ?,
1255
+ last_attempt = ?
1256
+ WHERE event_uuid = ?`,
1257
+ values
1258
+ );
1259
+ }
1260
+ async listPending(limit) {
1261
+ const result = await this.connection.query(
1262
+ `SELECT * FROM events_outbox WHERE status IN ('PENDING','FAILED') AND published_at IS NULL LIMIT 50`,
1263
+ []
1264
+ );
1265
+ return result.length > 0 ? result.map((r) => OutboxRecord.reconstitute(r)) : [];
1253
1266
  }
1254
1267
  };
1255
1268
 
@@ -1383,56 +1396,26 @@ function adaptExpressErrorHandler(errorManager) {
1383
1396
  };
1384
1397
  }
1385
1398
 
1386
- // src/infrastructure/event-bus/EventBusMysqlRepository.ts
1387
- var EventBusMysqlRepository = class {
1388
- constructor(connection) {
1389
- this.connection = connection;
1390
- }
1391
- eventToRowValues(e) {
1392
- return [
1393
- e.eventUuid.value,
1394
- e.eventType,
1395
- e.tenantUuid.value,
1396
- e.aggregateUuid.value,
1397
- e.aggregateType,
1398
- e.topic,
1399
- e.payload,
1400
- e.status.value,
1401
- e.attempts,
1402
- e.errorMessage,
1403
- e.publishedAt?.value,
1404
- e.lastAttempt?.value,
1405
- e.createdAt.value
1406
- ];
1407
- }
1408
- async create(event) {
1409
- const values = this.eventToRowValues(event);
1410
- await this.connection.query(
1411
- `INSERT INTO events_outbox (event_uuid, event_type, tenant_uuid, aggregate_uuid, aggregate_type, topic,
1412
- payload, status, attempts, error_message, published_at, last_attempt, created_at)
1413
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
1414
- values
1415
- );
1416
- }
1417
- async update(event) {
1418
- const values = [event.status.value, event.attempts, event.errorMessage, event.publishedAt?.value, event.lastAttempt?.value, event.eventUuid.value];
1419
- await this.connection.query(
1420
- `UPDATE events_outbox
1421
- SET status = ?,
1422
- attempts = ?,
1423
- error_message = ?,
1424
- published_at = ?,
1425
- last_attempt = ?
1426
- WHERE event_uuid = ?`,
1427
- values
1428
- );
1399
+ // src/infrastructure/http/DefaultController.ts
1400
+ var HttpHealthCheckController = class {
1401
+ async handle(request) {
1402
+ return {
1403
+ statusCode: 200,
1404
+ body: {
1405
+ date: DateTime.create().value,
1406
+ code: 200
1407
+ }
1408
+ };
1429
1409
  }
1430
- async listPending(limit) {
1431
- const result = await this.connection.query(
1432
- `SELECT * FROM events_outbox WHERE status IN ('PENDING','FAILED') AND published_at IS NULL LIMIT 50`,
1433
- []
1434
- );
1435
- return result.length > 0 ? result.map((r) => DomainEvent.reconstitute(r)) : [];
1410
+ };
1411
+ var HttpNotFoundController = class {
1412
+ async handle(request) {
1413
+ return {
1414
+ statusCode: 404,
1415
+ body: {
1416
+ message: `Route ${request.headers.location} not found`
1417
+ }
1418
+ };
1436
1419
  }
1437
1420
  };
1438
1421
 
@@ -1473,8 +1456,7 @@ var KafkaManager = class extends EventManager {
1473
1456
  eachMessage: async ({ topic, partition, message, heartbeat }) => {
1474
1457
  try {
1475
1458
  await this.execCallback(this._onMessage, `[New message detected for ${topic}]: ${message.value?.toString()}`);
1476
- const evt = JSON.parse(String(message.value?.toString()));
1477
- await this.execRoute(topic, evt);
1459
+ await this.execRoute(topic, String(message.value?.toString()));
1478
1460
  const next = (BigInt(message.offset) + 1n).toString();
1479
1461
  await this.consumer.commitOffsets([{ topic, partition, offset: next }]);
1480
1462
  await heartbeat();
@@ -1490,28 +1472,12 @@ var KafkaManager = class extends EventManager {
1490
1472
  await this.execCallback(this._onError, new InternalError(ErrorManager.APP_ERRORS.PROCESS, error.toString()));
1491
1473
  }
1492
1474
  }
1493
- async send(message) {
1494
- if (!message.producer) {
1495
- message.producer = process.env.NAME && process.env.ENVIRONMENT ? `${process.env.NAME}-${process.env.ENVIRONMENT}` : "unknown";
1496
- }
1497
- if (!message.date) {
1498
- message.date = DateTime.now().value;
1499
- }
1500
- try {
1501
- if (!this.producer) {
1502
- throw new InternalError(ErrorManager.APP_ERRORS.PROCESS, "Producer not initialized");
1503
- }
1504
- await this.producer.connect();
1505
- await this.producer.send({
1506
- topic: message.topic,
1507
- messages: [{ value: JSON.stringify({ producer: message.producer, data: message }) }]
1508
- });
1509
- await this.producer.disconnect();
1510
- } catch (error) {
1511
- throw new InternalError(error.toString());
1512
- }
1513
- }
1514
- async sendRaw(message, topic) {
1475
+ async send(topic, message) {
1476
+ const evt = {
1477
+ date: DateTime.now().value,
1478
+ producer: process.env.NAME && process.env.ENVIRONMENT ? `${process.env.NAME}-${process.env.ENVIRONMENT}` : "unknown",
1479
+ data: message
1480
+ };
1515
1481
  try {
1516
1482
  if (!this.producer) {
1517
1483
  throw new InternalError(ErrorManager.APP_ERRORS.PROCESS, "Producer not initialized");
@@ -1519,7 +1485,7 @@ var KafkaManager = class extends EventManager {
1519
1485
  await this.producer.connect();
1520
1486
  await this.producer.send({
1521
1487
  topic,
1522
- messages: [{ value: message }]
1488
+ messages: [{ value: JSON.stringify(evt) }]
1523
1489
  });
1524
1490
  await this.producer.disconnect();
1525
1491
  } catch (error) {
@@ -1543,6 +1509,84 @@ var KafkaManager = class extends EventManager {
1543
1509
  }
1544
1510
  };
1545
1511
 
1512
+ // src/infrastructure/mysql/Mysql.ts
1513
+ import { createPool } from "mysql2/promise";
1514
+ var _MysqlConnector = class _MysqlConnector {
1515
+ constructor(pool) {
1516
+ this._pool = pool ?? createPool({
1517
+ host: process.env.DB_HOST,
1518
+ port: Number(process.env.DB_PORT ?? 3306),
1519
+ user: process.env.DB_USER,
1520
+ password: process.env.DB_PASSWORD,
1521
+ database: process.env.DB_DATABASE,
1522
+ dateStrings: true,
1523
+ connectionLimit: Number(process.env.DB_POOL_SIZE) || _MysqlConnector.DEFAULT_POOL_SIZE,
1524
+ decimalNumbers: true
1525
+ });
1526
+ }
1527
+ async wrap(conn) {
1528
+ return new MysqlConnection(conn);
1529
+ }
1530
+ async query(sql, params = []) {
1531
+ const [rows] = await this._pool.query(sql, params);
1532
+ return rows;
1533
+ }
1534
+ async getConnection() {
1535
+ const conn = await this._pool.getConnection();
1536
+ return this.wrap(conn);
1537
+ }
1538
+ async closePool() {
1539
+ await this._pool.end();
1540
+ }
1541
+ static async ping() {
1542
+ const connector = new _MysqlConnector();
1543
+ try {
1544
+ const conn = await connector._pool.getConnection();
1545
+ await conn.ping();
1546
+ conn.release();
1547
+ return true;
1548
+ } catch {
1549
+ return false;
1550
+ } finally {
1551
+ await connector.closePool();
1552
+ }
1553
+ }
1554
+ };
1555
+ _MysqlConnector.DEFAULT_POOL_SIZE = 20;
1556
+ var MysqlConnector = _MysqlConnector;
1557
+ var MysqlConnection = class {
1558
+ constructor(conn) {
1559
+ this._conn = conn;
1560
+ }
1561
+ async query(statement, params = []) {
1562
+ const [rows] = await this._conn.query(statement, params);
1563
+ return rows;
1564
+ }
1565
+ async begin() {
1566
+ await this._conn.beginTransaction();
1567
+ }
1568
+ async commit() {
1569
+ await this._conn.commit();
1570
+ }
1571
+ async rollback() {
1572
+ await this._conn.rollback();
1573
+ }
1574
+ async transaction(fn) {
1575
+ await this.begin();
1576
+ try {
1577
+ const result = await fn(this);
1578
+ await this.commit();
1579
+ return result;
1580
+ } catch (err) {
1581
+ await this.rollback();
1582
+ throw err;
1583
+ }
1584
+ }
1585
+ async close() {
1586
+ await this._conn.release();
1587
+ }
1588
+ };
1589
+
1546
1590
  // src/utils/ExchangeRates.ts
1547
1591
  var ExchangeRates = class _ExchangeRates extends BaseObject {
1548
1592
  constructor(props) {
@@ -1595,6 +1639,7 @@ var ExchangeRates = class _ExchangeRates extends BaseObject {
1595
1639
  }
1596
1640
  };
1597
1641
  export {
1642
+ BaseEvent,
1598
1643
  BaseObject,
1599
1644
  BasicUnitOfWork,
1600
1645
  BasicUnitOfWorkFactory,
@@ -1604,7 +1649,6 @@ export {
1604
1649
  DomainEntity,
1605
1650
  DomainError,
1606
1651
  DomainEvent,
1607
- DomainEventStatus,
1608
1652
  Email,
1609
1653
  ErrorManager,
1610
1654
  EventBus,
@@ -1614,14 +1658,18 @@ export {
1614
1658
  FatalError,
1615
1659
  HttpHealthCheckController,
1616
1660
  HttpNotFoundController,
1661
+ IntegrationEvent,
1617
1662
  InternalError,
1618
1663
  KafkaManager,
1619
1664
  Language,
1620
1665
  MysqlConnection,
1621
1666
  MysqlConnector,
1667
+ OutboxRecord,
1622
1668
  PaymentGateway,
1623
1669
  PaymentStatus,
1624
1670
  Price,
1671
+ ProcessStatus,
1672
+ StringVars,
1625
1673
  UUID,
1626
1674
  UsageError,
1627
1675
  ValueObject,