wsp-ms-core 1.0.67 → 1.0.69
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 +183 -41
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +66 -2
- package/dist/index.d.ts +66 -2
- package/dist/index.js +181 -41
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -277,6 +277,7 @@ declare class PaymentStatus extends ValueObject<string> {
|
|
|
277
277
|
static readonly FAILED: PaymentStatus;
|
|
278
278
|
static readonly CANCELED: PaymentStatus;
|
|
279
279
|
static readonly HOLD: PaymentStatus;
|
|
280
|
+
static readonly PENDING_REFUND: PaymentStatus;
|
|
280
281
|
static readonly REFUNDED: PaymentStatus;
|
|
281
282
|
private constructor();
|
|
282
283
|
protected validate(status: string): void;
|
|
@@ -286,6 +287,7 @@ declare class PaymentStatus extends ValueObject<string> {
|
|
|
286
287
|
get isFailed(): boolean;
|
|
287
288
|
get isCanceled(): boolean;
|
|
288
289
|
get isHold(): boolean;
|
|
290
|
+
get isPendingRefund(): boolean;
|
|
289
291
|
get isRefunded(): boolean;
|
|
290
292
|
toPrimitives(): Record<string, unknown>;
|
|
291
293
|
static create(gateway: string): PaymentStatus;
|
|
@@ -337,6 +339,55 @@ declare class BasicUnitOfWorkFactory {
|
|
|
337
339
|
create(): Promise<BasicUnitOfWork>;
|
|
338
340
|
}
|
|
339
341
|
|
|
342
|
+
interface EventMessage {
|
|
343
|
+
producer?: string;
|
|
344
|
+
eventUuid: string;
|
|
345
|
+
eventType: string;
|
|
346
|
+
tenantUuid: string;
|
|
347
|
+
topic: string;
|
|
348
|
+
payload: Record<string, any>;
|
|
349
|
+
date?: string;
|
|
350
|
+
}
|
|
351
|
+
interface EventManagerConnection {
|
|
352
|
+
userName: string;
|
|
353
|
+
password: string;
|
|
354
|
+
brokers: string[];
|
|
355
|
+
}
|
|
356
|
+
type RouteCallback = (message: EventMessage) => void;
|
|
357
|
+
interface RoutesCallbackList {
|
|
358
|
+
[key: string]: RouteCallback;
|
|
359
|
+
}
|
|
360
|
+
declare abstract class EventManager {
|
|
361
|
+
protected _connection: EventManagerConnection;
|
|
362
|
+
protected _topics: string[];
|
|
363
|
+
protected _callbackList: RoutesCallbackList;
|
|
364
|
+
protected _onStart: CallableFunction | null;
|
|
365
|
+
protected _onConnected: CallableFunction | null;
|
|
366
|
+
protected _onSubscribe: CallableFunction | null;
|
|
367
|
+
protected _onMessage: CallableFunction | null;
|
|
368
|
+
protected _onError: CallableFunction | null;
|
|
369
|
+
protected _onCrash: CallableFunction | null;
|
|
370
|
+
protected _onReconnect: CallableFunction | null;
|
|
371
|
+
protected constructor(connection: EventManagerConnection);
|
|
372
|
+
protected execRoute(topic: string, message: EventMessage): Promise<void>;
|
|
373
|
+
protected execCallback(callback: CallableFunction | null, data: any): Promise<void>;
|
|
374
|
+
abstract send(message: EventMessage): void;
|
|
375
|
+
abstract start(): void;
|
|
376
|
+
abstract restart(): void;
|
|
377
|
+
abstract stop(): void;
|
|
378
|
+
abstract pause(): void;
|
|
379
|
+
route(topic: string, callback: RouteCallback): void;
|
|
380
|
+
onStart(callback: CallableFunction): void;
|
|
381
|
+
onConnected(callback: CallableFunction): void;
|
|
382
|
+
onSubscribe(callback: CallableFunction): void;
|
|
383
|
+
onMessage(callback: CallableFunction): void;
|
|
384
|
+
onError(callback: CallableFunction): void;
|
|
385
|
+
onCrash(callback: CallableFunction): void;
|
|
386
|
+
onReconnect(callback: CallableFunction): void;
|
|
387
|
+
get topics(): string[];
|
|
388
|
+
get callbackList(): RoutesCallbackList;
|
|
389
|
+
}
|
|
390
|
+
|
|
340
391
|
interface Logger {
|
|
341
392
|
debug(type: string, message: string, meta?: Record<string, any>): void;
|
|
342
393
|
info(type: string, message: string, meta?: Record<string, any>): void;
|
|
@@ -430,13 +481,26 @@ declare function adaptExpressErrorHandler(errorManager: ErrorManager): ErrorRequ
|
|
|
430
481
|
|
|
431
482
|
declare class EventBusMysqlRepository implements EventBusRepository {
|
|
432
483
|
private readonly connection;
|
|
433
|
-
constructor(connection:
|
|
484
|
+
constructor(connection: DatabaseConnection);
|
|
434
485
|
private eventToRowValues;
|
|
435
486
|
create(event: DomainEvent): Promise<void>;
|
|
436
487
|
update(event: DomainEvent): Promise<void>;
|
|
437
488
|
listPending(limit: number): Promise<DomainEvent[]>;
|
|
438
489
|
}
|
|
439
490
|
|
|
491
|
+
declare class KafkaManager extends EventManager {
|
|
492
|
+
private readonly kafka;
|
|
493
|
+
private readonly consumer;
|
|
494
|
+
private readonly producer;
|
|
495
|
+
constructor(connection: EventManagerConnection);
|
|
496
|
+
private run;
|
|
497
|
+
send(message: EventMessage): Promise<void>;
|
|
498
|
+
start(autocommit?: boolean): Promise<void>;
|
|
499
|
+
pause(): Promise<void>;
|
|
500
|
+
restart(): Promise<void>;
|
|
501
|
+
stop(): Promise<void>;
|
|
502
|
+
}
|
|
503
|
+
|
|
440
504
|
interface ExchangeRatesProps {
|
|
441
505
|
base: Currency;
|
|
442
506
|
rates: Record<string, number>;
|
|
@@ -455,4 +519,4 @@ declare class ExchangeRates extends BaseObject<ExchangeRatesProps> {
|
|
|
455
519
|
static createFromPrimitives(data: any): ExchangeRates;
|
|
456
520
|
}
|
|
457
521
|
|
|
458
|
-
export { BaseEntity, BaseObject, BasicUnitOfWork, BasicUnitOfWorkFactory, Currency, DatabaseConnection, DatabaseConnector, DateTime, DomainEntity, DomainError, DomainEvent, DomainEventStatus, Email, ErrorManager, ErrorManagerHandleResult, ErrorTemplate, EventBus, EventBusMysqlRepository, EventBusRepository, ExchangeRates, FatalError, HttpController, HttpHealthCheckController, HttpNotFoundController, HttpRequest, HttpResponse, InternalError, Language, Logger, MysqlConnection, MysqlConnector, PaymentGateway, PaymentStatus, Price, UUID, UnitOfWork, UploadedFile, UsageError, ValueObject, adaptExpressErrorHandler, adaptExpressRoute };
|
|
522
|
+
export { BaseEntity, BaseObject, BasicUnitOfWork, BasicUnitOfWorkFactory, Currency, DatabaseConnection, DatabaseConnector, DateTime, DomainEntity, DomainError, DomainEvent, DomainEventStatus, Email, ErrorManager, ErrorManagerHandleResult, ErrorTemplate, EventBus, EventBusMysqlRepository, EventBusRepository, EventManager, EventManagerConnection, EventMessage, ExchangeRates, FatalError, HttpController, HttpHealthCheckController, HttpNotFoundController, HttpRequest, HttpResponse, InternalError, KafkaManager, Language, Logger, MysqlConnection, MysqlConnector, PaymentGateway, PaymentStatus, Price, RouteCallback, RoutesCallbackList, UUID, UnitOfWork, UploadedFile, UsageError, ValueObject, adaptExpressErrorHandler, adaptExpressRoute };
|
package/dist/index.js
CHANGED
|
@@ -194,7 +194,7 @@ var _UUID = class _UUID extends ValueObject {
|
|
|
194
194
|
return { value: this.value };
|
|
195
195
|
}
|
|
196
196
|
static create(uuid) {
|
|
197
|
-
return new _UUID(uuid
|
|
197
|
+
return new _UUID(uuid ?? crypto.randomUUID());
|
|
198
198
|
}
|
|
199
199
|
static version(uuid) {
|
|
200
200
|
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);
|
|
@@ -207,9 +207,8 @@ var _UUID = class _UUID extends ValueObject {
|
|
|
207
207
|
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);
|
|
208
208
|
}
|
|
209
209
|
static isValid(uuid, opts = {}) {
|
|
210
|
-
|
|
211
|
-
const
|
|
212
|
-
const allowNil = (_b = opts.allowNil) != null ? _b : false;
|
|
210
|
+
const allowed = opts.allowedVersions ?? [1, 2, 3, 4, 5, 6, 7, 8];
|
|
211
|
+
const allowNil = opts.allowNil ?? false;
|
|
213
212
|
if (allowNil && _UUID.isNil(uuid))
|
|
214
213
|
return true;
|
|
215
214
|
if (!_UUID.isRFCStyle(uuid))
|
|
@@ -318,7 +317,6 @@ var DomainEvent = class _DomainEvent {
|
|
|
318
317
|
this._errorMessage = error;
|
|
319
318
|
}
|
|
320
319
|
toPrimitives() {
|
|
321
|
-
var _a, _b, _c, _d, _e;
|
|
322
320
|
return {
|
|
323
321
|
eventUuid: this.eventUuid.value,
|
|
324
322
|
eventType: this.eventType,
|
|
@@ -329,14 +327,13 @@ var DomainEvent = class _DomainEvent {
|
|
|
329
327
|
payload: this.payload,
|
|
330
328
|
status: this.status.value,
|
|
331
329
|
attempts: this.attempts,
|
|
332
|
-
errorMessage:
|
|
333
|
-
publishedAt:
|
|
334
|
-
lastAttempt:
|
|
330
|
+
errorMessage: this.errorMessage ?? void 0,
|
|
331
|
+
publishedAt: this.publishedAt?.value ?? void 0,
|
|
332
|
+
lastAttempt: this.lastAttempt?.value ?? void 0,
|
|
335
333
|
createdAt: this.createdAt.value
|
|
336
334
|
};
|
|
337
335
|
}
|
|
338
336
|
static reconstitute(data) {
|
|
339
|
-
var _a;
|
|
340
337
|
return new _DomainEvent(
|
|
341
338
|
UUID.create(data.event_uuid),
|
|
342
339
|
String(data.event_type),
|
|
@@ -347,7 +344,7 @@ var DomainEvent = class _DomainEvent {
|
|
|
347
344
|
String(data.payload),
|
|
348
345
|
DomainEventStatus.create(data.status),
|
|
349
346
|
Number(data.attempts),
|
|
350
|
-
|
|
347
|
+
data.error_message ?? void 0,
|
|
351
348
|
data.published_at ? DateTime.create(data.published_at) : void 0,
|
|
352
349
|
data.last_attempt ? DateTime.create(data.last_attempt) : void 0,
|
|
353
350
|
data.created_at ? DateTime.create(data.created_at) : void 0
|
|
@@ -586,38 +583,33 @@ var _ErrorManager = class _ErrorManager {
|
|
|
586
583
|
return _ErrorManager.DEFAULT_MESSAGES[lang.value] || _ErrorManager.DEFAULT_MESSAGES[lang.base()] || "error";
|
|
587
584
|
}
|
|
588
585
|
onFatal(err, lang) {
|
|
589
|
-
|
|
590
|
-
(_a = this.logger) == null ? void 0 : _a.fatal(err.type, err.message);
|
|
586
|
+
this.logger?.fatal(err.type, err.message);
|
|
591
587
|
return { status: "ERROR", message: this.getDefaultMessage(lang) };
|
|
592
588
|
}
|
|
593
589
|
onInternal(err, lang) {
|
|
594
|
-
|
|
595
|
-
(_a = this.logger) == null ? void 0 : _a.error(err.type, err.message);
|
|
590
|
+
this.logger?.error(err.type, err.message);
|
|
596
591
|
return { status: "ERROR", message: this.getDefaultMessage(lang) };
|
|
597
592
|
}
|
|
598
593
|
onUsage(err, lang) {
|
|
599
|
-
var _a, _b, _c;
|
|
600
594
|
const tmpl = _ErrorManager.TEMPLATES.get(err.type);
|
|
601
595
|
if (!tmpl) {
|
|
602
|
-
|
|
596
|
+
this.logger?.error("TEMPLATE_NOT_FOUND", `${err.type}`);
|
|
603
597
|
return { status: "ERROR", message: this.getDefaultMessage(lang) };
|
|
604
598
|
}
|
|
605
599
|
const code = lang.value;
|
|
606
600
|
const base = lang.base();
|
|
607
|
-
const rawMsg =
|
|
601
|
+
const rawMsg = tmpl.languages[code] ?? tmpl.languages[base] ?? this.getDefaultMessage(lang);
|
|
608
602
|
return {
|
|
609
603
|
status: 400,
|
|
610
604
|
message: StringVars.parse(rawMsg, err.vars)
|
|
611
605
|
};
|
|
612
606
|
}
|
|
613
607
|
onUnknown(err, lang) {
|
|
614
|
-
|
|
615
|
-
(_a = this.logger) == null ? void 0 : _a.error("UNKNOWN_ERROR", err.message);
|
|
608
|
+
this.logger?.error("UNKNOWN_ERROR", err.message);
|
|
616
609
|
return { status: "ERROR", message: this.getDefaultMessage(lang) };
|
|
617
610
|
}
|
|
618
611
|
handle(err, lang) {
|
|
619
|
-
|
|
620
|
-
if (["local", "dev"].includes((_a = process.env.ENVIRONMENT) != null ? _a : "")) {
|
|
612
|
+
if (["local", "dev"].includes(process.env.ENVIRONMENT ?? "")) {
|
|
621
613
|
console.log(err);
|
|
622
614
|
}
|
|
623
615
|
if (err instanceof FatalError) {
|
|
@@ -785,6 +777,9 @@ var _PaymentStatus = class _PaymentStatus extends ValueObject {
|
|
|
785
777
|
get isHold() {
|
|
786
778
|
return this.value === "HOLD";
|
|
787
779
|
}
|
|
780
|
+
get isPendingRefund() {
|
|
781
|
+
return this.value === "PENDING_REFUND";
|
|
782
|
+
}
|
|
788
783
|
get isRefunded() {
|
|
789
784
|
return this.value === "REFUNDED";
|
|
790
785
|
}
|
|
@@ -795,13 +790,14 @@ var _PaymentStatus = class _PaymentStatus extends ValueObject {
|
|
|
795
790
|
return new _PaymentStatus(gateway.trim().toUpperCase());
|
|
796
791
|
}
|
|
797
792
|
};
|
|
798
|
-
_PaymentStatus.SUPPORTED = ["DONE", "PENDING", "FAILED", "CANCELED", "HOLD", "REFUNDED", "IN_PROGRESS"];
|
|
793
|
+
_PaymentStatus.SUPPORTED = ["DONE", "PENDING", "FAILED", "CANCELED", "HOLD", "PENDING_REFUND", "REFUNDED", "IN_PROGRESS"];
|
|
799
794
|
_PaymentStatus.DONE = new _PaymentStatus("DONE");
|
|
800
795
|
_PaymentStatus.PENDING = new _PaymentStatus("PENDING");
|
|
801
796
|
_PaymentStatus.IN_PROGRESS = new _PaymentStatus("IN_PROGRESS");
|
|
802
797
|
_PaymentStatus.FAILED = new _PaymentStatus("FAILED");
|
|
803
798
|
_PaymentStatus.CANCELED = new _PaymentStatus("CANCELED");
|
|
804
799
|
_PaymentStatus.HOLD = new _PaymentStatus("HOLD");
|
|
800
|
+
_PaymentStatus.PENDING_REFUND = new _PaymentStatus("PENDING_REFUND");
|
|
805
801
|
_PaymentStatus.REFUNDED = new _PaymentStatus("REFUNDED");
|
|
806
802
|
var PaymentStatus = _PaymentStatus;
|
|
807
803
|
|
|
@@ -851,14 +847,70 @@ var BasicUnitOfWorkFactory = class {
|
|
|
851
847
|
}
|
|
852
848
|
};
|
|
853
849
|
|
|
850
|
+
// src/infrastructure/contracts/EventManager.ts
|
|
851
|
+
var EventManager = class {
|
|
852
|
+
constructor(connection) {
|
|
853
|
+
this._connection = connection;
|
|
854
|
+
this._topics = [];
|
|
855
|
+
this._callbackList = {};
|
|
856
|
+
this._onStart = null;
|
|
857
|
+
this._onConnected = null;
|
|
858
|
+
this._onSubscribe = null;
|
|
859
|
+
this._onMessage = null;
|
|
860
|
+
this._onError = null;
|
|
861
|
+
this._onCrash = null;
|
|
862
|
+
this._onReconnect = null;
|
|
863
|
+
}
|
|
864
|
+
async execRoute(topic, message) {
|
|
865
|
+
if (this._callbackList[topic]) {
|
|
866
|
+
await this._callbackList[topic](message);
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
async execCallback(callback, data) {
|
|
870
|
+
if (callback) {
|
|
871
|
+
await callback(data);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
route(topic, callback) {
|
|
875
|
+
this._topics.push(topic);
|
|
876
|
+
this._callbackList[topic] = callback;
|
|
877
|
+
}
|
|
878
|
+
onStart(callback) {
|
|
879
|
+
this._onStart = callback;
|
|
880
|
+
}
|
|
881
|
+
onConnected(callback) {
|
|
882
|
+
this._onConnected = callback;
|
|
883
|
+
}
|
|
884
|
+
onSubscribe(callback) {
|
|
885
|
+
this._onSubscribe = callback;
|
|
886
|
+
}
|
|
887
|
+
onMessage(callback) {
|
|
888
|
+
this._onMessage = callback;
|
|
889
|
+
}
|
|
890
|
+
onError(callback) {
|
|
891
|
+
this._onError = callback;
|
|
892
|
+
}
|
|
893
|
+
onCrash(callback) {
|
|
894
|
+
this._onCrash = callback;
|
|
895
|
+
}
|
|
896
|
+
onReconnect(callback) {
|
|
897
|
+
this._onReconnect = callback;
|
|
898
|
+
}
|
|
899
|
+
get topics() {
|
|
900
|
+
return this._topics;
|
|
901
|
+
}
|
|
902
|
+
get callbackList() {
|
|
903
|
+
return this._callbackList;
|
|
904
|
+
}
|
|
905
|
+
};
|
|
906
|
+
|
|
854
907
|
// src/infrastructure/mysql/Mysql.ts
|
|
855
908
|
import { createPool } from "mysql2/promise";
|
|
856
909
|
var _MysqlConnector = class _MysqlConnector {
|
|
857
910
|
constructor(pool) {
|
|
858
|
-
|
|
859
|
-
this._pool = pool != null ? pool : createPool({
|
|
911
|
+
this._pool = pool ?? createPool({
|
|
860
912
|
host: process.env.DB_HOST,
|
|
861
|
-
port: Number(
|
|
913
|
+
port: Number(process.env.DB_PORT ?? 3306),
|
|
862
914
|
user: process.env.DB_USER,
|
|
863
915
|
password: process.env.DB_PASSWORD,
|
|
864
916
|
database: process.env.DB_DATABASE,
|
|
@@ -971,7 +1023,6 @@ function toUtcDateTimeString(raw) {
|
|
|
971
1023
|
return `${d.getUTCFullYear()}-${pad(d.getUTCMonth() + 1)}-${pad(d.getUTCDate())} ${pad(d.getUTCHours())}:${pad(d.getUTCMinutes())}:${pad(d.getUTCSeconds())}`;
|
|
972
1024
|
}
|
|
973
1025
|
function coerceScalar(key, v) {
|
|
974
|
-
var _a;
|
|
975
1026
|
if (v == null)
|
|
976
1027
|
return void 0;
|
|
977
1028
|
if (typeof v !== "string")
|
|
@@ -992,7 +1043,7 @@ function coerceScalar(key, v) {
|
|
|
992
1043
|
return n;
|
|
993
1044
|
}
|
|
994
1045
|
if (DATE_KEY_RE.test(key)) {
|
|
995
|
-
return
|
|
1046
|
+
return toUtcDateTimeString(s) ?? s;
|
|
996
1047
|
}
|
|
997
1048
|
return s;
|
|
998
1049
|
}
|
|
@@ -1019,8 +1070,7 @@ function hasHeader(headers, name) {
|
|
|
1019
1070
|
}
|
|
1020
1071
|
function adaptExpressRoute(Controller) {
|
|
1021
1072
|
return async (req, res, next) => {
|
|
1022
|
-
|
|
1023
|
-
const rawLangHeader = (_b = (_a = req.headers["accept-language"]) != null ? _a : req.headers["Accept-Language"]) != null ? _b : "es";
|
|
1073
|
+
const rawLangHeader = req.headers["accept-language"] ?? req.headers["Accept-Language"] ?? "es";
|
|
1024
1074
|
const rawLang = Array.isArray(rawLangHeader) ? rawLangHeader[0] : rawLangHeader || "";
|
|
1025
1075
|
const lang = rawLang.split(",")[0].split(";")[0].trim().toLowerCase();
|
|
1026
1076
|
const httpRequest = {
|
|
@@ -1068,7 +1118,7 @@ function adaptExpressRoute(Controller) {
|
|
|
1068
1118
|
res.status(statusCode).send(body);
|
|
1069
1119
|
return;
|
|
1070
1120
|
}
|
|
1071
|
-
res.status(statusCode).json(body
|
|
1121
|
+
res.status(statusCode).json(body ?? {});
|
|
1072
1122
|
} catch (err) {
|
|
1073
1123
|
next(err);
|
|
1074
1124
|
}
|
|
@@ -1076,9 +1126,8 @@ function adaptExpressRoute(Controller) {
|
|
|
1076
1126
|
}
|
|
1077
1127
|
function adaptExpressErrorHandler(errorManager) {
|
|
1078
1128
|
return (err, req, res, next) => {
|
|
1079
|
-
|
|
1080
|
-
const
|
|
1081
|
-
const rawLang = Array.isArray(raw) ? raw[0] : raw != null ? raw : "";
|
|
1129
|
+
const raw = req.headers["accept-language"] ?? req.headers["Accept-Language"] ?? "es";
|
|
1130
|
+
const rawLang = Array.isArray(raw) ? raw[0] : raw ?? "";
|
|
1082
1131
|
const langCode = rawLang.split(",")[0].split(";")[0].trim().toLowerCase();
|
|
1083
1132
|
const result = errorManager.handle(err, Language.create(langCode));
|
|
1084
1133
|
const statusCode = typeof result.status === "number" ? result.status : 500;
|
|
@@ -1092,7 +1141,6 @@ var EventBusMysqlRepository = class {
|
|
|
1092
1141
|
this.connection = connection;
|
|
1093
1142
|
}
|
|
1094
1143
|
eventToRowValues(e) {
|
|
1095
|
-
var _a, _b;
|
|
1096
1144
|
return [
|
|
1097
1145
|
e.eventUuid.value,
|
|
1098
1146
|
e.eventType,
|
|
@@ -1104,8 +1152,8 @@ var EventBusMysqlRepository = class {
|
|
|
1104
1152
|
e.status.value,
|
|
1105
1153
|
e.attempts,
|
|
1106
1154
|
e.errorMessage,
|
|
1107
|
-
|
|
1108
|
-
|
|
1155
|
+
e.publishedAt?.value,
|
|
1156
|
+
e.lastAttempt?.value,
|
|
1109
1157
|
e.createdAt.value
|
|
1110
1158
|
];
|
|
1111
1159
|
}
|
|
@@ -1119,8 +1167,7 @@ var EventBusMysqlRepository = class {
|
|
|
1119
1167
|
);
|
|
1120
1168
|
}
|
|
1121
1169
|
async update(event) {
|
|
1122
|
-
|
|
1123
|
-
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];
|
|
1170
|
+
const values = [event.status.value, event.attempts, event.errorMessage, event.publishedAt?.value, event.lastAttempt?.value, event.eventUuid.value];
|
|
1124
1171
|
await this.connection.query(
|
|
1125
1172
|
`UPDATE events_outbox
|
|
1126
1173
|
SET status = ?,
|
|
@@ -1141,6 +1188,98 @@ var EventBusMysqlRepository = class {
|
|
|
1141
1188
|
}
|
|
1142
1189
|
};
|
|
1143
1190
|
|
|
1191
|
+
// src/infrastructure/kafka/KafkaManager.ts
|
|
1192
|
+
import { Kafka, logLevel } from "kafkajs";
|
|
1193
|
+
var KafkaManager = class extends EventManager {
|
|
1194
|
+
constructor(connection) {
|
|
1195
|
+
super(connection);
|
|
1196
|
+
this.kafka = new Kafka({
|
|
1197
|
+
brokers: this._connection.brokers,
|
|
1198
|
+
ssl: true,
|
|
1199
|
+
sasl: {
|
|
1200
|
+
mechanism: "scram-sha-256",
|
|
1201
|
+
username: this._connection.userName,
|
|
1202
|
+
password: this._connection.password
|
|
1203
|
+
},
|
|
1204
|
+
logLevel: logLevel.ERROR
|
|
1205
|
+
});
|
|
1206
|
+
this.consumer = this.kafka.consumer({ groupId: process.env.KAFKA_GROUP || "default" });
|
|
1207
|
+
this.producer = this.kafka.producer();
|
|
1208
|
+
}
|
|
1209
|
+
async run(autocommit) {
|
|
1210
|
+
const __run = async () => {
|
|
1211
|
+
await this.execCallback(this._onStart, { message: `--- ${this.constructor.name} started ---` });
|
|
1212
|
+
await this.consumer.connect();
|
|
1213
|
+
await this.execCallback(this._onConnected, { message: `--- ${this.constructor.name} connected to ${this._connection.brokers} ---` });
|
|
1214
|
+
for (let topic of this._topics) {
|
|
1215
|
+
try {
|
|
1216
|
+
await this.consumer.subscribe({ topic, fromBeginning: true });
|
|
1217
|
+
await this.execCallback(this._onSubscribe, { message: `--- ${this.constructor.name} subscribed to ${topic} ---` });
|
|
1218
|
+
} catch (error) {
|
|
1219
|
+
await this.execCallback(this._onConnected, { message: `Error on subscribe to kafka topic: ${topic}` });
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
await this.consumer.run({
|
|
1223
|
+
autoCommit: autocommit,
|
|
1224
|
+
// @ts-ignore
|
|
1225
|
+
eachMessage: async ({ topic, partition, message, heartbeat }) => {
|
|
1226
|
+
try {
|
|
1227
|
+
await this.execCallback(this._onMessage, `[New message detected for ${topic}]: ${message.value?.toString()}`);
|
|
1228
|
+
const evt = JSON.parse(String(message.value?.toString()));
|
|
1229
|
+
await this.execRoute(topic, evt);
|
|
1230
|
+
const next = (BigInt(message.offset) + 1n).toString();
|
|
1231
|
+
await this.consumer.commitOffsets([{ topic, partition, offset: next }]);
|
|
1232
|
+
await heartbeat();
|
|
1233
|
+
} catch (error) {
|
|
1234
|
+
await this.execCallback(this._onError, error);
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
});
|
|
1238
|
+
};
|
|
1239
|
+
try {
|
|
1240
|
+
await __run();
|
|
1241
|
+
} catch (error) {
|
|
1242
|
+
await this.execCallback(this._onError, new InternalError(ErrorManager.APP_ERRORS.PROCESS, error.toString()));
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
async send(message) {
|
|
1246
|
+
if (!message.producer) {
|
|
1247
|
+
message.producer = process.env.NAME && process.env.ENVIRONMENT ? `${process.env.NAME}-${process.env.ENVIRONMENT}` : "unknown";
|
|
1248
|
+
}
|
|
1249
|
+
if (!message.date) {
|
|
1250
|
+
message.date = DateTime.now().value;
|
|
1251
|
+
}
|
|
1252
|
+
try {
|
|
1253
|
+
if (!this.producer) {
|
|
1254
|
+
throw new InternalError(ErrorManager.APP_ERRORS.PROCESS, "Producer not initialized");
|
|
1255
|
+
}
|
|
1256
|
+
await this.producer.connect();
|
|
1257
|
+
await this.producer.send({
|
|
1258
|
+
topic: message.topic,
|
|
1259
|
+
messages: [{ value: JSON.stringify({ producer: message.producer, data: message }) }]
|
|
1260
|
+
});
|
|
1261
|
+
await this.producer.disconnect();
|
|
1262
|
+
} catch (error) {
|
|
1263
|
+
await this.execCallback(this._onError, new InternalError(ErrorManager.APP_ERRORS.PROCESS, error.toString()));
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
async start(autocommit = false) {
|
|
1267
|
+
this.consumer.on(this.consumer.events.CRASH, async (error) => {
|
|
1268
|
+
await this.execCallback(this._onError, new InternalError(ErrorManager.APP_ERRORS.PROCESS, error.payload.error.stack));
|
|
1269
|
+
});
|
|
1270
|
+
await this.run(autocommit);
|
|
1271
|
+
}
|
|
1272
|
+
async pause() {
|
|
1273
|
+
}
|
|
1274
|
+
async restart() {
|
|
1275
|
+
await this.consumer.stop();
|
|
1276
|
+
await this.start();
|
|
1277
|
+
}
|
|
1278
|
+
async stop() {
|
|
1279
|
+
await this.consumer.stop();
|
|
1280
|
+
}
|
|
1281
|
+
};
|
|
1282
|
+
|
|
1144
1283
|
// src/utils/ExchangeRates.ts
|
|
1145
1284
|
var ExchangeRates = class _ExchangeRates extends BaseObject {
|
|
1146
1285
|
constructor(props) {
|
|
@@ -1185,11 +1324,10 @@ var ExchangeRates = class _ExchangeRates extends BaseObject {
|
|
|
1185
1324
|
return new _ExchangeRates(props);
|
|
1186
1325
|
}
|
|
1187
1326
|
static createFromPrimitives(data) {
|
|
1188
|
-
var _a, _b;
|
|
1189
1327
|
return _ExchangeRates.create({
|
|
1190
1328
|
base: Currency.create(data.base),
|
|
1191
|
-
rates:
|
|
1192
|
-
date: DateTime.create(
|
|
1329
|
+
rates: data.rates ?? [],
|
|
1330
|
+
date: DateTime.create(data.date ?? "")
|
|
1193
1331
|
});
|
|
1194
1332
|
}
|
|
1195
1333
|
};
|
|
@@ -1207,11 +1345,13 @@ export {
|
|
|
1207
1345
|
ErrorManager,
|
|
1208
1346
|
EventBus,
|
|
1209
1347
|
EventBusMysqlRepository,
|
|
1348
|
+
EventManager,
|
|
1210
1349
|
ExchangeRates,
|
|
1211
1350
|
FatalError,
|
|
1212
1351
|
HttpHealthCheckController,
|
|
1213
1352
|
HttpNotFoundController,
|
|
1214
1353
|
InternalError,
|
|
1354
|
+
KafkaManager,
|
|
1215
1355
|
Language,
|
|
1216
1356
|
MysqlConnection,
|
|
1217
1357
|
MysqlConnector,
|