wsp-ms-core 1.0.35 → 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 +175 -116
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +30 -16
- package/dist/index.d.ts +30 -16
- package/dist/index.js +175 -116
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { Pool, RowDataPacket, PoolConnection } from 'mysql2/promise';
|
|
2
2
|
import { RequestHandler, ErrorRequestHandler } from 'express';
|
|
3
|
-
import { RatesDTO } from 'wsp-contracts/rates';
|
|
4
3
|
|
|
5
4
|
declare abstract class ValueObject<TPrimitive = unknown> {
|
|
6
5
|
protected readonly _value: TPrimitive;
|
|
7
6
|
protected constructor(value: TPrimitive);
|
|
8
7
|
protected abstract validate(value: TPrimitive): void;
|
|
8
|
+
protected abstract toPrimitives(): Record<string, unknown>;
|
|
9
|
+
toProps(): TPrimitive;
|
|
9
10
|
get value(): TPrimitive;
|
|
10
11
|
toString(): string;
|
|
11
12
|
equals(vo?: ValueObject<TPrimitive> | null): boolean;
|
|
@@ -14,6 +15,7 @@ declare abstract class ValueObject<TPrimitive = unknown> {
|
|
|
14
15
|
declare class UUID extends ValueObject<string> {
|
|
15
16
|
private constructor();
|
|
16
17
|
protected validate(uuid: string): void;
|
|
18
|
+
toPrimitives(): Record<string, unknown>;
|
|
17
19
|
static create(uuid?: string): UUID;
|
|
18
20
|
static isValid(uuid: string): boolean;
|
|
19
21
|
}
|
|
@@ -45,6 +47,7 @@ declare class DateTime extends ValueObject<string> {
|
|
|
45
47
|
get second(): number;
|
|
46
48
|
getMonthName(locale?: string): string;
|
|
47
49
|
getWeekdayName(locale?: string): string;
|
|
50
|
+
toPrimitives(): Record<string, unknown>;
|
|
48
51
|
static create(input?: string | number): DateTime;
|
|
49
52
|
static now(): DateTime;
|
|
50
53
|
}
|
|
@@ -108,6 +111,7 @@ declare class Currency extends ValueObject<string> {
|
|
|
108
111
|
readonly numeric: number;
|
|
109
112
|
private constructor();
|
|
110
113
|
protected validate(alpha: string): void;
|
|
114
|
+
toPrimitives(): Record<string, unknown>;
|
|
111
115
|
static create(raw: string | number): Currency;
|
|
112
116
|
static isValid(raw: string | number): boolean;
|
|
113
117
|
}
|
|
@@ -116,6 +120,7 @@ declare class Email extends ValueObject<string> {
|
|
|
116
120
|
static readonly REGEX: RegExp;
|
|
117
121
|
private constructor();
|
|
118
122
|
protected validate(value: string): void;
|
|
123
|
+
toPrimitives(): Record<string, unknown>;
|
|
119
124
|
static create(raw: string): Email;
|
|
120
125
|
static isValid(raw: string): boolean;
|
|
121
126
|
}
|
|
@@ -158,16 +163,17 @@ declare class Language extends ValueObject<string> {
|
|
|
158
163
|
private constructor();
|
|
159
164
|
protected validate(value: string): void;
|
|
160
165
|
base(): string;
|
|
166
|
+
toPrimitives(): Record<string, unknown>;
|
|
161
167
|
static create(raw: string): Language;
|
|
162
168
|
}
|
|
163
169
|
|
|
164
|
-
|
|
170
|
+
interface PriceProps {
|
|
165
171
|
amount: number;
|
|
166
172
|
currency: Currency;
|
|
167
|
-
}
|
|
173
|
+
}
|
|
174
|
+
declare class Price extends ValueObject<PriceProps> {
|
|
168
175
|
static readonly MIN_AMOUNT: number;
|
|
169
|
-
|
|
170
|
-
private readonly _currency;
|
|
176
|
+
static readonly MAX_AMOUNT: number;
|
|
171
177
|
private constructor();
|
|
172
178
|
protected validate(props: {
|
|
173
179
|
amount: number;
|
|
@@ -179,6 +185,7 @@ declare class Price extends ValueObject<{
|
|
|
179
185
|
private assertSameCurrency;
|
|
180
186
|
add(other: Price): Price;
|
|
181
187
|
subtract(other: Price): Price;
|
|
188
|
+
toPrimitives(): Record<string, unknown>;
|
|
182
189
|
static create(amount: number, currency: Currency | string | number): Price;
|
|
183
190
|
}
|
|
184
191
|
|
|
@@ -314,21 +321,28 @@ declare class HttpNotFoundController implements HttpController {
|
|
|
314
321
|
declare function adaptExpressRoute(Controller: new () => HttpController): RequestHandler;
|
|
315
322
|
declare function adaptExpressErrorHandler(errorManager: ErrorManager): ErrorRequestHandler;
|
|
316
323
|
|
|
317
|
-
declare class
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
324
|
+
declare abstract class BaseObject<T = unknown> {
|
|
325
|
+
protected readonly props: T;
|
|
326
|
+
protected constructor(props: T);
|
|
327
|
+
abstract toProps(): T;
|
|
328
|
+
abstract toPrimitives(): Record<string, unknown>;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
interface ExchangeRatesProps {
|
|
332
|
+
base: Currency;
|
|
333
|
+
rates: Record<string, number>;
|
|
334
|
+
date: DateTime;
|
|
335
|
+
}
|
|
336
|
+
declare class ExchangeRates extends BaseObject<ExchangeRatesProps> {
|
|
337
|
+
constructor(props: ExchangeRatesProps);
|
|
323
338
|
private getRate;
|
|
324
339
|
get base(): Currency;
|
|
325
|
-
get rates():
|
|
326
|
-
[key: string]: number;
|
|
327
|
-
};
|
|
340
|
+
get rates(): Record<string, number>;
|
|
328
341
|
get date(): DateTime;
|
|
329
|
-
|
|
342
|
+
toProps(): ExchangeRatesProps;
|
|
343
|
+
toPrimitives(): Record<string, unknown>;
|
|
330
344
|
exchangeToBase(price: Price): Price;
|
|
331
|
-
static create(
|
|
345
|
+
static create(props: ExchangeRatesProps): ExchangeRates;
|
|
332
346
|
}
|
|
333
347
|
|
|
334
348
|
export { BaseEntity, BasicUnitOfWork, BasicUnitOfWorkFactory, Currency, DatabaseConnection, DatabaseConnector, DateTime, DomainEntity, DomainError, DomainEvent, Email, ErrorManager, ErrorManagerHandleResult, ErrorTemplate, EventBus, EventBusRepository, ExchangeRates, FatalError, HttpController, HttpHealthCheckController, HttpNotFoundController, HttpRequest, HttpResponse, InternalError, Language, Logger, MysqlConnection, MysqlConnector, Price, UUID, UnitOfWork, UploadedFile, UsageError, ValueObject, adaptExpressErrorHandler, adaptExpressRoute };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { Pool, RowDataPacket, PoolConnection } from 'mysql2/promise';
|
|
2
2
|
import { RequestHandler, ErrorRequestHandler } from 'express';
|
|
3
|
-
import { RatesDTO } from 'wsp-contracts/rates';
|
|
4
3
|
|
|
5
4
|
declare abstract class ValueObject<TPrimitive = unknown> {
|
|
6
5
|
protected readonly _value: TPrimitive;
|
|
7
6
|
protected constructor(value: TPrimitive);
|
|
8
7
|
protected abstract validate(value: TPrimitive): void;
|
|
8
|
+
protected abstract toPrimitives(): Record<string, unknown>;
|
|
9
|
+
toProps(): TPrimitive;
|
|
9
10
|
get value(): TPrimitive;
|
|
10
11
|
toString(): string;
|
|
11
12
|
equals(vo?: ValueObject<TPrimitive> | null): boolean;
|
|
@@ -14,6 +15,7 @@ declare abstract class ValueObject<TPrimitive = unknown> {
|
|
|
14
15
|
declare class UUID extends ValueObject<string> {
|
|
15
16
|
private constructor();
|
|
16
17
|
protected validate(uuid: string): void;
|
|
18
|
+
toPrimitives(): Record<string, unknown>;
|
|
17
19
|
static create(uuid?: string): UUID;
|
|
18
20
|
static isValid(uuid: string): boolean;
|
|
19
21
|
}
|
|
@@ -45,6 +47,7 @@ declare class DateTime extends ValueObject<string> {
|
|
|
45
47
|
get second(): number;
|
|
46
48
|
getMonthName(locale?: string): string;
|
|
47
49
|
getWeekdayName(locale?: string): string;
|
|
50
|
+
toPrimitives(): Record<string, unknown>;
|
|
48
51
|
static create(input?: string | number): DateTime;
|
|
49
52
|
static now(): DateTime;
|
|
50
53
|
}
|
|
@@ -108,6 +111,7 @@ declare class Currency extends ValueObject<string> {
|
|
|
108
111
|
readonly numeric: number;
|
|
109
112
|
private constructor();
|
|
110
113
|
protected validate(alpha: string): void;
|
|
114
|
+
toPrimitives(): Record<string, unknown>;
|
|
111
115
|
static create(raw: string | number): Currency;
|
|
112
116
|
static isValid(raw: string | number): boolean;
|
|
113
117
|
}
|
|
@@ -116,6 +120,7 @@ declare class Email extends ValueObject<string> {
|
|
|
116
120
|
static readonly REGEX: RegExp;
|
|
117
121
|
private constructor();
|
|
118
122
|
protected validate(value: string): void;
|
|
123
|
+
toPrimitives(): Record<string, unknown>;
|
|
119
124
|
static create(raw: string): Email;
|
|
120
125
|
static isValid(raw: string): boolean;
|
|
121
126
|
}
|
|
@@ -158,16 +163,17 @@ declare class Language extends ValueObject<string> {
|
|
|
158
163
|
private constructor();
|
|
159
164
|
protected validate(value: string): void;
|
|
160
165
|
base(): string;
|
|
166
|
+
toPrimitives(): Record<string, unknown>;
|
|
161
167
|
static create(raw: string): Language;
|
|
162
168
|
}
|
|
163
169
|
|
|
164
|
-
|
|
170
|
+
interface PriceProps {
|
|
165
171
|
amount: number;
|
|
166
172
|
currency: Currency;
|
|
167
|
-
}
|
|
173
|
+
}
|
|
174
|
+
declare class Price extends ValueObject<PriceProps> {
|
|
168
175
|
static readonly MIN_AMOUNT: number;
|
|
169
|
-
|
|
170
|
-
private readonly _currency;
|
|
176
|
+
static readonly MAX_AMOUNT: number;
|
|
171
177
|
private constructor();
|
|
172
178
|
protected validate(props: {
|
|
173
179
|
amount: number;
|
|
@@ -179,6 +185,7 @@ declare class Price extends ValueObject<{
|
|
|
179
185
|
private assertSameCurrency;
|
|
180
186
|
add(other: Price): Price;
|
|
181
187
|
subtract(other: Price): Price;
|
|
188
|
+
toPrimitives(): Record<string, unknown>;
|
|
182
189
|
static create(amount: number, currency: Currency | string | number): Price;
|
|
183
190
|
}
|
|
184
191
|
|
|
@@ -314,21 +321,28 @@ declare class HttpNotFoundController implements HttpController {
|
|
|
314
321
|
declare function adaptExpressRoute(Controller: new () => HttpController): RequestHandler;
|
|
315
322
|
declare function adaptExpressErrorHandler(errorManager: ErrorManager): ErrorRequestHandler;
|
|
316
323
|
|
|
317
|
-
declare class
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
324
|
+
declare abstract class BaseObject<T = unknown> {
|
|
325
|
+
protected readonly props: T;
|
|
326
|
+
protected constructor(props: T);
|
|
327
|
+
abstract toProps(): T;
|
|
328
|
+
abstract toPrimitives(): Record<string, unknown>;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
interface ExchangeRatesProps {
|
|
332
|
+
base: Currency;
|
|
333
|
+
rates: Record<string, number>;
|
|
334
|
+
date: DateTime;
|
|
335
|
+
}
|
|
336
|
+
declare class ExchangeRates extends BaseObject<ExchangeRatesProps> {
|
|
337
|
+
constructor(props: ExchangeRatesProps);
|
|
323
338
|
private getRate;
|
|
324
339
|
get base(): Currency;
|
|
325
|
-
get rates():
|
|
326
|
-
[key: string]: number;
|
|
327
|
-
};
|
|
340
|
+
get rates(): Record<string, number>;
|
|
328
341
|
get date(): DateTime;
|
|
329
|
-
|
|
342
|
+
toProps(): ExchangeRatesProps;
|
|
343
|
+
toPrimitives(): Record<string, unknown>;
|
|
330
344
|
exchangeToBase(price: Price): Price;
|
|
331
|
-
static create(
|
|
345
|
+
static create(props: ExchangeRatesProps): ExchangeRates;
|
|
332
346
|
}
|
|
333
347
|
|
|
334
348
|
export { BaseEntity, BasicUnitOfWork, BasicUnitOfWorkFactory, Currency, DatabaseConnection, DatabaseConnector, DateTime, DomainEntity, DomainError, DomainEvent, Email, ErrorManager, ErrorManagerHandleResult, ErrorTemplate, EventBus, EventBusRepository, ExchangeRates, FatalError, HttpController, HttpHealthCheckController, HttpNotFoundController, HttpRequest, HttpResponse, InternalError, Language, Logger, MysqlConnection, MysqlConnector, Price, UUID, UnitOfWork, UploadedFile, UsageError, ValueObject, adaptExpressErrorHandler, adaptExpressRoute };
|
package/dist/index.js
CHANGED
|
@@ -7,6 +7,9 @@ var ValueObject = class {
|
|
|
7
7
|
this.validate(value);
|
|
8
8
|
this._value = Object.freeze(value);
|
|
9
9
|
}
|
|
10
|
+
toProps() {
|
|
11
|
+
return this._value;
|
|
12
|
+
}
|
|
10
13
|
get value() {
|
|
11
14
|
return this._value;
|
|
12
15
|
}
|
|
@@ -100,6 +103,11 @@ var _DateTime = class _DateTime extends ValueObject {
|
|
|
100
103
|
getWeekdayName(locale = "en") {
|
|
101
104
|
return this._dt.setLocale(locale).toFormat("cccc");
|
|
102
105
|
}
|
|
106
|
+
toPrimitives() {
|
|
107
|
+
return {
|
|
108
|
+
value: this.value
|
|
109
|
+
};
|
|
110
|
+
}
|
|
103
111
|
static create(input) {
|
|
104
112
|
if (input === void 0) {
|
|
105
113
|
return new _DateTime(_DateTime.toUtcFormat(LuxonDateTime.now()));
|
|
@@ -206,6 +214,11 @@ var _Currency = class _Currency extends ValueObject {
|
|
|
206
214
|
throw new Error(`Currency <${code}> is not supported`);
|
|
207
215
|
}
|
|
208
216
|
}
|
|
217
|
+
toPrimitives() {
|
|
218
|
+
return {
|
|
219
|
+
value: this.value
|
|
220
|
+
};
|
|
221
|
+
}
|
|
209
222
|
static create(raw) {
|
|
210
223
|
if (typeof raw === "number" || _Currency.NUM_REGEX.test(raw)) {
|
|
211
224
|
const num = Number(raw);
|
|
@@ -259,6 +272,11 @@ var _Email = class _Email extends ValueObject {
|
|
|
259
272
|
throw new Error(`Email <${value}> is not a valid address`);
|
|
260
273
|
}
|
|
261
274
|
}
|
|
275
|
+
toPrimitives() {
|
|
276
|
+
return {
|
|
277
|
+
value: this.value
|
|
278
|
+
};
|
|
279
|
+
}
|
|
262
280
|
static create(raw) {
|
|
263
281
|
return new _Email(raw);
|
|
264
282
|
}
|
|
@@ -276,12 +294,18 @@ var _Language = class _Language extends ValueObject {
|
|
|
276
294
|
}
|
|
277
295
|
validate(value) {
|
|
278
296
|
if (!_Language.SUPPORTED.includes(value)) {
|
|
279
|
-
throw new
|
|
297
|
+
throw new InternalError(`Language <${value}> is not supported`);
|
|
280
298
|
}
|
|
281
299
|
}
|
|
282
300
|
base() {
|
|
283
301
|
return this.value.split("-")[0];
|
|
284
302
|
}
|
|
303
|
+
toPrimitives() {
|
|
304
|
+
return {
|
|
305
|
+
base: this.base(),
|
|
306
|
+
value: this.value
|
|
307
|
+
};
|
|
308
|
+
}
|
|
285
309
|
static create(raw) {
|
|
286
310
|
const normalized = raw.trim().toLowerCase().replace("_", "-");
|
|
287
311
|
try {
|
|
@@ -361,103 +385,6 @@ _Language.SPANISH_NICARAGUA = new _Language("es-ni");
|
|
|
361
385
|
_Language.SPANISH_PUERTO_RICO = new _Language("es-pr");
|
|
362
386
|
var Language = _Language;
|
|
363
387
|
|
|
364
|
-
// src/domain/value-objects/Price.ts
|
|
365
|
-
var _Price = class _Price extends ValueObject {
|
|
366
|
-
constructor(amount, currency) {
|
|
367
|
-
super({ amount, currency });
|
|
368
|
-
this._amount = amount;
|
|
369
|
-
this._currency = currency;
|
|
370
|
-
}
|
|
371
|
-
validate(props) {
|
|
372
|
-
const { amount, currency } = props;
|
|
373
|
-
if (typeof amount !== "number" || Number.isNaN(amount) || !Number.isFinite(amount)) {
|
|
374
|
-
throw new Error(`Price amount <${amount}> is not a valid number`);
|
|
375
|
-
}
|
|
376
|
-
if (amount < _Price.MIN_AMOUNT) {
|
|
377
|
-
throw new Error(`Price amount <${amount}> must be \u2265 ${_Price.MIN_AMOUNT}`);
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
get amount() {
|
|
381
|
-
return this._amount;
|
|
382
|
-
}
|
|
383
|
-
get currency() {
|
|
384
|
-
return this._currency;
|
|
385
|
-
}
|
|
386
|
-
equals(other) {
|
|
387
|
-
if (!other)
|
|
388
|
-
return false;
|
|
389
|
-
return this._amount === other._amount && this._currency.equals(other._currency);
|
|
390
|
-
}
|
|
391
|
-
assertSameCurrency(other) {
|
|
392
|
-
if (!this._currency.equals(other._currency)) {
|
|
393
|
-
throw new Error("Cannot operate on Price objects with different currencies");
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
add(other) {
|
|
397
|
-
this.assertSameCurrency(other);
|
|
398
|
-
return _Price.create(this._amount + other._amount, this._currency);
|
|
399
|
-
}
|
|
400
|
-
subtract(other) {
|
|
401
|
-
this.assertSameCurrency(other);
|
|
402
|
-
return _Price.create(this._amount - other._amount, this._currency);
|
|
403
|
-
}
|
|
404
|
-
static create(amount, currency) {
|
|
405
|
-
const cur = currency instanceof Currency ? currency : Currency.create(currency);
|
|
406
|
-
return new _Price(amount, cur);
|
|
407
|
-
}
|
|
408
|
-
};
|
|
409
|
-
_Price.MIN_AMOUNT = -1e6;
|
|
410
|
-
var Price = _Price;
|
|
411
|
-
|
|
412
|
-
// src/domain/value-objects/UUID.ts
|
|
413
|
-
var UUID = class _UUID extends ValueObject {
|
|
414
|
-
constructor(value) {
|
|
415
|
-
super(value);
|
|
416
|
-
}
|
|
417
|
-
validate(uuid) {
|
|
418
|
-
if (!_UUID.isValid(uuid)) {
|
|
419
|
-
throw new Error(`Invalid uuid ${uuid}`);
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
static create(uuid) {
|
|
423
|
-
return new _UUID(uuid != null ? uuid : crypto.randomUUID());
|
|
424
|
-
}
|
|
425
|
-
static isValid(uuid) {
|
|
426
|
-
return /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(uuid);
|
|
427
|
-
}
|
|
428
|
-
};
|
|
429
|
-
|
|
430
|
-
// src/application/unit-of-work/BasicUnitOfWork.ts
|
|
431
|
-
var BasicUnitOfWork = class {
|
|
432
|
-
constructor(conn) {
|
|
433
|
-
this.connection = conn;
|
|
434
|
-
}
|
|
435
|
-
async execute(fn) {
|
|
436
|
-
await this.connection.begin();
|
|
437
|
-
try {
|
|
438
|
-
const result = await fn();
|
|
439
|
-
await this.connection.commit();
|
|
440
|
-
return result;
|
|
441
|
-
} catch (err) {
|
|
442
|
-
await this.connection.rollback();
|
|
443
|
-
throw err;
|
|
444
|
-
} finally {
|
|
445
|
-
await this.connection.close();
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
};
|
|
449
|
-
|
|
450
|
-
// src/application/unit-of-work/BasicUnitOfWorkFactory.ts
|
|
451
|
-
var BasicUnitOfWorkFactory = class {
|
|
452
|
-
constructor(connector) {
|
|
453
|
-
this.connector = connector;
|
|
454
|
-
}
|
|
455
|
-
async create() {
|
|
456
|
-
const conn = await this.connector.getConnection();
|
|
457
|
-
return new BasicUnitOfWork(conn);
|
|
458
|
-
}
|
|
459
|
-
};
|
|
460
|
-
|
|
461
388
|
// src/utils/StringVars.ts
|
|
462
389
|
var StringVars = class {
|
|
463
390
|
static parse(str, ob) {
|
|
@@ -543,6 +470,127 @@ _ErrorManager.APP_ERRORS = {
|
|
|
543
470
|
_ErrorManager.TEMPLATES = /* @__PURE__ */ new Map();
|
|
544
471
|
var ErrorManager = _ErrorManager;
|
|
545
472
|
|
|
473
|
+
// src/domain/value-objects/Price.ts
|
|
474
|
+
ErrorManager.addTemplate({
|
|
475
|
+
type: "INVALID_PRICE_AMOUNT",
|
|
476
|
+
languages: {
|
|
477
|
+
"es": "El precio <{{amount}}> no es v\xE1lido",
|
|
478
|
+
"en": "Price amount <{{amount}}> is not a valid number"
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
ErrorManager.addTemplate({
|
|
482
|
+
type: "INVALID_PRICE_RANGE",
|
|
483
|
+
languages: {
|
|
484
|
+
"es": "El precio <{{amount}}> debe ser \u2265 {{min}} y \u2264 {{max}}",
|
|
485
|
+
"en": "Price amount <{{amount}}> must be \u2265 {{min}} and \u2264 {{max}}"
|
|
486
|
+
}
|
|
487
|
+
});
|
|
488
|
+
var _Price = class _Price extends ValueObject {
|
|
489
|
+
constructor(amount, currency) {
|
|
490
|
+
super({ amount, currency });
|
|
491
|
+
}
|
|
492
|
+
validate(props) {
|
|
493
|
+
const { amount, currency } = props;
|
|
494
|
+
if (typeof amount !== "number" || Number.isNaN(amount) || !Number.isFinite(amount)) {
|
|
495
|
+
throw new UsageError("INVALID_PRICE_AMOUNT", { amount });
|
|
496
|
+
}
|
|
497
|
+
if (amount < _Price.MIN_AMOUNT || amount > _Price.MAX_AMOUNT) {
|
|
498
|
+
throw new UsageError("INVALID_PRICE_RANGE", { amount, min: _Price.MIN_AMOUNT, max: _Price.MAX_AMOUNT });
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
get amount() {
|
|
502
|
+
return this._value.amount;
|
|
503
|
+
}
|
|
504
|
+
get currency() {
|
|
505
|
+
return this._value.currency;
|
|
506
|
+
}
|
|
507
|
+
equals(other) {
|
|
508
|
+
if (!other)
|
|
509
|
+
return false;
|
|
510
|
+
return this._value.amount === other.amount && this.currency.equals(other.currency);
|
|
511
|
+
}
|
|
512
|
+
assertSameCurrency(other) {
|
|
513
|
+
if (!this.currency.equals(other.currency)) {
|
|
514
|
+
throw new Error("Cannot operate on Price objects with different currencies");
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
add(other) {
|
|
518
|
+
this.assertSameCurrency(other);
|
|
519
|
+
return _Price.create(this.amount + other.amount, this.currency);
|
|
520
|
+
}
|
|
521
|
+
subtract(other) {
|
|
522
|
+
this.assertSameCurrency(other);
|
|
523
|
+
return _Price.create(this.amount - other.amount, this.currency);
|
|
524
|
+
}
|
|
525
|
+
toPrimitives() {
|
|
526
|
+
return {
|
|
527
|
+
amount: this.amount,
|
|
528
|
+
currency: this.currency.value
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
static create(amount, currency) {
|
|
532
|
+
const cur = currency instanceof Currency ? currency : Currency.create(currency);
|
|
533
|
+
return new _Price(amount, cur);
|
|
534
|
+
}
|
|
535
|
+
};
|
|
536
|
+
_Price.MIN_AMOUNT = -1e6;
|
|
537
|
+
_Price.MAX_AMOUNT = 1e9;
|
|
538
|
+
var Price = _Price;
|
|
539
|
+
|
|
540
|
+
// src/domain/value-objects/UUID.ts
|
|
541
|
+
var UUID = class _UUID extends ValueObject {
|
|
542
|
+
constructor(value) {
|
|
543
|
+
super(value);
|
|
544
|
+
}
|
|
545
|
+
validate(uuid) {
|
|
546
|
+
if (!_UUID.isValid(uuid)) {
|
|
547
|
+
throw new InternalError(`Invalid uuid <${uuid}>`);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
toPrimitives() {
|
|
551
|
+
return {
|
|
552
|
+
value: this.value
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
static create(uuid) {
|
|
556
|
+
return new _UUID(uuid != null ? uuid : crypto.randomUUID());
|
|
557
|
+
}
|
|
558
|
+
static isValid(uuid) {
|
|
559
|
+
return /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(uuid);
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
|
|
563
|
+
// src/application/unit-of-work/BasicUnitOfWork.ts
|
|
564
|
+
var BasicUnitOfWork = class {
|
|
565
|
+
constructor(conn) {
|
|
566
|
+
this.connection = conn;
|
|
567
|
+
}
|
|
568
|
+
async execute(fn) {
|
|
569
|
+
await this.connection.begin();
|
|
570
|
+
try {
|
|
571
|
+
const result = await fn();
|
|
572
|
+
await this.connection.commit();
|
|
573
|
+
return result;
|
|
574
|
+
} catch (err) {
|
|
575
|
+
await this.connection.rollback();
|
|
576
|
+
throw err;
|
|
577
|
+
} finally {
|
|
578
|
+
await this.connection.close();
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
};
|
|
582
|
+
|
|
583
|
+
// src/application/unit-of-work/BasicUnitOfWorkFactory.ts
|
|
584
|
+
var BasicUnitOfWorkFactory = class {
|
|
585
|
+
constructor(connector) {
|
|
586
|
+
this.connector = connector;
|
|
587
|
+
}
|
|
588
|
+
async create() {
|
|
589
|
+
const conn = await this.connector.getConnection();
|
|
590
|
+
return new BasicUnitOfWork(conn);
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
|
|
546
594
|
// src/infrastructure/mysql/Mysql.ts
|
|
547
595
|
import { createPool } from "mysql2/promise";
|
|
548
596
|
var _MysqlConnector = class _MysqlConnector {
|
|
@@ -677,44 +725,55 @@ function adaptExpressErrorHandler(errorManager) {
|
|
|
677
725
|
};
|
|
678
726
|
}
|
|
679
727
|
|
|
728
|
+
// src/domain/contracts/BaseObject.ts
|
|
729
|
+
var BaseObject = class {
|
|
730
|
+
constructor(props) {
|
|
731
|
+
this.props = props;
|
|
732
|
+
}
|
|
733
|
+
};
|
|
734
|
+
|
|
680
735
|
// src/utils/ExchangeRates.ts
|
|
681
|
-
var ExchangeRates = class _ExchangeRates {
|
|
682
|
-
constructor(
|
|
683
|
-
|
|
684
|
-
this._base = Currency.create(exchangeRates.base);
|
|
685
|
-
this._rates = exchangeRates.rates;
|
|
686
|
-
this._date = DateTime.create(exchangeRates.date);
|
|
736
|
+
var ExchangeRates = class _ExchangeRates extends BaseObject {
|
|
737
|
+
constructor(props) {
|
|
738
|
+
super(props);
|
|
687
739
|
}
|
|
688
740
|
getRate(currency) {
|
|
689
|
-
if (Object.keys(this.
|
|
690
|
-
return this.
|
|
741
|
+
if (Object.keys(this.props.rates).includes(currency.value)) {
|
|
742
|
+
return this.props.rates[currency.value];
|
|
691
743
|
}
|
|
692
744
|
return null;
|
|
693
745
|
}
|
|
694
746
|
get base() {
|
|
695
|
-
return this.
|
|
747
|
+
return this.props.base;
|
|
696
748
|
}
|
|
697
749
|
get rates() {
|
|
698
|
-
return this.
|
|
750
|
+
return this.props.rates;
|
|
699
751
|
}
|
|
700
752
|
get date() {
|
|
701
|
-
return this.
|
|
753
|
+
return this.props.date;
|
|
702
754
|
}
|
|
703
|
-
|
|
704
|
-
return this.
|
|
755
|
+
toProps() {
|
|
756
|
+
return this.props;
|
|
757
|
+
}
|
|
758
|
+
toPrimitives() {
|
|
759
|
+
return {
|
|
760
|
+
base: this.props.base.value,
|
|
761
|
+
rates: this.props.rates,
|
|
762
|
+
date: this.props.date.value
|
|
763
|
+
};
|
|
705
764
|
}
|
|
706
765
|
exchangeToBase(price) {
|
|
707
766
|
const rate = this.getRate(price.currency);
|
|
708
767
|
if (!rate) {
|
|
709
|
-
throw new InternalError("INVALID_EXCHANGE_RATE_CURRENCY", `Avaiable rates: ${this.
|
|
768
|
+
throw new InternalError("INVALID_EXCHANGE_RATE_CURRENCY", `Avaiable rates: ${this.props.rates} - Base Currency:${this.props.base.value} - Price Currency: ${price.currency.value}`);
|
|
710
769
|
}
|
|
711
|
-
if (price.currency.value === this.
|
|
770
|
+
if (price.currency.value === this.props.base.value) {
|
|
712
771
|
return price;
|
|
713
772
|
}
|
|
714
|
-
return Price.create(parseFloat((price.amount / rate).toFixed(2)), this.
|
|
773
|
+
return Price.create(parseFloat((price.amount / rate).toFixed(2)), this.props.base.value);
|
|
715
774
|
}
|
|
716
|
-
static create(
|
|
717
|
-
return new _ExchangeRates(
|
|
775
|
+
static create(props) {
|
|
776
|
+
return new _ExchangeRates(props);
|
|
718
777
|
}
|
|
719
778
|
};
|
|
720
779
|
export {
|