wsp-ms-core 1.0.36 → 1.0.37

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
@@ -54,6 +54,9 @@ var ValueObject = class {
54
54
  this.validate(value);
55
55
  this._value = Object.freeze(value);
56
56
  }
57
+ toProps() {
58
+ return this._value;
59
+ }
57
60
  get value() {
58
61
  return this._value;
59
62
  }
@@ -147,6 +150,11 @@ var _DateTime = class _DateTime extends ValueObject {
147
150
  getWeekdayName(locale = "en") {
148
151
  return this._dt.setLocale(locale).toFormat("cccc");
149
152
  }
153
+ toPrimitives() {
154
+ return {
155
+ value: this.value
156
+ };
157
+ }
150
158
  static create(input) {
151
159
  if (input === void 0) {
152
160
  return new _DateTime(_DateTime.toUtcFormat(import_luxon.DateTime.now()));
@@ -253,6 +261,11 @@ var _Currency = class _Currency extends ValueObject {
253
261
  throw new Error(`Currency <${code}> is not supported`);
254
262
  }
255
263
  }
264
+ toPrimitives() {
265
+ return {
266
+ value: this.value
267
+ };
268
+ }
256
269
  static create(raw) {
257
270
  if (typeof raw === "number" || _Currency.NUM_REGEX.test(raw)) {
258
271
  const num = Number(raw);
@@ -306,6 +319,11 @@ var _Email = class _Email extends ValueObject {
306
319
  throw new Error(`Email <${value}> is not a valid address`);
307
320
  }
308
321
  }
322
+ toPrimitives() {
323
+ return {
324
+ value: this.value
325
+ };
326
+ }
309
327
  static create(raw) {
310
328
  return new _Email(raw);
311
329
  }
@@ -323,12 +341,18 @@ var _Language = class _Language extends ValueObject {
323
341
  }
324
342
  validate(value) {
325
343
  if (!_Language.SUPPORTED.includes(value)) {
326
- throw new Error(`Language <${value}> is not supported`);
344
+ throw new InternalError(`Language <${value}> is not supported`);
327
345
  }
328
346
  }
329
347
  base() {
330
348
  return this.value.split("-")[0];
331
349
  }
350
+ toPrimitives() {
351
+ return {
352
+ base: this.base(),
353
+ value: this.value
354
+ };
355
+ }
332
356
  static create(raw) {
333
357
  const normalized = raw.trim().toLowerCase().replace("_", "-");
334
358
  try {
@@ -408,47 +432,144 @@ _Language.SPANISH_NICARAGUA = new _Language("es-ni");
408
432
  _Language.SPANISH_PUERTO_RICO = new _Language("es-pr");
409
433
  var Language = _Language;
410
434
 
435
+ // src/utils/StringVars.ts
436
+ var StringVars = class {
437
+ static parse(str, ob) {
438
+ const regex = /{{(.*?)}}/g;
439
+ return str.replace(regex, (match, variable) => {
440
+ if (ob.hasOwnProperty(variable.trim())) {
441
+ return ob[variable.trim()];
442
+ } else {
443
+ return match;
444
+ }
445
+ });
446
+ }
447
+ };
448
+
449
+ // src/infrastructure/errors/ErrorManager.ts
450
+ var _ErrorManager = class _ErrorManager {
451
+ constructor(logger = null) {
452
+ this.logger = logger;
453
+ }
454
+ getDefaultMessage(lang) {
455
+ return _ErrorManager.DEFAULT_MESSAGES[lang.value] || _ErrorManager.DEFAULT_MESSAGES[lang.base()] || "error";
456
+ }
457
+ onFatal(err, lang) {
458
+ var _a;
459
+ (_a = this.logger) == null ? void 0 : _a.fatal(err.type, err.message);
460
+ return { status: "ERROR", message: this.getDefaultMessage(lang) };
461
+ }
462
+ onInternal(err, lang) {
463
+ var _a;
464
+ (_a = this.logger) == null ? void 0 : _a.error(err.type, err.message);
465
+ return { status: "ERROR", message: this.getDefaultMessage(lang) };
466
+ }
467
+ onUsage(err, lang) {
468
+ var _a, _b, _c;
469
+ const tmpl = _ErrorManager.TEMPLATES.get(err.type);
470
+ if (!tmpl) {
471
+ (_a = this.logger) == null ? void 0 : _a.error("TEMPLATE_NOT_FOUND", `${err.type}`);
472
+ return { status: "ERROR", message: this.getDefaultMessage(lang) };
473
+ }
474
+ const code = lang.value;
475
+ const base = lang.base();
476
+ const rawMsg = (_c = (_b = tmpl.languages[code]) != null ? _b : tmpl.languages[base]) != null ? _c : this.getDefaultMessage(lang);
477
+ return {
478
+ status: "ERROR",
479
+ message: StringVars.parse(rawMsg, err.vars)
480
+ };
481
+ }
482
+ onUnknown(err, lang) {
483
+ var _a;
484
+ (_a = this.logger) == null ? void 0 : _a.error("UNKNOWN_ERROR", err.message);
485
+ return { status: "ERROR", message: this.getDefaultMessage(lang) };
486
+ }
487
+ handle(err, lang) {
488
+ var _a;
489
+ if (["local", "dev"].includes((_a = process.env.ENVIRONMENT) != null ? _a : "")) {
490
+ console.log(err);
491
+ }
492
+ if (err instanceof FatalError) {
493
+ return this.onFatal(err, lang);
494
+ }
495
+ if (err instanceof InternalError) {
496
+ return this.onInternal(err, lang);
497
+ }
498
+ if (err instanceof UsageError) {
499
+ return this.onUsage(err, lang);
500
+ }
501
+ return this.onUnknown(err, lang);
502
+ }
503
+ static addTemplate(template) {
504
+ _ErrorManager.TEMPLATES.set(template.type, template);
505
+ }
506
+ };
507
+ _ErrorManager.DEFAULT_MESSAGES = {
508
+ "es": "Ups, hemos encontrado un error. Nuestro equipo ya est\xE1 trabajando para solucionarlo",
509
+ "en": "Ups, we found an error. Our team is working on it.",
510
+ "pt": "Ops, encontramos um bug. Nossa equipe j\xE1 est\xE1 trabalhando para resolver isso."
511
+ };
512
+ _ErrorManager.APP_ERRORS = {
513
+ UNDEFINED: "UNDEFINED_ERROR",
514
+ PROCESS: "PROCESS_ERROR",
515
+ DATABASE: "DATABASE_ERROR"
516
+ };
517
+ _ErrorManager.TEMPLATES = /* @__PURE__ */ new Map();
518
+ var ErrorManager = _ErrorManager;
519
+
411
520
  // src/domain/value-objects/Price.ts
521
+ ErrorManager.addTemplate({
522
+ type: "INVALID_PRICE_AMOUNT",
523
+ languages: {
524
+ "es": "El precio <{{amount}}> no es v\xE1lido",
525
+ "en": "Price amount <{{amount}}> is not a valid number"
526
+ }
527
+ });
528
+ ErrorManager.addTemplate({
529
+ type: "INVALID_PRICE_RANGE",
530
+ languages: {
531
+ "es": "El precio <{{amount}}> debe ser \u2265 {{min}} y \u2264 {{max}}",
532
+ "en": "Price amount <{{amount}}> must be \u2265 {{min}} and \u2264 {{max}}"
533
+ }
534
+ });
412
535
  var _Price = class _Price extends ValueObject {
413
536
  constructor(amount, currency) {
414
537
  super({ amount, currency });
415
- this._amount = amount;
416
- this._currency = currency;
417
538
  }
418
539
  validate(props) {
419
540
  const { amount, currency } = props;
420
541
  if (typeof amount !== "number" || Number.isNaN(amount) || !Number.isFinite(amount)) {
421
- throw new Error(`Price amount <${amount}> is not a valid number`);
542
+ throw new UsageError("INVALID_PRICE_AMOUNT", { amount });
422
543
  }
423
- if (amount < _Price.MIN_AMOUNT) {
424
- throw new Error(`Price amount <${amount}> must be \u2265 ${_Price.MIN_AMOUNT}`);
544
+ if (amount < _Price.MIN_AMOUNT || amount > _Price.MAX_AMOUNT) {
545
+ throw new UsageError("INVALID_PRICE_RANGE", { amount, min: _Price.MIN_AMOUNT, max: _Price.MAX_AMOUNT });
425
546
  }
426
547
  }
427
548
  get amount() {
428
- return this._amount;
549
+ return this._value.amount;
429
550
  }
430
551
  get currency() {
431
- return this._currency;
552
+ return this._value.currency;
432
553
  }
433
554
  equals(other) {
434
555
  if (!other)
435
556
  return false;
436
- return this._amount === other._amount && this._currency.equals(other._currency);
557
+ return this._value.amount === other.amount && this.currency.equals(other.currency);
437
558
  }
438
559
  assertSameCurrency(other) {
439
- if (!this._currency.equals(other._currency)) {
560
+ if (!this.currency.equals(other.currency)) {
440
561
  throw new Error("Cannot operate on Price objects with different currencies");
441
562
  }
442
563
  }
443
564
  add(other) {
444
565
  this.assertSameCurrency(other);
445
- return _Price.create(this._amount + other._amount, this._currency);
566
+ return _Price.create(this.amount + other.amount, this.currency);
446
567
  }
447
568
  subtract(other) {
448
569
  this.assertSameCurrency(other);
449
- return _Price.create(this._amount - other._amount, this._currency);
570
+ return _Price.create(this.amount - other.amount, this.currency);
450
571
  }
451
- toProps() {
572
+ toPrimitives() {
452
573
  return {
453
574
  amount: this.amount,
454
575
  currency: this.currency.value
@@ -460,6 +581,7 @@ var _Price = class _Price extends ValueObject {
460
581
  }
461
582
  };
462
583
  _Price.MIN_AMOUNT = -1e6;
584
+ _Price.MAX_AMOUNT = 1e9;
463
585
  var Price = _Price;
464
586
 
465
587
  // src/domain/value-objects/UUID.ts
@@ -469,9 +591,14 @@ var UUID = class _UUID extends ValueObject {
469
591
  }
470
592
  validate(uuid) {
471
593
  if (!_UUID.isValid(uuid)) {
472
- throw new Error(`Invalid uuid ${uuid}`);
594
+ throw new InternalError(`Invalid uuid <${uuid}>`);
473
595
  }
474
596
  }
597
+ toPrimitives() {
598
+ return {
599
+ value: this.value
600
+ };
601
+ }
475
602
  static create(uuid) {
476
603
  return new _UUID(uuid != null ? uuid : crypto.randomUUID());
477
604
  }
@@ -511,91 +638,6 @@ var BasicUnitOfWorkFactory = class {
511
638
  }
512
639
  };
513
640
 
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
641
  // src/infrastructure/mysql/Mysql.ts
600
642
  var import_promise = require("mysql2/promise");
601
643
  var _MysqlConnector = class _MysqlConnector {
@@ -730,51 +772,55 @@ function adaptExpressErrorHandler(errorManager) {
730
772
  };
731
773
  }
732
774
 
775
+ // src/domain/contracts/BaseObject.ts
776
+ var BaseObject = class {
777
+ constructor(props) {
778
+ this.props = props;
779
+ }
780
+ };
781
+
733
782
  // 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);
783
+ var ExchangeRates = class _ExchangeRates extends BaseObject {
784
+ constructor(props) {
785
+ super(props);
740
786
  }
741
787
  getRate(currency) {
742
- if (Object.keys(this._rates).includes(currency.value)) {
743
- return this._rates[currency.value];
788
+ if (Object.keys(this.props.rates).includes(currency.value)) {
789
+ return this.props.rates[currency.value];
744
790
  }
745
791
  return null;
746
792
  }
747
793
  get base() {
748
- return this._base;
794
+ return this.props.base;
749
795
  }
750
796
  get rates() {
751
- return this._rates;
797
+ return this.props.rates;
752
798
  }
753
799
  get date() {
754
- return this._date;
755
- }
756
- get rawDTO() {
757
- return this._rawDTO;
800
+ return this.props.date;
758
801
  }
759
802
  toProps() {
803
+ return this.props;
804
+ }
805
+ toPrimitives() {
760
806
  return {
761
- base: this.base.value,
762
- rates: this.rates,
763
- date: this.date.value
807
+ base: this.props.base.value,
808
+ rates: this.props.rates,
809
+ date: this.props.date.value
764
810
  };
765
811
  }
766
812
  exchangeToBase(price) {
767
813
  const rate = this.getRate(price.currency);
768
814
  if (!rate) {
769
- throw new InternalError("INVALID_EXCHANGE_RATE_CURRENCY", `Avaiable rates: ${this._rates} - Base Currency:${this._base.value} - Price Currency: ${price.currency.value}`);
815
+ throw new InternalError("INVALID_EXCHANGE_RATE_CURRENCY", `Avaiable rates: ${this.props.rates} - Base Currency:${this.props.base.value} - Price Currency: ${price.currency.value}`);
770
816
  }
771
- if (price.currency.value === this._base.value) {
817
+ if (price.currency.value === this.props.base.value) {
772
818
  return price;
773
819
  }
774
- return Price.create(parseFloat((price.amount / rate).toFixed(2)), this._base.value);
820
+ return Price.create(parseFloat((price.amount / rate).toFixed(2)), this.props.base.value);
775
821
  }
776
- static create(data) {
777
- return new _ExchangeRates(data);
822
+ static create(props) {
823
+ return new _ExchangeRates(props);
778
824
  }
779
825
  };
780
826
  // Annotate the CommonJS export names for ESM import in node: