wsp-ms-core 1.0.68 → 1.0.70
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 +465 -114
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +110 -1
- package/dist/index.d.ts +110 -1
- package/dist/index.js +462 -114
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -32,6 +32,7 @@ __export(src_exports, {
|
|
|
32
32
|
BaseObject: () => BaseObject,
|
|
33
33
|
BasicUnitOfWork: () => BasicUnitOfWork,
|
|
34
34
|
BasicUnitOfWorkFactory: () => BasicUnitOfWorkFactory,
|
|
35
|
+
Country: () => Country,
|
|
35
36
|
Currency: () => Currency,
|
|
36
37
|
DateTime: () => DateTime,
|
|
37
38
|
DomainEntity: () => DomainEntity,
|
|
@@ -42,11 +43,13 @@ __export(src_exports, {
|
|
|
42
43
|
ErrorManager: () => ErrorManager,
|
|
43
44
|
EventBus: () => EventBus,
|
|
44
45
|
EventBusMysqlRepository: () => EventBusMysqlRepository,
|
|
46
|
+
EventManager: () => EventManager,
|
|
45
47
|
ExchangeRates: () => ExchangeRates,
|
|
46
48
|
FatalError: () => FatalError,
|
|
47
49
|
HttpHealthCheckController: () => HttpHealthCheckController,
|
|
48
50
|
HttpNotFoundController: () => HttpNotFoundController,
|
|
49
51
|
InternalError: () => InternalError,
|
|
52
|
+
KafkaManager: () => KafkaManager,
|
|
50
53
|
Language: () => Language,
|
|
51
54
|
MysqlConnection: () => MysqlConnection,
|
|
52
55
|
MysqlConnector: () => MysqlConnector,
|
|
@@ -257,7 +260,7 @@ var _UUID = class _UUID extends ValueObject {
|
|
|
257
260
|
return { value: this.value };
|
|
258
261
|
}
|
|
259
262
|
static create(uuid) {
|
|
260
|
-
return new _UUID(uuid
|
|
263
|
+
return new _UUID(uuid ?? crypto.randomUUID());
|
|
261
264
|
}
|
|
262
265
|
static version(uuid) {
|
|
263
266
|
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);
|
|
@@ -270,9 +273,8 @@ var _UUID = class _UUID extends ValueObject {
|
|
|
270
273
|
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);
|
|
271
274
|
}
|
|
272
275
|
static isValid(uuid, opts = {}) {
|
|
273
|
-
|
|
274
|
-
const
|
|
275
|
-
const allowNil = (_b = opts.allowNil) != null ? _b : false;
|
|
276
|
+
const allowed = opts.allowedVersions ?? [1, 2, 3, 4, 5, 6, 7, 8];
|
|
277
|
+
const allowNil = opts.allowNil ?? false;
|
|
276
278
|
if (allowNil && _UUID.isNil(uuid))
|
|
277
279
|
return true;
|
|
278
280
|
if (!_UUID.isRFCStyle(uuid))
|
|
@@ -381,7 +383,6 @@ var DomainEvent = class _DomainEvent {
|
|
|
381
383
|
this._errorMessage = error;
|
|
382
384
|
}
|
|
383
385
|
toPrimitives() {
|
|
384
|
-
var _a, _b, _c, _d, _e;
|
|
385
386
|
return {
|
|
386
387
|
eventUuid: this.eventUuid.value,
|
|
387
388
|
eventType: this.eventType,
|
|
@@ -392,14 +393,13 @@ var DomainEvent = class _DomainEvent {
|
|
|
392
393
|
payload: this.payload,
|
|
393
394
|
status: this.status.value,
|
|
394
395
|
attempts: this.attempts,
|
|
395
|
-
errorMessage:
|
|
396
|
-
publishedAt:
|
|
397
|
-
lastAttempt:
|
|
396
|
+
errorMessage: this.errorMessage ?? void 0,
|
|
397
|
+
publishedAt: this.publishedAt?.value ?? void 0,
|
|
398
|
+
lastAttempt: this.lastAttempt?.value ?? void 0,
|
|
398
399
|
createdAt: this.createdAt.value
|
|
399
400
|
};
|
|
400
401
|
}
|
|
401
402
|
static reconstitute(data) {
|
|
402
|
-
var _a;
|
|
403
403
|
return new _DomainEvent(
|
|
404
404
|
UUID.create(data.event_uuid),
|
|
405
405
|
String(data.event_type),
|
|
@@ -410,7 +410,7 @@ var DomainEvent = class _DomainEvent {
|
|
|
410
410
|
String(data.payload),
|
|
411
411
|
DomainEventStatus.create(data.status),
|
|
412
412
|
Number(data.attempts),
|
|
413
|
-
|
|
413
|
+
data.error_message ?? void 0,
|
|
414
414
|
data.published_at ? DateTime.create(data.published_at) : void 0,
|
|
415
415
|
data.last_attempt ? DateTime.create(data.last_attempt) : void 0,
|
|
416
416
|
data.created_at ? DateTime.create(data.created_at) : void 0
|
|
@@ -503,6 +503,297 @@ _Currency.ARS = new _Currency("ARS");
|
|
|
503
503
|
_Currency.BRL = new _Currency("BRL");
|
|
504
504
|
var Currency = _Currency;
|
|
505
505
|
|
|
506
|
+
// src/utils/StringVars.ts
|
|
507
|
+
var StringVars = class {
|
|
508
|
+
static parse(str, ob) {
|
|
509
|
+
const regex = /{{(.*?)}}/g;
|
|
510
|
+
return str.replace(regex, (match, variable) => {
|
|
511
|
+
if (ob.hasOwnProperty(variable.trim())) {
|
|
512
|
+
return ob[variable.trim()];
|
|
513
|
+
} else {
|
|
514
|
+
return match;
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
// src/infrastructure/errors/ErrorManager.ts
|
|
521
|
+
var _ErrorManager = class _ErrorManager {
|
|
522
|
+
constructor(logger = null) {
|
|
523
|
+
this.logger = logger;
|
|
524
|
+
}
|
|
525
|
+
getDefaultMessage(lang) {
|
|
526
|
+
return _ErrorManager.DEFAULT_MESSAGES[lang.value] || _ErrorManager.DEFAULT_MESSAGES[lang.base()] || "error";
|
|
527
|
+
}
|
|
528
|
+
onFatal(err, lang) {
|
|
529
|
+
this.logger?.fatal(err.type, err.message);
|
|
530
|
+
return { status: "ERROR", message: this.getDefaultMessage(lang) };
|
|
531
|
+
}
|
|
532
|
+
onInternal(err, lang) {
|
|
533
|
+
this.logger?.error(err.type, err.message);
|
|
534
|
+
return { status: "ERROR", message: this.getDefaultMessage(lang) };
|
|
535
|
+
}
|
|
536
|
+
onUsage(err, lang) {
|
|
537
|
+
const tmpl = _ErrorManager.TEMPLATES.get(err.type);
|
|
538
|
+
if (!tmpl) {
|
|
539
|
+
this.logger?.error("TEMPLATE_NOT_FOUND", `${err.type}`);
|
|
540
|
+
return { status: "ERROR", message: this.getDefaultMessage(lang) };
|
|
541
|
+
}
|
|
542
|
+
const code = lang.value;
|
|
543
|
+
const base = lang.base();
|
|
544
|
+
const rawMsg = tmpl.languages[code] ?? tmpl.languages[base] ?? this.getDefaultMessage(lang);
|
|
545
|
+
return {
|
|
546
|
+
status: 400,
|
|
547
|
+
message: StringVars.parse(rawMsg, err.vars)
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
onUnknown(err, lang) {
|
|
551
|
+
this.logger?.error("UNKNOWN_ERROR", err.message);
|
|
552
|
+
return { status: "ERROR", message: this.getDefaultMessage(lang) };
|
|
553
|
+
}
|
|
554
|
+
handle(err, lang) {
|
|
555
|
+
if (["local", "dev"].includes(process.env.ENVIRONMENT ?? "")) {
|
|
556
|
+
console.log(err);
|
|
557
|
+
}
|
|
558
|
+
if (err instanceof FatalError) {
|
|
559
|
+
return this.onFatal(err, lang);
|
|
560
|
+
}
|
|
561
|
+
if (err instanceof InternalError) {
|
|
562
|
+
return this.onInternal(err, lang);
|
|
563
|
+
}
|
|
564
|
+
if (err instanceof UsageError) {
|
|
565
|
+
return this.onUsage(err, lang);
|
|
566
|
+
}
|
|
567
|
+
return this.onUnknown(err, lang);
|
|
568
|
+
}
|
|
569
|
+
static addTemplate(template) {
|
|
570
|
+
_ErrorManager.TEMPLATES.set(template.type, template);
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
_ErrorManager.DEFAULT_MESSAGES = {
|
|
574
|
+
"es": "Ups, hemos encontrado un error. Nuestro equipo ya est\xE1 trabajando para solucionarlo",
|
|
575
|
+
"en": "Ups, we found an error. Our team is working on it.",
|
|
576
|
+
"pt": "Ops, encontramos um bug. Nossa equipe j\xE1 est\xE1 trabalhando para resolver isso."
|
|
577
|
+
};
|
|
578
|
+
_ErrorManager.APP_ERRORS = {
|
|
579
|
+
UNDEFINED: "UNDEFINED_ERROR",
|
|
580
|
+
PROCESS: "PROCESS_ERROR",
|
|
581
|
+
DATABASE: "DATABASE_ERROR"
|
|
582
|
+
};
|
|
583
|
+
_ErrorManager.TEMPLATES = /* @__PURE__ */ new Map();
|
|
584
|
+
var ErrorManager = _ErrorManager;
|
|
585
|
+
|
|
586
|
+
// src/domain/value-objects/Country.ts
|
|
587
|
+
ErrorManager.addTemplate({
|
|
588
|
+
type: "INVALID_COUNTRY",
|
|
589
|
+
languages: {
|
|
590
|
+
"es": "El pa\xEDs <{{country}}> no es v\xE1lido o no est\xE1 soportado",
|
|
591
|
+
"en": "Country <{{country}}> is not valid or not supported"
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
ErrorManager.addTemplate({
|
|
595
|
+
type: "COUNTRY_NOT_FOUND_BY_ALPHA2",
|
|
596
|
+
languages: {
|
|
597
|
+
"es": "No se encontr\xF3 pa\xEDs con c\xF3digo alpha2 <{{alpha2}}>",
|
|
598
|
+
"en": "Country not found with alpha2 code <{{alpha2}}>"
|
|
599
|
+
}
|
|
600
|
+
});
|
|
601
|
+
ErrorManager.addTemplate({
|
|
602
|
+
type: "COUNTRY_NOT_FOUND_BY_UUID",
|
|
603
|
+
languages: {
|
|
604
|
+
"es": "No se encontr\xF3 pa\xEDs con UUID <{{uuid}}>",
|
|
605
|
+
"en": "Country not found with UUID <{{uuid}}>"
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
var _Country = class _Country extends ValueObject {
|
|
609
|
+
constructor(country) {
|
|
610
|
+
const normalizedCountry = country.toUpperCase().trim();
|
|
611
|
+
super(normalizedCountry);
|
|
612
|
+
this._name = normalizedCountry;
|
|
613
|
+
this._alpha2 = _Country.COUNTRIES[normalizedCountry].alpha2;
|
|
614
|
+
this._alpha3 = _Country.COUNTRIES[normalizedCountry].alpha3;
|
|
615
|
+
this._numeric = _Country.COUNTRIES[normalizedCountry].numeric;
|
|
616
|
+
this._uuid = _Country.COUNTRIES[normalizedCountry].uuid;
|
|
617
|
+
this._phoneCode = _Country.COUNTRIES[normalizedCountry].phoneCode;
|
|
618
|
+
this._url = _Country.COUNTRIES[normalizedCountry].url;
|
|
619
|
+
}
|
|
620
|
+
validate(country) {
|
|
621
|
+
if (!_Country.NAMES.includes(country)) {
|
|
622
|
+
throw new UsageError("INVALID_COUNTRY", { country });
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
name() {
|
|
626
|
+
return this._name;
|
|
627
|
+
}
|
|
628
|
+
alpha2() {
|
|
629
|
+
return this._alpha2;
|
|
630
|
+
}
|
|
631
|
+
alpha3() {
|
|
632
|
+
return this._alpha3;
|
|
633
|
+
}
|
|
634
|
+
numeric() {
|
|
635
|
+
return this._numeric;
|
|
636
|
+
}
|
|
637
|
+
uuid() {
|
|
638
|
+
return this._uuid;
|
|
639
|
+
}
|
|
640
|
+
phoneCode() {
|
|
641
|
+
return this._phoneCode;
|
|
642
|
+
}
|
|
643
|
+
url() {
|
|
644
|
+
return this._url;
|
|
645
|
+
}
|
|
646
|
+
static findCountryByAlpha2(alpha2) {
|
|
647
|
+
for (const [country, codes] of Object.entries(_Country.COUNTRIES)) {
|
|
648
|
+
if (codes.alpha2 === alpha2.toUpperCase()) {
|
|
649
|
+
return new _Country(country);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
throw new UsageError("COUNTRY_NOT_FOUND_BY_ALPHA2", { alpha2 });
|
|
653
|
+
}
|
|
654
|
+
static findCountryByUUID(uuid) {
|
|
655
|
+
for (const [country, codes] of Object.entries(_Country.COUNTRIES)) {
|
|
656
|
+
if (codes.uuid === uuid) {
|
|
657
|
+
return new _Country(country);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
throw new UsageError("COUNTRY_NOT_FOUND_BY_UUID", { uuid });
|
|
661
|
+
}
|
|
662
|
+
static create(country) {
|
|
663
|
+
return new _Country(country);
|
|
664
|
+
}
|
|
665
|
+
static createOrDefault(country) {
|
|
666
|
+
try {
|
|
667
|
+
return new _Country(country);
|
|
668
|
+
} catch (error) {
|
|
669
|
+
return _Country.DEFAULT;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
toPrimitives() {
|
|
673
|
+
return {
|
|
674
|
+
value: this.value,
|
|
675
|
+
name: this._name,
|
|
676
|
+
alpha2: this._alpha2,
|
|
677
|
+
alpha3: this._alpha3,
|
|
678
|
+
numeric: this._numeric,
|
|
679
|
+
uuid: this._uuid,
|
|
680
|
+
phoneCode: this._phoneCode,
|
|
681
|
+
url: this._url
|
|
682
|
+
};
|
|
683
|
+
}
|
|
684
|
+
static isValid(country) {
|
|
685
|
+
try {
|
|
686
|
+
_Country.create(country);
|
|
687
|
+
return true;
|
|
688
|
+
} catch {
|
|
689
|
+
return false;
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
};
|
|
693
|
+
_Country.COUNTRIES = {
|
|
694
|
+
URUGUAY: {
|
|
695
|
+
alpha2: "UY",
|
|
696
|
+
alpha3: "URY",
|
|
697
|
+
numeric: "858",
|
|
698
|
+
phoneCode: "+598",
|
|
699
|
+
uuid: "5739ecc0-d12b-4db5-8897-e03a04a95c72",
|
|
700
|
+
url: "https://dev-wonasports.s3.us-east-2.amazonaws.com/assets/uruguay.png"
|
|
701
|
+
},
|
|
702
|
+
ARGENTINA: {
|
|
703
|
+
alpha2: "AR",
|
|
704
|
+
alpha3: "ARG",
|
|
705
|
+
numeric: "032",
|
|
706
|
+
phoneCode: "+54",
|
|
707
|
+
uuid: "66663efe-ab7a-4166-b971-9f36fd0ea6b2",
|
|
708
|
+
url: "https://dev-wonasports.s3.us-east-2.amazonaws.com/assets/argentina.png"
|
|
709
|
+
},
|
|
710
|
+
ECUADOR: {
|
|
711
|
+
alpha2: "EC",
|
|
712
|
+
alpha3: "ECU",
|
|
713
|
+
numeric: "218",
|
|
714
|
+
phoneCode: "+593",
|
|
715
|
+
uuid: "ee109239-0150-4e5f-9ff2-a85f270092b1",
|
|
716
|
+
url: "https://dev-wonasports.s3.us-east-2.amazonaws.com/assets/ecuador.png"
|
|
717
|
+
},
|
|
718
|
+
PERU: {
|
|
719
|
+
alpha2: "PE",
|
|
720
|
+
alpha3: "PER",
|
|
721
|
+
numeric: "604",
|
|
722
|
+
phoneCode: "+51",
|
|
723
|
+
uuid: "e4d61ef5-b92d-4f9c-8ec1-23f4beb50abd",
|
|
724
|
+
url: "https://dev-wonasports.s3.us-east-2.amazonaws.com/assets/peru.png"
|
|
725
|
+
},
|
|
726
|
+
BRASIL: {
|
|
727
|
+
alpha2: "BR",
|
|
728
|
+
alpha3: "BRA",
|
|
729
|
+
numeric: "076",
|
|
730
|
+
phoneCode: "+55",
|
|
731
|
+
uuid: "b7b91d72-deaf-4641-957c-a65003e33104",
|
|
732
|
+
url: "https://dev-wonasports.s3.us-east-2.amazonaws.com/assets/brasil.png"
|
|
733
|
+
},
|
|
734
|
+
CHILE: {
|
|
735
|
+
alpha2: "CL",
|
|
736
|
+
alpha3: "CHL",
|
|
737
|
+
numeric: "152",
|
|
738
|
+
phoneCode: "+56",
|
|
739
|
+
uuid: "f69b35f4-d734-4c76-866c-29a18bf000fb",
|
|
740
|
+
url: "https://dev-wonasports.s3.us-east-2.amazonaws.com/assets/chile.png"
|
|
741
|
+
},
|
|
742
|
+
VENEZUELA: {
|
|
743
|
+
alpha2: "VE",
|
|
744
|
+
alpha3: "VEN",
|
|
745
|
+
numeric: "862",
|
|
746
|
+
phoneCode: "+58",
|
|
747
|
+
uuid: "31b6c591-63f6-43db-8ea9-829bb03746c5",
|
|
748
|
+
url: "https://dev-wonasports.s3.us-east-2.amazonaws.com/assets/venezuela.png"
|
|
749
|
+
},
|
|
750
|
+
COLOMBIA: {
|
|
751
|
+
alpha2: "CO",
|
|
752
|
+
alpha3: "COL",
|
|
753
|
+
numeric: "170",
|
|
754
|
+
phoneCode: "+57",
|
|
755
|
+
uuid: "6fdfe34b-6726-4604-96af-665ea5fc9239",
|
|
756
|
+
url: "https://dev-wonasports.s3.us-east-2.amazonaws.com/assets/colombia.png"
|
|
757
|
+
},
|
|
758
|
+
BOLIVIA: {
|
|
759
|
+
alpha2: "BO",
|
|
760
|
+
alpha3: "BOL",
|
|
761
|
+
numeric: "068",
|
|
762
|
+
phoneCode: "+591",
|
|
763
|
+
uuid: "948886db-c280-4ba7-a777-a76c180b295b",
|
|
764
|
+
url: "https://dev-wonasports.s3.us-east-2.amazonaws.com/assets/bolivia.png"
|
|
765
|
+
},
|
|
766
|
+
PARAGUAY: {
|
|
767
|
+
alpha2: "PY",
|
|
768
|
+
alpha3: "PRY",
|
|
769
|
+
numeric: "600",
|
|
770
|
+
phoneCode: "+595",
|
|
771
|
+
uuid: "d67b3472-e38d-4900-8ae3-02d99cd1d884",
|
|
772
|
+
url: "https://dev-wonasports.s3.us-east-2.amazonaws.com/assets/paraguay.png"
|
|
773
|
+
},
|
|
774
|
+
USA: {
|
|
775
|
+
alpha2: "US",
|
|
776
|
+
alpha3: "USA",
|
|
777
|
+
numeric: "840",
|
|
778
|
+
phoneCode: "+1",
|
|
779
|
+
uuid: "16ac3b9b-8f7b-4c54-91d5-d62c7cd74ad4",
|
|
780
|
+
url: "https://dev-wonasports.s3.us-east-2.amazonaws.com/assets/usa.png"
|
|
781
|
+
}
|
|
782
|
+
};
|
|
783
|
+
_Country.NAMES = Object.keys(_Country.COUNTRIES);
|
|
784
|
+
_Country.DEFAULT = new _Country("URUGUAY");
|
|
785
|
+
_Country.ARGENTINA = new _Country("ARGENTINA");
|
|
786
|
+
_Country.ECUADOR = new _Country("ECUADOR");
|
|
787
|
+
_Country.PERU = new _Country("PERU");
|
|
788
|
+
_Country.BRASIL = new _Country("BRASIL");
|
|
789
|
+
_Country.CHILE = new _Country("CHILE");
|
|
790
|
+
_Country.VENEZUELA = new _Country("VENEZUELA");
|
|
791
|
+
_Country.COLOMBIA = new _Country("COLOMBIA");
|
|
792
|
+
_Country.BOLIVIA = new _Country("BOLIVIA");
|
|
793
|
+
_Country.PARAGUAY = new _Country("PARAGUAY");
|
|
794
|
+
_Country.USA = new _Country("USA");
|
|
795
|
+
var Country = _Country;
|
|
796
|
+
|
|
506
797
|
// src/domain/value-objects/Email.ts
|
|
507
798
|
var _Email = class _Email extends ValueObject {
|
|
508
799
|
constructor(email) {
|
|
@@ -626,91 +917,6 @@ _Language.SPANISH_NICARAGUA = new _Language("es-ni");
|
|
|
626
917
|
_Language.SPANISH_PUERTO_RICO = new _Language("es-pr");
|
|
627
918
|
var Language = _Language;
|
|
628
919
|
|
|
629
|
-
// src/utils/StringVars.ts
|
|
630
|
-
var StringVars = class {
|
|
631
|
-
static parse(str, ob) {
|
|
632
|
-
const regex = /{{(.*?)}}/g;
|
|
633
|
-
return str.replace(regex, (match, variable) => {
|
|
634
|
-
if (ob.hasOwnProperty(variable.trim())) {
|
|
635
|
-
return ob[variable.trim()];
|
|
636
|
-
} else {
|
|
637
|
-
return match;
|
|
638
|
-
}
|
|
639
|
-
});
|
|
640
|
-
}
|
|
641
|
-
};
|
|
642
|
-
|
|
643
|
-
// src/infrastructure/errors/ErrorManager.ts
|
|
644
|
-
var _ErrorManager = class _ErrorManager {
|
|
645
|
-
constructor(logger = null) {
|
|
646
|
-
this.logger = logger;
|
|
647
|
-
}
|
|
648
|
-
getDefaultMessage(lang) {
|
|
649
|
-
return _ErrorManager.DEFAULT_MESSAGES[lang.value] || _ErrorManager.DEFAULT_MESSAGES[lang.base()] || "error";
|
|
650
|
-
}
|
|
651
|
-
onFatal(err, lang) {
|
|
652
|
-
var _a;
|
|
653
|
-
(_a = this.logger) == null ? void 0 : _a.fatal(err.type, err.message);
|
|
654
|
-
return { status: "ERROR", message: this.getDefaultMessage(lang) };
|
|
655
|
-
}
|
|
656
|
-
onInternal(err, lang) {
|
|
657
|
-
var _a;
|
|
658
|
-
(_a = this.logger) == null ? void 0 : _a.error(err.type, err.message);
|
|
659
|
-
return { status: "ERROR", message: this.getDefaultMessage(lang) };
|
|
660
|
-
}
|
|
661
|
-
onUsage(err, lang) {
|
|
662
|
-
var _a, _b, _c;
|
|
663
|
-
const tmpl = _ErrorManager.TEMPLATES.get(err.type);
|
|
664
|
-
if (!tmpl) {
|
|
665
|
-
(_a = this.logger) == null ? void 0 : _a.error("TEMPLATE_NOT_FOUND", `${err.type}`);
|
|
666
|
-
return { status: "ERROR", message: this.getDefaultMessage(lang) };
|
|
667
|
-
}
|
|
668
|
-
const code = lang.value;
|
|
669
|
-
const base = lang.base();
|
|
670
|
-
const rawMsg = (_c = (_b = tmpl.languages[code]) != null ? _b : tmpl.languages[base]) != null ? _c : this.getDefaultMessage(lang);
|
|
671
|
-
return {
|
|
672
|
-
status: 400,
|
|
673
|
-
message: StringVars.parse(rawMsg, err.vars)
|
|
674
|
-
};
|
|
675
|
-
}
|
|
676
|
-
onUnknown(err, lang) {
|
|
677
|
-
var _a;
|
|
678
|
-
(_a = this.logger) == null ? void 0 : _a.error("UNKNOWN_ERROR", err.message);
|
|
679
|
-
return { status: "ERROR", message: this.getDefaultMessage(lang) };
|
|
680
|
-
}
|
|
681
|
-
handle(err, lang) {
|
|
682
|
-
var _a;
|
|
683
|
-
if (["local", "dev"].includes((_a = process.env.ENVIRONMENT) != null ? _a : "")) {
|
|
684
|
-
console.log(err);
|
|
685
|
-
}
|
|
686
|
-
if (err instanceof FatalError) {
|
|
687
|
-
return this.onFatal(err, lang);
|
|
688
|
-
}
|
|
689
|
-
if (err instanceof InternalError) {
|
|
690
|
-
return this.onInternal(err, lang);
|
|
691
|
-
}
|
|
692
|
-
if (err instanceof UsageError) {
|
|
693
|
-
return this.onUsage(err, lang);
|
|
694
|
-
}
|
|
695
|
-
return this.onUnknown(err, lang);
|
|
696
|
-
}
|
|
697
|
-
static addTemplate(template) {
|
|
698
|
-
_ErrorManager.TEMPLATES.set(template.type, template);
|
|
699
|
-
}
|
|
700
|
-
};
|
|
701
|
-
_ErrorManager.DEFAULT_MESSAGES = {
|
|
702
|
-
"es": "Ups, hemos encontrado un error. Nuestro equipo ya est\xE1 trabajando para solucionarlo",
|
|
703
|
-
"en": "Ups, we found an error. Our team is working on it.",
|
|
704
|
-
"pt": "Ops, encontramos um bug. Nossa equipe j\xE1 est\xE1 trabalhando para resolver isso."
|
|
705
|
-
};
|
|
706
|
-
_ErrorManager.APP_ERRORS = {
|
|
707
|
-
UNDEFINED: "UNDEFINED_ERROR",
|
|
708
|
-
PROCESS: "PROCESS_ERROR",
|
|
709
|
-
DATABASE: "DATABASE_ERROR"
|
|
710
|
-
};
|
|
711
|
-
_ErrorManager.TEMPLATES = /* @__PURE__ */ new Map();
|
|
712
|
-
var ErrorManager = _ErrorManager;
|
|
713
|
-
|
|
714
920
|
// src/domain/value-objects/Price.ts
|
|
715
921
|
ErrorManager.addTemplate({
|
|
716
922
|
type: "INVALID_PRICE_AMOUNT",
|
|
@@ -918,14 +1124,70 @@ var BasicUnitOfWorkFactory = class {
|
|
|
918
1124
|
}
|
|
919
1125
|
};
|
|
920
1126
|
|
|
1127
|
+
// src/infrastructure/contracts/EventManager.ts
|
|
1128
|
+
var EventManager = class {
|
|
1129
|
+
constructor(connection) {
|
|
1130
|
+
this._connection = connection;
|
|
1131
|
+
this._topics = [];
|
|
1132
|
+
this._callbackList = {};
|
|
1133
|
+
this._onStart = null;
|
|
1134
|
+
this._onConnected = null;
|
|
1135
|
+
this._onSubscribe = null;
|
|
1136
|
+
this._onMessage = null;
|
|
1137
|
+
this._onError = null;
|
|
1138
|
+
this._onCrash = null;
|
|
1139
|
+
this._onReconnect = null;
|
|
1140
|
+
}
|
|
1141
|
+
async execRoute(topic, message) {
|
|
1142
|
+
if (this._callbackList[topic]) {
|
|
1143
|
+
await this._callbackList[topic](message);
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
async execCallback(callback, data) {
|
|
1147
|
+
if (callback) {
|
|
1148
|
+
await callback(data);
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
route(topic, callback) {
|
|
1152
|
+
this._topics.push(topic);
|
|
1153
|
+
this._callbackList[topic] = callback;
|
|
1154
|
+
}
|
|
1155
|
+
onStart(callback) {
|
|
1156
|
+
this._onStart = callback;
|
|
1157
|
+
}
|
|
1158
|
+
onConnected(callback) {
|
|
1159
|
+
this._onConnected = callback;
|
|
1160
|
+
}
|
|
1161
|
+
onSubscribe(callback) {
|
|
1162
|
+
this._onSubscribe = callback;
|
|
1163
|
+
}
|
|
1164
|
+
onMessage(callback) {
|
|
1165
|
+
this._onMessage = callback;
|
|
1166
|
+
}
|
|
1167
|
+
onError(callback) {
|
|
1168
|
+
this._onError = callback;
|
|
1169
|
+
}
|
|
1170
|
+
onCrash(callback) {
|
|
1171
|
+
this._onCrash = callback;
|
|
1172
|
+
}
|
|
1173
|
+
onReconnect(callback) {
|
|
1174
|
+
this._onReconnect = callback;
|
|
1175
|
+
}
|
|
1176
|
+
get topics() {
|
|
1177
|
+
return this._topics;
|
|
1178
|
+
}
|
|
1179
|
+
get callbackList() {
|
|
1180
|
+
return this._callbackList;
|
|
1181
|
+
}
|
|
1182
|
+
};
|
|
1183
|
+
|
|
921
1184
|
// src/infrastructure/mysql/Mysql.ts
|
|
922
1185
|
var import_promise = require("mysql2/promise");
|
|
923
1186
|
var _MysqlConnector = class _MysqlConnector {
|
|
924
1187
|
constructor(pool) {
|
|
925
|
-
|
|
926
|
-
this._pool = pool != null ? pool : (0, import_promise.createPool)({
|
|
1188
|
+
this._pool = pool ?? (0, import_promise.createPool)({
|
|
927
1189
|
host: process.env.DB_HOST,
|
|
928
|
-
port: Number(
|
|
1190
|
+
port: Number(process.env.DB_PORT ?? 3306),
|
|
929
1191
|
user: process.env.DB_USER,
|
|
930
1192
|
password: process.env.DB_PASSWORD,
|
|
931
1193
|
database: process.env.DB_DATABASE,
|
|
@@ -1038,7 +1300,6 @@ function toUtcDateTimeString(raw) {
|
|
|
1038
1300
|
return `${d.getUTCFullYear()}-${pad(d.getUTCMonth() + 1)}-${pad(d.getUTCDate())} ${pad(d.getUTCHours())}:${pad(d.getUTCMinutes())}:${pad(d.getUTCSeconds())}`;
|
|
1039
1301
|
}
|
|
1040
1302
|
function coerceScalar(key, v) {
|
|
1041
|
-
var _a;
|
|
1042
1303
|
if (v == null)
|
|
1043
1304
|
return void 0;
|
|
1044
1305
|
if (typeof v !== "string")
|
|
@@ -1059,7 +1320,7 @@ function coerceScalar(key, v) {
|
|
|
1059
1320
|
return n;
|
|
1060
1321
|
}
|
|
1061
1322
|
if (DATE_KEY_RE.test(key)) {
|
|
1062
|
-
return
|
|
1323
|
+
return toUtcDateTimeString(s) ?? s;
|
|
1063
1324
|
}
|
|
1064
1325
|
return s;
|
|
1065
1326
|
}
|
|
@@ -1086,8 +1347,7 @@ function hasHeader(headers, name) {
|
|
|
1086
1347
|
}
|
|
1087
1348
|
function adaptExpressRoute(Controller) {
|
|
1088
1349
|
return async (req, res, next) => {
|
|
1089
|
-
|
|
1090
|
-
const rawLangHeader = (_b = (_a = req.headers["accept-language"]) != null ? _a : req.headers["Accept-Language"]) != null ? _b : "es";
|
|
1350
|
+
const rawLangHeader = req.headers["accept-language"] ?? req.headers["Accept-Language"] ?? "es";
|
|
1091
1351
|
const rawLang = Array.isArray(rawLangHeader) ? rawLangHeader[0] : rawLangHeader || "";
|
|
1092
1352
|
const lang = rawLang.split(",")[0].split(";")[0].trim().toLowerCase();
|
|
1093
1353
|
const httpRequest = {
|
|
@@ -1135,7 +1395,7 @@ function adaptExpressRoute(Controller) {
|
|
|
1135
1395
|
res.status(statusCode).send(body);
|
|
1136
1396
|
return;
|
|
1137
1397
|
}
|
|
1138
|
-
res.status(statusCode).json(body
|
|
1398
|
+
res.status(statusCode).json(body ?? {});
|
|
1139
1399
|
} catch (err) {
|
|
1140
1400
|
next(err);
|
|
1141
1401
|
}
|
|
@@ -1143,9 +1403,8 @@ function adaptExpressRoute(Controller) {
|
|
|
1143
1403
|
}
|
|
1144
1404
|
function adaptExpressErrorHandler(errorManager) {
|
|
1145
1405
|
return (err, req, res, next) => {
|
|
1146
|
-
|
|
1147
|
-
const
|
|
1148
|
-
const rawLang = Array.isArray(raw) ? raw[0] : raw != null ? raw : "";
|
|
1406
|
+
const raw = req.headers["accept-language"] ?? req.headers["Accept-Language"] ?? "es";
|
|
1407
|
+
const rawLang = Array.isArray(raw) ? raw[0] : raw ?? "";
|
|
1149
1408
|
const langCode = rawLang.split(",")[0].split(";")[0].trim().toLowerCase();
|
|
1150
1409
|
const result = errorManager.handle(err, Language.create(langCode));
|
|
1151
1410
|
const statusCode = typeof result.status === "number" ? result.status : 500;
|
|
@@ -1159,7 +1418,6 @@ var EventBusMysqlRepository = class {
|
|
|
1159
1418
|
this.connection = connection;
|
|
1160
1419
|
}
|
|
1161
1420
|
eventToRowValues(e) {
|
|
1162
|
-
var _a, _b;
|
|
1163
1421
|
return [
|
|
1164
1422
|
e.eventUuid.value,
|
|
1165
1423
|
e.eventType,
|
|
@@ -1171,8 +1429,8 @@ var EventBusMysqlRepository = class {
|
|
|
1171
1429
|
e.status.value,
|
|
1172
1430
|
e.attempts,
|
|
1173
1431
|
e.errorMessage,
|
|
1174
|
-
|
|
1175
|
-
|
|
1432
|
+
e.publishedAt?.value,
|
|
1433
|
+
e.lastAttempt?.value,
|
|
1176
1434
|
e.createdAt.value
|
|
1177
1435
|
];
|
|
1178
1436
|
}
|
|
@@ -1186,8 +1444,7 @@ var EventBusMysqlRepository = class {
|
|
|
1186
1444
|
);
|
|
1187
1445
|
}
|
|
1188
1446
|
async update(event) {
|
|
1189
|
-
|
|
1190
|
-
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];
|
|
1447
|
+
const values = [event.status.value, event.attempts, event.errorMessage, event.publishedAt?.value, event.lastAttempt?.value, event.eventUuid.value];
|
|
1191
1448
|
await this.connection.query(
|
|
1192
1449
|
`UPDATE events_outbox
|
|
1193
1450
|
SET status = ?,
|
|
@@ -1208,6 +1465,98 @@ var EventBusMysqlRepository = class {
|
|
|
1208
1465
|
}
|
|
1209
1466
|
};
|
|
1210
1467
|
|
|
1468
|
+
// src/infrastructure/kafka/KafkaManager.ts
|
|
1469
|
+
var import_kafkajs = require("kafkajs");
|
|
1470
|
+
var KafkaManager = class extends EventManager {
|
|
1471
|
+
constructor(connection) {
|
|
1472
|
+
super(connection);
|
|
1473
|
+
this.kafka = new import_kafkajs.Kafka({
|
|
1474
|
+
brokers: this._connection.brokers,
|
|
1475
|
+
ssl: true,
|
|
1476
|
+
sasl: {
|
|
1477
|
+
mechanism: "scram-sha-256",
|
|
1478
|
+
username: this._connection.userName,
|
|
1479
|
+
password: this._connection.password
|
|
1480
|
+
},
|
|
1481
|
+
logLevel: import_kafkajs.logLevel.ERROR
|
|
1482
|
+
});
|
|
1483
|
+
this.consumer = this.kafka.consumer({ groupId: process.env.KAFKA_GROUP || "default" });
|
|
1484
|
+
this.producer = this.kafka.producer();
|
|
1485
|
+
}
|
|
1486
|
+
async run(autocommit) {
|
|
1487
|
+
const __run = async () => {
|
|
1488
|
+
await this.execCallback(this._onStart, { message: `--- ${this.constructor.name} started ---` });
|
|
1489
|
+
await this.consumer.connect();
|
|
1490
|
+
await this.execCallback(this._onConnected, { message: `--- ${this.constructor.name} connected to ${this._connection.brokers} ---` });
|
|
1491
|
+
for (let topic of this._topics) {
|
|
1492
|
+
try {
|
|
1493
|
+
await this.consumer.subscribe({ topic, fromBeginning: true });
|
|
1494
|
+
await this.execCallback(this._onSubscribe, { message: `--- ${this.constructor.name} subscribed to ${topic} ---` });
|
|
1495
|
+
} catch (error) {
|
|
1496
|
+
await this.execCallback(this._onConnected, { message: `Error on subscribe to kafka topic: ${topic}` });
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
await this.consumer.run({
|
|
1500
|
+
autoCommit: autocommit,
|
|
1501
|
+
// @ts-ignore
|
|
1502
|
+
eachMessage: async ({ topic, partition, message, heartbeat }) => {
|
|
1503
|
+
try {
|
|
1504
|
+
await this.execCallback(this._onMessage, `[New message detected for ${topic}]: ${message.value?.toString()}`);
|
|
1505
|
+
const evt = JSON.parse(String(message.value?.toString()));
|
|
1506
|
+
await this.execRoute(topic, evt);
|
|
1507
|
+
const next = (BigInt(message.offset) + 1n).toString();
|
|
1508
|
+
await this.consumer.commitOffsets([{ topic, partition, offset: next }]);
|
|
1509
|
+
await heartbeat();
|
|
1510
|
+
} catch (error) {
|
|
1511
|
+
await this.execCallback(this._onError, error);
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
});
|
|
1515
|
+
};
|
|
1516
|
+
try {
|
|
1517
|
+
await __run();
|
|
1518
|
+
} catch (error) {
|
|
1519
|
+
await this.execCallback(this._onError, new InternalError(ErrorManager.APP_ERRORS.PROCESS, error.toString()));
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
async send(message) {
|
|
1523
|
+
if (!message.producer) {
|
|
1524
|
+
message.producer = process.env.NAME && process.env.ENVIRONMENT ? `${process.env.NAME}-${process.env.ENVIRONMENT}` : "unknown";
|
|
1525
|
+
}
|
|
1526
|
+
if (!message.date) {
|
|
1527
|
+
message.date = DateTime.now().value;
|
|
1528
|
+
}
|
|
1529
|
+
try {
|
|
1530
|
+
if (!this.producer) {
|
|
1531
|
+
throw new InternalError(ErrorManager.APP_ERRORS.PROCESS, "Producer not initialized");
|
|
1532
|
+
}
|
|
1533
|
+
await this.producer.connect();
|
|
1534
|
+
await this.producer.send({
|
|
1535
|
+
topic: message.topic,
|
|
1536
|
+
messages: [{ value: JSON.stringify({ producer: message.producer, data: message }) }]
|
|
1537
|
+
});
|
|
1538
|
+
await this.producer.disconnect();
|
|
1539
|
+
} catch (error) {
|
|
1540
|
+
await this.execCallback(this._onError, new InternalError(ErrorManager.APP_ERRORS.PROCESS, error.toString()));
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
async start(autocommit = false) {
|
|
1544
|
+
this.consumer.on(this.consumer.events.CRASH, async (error) => {
|
|
1545
|
+
await this.execCallback(this._onError, new InternalError(ErrorManager.APP_ERRORS.PROCESS, error.payload.error.stack));
|
|
1546
|
+
});
|
|
1547
|
+
await this.run(autocommit);
|
|
1548
|
+
}
|
|
1549
|
+
async pause() {
|
|
1550
|
+
}
|
|
1551
|
+
async restart() {
|
|
1552
|
+
await this.consumer.stop();
|
|
1553
|
+
await this.start();
|
|
1554
|
+
}
|
|
1555
|
+
async stop() {
|
|
1556
|
+
await this.consumer.stop();
|
|
1557
|
+
}
|
|
1558
|
+
};
|
|
1559
|
+
|
|
1211
1560
|
// src/utils/ExchangeRates.ts
|
|
1212
1561
|
var ExchangeRates = class _ExchangeRates extends BaseObject {
|
|
1213
1562
|
constructor(props) {
|
|
@@ -1252,11 +1601,10 @@ var ExchangeRates = class _ExchangeRates extends BaseObject {
|
|
|
1252
1601
|
return new _ExchangeRates(props);
|
|
1253
1602
|
}
|
|
1254
1603
|
static createFromPrimitives(data) {
|
|
1255
|
-
var _a, _b;
|
|
1256
1604
|
return _ExchangeRates.create({
|
|
1257
1605
|
base: Currency.create(data.base),
|
|
1258
|
-
rates:
|
|
1259
|
-
date: DateTime.create(
|
|
1606
|
+
rates: data.rates ?? [],
|
|
1607
|
+
date: DateTime.create(data.date ?? "")
|
|
1260
1608
|
});
|
|
1261
1609
|
}
|
|
1262
1610
|
};
|
|
@@ -1265,6 +1613,7 @@ var ExchangeRates = class _ExchangeRates extends BaseObject {
|
|
|
1265
1613
|
BaseObject,
|
|
1266
1614
|
BasicUnitOfWork,
|
|
1267
1615
|
BasicUnitOfWorkFactory,
|
|
1616
|
+
Country,
|
|
1268
1617
|
Currency,
|
|
1269
1618
|
DateTime,
|
|
1270
1619
|
DomainEntity,
|
|
@@ -1275,11 +1624,13 @@ var ExchangeRates = class _ExchangeRates extends BaseObject {
|
|
|
1275
1624
|
ErrorManager,
|
|
1276
1625
|
EventBus,
|
|
1277
1626
|
EventBusMysqlRepository,
|
|
1627
|
+
EventManager,
|
|
1278
1628
|
ExchangeRates,
|
|
1279
1629
|
FatalError,
|
|
1280
1630
|
HttpHealthCheckController,
|
|
1281
1631
|
HttpNotFoundController,
|
|
1282
1632
|
InternalError,
|
|
1633
|
+
KafkaManager,
|
|
1283
1634
|
Language,
|
|
1284
1635
|
MysqlConnection,
|
|
1285
1636
|
MysqlConnector,
|