wsp-ms-core 1.0.36 → 1.0.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -19,6 +19,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  // src/index.ts
20
20
  var src_exports = {};
21
21
  __export(src_exports, {
22
+ BaseObject: () => BaseObject,
22
23
  BasicUnitOfWork: () => BasicUnitOfWork,
23
24
  BasicUnitOfWorkFactory: () => BasicUnitOfWorkFactory,
24
25
  Currency: () => Currency,
@@ -54,6 +55,9 @@ var ValueObject = class {
54
55
  this.validate(value);
55
56
  this._value = Object.freeze(value);
56
57
  }
58
+ toProps() {
59
+ return this._value;
60
+ }
57
61
  get value() {
58
62
  return this._value;
59
63
  }
@@ -147,6 +151,11 @@ var _DateTime = class _DateTime extends ValueObject {
147
151
  getWeekdayName(locale = "en") {
148
152
  return this._dt.setLocale(locale).toFormat("cccc");
149
153
  }
154
+ toPrimitives() {
155
+ return {
156
+ value: this.value
157
+ };
158
+ }
150
159
  static create(input) {
151
160
  if (input === void 0) {
152
161
  return new _DateTime(_DateTime.toUtcFormat(import_luxon.DateTime.now()));
@@ -216,6 +225,13 @@ var DomainEvent = class {
216
225
  }
217
226
  };
218
227
 
228
+ // src/domain/contracts/BaseObject.ts
229
+ var BaseObject = class {
230
+ constructor(props) {
231
+ this.props = props;
232
+ }
233
+ };
234
+
219
235
  // src/domain/errors/FatalError.ts
220
236
  var FatalError = class extends DomainError {
221
237
  constructor(type, message = "") {
@@ -253,6 +269,11 @@ var _Currency = class _Currency extends ValueObject {
253
269
  throw new Error(`Currency <${code}> is not supported`);
254
270
  }
255
271
  }
272
+ toPrimitives() {
273
+ return {
274
+ value: this.value
275
+ };
276
+ }
256
277
  static create(raw) {
257
278
  if (typeof raw === "number" || _Currency.NUM_REGEX.test(raw)) {
258
279
  const num = Number(raw);
@@ -306,6 +327,11 @@ var _Email = class _Email extends ValueObject {
306
327
  throw new Error(`Email <${value}> is not a valid address`);
307
328
  }
308
329
  }
330
+ toPrimitives() {
331
+ return {
332
+ value: this.value
333
+ };
334
+ }
309
335
  static create(raw) {
310
336
  return new _Email(raw);
311
337
  }
@@ -323,12 +349,18 @@ var _Language = class _Language extends ValueObject {
323
349
  }
324
350
  validate(value) {
325
351
  if (!_Language.SUPPORTED.includes(value)) {
326
- throw new Error(`Language <${value}> is not supported`);
352
+ throw new InternalError(`Language <${value}> is not supported`);
327
353
  }
328
354
  }
329
355
  base() {
330
356
  return this.value.split("-")[0];
331
357
  }
358
+ toPrimitives() {
359
+ return {
360
+ base: this.base(),
361
+ value: this.value
362
+ };
363
+ }
332
364
  static create(raw) {
333
365
  const normalized = raw.trim().toLowerCase().replace("_", "-");
334
366
  try {
@@ -408,47 +440,144 @@ _Language.SPANISH_NICARAGUA = new _Language("es-ni");
408
440
  _Language.SPANISH_PUERTO_RICO = new _Language("es-pr");
409
441
  var Language = _Language;
410
442
 
443
+ // src/utils/StringVars.ts
444
+ var StringVars = class {
445
+ static parse(str, ob) {
446
+ const regex = /{{(.*?)}}/g;
447
+ return str.replace(regex, (match, variable) => {
448
+ if (ob.hasOwnProperty(variable.trim())) {
449
+ return ob[variable.trim()];
450
+ } else {
451
+ return match;
452
+ }
453
+ });
454
+ }
455
+ };
456
+
457
+ // src/infrastructure/errors/ErrorManager.ts
458
+ var _ErrorManager = class _ErrorManager {
459
+ constructor(logger = null) {
460
+ this.logger = logger;
461
+ }
462
+ getDefaultMessage(lang) {
463
+ return _ErrorManager.DEFAULT_MESSAGES[lang.value] || _ErrorManager.DEFAULT_MESSAGES[lang.base()] || "error";
464
+ }
465
+ onFatal(err, lang) {
466
+ var _a;
467
+ (_a = this.logger) == null ? void 0 : _a.fatal(err.type, err.message);
468
+ return { status: "ERROR", message: this.getDefaultMessage(lang) };
469
+ }
470
+ onInternal(err, lang) {
471
+ var _a;
472
+ (_a = this.logger) == null ? void 0 : _a.error(err.type, err.message);
473
+ return { status: "ERROR", message: this.getDefaultMessage(lang) };
474
+ }
475
+ onUsage(err, lang) {
476
+ var _a, _b, _c;
477
+ const tmpl = _ErrorManager.TEMPLATES.get(err.type);
478
+ if (!tmpl) {
479
+ (_a = this.logger) == null ? void 0 : _a.error("TEMPLATE_NOT_FOUND", `${err.type}`);
480
+ return { status: "ERROR", message: this.getDefaultMessage(lang) };
481
+ }
482
+ const code = lang.value;
483
+ const base = lang.base();
484
+ const rawMsg = (_c = (_b = tmpl.languages[code]) != null ? _b : tmpl.languages[base]) != null ? _c : this.getDefaultMessage(lang);
485
+ return {
486
+ status: "ERROR",
487
+ message: StringVars.parse(rawMsg, err.vars)
488
+ };
489
+ }
490
+ onUnknown(err, lang) {
491
+ var _a;
492
+ (_a = this.logger) == null ? void 0 : _a.error("UNKNOWN_ERROR", err.message);
493
+ return { status: "ERROR", message: this.getDefaultMessage(lang) };
494
+ }
495
+ handle(err, lang) {
496
+ var _a;
497
+ if (["local", "dev"].includes((_a = process.env.ENVIRONMENT) != null ? _a : "")) {
498
+ console.log(err);
499
+ }
500
+ if (err instanceof FatalError) {
501
+ return this.onFatal(err, lang);
502
+ }
503
+ if (err instanceof InternalError) {
504
+ return this.onInternal(err, lang);
505
+ }
506
+ if (err instanceof UsageError) {
507
+ return this.onUsage(err, lang);
508
+ }
509
+ return this.onUnknown(err, lang);
510
+ }
511
+ static addTemplate(template) {
512
+ _ErrorManager.TEMPLATES.set(template.type, template);
513
+ }
514
+ };
515
+ _ErrorManager.DEFAULT_MESSAGES = {
516
+ "es": "Ups, hemos encontrado un error. Nuestro equipo ya est\xE1 trabajando para solucionarlo",
517
+ "en": "Ups, we found an error. Our team is working on it.",
518
+ "pt": "Ops, encontramos um bug. Nossa equipe j\xE1 est\xE1 trabalhando para resolver isso."
519
+ };
520
+ _ErrorManager.APP_ERRORS = {
521
+ UNDEFINED: "UNDEFINED_ERROR",
522
+ PROCESS: "PROCESS_ERROR",
523
+ DATABASE: "DATABASE_ERROR"
524
+ };
525
+ _ErrorManager.TEMPLATES = /* @__PURE__ */ new Map();
526
+ var ErrorManager = _ErrorManager;
527
+
411
528
  // src/domain/value-objects/Price.ts
529
+ ErrorManager.addTemplate({
530
+ type: "INVALID_PRICE_AMOUNT",
531
+ languages: {
532
+ "es": "El precio <{{amount}}> no es v\xE1lido",
533
+ "en": "Price amount <{{amount}}> is not a valid number"
534
+ }
535
+ });
536
+ ErrorManager.addTemplate({
537
+ type: "INVALID_PRICE_RANGE",
538
+ languages: {
539
+ "es": "El precio <{{amount}}> debe ser \u2265 {{min}} y \u2264 {{max}}",
540
+ "en": "Price amount <{{amount}}> must be \u2265 {{min}} and \u2264 {{max}}"
541
+ }
542
+ });
412
543
  var _Price = class _Price extends ValueObject {
413
544
  constructor(amount, currency) {
414
545
  super({ amount, currency });
415
- this._amount = amount;
416
- this._currency = currency;
417
546
  }
418
547
  validate(props) {
419
548
  const { amount, currency } = props;
420
549
  if (typeof amount !== "number" || Number.isNaN(amount) || !Number.isFinite(amount)) {
421
- throw new Error(`Price amount <${amount}> is not a valid number`);
550
+ throw new UsageError("INVALID_PRICE_AMOUNT", { amount });
422
551
  }
423
- if (amount < _Price.MIN_AMOUNT) {
424
- throw new Error(`Price amount <${amount}> must be \u2265 ${_Price.MIN_AMOUNT}`);
552
+ if (amount < _Price.MIN_AMOUNT || amount > _Price.MAX_AMOUNT) {
553
+ throw new UsageError("INVALID_PRICE_RANGE", { amount, min: _Price.MIN_AMOUNT, max: _Price.MAX_AMOUNT });
425
554
  }
426
555
  }
427
556
  get amount() {
428
- return this._amount;
557
+ return this._value.amount;
429
558
  }
430
559
  get currency() {
431
- return this._currency;
560
+ return this._value.currency;
432
561
  }
433
562
  equals(other) {
434
563
  if (!other)
435
564
  return false;
436
- return this._amount === other._amount && this._currency.equals(other._currency);
565
+ return this._value.amount === other.amount && this.currency.equals(other.currency);
437
566
  }
438
567
  assertSameCurrency(other) {
439
- if (!this._currency.equals(other._currency)) {
568
+ if (!this.currency.equals(other.currency)) {
440
569
  throw new Error("Cannot operate on Price objects with different currencies");
441
570
  }
442
571
  }
443
572
  add(other) {
444
573
  this.assertSameCurrency(other);
445
- return _Price.create(this._amount + other._amount, this._currency);
574
+ return _Price.create(this.amount + other.amount, this.currency);
446
575
  }
447
576
  subtract(other) {
448
577
  this.assertSameCurrency(other);
449
- return _Price.create(this._amount - other._amount, this._currency);
578
+ return _Price.create(this.amount - other.amount, this.currency);
450
579
  }
451
- toProps() {
580
+ toPrimitives() {
452
581
  return {
453
582
  amount: this.amount,
454
583
  currency: this.currency.value
@@ -460,6 +589,7 @@ var _Price = class _Price extends ValueObject {
460
589
  }
461
590
  };
462
591
  _Price.MIN_AMOUNT = -1e6;
592
+ _Price.MAX_AMOUNT = 1e9;
463
593
  var Price = _Price;
464
594
 
465
595
  // src/domain/value-objects/UUID.ts
@@ -469,9 +599,14 @@ var UUID = class _UUID extends ValueObject {
469
599
  }
470
600
  validate(uuid) {
471
601
  if (!_UUID.isValid(uuid)) {
472
- throw new Error(`Invalid uuid ${uuid}`);
602
+ throw new InternalError(`Invalid uuid <${uuid}>`);
473
603
  }
474
604
  }
605
+ toPrimitives() {
606
+ return {
607
+ value: this.value
608
+ };
609
+ }
475
610
  static create(uuid) {
476
611
  return new _UUID(uuid != null ? uuid : crypto.randomUUID());
477
612
  }
@@ -511,91 +646,6 @@ var BasicUnitOfWorkFactory = class {
511
646
  }
512
647
  };
513
648
 
514
- // src/utils/StringVars.ts
515
- var StringVars = class {
516
- static parse(str, ob) {
517
- const regex = /{{(.*?)}}/g;
518
- return str.replace(regex, (match, variable) => {
519
- if (ob.hasOwnProperty(variable.trim())) {
520
- return ob[variable.trim()];
521
- } else {
522
- return match;
523
- }
524
- });
525
- }
526
- };
527
-
528
- // src/infrastructure/errors/ErrorManager.ts
529
- var _ErrorManager = class _ErrorManager {
530
- constructor(logger = null) {
531
- this.logger = logger;
532
- }
533
- getDefaultMessage(lang) {
534
- return _ErrorManager.DEFAULT_MESSAGES[lang.value] || _ErrorManager.DEFAULT_MESSAGES[lang.base()] || "error";
535
- }
536
- onFatal(err, lang) {
537
- var _a;
538
- (_a = this.logger) == null ? void 0 : _a.fatal(err.type, err.message);
539
- return { status: "ERROR", message: this.getDefaultMessage(lang) };
540
- }
541
- onInternal(err, lang) {
542
- var _a;
543
- (_a = this.logger) == null ? void 0 : _a.error(err.type, err.message);
544
- return { status: "ERROR", message: this.getDefaultMessage(lang) };
545
- }
546
- onUsage(err, lang) {
547
- var _a, _b, _c;
548
- const tmpl = _ErrorManager.TEMPLATES.get(err.type);
549
- if (!tmpl) {
550
- (_a = this.logger) == null ? void 0 : _a.error("TEMPLATE_NOT_FOUND", `${err.type}`);
551
- return { status: "ERROR", message: this.getDefaultMessage(lang) };
552
- }
553
- const code = lang.value;
554
- const base = lang.base();
555
- const rawMsg = (_c = (_b = tmpl.languages[code]) != null ? _b : tmpl.languages[base]) != null ? _c : this.getDefaultMessage(lang);
556
- return {
557
- status: "ERROR",
558
- message: StringVars.parse(rawMsg, err.vars)
559
- };
560
- }
561
- onUnknown(err, lang) {
562
- var _a;
563
- (_a = this.logger) == null ? void 0 : _a.error("UNKNOWN_ERROR", err.message);
564
- return { status: "ERROR", message: this.getDefaultMessage(lang) };
565
- }
566
- handle(err, lang) {
567
- var _a;
568
- if (["local", "dev"].includes((_a = process.env.ENVIRONMENT) != null ? _a : "")) {
569
- console.log(err);
570
- }
571
- if (err instanceof FatalError) {
572
- return this.onFatal(err, lang);
573
- }
574
- if (err instanceof InternalError) {
575
- return this.onInternal(err, lang);
576
- }
577
- if (err instanceof UsageError) {
578
- return this.onUsage(err, lang);
579
- }
580
- return this.onUnknown(err, lang);
581
- }
582
- static addTemplate(template) {
583
- _ErrorManager.TEMPLATES.set(template.type, template);
584
- }
585
- };
586
- _ErrorManager.DEFAULT_MESSAGES = {
587
- "es": "Ups, hemos encontrado un error. Nuestro equipo ya est\xE1 trabajando para solucionarlo",
588
- "en": "Ups, we found an error. Our team is working on it.",
589
- "pt": "Ops, encontramos um bug. Nossa equipe j\xE1 est\xE1 trabalhando para resolver isso."
590
- };
591
- _ErrorManager.APP_ERRORS = {
592
- UNDEFINED: "UNDEFINED_ERROR",
593
- PROCESS: "PROCESS_ERROR",
594
- DATABASE: "DATABASE_ERROR"
595
- };
596
- _ErrorManager.TEMPLATES = /* @__PURE__ */ new Map();
597
- var ErrorManager = _ErrorManager;
598
-
599
649
  // src/infrastructure/mysql/Mysql.ts
600
650
  var import_promise = require("mysql2/promise");
601
651
  var _MysqlConnector = class _MysqlConnector {
@@ -731,54 +781,52 @@ function adaptExpressErrorHandler(errorManager) {
731
781
  }
732
782
 
733
783
  // src/utils/ExchangeRates.ts
734
- var ExchangeRates = class _ExchangeRates {
735
- constructor(exchangeRates) {
736
- this._rawDTO = exchangeRates;
737
- this._base = Currency.create(exchangeRates.base);
738
- this._rates = exchangeRates.rates;
739
- this._date = DateTime.create(exchangeRates.date);
784
+ var ExchangeRates = class _ExchangeRates extends BaseObject {
785
+ constructor(props) {
786
+ super(props);
740
787
  }
741
788
  getRate(currency) {
742
- if (Object.keys(this._rates).includes(currency.value)) {
743
- return this._rates[currency.value];
789
+ if (Object.keys(this.props.rates).includes(currency.value)) {
790
+ return this.props.rates[currency.value];
744
791
  }
745
792
  return null;
746
793
  }
747
794
  get base() {
748
- return this._base;
795
+ return this.props.base;
749
796
  }
750
797
  get rates() {
751
- return this._rates;
798
+ return this.props.rates;
752
799
  }
753
800
  get date() {
754
- return this._date;
755
- }
756
- get rawDTO() {
757
- return this._rawDTO;
801
+ return this.props.date;
758
802
  }
759
803
  toProps() {
804
+ return this.props;
805
+ }
806
+ toPrimitives() {
760
807
  return {
761
- base: this.base.value,
762
- rates: this.rates,
763
- date: this.date.value
808
+ base: this.props.base.value,
809
+ rates: this.props.rates,
810
+ date: this.props.date.value
764
811
  };
765
812
  }
766
813
  exchangeToBase(price) {
767
814
  const rate = this.getRate(price.currency);
768
815
  if (!rate) {
769
- throw new InternalError("INVALID_EXCHANGE_RATE_CURRENCY", `Avaiable rates: ${this._rates} - Base Currency:${this._base.value} - Price Currency: ${price.currency.value}`);
816
+ throw new InternalError("INVALID_EXCHANGE_RATE_CURRENCY", `Avaiable rates: ${this.props.rates} - Base Currency:${this.props.base.value} - Price Currency: ${price.currency.value}`);
770
817
  }
771
- if (price.currency.value === this._base.value) {
818
+ if (price.currency.value === this.props.base.value) {
772
819
  return price;
773
820
  }
774
- return Price.create(parseFloat((price.amount / rate).toFixed(2)), this._base.value);
821
+ return Price.create(parseFloat((price.amount / rate).toFixed(2)), this.props.base.value);
775
822
  }
776
- static create(data) {
777
- return new _ExchangeRates(data);
823
+ static create(props) {
824
+ return new _ExchangeRates(props);
778
825
  }
779
826
  };
780
827
  // Annotate the CommonJS export names for ESM import in node:
781
828
  0 && (module.exports = {
829
+ BaseObject,
782
830
  BasicUnitOfWork,
783
831
  BasicUnitOfWorkFactory,
784
832
  Currency,