wsp-ms-core 1.0.0
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/README.md +29 -0
- package/jest.config.cjs +9 -0
- package/package.json +71 -0
- package/src/application/contracts/EventBus.ts +7 -0
- package/src/application/contracts/EventBusRepository.ts +10 -0
- package/src/domain/contracts/DomainEntity.ts +58 -0
- package/src/domain/contracts/DomainError.ts +9 -0
- package/src/domain/contracts/DomainEvent.ts +22 -0
- package/src/domain/contracts/ValueObject.ts +26 -0
- package/src/domain/errors/FatalError.ts +9 -0
- package/src/domain/errors/InternalError.ts +9 -0
- package/src/domain/errors/UsageError.ts +12 -0
- package/src/domain/value-objects/Currency.ts +68 -0
- package/src/domain/value-objects/DateTime.ts +132 -0
- package/src/domain/value-objects/Email.ts +24 -0
- package/src/domain/value-objects/Language.ts +94 -0
- package/src/domain/value-objects/Price.ts +54 -0
- package/src/domain/value-objects/UUID.ts +23 -0
- package/src/index.ts +43 -0
- package/src/infrastructure/contracts/DatabaseConnection.ts +11 -0
- package/src/infrastructure/contracts/DatabaseConnector.ts +8 -0
- package/src/infrastructure/contracts/Logger.ts +9 -0
- package/src/infrastructure/errors/ErrorManager.ts +93 -0
- package/src/infrastructure/mysql/MysqlConnection.ts +45 -0
- package/src/infrastructure/mysql/MysqlConnector.ts +51 -0
- package/src/utils/StringVars.ts +14 -0
- package/test/domain/value-objects/Currency.test.ts +48 -0
- package/test/domain/value-objects/DateTime.test.ts +32 -0
- package/test/domain/value-objects/Email.test.ts +38 -0
- package/test/domain/value-objects/Language.test.ts +76 -0
- package/test/domain/value-objects/Price.test.ts +96 -0
- package/test/domain/value-objects/UUID.test.ts +18 -0
- package/test/infrastructure/errors/ErrorManager.test.ts +125 -0
- package/test/infrastructure/mysql/MysqlConnection.test.ts +45 -0
- package/tsconfig.json +14 -0
- package/tsup.config.ts +18 -0
package/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# README #
|
|
2
|
+
|
|
3
|
+
This README would normally document whatever steps are necessary to get your application up and running.
|
|
4
|
+
|
|
5
|
+
### What is this repository for? ###
|
|
6
|
+
|
|
7
|
+
* Quick summary
|
|
8
|
+
* Version
|
|
9
|
+
* [Learn Markdown](https://bitbucket.org/tutorials/markdowndemo)
|
|
10
|
+
|
|
11
|
+
### How do I get set up? ###
|
|
12
|
+
|
|
13
|
+
* Summary of set up
|
|
14
|
+
* Configuration
|
|
15
|
+
* Dependencies
|
|
16
|
+
* Database configuration
|
|
17
|
+
* How to run tests
|
|
18
|
+
* Deployment instructions
|
|
19
|
+
|
|
20
|
+
### Contribution guidelines ###
|
|
21
|
+
|
|
22
|
+
* Writing tests
|
|
23
|
+
* Code review
|
|
24
|
+
* Other guidelines
|
|
25
|
+
|
|
26
|
+
### Who do I talk to? ###
|
|
27
|
+
|
|
28
|
+
* Repo owner or admin
|
|
29
|
+
* Other community or team contact
|
package/jest.config.cjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
preset: 'ts-jest',
|
|
3
|
+
moduleNameMapper: {
|
|
4
|
+
'^@domain/(.*)$': '<rootDir>/src/domain/$1',
|
|
5
|
+
'^@application/(.*)$': '<rootDir>/src/application/$1',
|
|
6
|
+
'^@infrastructure/(.*)$': '<rootDir>/src/infrastructure/$1',
|
|
7
|
+
'^@utils/(.*)$': '<rootDir>/src/utils/$1',
|
|
8
|
+
},
|
|
9
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "wsp-ms-core",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"author": "Wonasports",
|
|
6
|
+
"license": "ISC",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/index.cjs",
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.cjs",
|
|
15
|
+
"types": "./dist/index.d.ts"
|
|
16
|
+
},
|
|
17
|
+
"./infrastructure/mysql": {
|
|
18
|
+
"import": "./dist/infrastructure/mysql/index.js",
|
|
19
|
+
"require": "./dist/infrastructure/mysql/index.cjs",
|
|
20
|
+
"types": "./dist/infrastructure/mysql/index.d.ts"
|
|
21
|
+
},
|
|
22
|
+
"./infrastructure/kafka": {
|
|
23
|
+
"import": "./dist/infrastructure/kafka/index.js",
|
|
24
|
+
"require": "./dist/infrastructure/kafka/index.cjs",
|
|
25
|
+
"types": "./dist/infrastructure/kafka/index.d.ts"
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"kafkajs": "^2.2.4",
|
|
31
|
+
"mysql2": "^3.10.0"
|
|
32
|
+
},
|
|
33
|
+
"peerDependenciesMeta": {
|
|
34
|
+
"mysql2": {
|
|
35
|
+
"optional": true
|
|
36
|
+
},
|
|
37
|
+
"kafkajs": {
|
|
38
|
+
"optional": true
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/jest": "^30.0.0",
|
|
43
|
+
"@types/luxon": "^3.6.2",
|
|
44
|
+
"@types/node": "^20.10.0",
|
|
45
|
+
"@typescript-eslint/eslint-plugin": "^6.13.0",
|
|
46
|
+
"@typescript-eslint/parser": "^6.13.0",
|
|
47
|
+
"eslint": "^8.56.0",
|
|
48
|
+
"jest": "^29.7.0",
|
|
49
|
+
"rimraf": "^5.0.5",
|
|
50
|
+
"ts-jest": "^29.1.1",
|
|
51
|
+
"tsconfig-paths": "^4.2.0",
|
|
52
|
+
"tsup": "^7.2.0",
|
|
53
|
+
"typescript": "^5.4.0"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"esbuild-plugin-alias": "^0.2.1",
|
|
57
|
+
"kafkajs": "^2.2.4",
|
|
58
|
+
"luxon": "^3.4.4",
|
|
59
|
+
"mysql2": "^3.10.0"
|
|
60
|
+
},
|
|
61
|
+
"engines": {
|
|
62
|
+
"node": ">=20.0"
|
|
63
|
+
},
|
|
64
|
+
"scripts": {
|
|
65
|
+
"clean": "rimraf dist",
|
|
66
|
+
"build": "npm run clean && tsup src/index.ts --dts --format esm,cjs --out-dir dist",
|
|
67
|
+
"lint": "eslint \"src/**/*.ts\"",
|
|
68
|
+
"test": "jest",
|
|
69
|
+
"prepublishOnly": "npm run build && npm run test"
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import {DomainEvent} from "@domain/contracts/DomainEvent";
|
|
2
|
+
|
|
3
|
+
export interface EventBusRepository {
|
|
4
|
+
|
|
5
|
+
save(event: DomainEvent): Promise<void>;
|
|
6
|
+
listPending(limit: number): Promise<DomainEvent[]>;
|
|
7
|
+
markProcessed(id: number): Promise<void>;
|
|
8
|
+
incrementRetries(id: number): Promise<void>;
|
|
9
|
+
|
|
10
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {UUID} from "@domain/value-objects/UUID";
|
|
2
|
+
import {DateTime} from "@domain/value-objects/DateTime";
|
|
3
|
+
|
|
4
|
+
export abstract class DomainEntity<T extends Record<string, string | number | boolean>> {
|
|
5
|
+
|
|
6
|
+
public readonly uuid: UUID;
|
|
7
|
+
|
|
8
|
+
private readonly _createdAt: DateTime;
|
|
9
|
+
private _updatedAt: DateTime;
|
|
10
|
+
private _deletedAt?: DateTime;
|
|
11
|
+
|
|
12
|
+
protected readonly props: T;
|
|
13
|
+
|
|
14
|
+
protected constructor(
|
|
15
|
+
uuid: UUID,
|
|
16
|
+
props: T,
|
|
17
|
+
audit?: { createdAt?: DateTime; updatedAt?: DateTime; deletedAt?: DateTime },
|
|
18
|
+
) {
|
|
19
|
+
this.uuid = uuid;
|
|
20
|
+
this.props = props;
|
|
21
|
+
|
|
22
|
+
this._createdAt = audit?.createdAt ?? DateTime.create();
|
|
23
|
+
this._updatedAt = audit?.updatedAt ?? this.createdAt;
|
|
24
|
+
this._deletedAt = audit?.deletedAt;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
protected touch(): void {
|
|
28
|
+
this._updatedAt = DateTime.create();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public get createdAt(): DateTime {
|
|
32
|
+
return this._createdAt;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public get updatedAt(): DateTime {
|
|
36
|
+
return this._updatedAt;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public get deletedAt(): DateTime | undefined {
|
|
40
|
+
return this._deletedAt;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public get isDeleted(): boolean {
|
|
44
|
+
return Boolean(this._deletedAt);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public abstract equals(entity?: DomainEntity<T>): boolean;
|
|
48
|
+
|
|
49
|
+
public softDelete(): void {
|
|
50
|
+
if (!this._deletedAt) {
|
|
51
|
+
this._deletedAt = DateTime.create();
|
|
52
|
+
this.touch();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public abstract toPrimitives(): Record<string, unknown>;
|
|
57
|
+
|
|
58
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {DateTime} from "../value-objects/DateTime";
|
|
2
|
+
|
|
3
|
+
export abstract class DomainEvent<T = unknown> {
|
|
4
|
+
|
|
5
|
+
readonly type: string;
|
|
6
|
+
private readonly _occurredAt: DateTime;
|
|
7
|
+
private readonly _payload: T;
|
|
8
|
+
|
|
9
|
+
protected constructor(payload: T) {
|
|
10
|
+
this._payload = payload;
|
|
11
|
+
this._occurredAt = DateTime.create();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
get payload(): T {
|
|
15
|
+
return this._payload;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
get occurredAt(): DateTime {
|
|
19
|
+
return this._occurredAt;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
|
|
2
|
+
export abstract class ValueObject<TPrimitive = unknown> {
|
|
3
|
+
|
|
4
|
+
protected readonly _value: TPrimitive;
|
|
5
|
+
|
|
6
|
+
protected constructor(value: TPrimitive) {
|
|
7
|
+
this.validate(value);
|
|
8
|
+
this._value = Object.freeze(value);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
protected abstract validate(value: TPrimitive): void;
|
|
12
|
+
|
|
13
|
+
public get value(): TPrimitive {
|
|
14
|
+
return this._value;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public toString(): string {
|
|
18
|
+
return String(this._value);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public equals(vo?: ValueObject<TPrimitive> | null): boolean {
|
|
22
|
+
if (vo === null || vo === undefined) return false;
|
|
23
|
+
if (vo.constructor !== this.constructor) return false;
|
|
24
|
+
return vo.value === this._value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {DomainError} from "@domain/contracts/DomainError";
|
|
2
|
+
|
|
3
|
+
export class UsageError extends DomainError {
|
|
4
|
+
|
|
5
|
+
public readonly vars: Record<string, any>;
|
|
6
|
+
|
|
7
|
+
public constructor(type: string, vars: Record<string, any> = {}) {
|
|
8
|
+
super(type);
|
|
9
|
+
this.vars = vars;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ValueObject } from '@domain/contracts/ValueObject';
|
|
2
|
+
|
|
3
|
+
export class Currency extends ValueObject<string> {
|
|
4
|
+
|
|
5
|
+
public static readonly ALPHA_REGEX = /^[A-Z]{3}$/u;
|
|
6
|
+
public static readonly NUM_REGEX = /^\d{3}$/u;
|
|
7
|
+
|
|
8
|
+
private static readonly ALPHA_TO_NUM: Record<string, number> = {
|
|
9
|
+
USD: 840,
|
|
10
|
+
EUR: 978,
|
|
11
|
+
UYU: 858,
|
|
12
|
+
ARS: 32,
|
|
13
|
+
BRL: 986,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
private static readonly NUM_TO_ALPHA: Record<number, string> =
|
|
17
|
+
Object.entries(Currency.ALPHA_TO_NUM).reduce(
|
|
18
|
+
(acc, [alpha, num]) => {
|
|
19
|
+
acc[num as number] = alpha;
|
|
20
|
+
return acc;
|
|
21
|
+
},
|
|
22
|
+
{} as Record<number, string>,
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
public static readonly USD = new Currency('USD');
|
|
26
|
+
public static readonly EUR = new Currency('EUR');
|
|
27
|
+
public static readonly UYU = new Currency('UYU');
|
|
28
|
+
public static readonly ARS = new Currency('ARS');
|
|
29
|
+
public static readonly BRL = new Currency('BRL');
|
|
30
|
+
|
|
31
|
+
public readonly numeric: number;
|
|
32
|
+
|
|
33
|
+
private constructor(alpha: string) {
|
|
34
|
+
super(alpha.toUpperCase().trim());
|
|
35
|
+
this.numeric = Currency.ALPHA_TO_NUM[this.value];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
protected validate(alpha: string): void {
|
|
39
|
+
const code = alpha.toUpperCase().trim();
|
|
40
|
+
if (!Currency.ALPHA_REGEX.test(code)) {
|
|
41
|
+
throw new Error(`Currency code <${alpha}> is not a valid ISO‑4217 alpha value`);
|
|
42
|
+
}
|
|
43
|
+
if (!(code in Currency.ALPHA_TO_NUM)) {
|
|
44
|
+
throw new Error(`Currency <${code}> is not supported`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public static create(raw: string | number): Currency {
|
|
49
|
+
if (typeof raw === 'number' || Currency.NUM_REGEX.test(raw)) {
|
|
50
|
+
const num = Number(raw);
|
|
51
|
+
const alpha = Currency.NUM_TO_ALPHA[num];
|
|
52
|
+
if (!alpha) {
|
|
53
|
+
throw new Error(`Numeric currency <${raw}> is not supported`);
|
|
54
|
+
}
|
|
55
|
+
return new Currency(alpha);
|
|
56
|
+
}
|
|
57
|
+
return new Currency(String(raw));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public static isValid(raw: string | number): boolean {
|
|
61
|
+
try {
|
|
62
|
+
Currency.create(raw);
|
|
63
|
+
return true;
|
|
64
|
+
} catch {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import {DateTime as LuxonDateTime} from 'luxon';
|
|
2
|
+
import {ValueObject} from "@domain/contracts/ValueObject";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
export class DateTime extends ValueObject<string> {
|
|
6
|
+
|
|
7
|
+
public static readonly DEFAULT_FORMAT: string = 'yyyy-MM-dd HH:mm:ss';
|
|
8
|
+
|
|
9
|
+
private readonly _dt: LuxonDateTime;
|
|
10
|
+
|
|
11
|
+
private static fromLuxon(dt: LuxonDateTime): DateTime {
|
|
12
|
+
return new DateTime(DateTime.toUtcFormat(dt));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
private static toUtcFormat(dt: LuxonDateTime): string {
|
|
16
|
+
return dt.setZone('utc').toFormat(DateTime.DEFAULT_FORMAT);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
private constructor(value: string) {
|
|
20
|
+
super(value);
|
|
21
|
+
this._dt = LuxonDateTime.fromFormat(value, DateTime.DEFAULT_FORMAT, { zone: 'utc' });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
protected validate(value: string): void {
|
|
25
|
+
const q = LuxonDateTime.fromFormat(value, DateTime.DEFAULT_FORMAT, { zone: 'utc' });
|
|
26
|
+
if (!q.isValid) {
|
|
27
|
+
throw new Error(`Invalid DateTime: ${q.invalidExplanation}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public plusYears(years: number): DateTime {
|
|
32
|
+
return DateTime.fromLuxon(this._dt.plus({years}));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public plusMonths(months: number): DateTime {
|
|
36
|
+
return DateTime.fromLuxon(this._dt.plus({months}));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public plusDays(days: number): DateTime {
|
|
40
|
+
return DateTime.fromLuxon(this._dt.plus({days}));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public plusHours(hours: number): DateTime {
|
|
44
|
+
return DateTime.fromLuxon(this._dt.plus({hours}));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public plusMinutes(minutes: number): DateTime {
|
|
48
|
+
return DateTime.fromLuxon(this._dt.plus({minutes}));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public plusSeconds(seconds: number): DateTime {
|
|
52
|
+
return DateTime.fromLuxon(this._dt.plus({seconds}));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public minusYears(years: number): DateTime {
|
|
56
|
+
return DateTime.fromLuxon(this._dt.minus({years}));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public minusMonths(months: number): DateTime {
|
|
60
|
+
return DateTime.fromLuxon(this._dt.minus({months}));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public minusDays(days: number): DateTime {
|
|
64
|
+
return DateTime.fromLuxon(this._dt.minus({days}));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public minusHours(hours: number): DateTime {
|
|
68
|
+
return DateTime.fromLuxon(this._dt.minus({hours}));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public minusMinutes(minutes: number): DateTime {
|
|
72
|
+
return DateTime.fromLuxon(this._dt.minus({minutes}));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public minusSeconds(seconds: number): DateTime {
|
|
76
|
+
return DateTime.fromLuxon(this._dt.minus({seconds}));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public get year(): number {
|
|
80
|
+
return this._dt.year;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public get month(): number {
|
|
84
|
+
return this._dt.month;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public get day(): number {
|
|
88
|
+
return this._dt.day;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
public get hour(): number {
|
|
92
|
+
return this._dt.hour;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public get minute(): number {
|
|
96
|
+
return this._dt.minute;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public get second(): number {
|
|
100
|
+
return this._dt.second;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public getMonthName(locale: string = 'en'): string {
|
|
104
|
+
return this._dt.setLocale(locale).toFormat('LLLL');
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public getWeekdayName(locale: string = 'en'): string {
|
|
108
|
+
return this._dt.setLocale(locale).toFormat('cccc');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public static create(input?: string | number): DateTime {
|
|
112
|
+
if (input === undefined) {
|
|
113
|
+
return new DateTime(DateTime.toUtcFormat(LuxonDateTime.now()));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/* ① timestamp */
|
|
117
|
+
if (typeof input === 'number') {
|
|
118
|
+
return new DateTime(
|
|
119
|
+
DateTime.toUtcFormat(LuxonDateTime.fromMillis(input, { zone: 'utc' })),
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/* ② ISO string o ya formateado */
|
|
124
|
+
const iso = LuxonDateTime.fromISO(input, { zone: 'utc' });
|
|
125
|
+
if (iso.isValid) {
|
|
126
|
+
return new DateTime(DateTime.toUtcFormat(iso));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/* ③ se asume que viene en formato DEFAULT_FORMAT (UTC) */
|
|
130
|
+
return new DateTime(input);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ValueObject } from '@domain/contracts/ValueObject';
|
|
2
|
+
|
|
3
|
+
export class Email extends ValueObject<string> {
|
|
4
|
+
|
|
5
|
+
public static readonly REGEX: RegExp = /^[^\s@]+@[^\s@]+\.[^\s@]+$/u;
|
|
6
|
+
|
|
7
|
+
private constructor(email: string) {
|
|
8
|
+
super(email.trim());
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
protected validate(value: string): void {
|
|
12
|
+
if (!Email.REGEX.test(value)) {
|
|
13
|
+
throw new Error(`Email <${value}> is not a valid address`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public static create(raw: string): Email {
|
|
18
|
+
return new Email(raw);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public static isValid(raw: string): boolean {
|
|
22
|
+
return Email.REGEX.test(raw.trim());
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { ValueObject } from '@domain/contracts/ValueObject';
|
|
2
|
+
|
|
3
|
+
export class Language extends ValueObject<string> {
|
|
4
|
+
|
|
5
|
+
public static readonly SUPPORTED: readonly string[] = [
|
|
6
|
+
'es',
|
|
7
|
+
'en',
|
|
8
|
+
'en-us',
|
|
9
|
+
'en-gb',
|
|
10
|
+
'en-au',
|
|
11
|
+
'en-ca',
|
|
12
|
+
'en-nz',
|
|
13
|
+
'en-ie',
|
|
14
|
+
'en-za',
|
|
15
|
+
'en-jm',
|
|
16
|
+
'en-bz',
|
|
17
|
+
'en-tt',
|
|
18
|
+
'pt-br',
|
|
19
|
+
'pt',
|
|
20
|
+
'es',
|
|
21
|
+
'es-ar',
|
|
22
|
+
'es-gt',
|
|
23
|
+
'es-cr',
|
|
24
|
+
'es-pa',
|
|
25
|
+
'es-do',
|
|
26
|
+
'es-mx',
|
|
27
|
+
'es-ve',
|
|
28
|
+
'es-co',
|
|
29
|
+
'es-pe',
|
|
30
|
+
'es-ec',
|
|
31
|
+
'es-cl',
|
|
32
|
+
'es-uy',
|
|
33
|
+
'es-py',
|
|
34
|
+
'es-bo',
|
|
35
|
+
'es-sv',
|
|
36
|
+
'es-hn',
|
|
37
|
+
'es-ni',
|
|
38
|
+
'es-pr',
|
|
39
|
+
] as const;
|
|
40
|
+
|
|
41
|
+
public static readonly DEFAULT: Language = new Language('es');
|
|
42
|
+
public static readonly ENGLISH: Language = new Language('en');
|
|
43
|
+
public static readonly ENGLISH_UNITED_STATES: Language = new Language('en-us');
|
|
44
|
+
public static readonly ENGLISH_UNITED_KINGDOM: Language = new Language('en-gb');
|
|
45
|
+
public static readonly ENGLISH_AUSTRALIA: Language = new Language('en-au');
|
|
46
|
+
public static readonly ENGLISH_CANADA: Language = new Language('en-ca');
|
|
47
|
+
public static readonly ENGLISH_NEW_ZEALAND: Language = new Language('en-nz');
|
|
48
|
+
public static readonly ENGLISH_IRELAND: Language = new Language('en-ie');
|
|
49
|
+
public static readonly ENGLISH_SOUTH_AFRICA: Language = new Language('en-za');
|
|
50
|
+
public static readonly ENGLISH_JAMAICA: Language = new Language('en-jm');
|
|
51
|
+
public static readonly ENGLISH_BELIZE: Language = new Language('en-bz');
|
|
52
|
+
public static readonly ENGLISH_TRINIDAD: Language = new Language('en-tt');
|
|
53
|
+
public static readonly PORTUGUESE_BRAZIL: Language = new Language('pt-br');
|
|
54
|
+
public static readonly PORTUGUESE_PORTUGAL: Language = new Language('pt');
|
|
55
|
+
public static readonly SPANISH: Language = new Language('es');
|
|
56
|
+
public static readonly SPANISH_ARGENTINA: Language = new Language('es-ar');
|
|
57
|
+
public static readonly SPANISH_GUATEMALA: Language = new Language('es-gt');
|
|
58
|
+
public static readonly SPANISH_COSTA_RICA: Language = new Language('es-cr');
|
|
59
|
+
public static readonly SPANISH_PANAMA: Language = new Language('es-pa');
|
|
60
|
+
public static readonly SPANISH_REPUBLICA_DOMINICANA: Language = new Language('es-do');
|
|
61
|
+
public static readonly SPANISH_MEXICO: Language = new Language('es-mx');
|
|
62
|
+
public static readonly SPANISH_VENEZUELA: Language = new Language('es-ve');
|
|
63
|
+
public static readonly SPANISH_COLOMBIA: Language = new Language('es-co');
|
|
64
|
+
public static readonly SPANISH_PERU: Language = new Language('es-pe');
|
|
65
|
+
public static readonly SPANISH_ECUADOR: Language = new Language('es-ec');
|
|
66
|
+
public static readonly SPANISH_CHILE: Language = new Language('es-cl');
|
|
67
|
+
public static readonly SPANISH_URUGUAY: Language = new Language('es-uy');
|
|
68
|
+
public static readonly SPANISH_PARAGUAY: Language = new Language('es-py');
|
|
69
|
+
public static readonly SPANISH_BOLIVIA: Language = new Language('es-bo');
|
|
70
|
+
public static readonly SPANISH_EL_SALVADOR: Language = new Language('es-sv');
|
|
71
|
+
public static readonly SPANISH_HONDURAS: Language = new Language('es-hn');
|
|
72
|
+
public static readonly SPANISH_NICARAGUA: Language = new Language('es-ni');
|
|
73
|
+
public static readonly SPANISH_PUERTO_RICO: Language = new Language('es-pr');
|
|
74
|
+
|
|
75
|
+
private constructor(code: string) {
|
|
76
|
+
super(code.trim().toLowerCase());
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
protected validate(value: string): void {
|
|
80
|
+
if (!Language.SUPPORTED.includes(value)) {
|
|
81
|
+
throw new Error(`Language <${value}> is not supported`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public base(): string {
|
|
86
|
+
return this.value.split('-')[0];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public static create(raw: string): Language {
|
|
90
|
+
const normalized = raw.trim().toLowerCase().replace('_', '-');
|
|
91
|
+
return new Language(normalized);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { ValueObject } from '@domain/contracts/ValueObject';
|
|
2
|
+
import { Currency } from '@domain/value-objects/Currency';
|
|
3
|
+
|
|
4
|
+
export class Price extends ValueObject<{ amount: number; currency: Currency }> {
|
|
5
|
+
|
|
6
|
+
public static readonly MIN_AMOUNT: number = -1000000;
|
|
7
|
+
|
|
8
|
+
public readonly amount: number;
|
|
9
|
+
public readonly currency: Currency;
|
|
10
|
+
|
|
11
|
+
private constructor(amount: number, currency: Currency) {
|
|
12
|
+
super({ amount, currency });
|
|
13
|
+
this.amount = amount;
|
|
14
|
+
this.currency = currency;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
protected validate(props: { amount: number; currency: Currency }): void {
|
|
18
|
+
const { amount, currency } = props;
|
|
19
|
+
|
|
20
|
+
if (typeof amount !== 'number' || Number.isNaN(amount) || !Number.isFinite(amount)) {
|
|
21
|
+
throw new Error(`Price amount <${amount}> is not a valid number`);
|
|
22
|
+
}
|
|
23
|
+
if (amount < Price.MIN_AMOUNT) {
|
|
24
|
+
throw new Error(`Price amount <${amount}> must be ≥ ${Price.MIN_AMOUNT}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public equals(other?: Price | null): boolean {
|
|
29
|
+
if (!other) return false;
|
|
30
|
+
return this.amount === other.amount && this.currency.equals(other.currency);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private assertSameCurrency(other: Price): void {
|
|
34
|
+
if (!this.currency.equals(other.currency)) {
|
|
35
|
+
throw new Error('Cannot operate on Price objects with different currencies');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public add(other: Price): Price {
|
|
40
|
+
this.assertSameCurrency(other);
|
|
41
|
+
return Price.create(this.amount + other.amount, this.currency);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public subtract(other: Price): Price {
|
|
45
|
+
this.assertSameCurrency(other);
|
|
46
|
+
return Price.create(this.amount - other.amount, this.currency);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public static create(amount: number, currency: Currency | string | number): Price {
|
|
50
|
+
const cur = currency instanceof Currency ? currency : Currency.create(currency);
|
|
51
|
+
return new Price(amount, cur);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {ValueObject} from "@domain/contracts/ValueObject";
|
|
2
|
+
|
|
3
|
+
export class UUID extends ValueObject<string> {
|
|
4
|
+
|
|
5
|
+
private constructor(value: string) {
|
|
6
|
+
super(value);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
protected validate(uuid: string) {
|
|
10
|
+
if (!UUID.isValid(uuid)) {
|
|
11
|
+
throw new Error(`Invalid uuid ${uuid}`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public static create(uuid?: string): UUID {
|
|
16
|
+
return new UUID(uuid ?? crypto.randomUUID());
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public static isValid(uuid: string): boolean {
|
|
20
|
+
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);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
}
|