wsp-ms-core 1.0.77 → 1.0.78-beta-1

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