wsp-ms-core 1.0.81 → 1.0.83

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.d.mts CHANGED
@@ -45,6 +45,7 @@ declare class DateTime extends ValueObject<string> {
45
45
  get second(): number;
46
46
  getMonthName(locale?: string): string;
47
47
  getWeekdayName(locale?: string): string;
48
+ getDayName(locale?: string): string;
48
49
  format(format: string): string;
49
50
  toPrimitives(): Record<string, unknown>;
50
51
  static create(input?: string | number): DateTime;
@@ -354,7 +355,7 @@ declare class OutboxRecord {
354
355
  private _publishedAt;
355
356
  private _lastAttempt;
356
357
  private readonly _createdAt;
357
- protected constructor(eventUuid: UUID, eventType: string, tenantUuid: UUID, aggregateUuid: UUID, aggregateType: string, topic: string, payload: string, status: ProcessStatus, attempts: number, errorMessage: string | undefined, publishedAt: DateTime | undefined, lastAttempt: DateTime | undefined, createdAt: DateTime);
358
+ protected constructor(eventUuid: UUID, eventType: string, tenantUuid: UUID, aggregateUuid: UUID, aggregateType: string, topic: string, payload: string, status: ProcessStatus, attempts: number, errorMessage: string | undefined, publishedAt: DateTime | undefined, lastAttempt: DateTime | undefined, createdAt?: DateTime);
358
359
  get eventUuid(): UUID;
359
360
  get tenantUuid(): UUID;
360
361
  get aggregateUuid(): UUID;
@@ -433,12 +434,18 @@ declare class BasicUnitOfWorkFactory {
433
434
  create(): Promise<BasicUnitOfWork>;
434
435
  }
435
436
 
437
+ interface EventMessage {
438
+ topic: string;
439
+ producer: string;
440
+ tenant: UUID;
441
+ message: string;
442
+ }
436
443
  interface EventManagerConnection {
437
444
  userName: string;
438
445
  password: string;
439
446
  brokers: string[];
440
447
  }
441
- type RouteCallback = (message: string) => void;
448
+ type RouteCallback = (e: EventMessage) => void;
442
449
  interface RoutesCallbackList {
443
450
  [key: string]: RouteCallback;
444
451
  }
@@ -454,9 +461,9 @@ declare abstract class EventManager {
454
461
  protected _onCrash: CallableFunction | null;
455
462
  protected _onReconnect: CallableFunction | null;
456
463
  protected constructor(connection: EventManagerConnection);
457
- protected execRoute(topic: string, message: string): Promise<void>;
464
+ protected execRoute(topic: string, event: EventMessage): Promise<void>;
458
465
  protected execCallback(callback: CallableFunction | null, data: any): Promise<void>;
459
- abstract send(topic: string, message: string): void;
466
+ abstract send(event: EventMessage): void;
460
467
  abstract start(): void;
461
468
  abstract restart(): void;
462
469
  abstract stop(): void;
@@ -536,9 +543,9 @@ declare class ErrorManager {
536
543
  declare class EventBusMysqlRepository implements EventBusRepository {
537
544
  private readonly connection;
538
545
  constructor(connection: DatabaseConnection);
539
- private eventToRowValues;
540
- create(event: OutboxRecord): Promise<void>;
541
- update(event: OutboxRecord): Promise<void>;
546
+ private recordToRowValues;
547
+ create(record: OutboxRecord): Promise<void>;
548
+ update(record: OutboxRecord): Promise<void>;
542
549
  listPending(limit: number): Promise<OutboxRecord[]>;
543
550
  }
544
551
 
@@ -558,7 +565,7 @@ declare class KafkaManager extends EventManager {
558
565
  private readonly producer;
559
566
  constructor(connection: EventManagerConnection);
560
567
  private run;
561
- send(topic: string, message: string): Promise<void>;
568
+ send(e: EventMessage): Promise<void>;
562
569
  start(autocommit?: boolean): Promise<void>;
563
570
  pause(): Promise<void>;
564
571
  restart(): Promise<void>;
package/dist/index.d.ts CHANGED
@@ -45,6 +45,7 @@ declare class DateTime extends ValueObject<string> {
45
45
  get second(): number;
46
46
  getMonthName(locale?: string): string;
47
47
  getWeekdayName(locale?: string): string;
48
+ getDayName(locale?: string): string;
48
49
  format(format: string): string;
49
50
  toPrimitives(): Record<string, unknown>;
50
51
  static create(input?: string | number): DateTime;
@@ -354,7 +355,7 @@ declare class OutboxRecord {
354
355
  private _publishedAt;
355
356
  private _lastAttempt;
356
357
  private readonly _createdAt;
357
- protected constructor(eventUuid: UUID, eventType: string, tenantUuid: UUID, aggregateUuid: UUID, aggregateType: string, topic: string, payload: string, status: ProcessStatus, attempts: number, errorMessage: string | undefined, publishedAt: DateTime | undefined, lastAttempt: DateTime | undefined, createdAt: DateTime);
358
+ protected constructor(eventUuid: UUID, eventType: string, tenantUuid: UUID, aggregateUuid: UUID, aggregateType: string, topic: string, payload: string, status: ProcessStatus, attempts: number, errorMessage: string | undefined, publishedAt: DateTime | undefined, lastAttempt: DateTime | undefined, createdAt?: DateTime);
358
359
  get eventUuid(): UUID;
359
360
  get tenantUuid(): UUID;
360
361
  get aggregateUuid(): UUID;
@@ -433,12 +434,18 @@ declare class BasicUnitOfWorkFactory {
433
434
  create(): Promise<BasicUnitOfWork>;
434
435
  }
435
436
 
437
+ interface EventMessage {
438
+ topic: string;
439
+ producer: string;
440
+ tenant: UUID;
441
+ message: string;
442
+ }
436
443
  interface EventManagerConnection {
437
444
  userName: string;
438
445
  password: string;
439
446
  brokers: string[];
440
447
  }
441
- type RouteCallback = (message: string) => void;
448
+ type RouteCallback = (e: EventMessage) => void;
442
449
  interface RoutesCallbackList {
443
450
  [key: string]: RouteCallback;
444
451
  }
@@ -454,9 +461,9 @@ declare abstract class EventManager {
454
461
  protected _onCrash: CallableFunction | null;
455
462
  protected _onReconnect: CallableFunction | null;
456
463
  protected constructor(connection: EventManagerConnection);
457
- protected execRoute(topic: string, message: string): Promise<void>;
464
+ protected execRoute(topic: string, event: EventMessage): Promise<void>;
458
465
  protected execCallback(callback: CallableFunction | null, data: any): Promise<void>;
459
- abstract send(topic: string, message: string): void;
466
+ abstract send(event: EventMessage): void;
460
467
  abstract start(): void;
461
468
  abstract restart(): void;
462
469
  abstract stop(): void;
@@ -536,9 +543,9 @@ declare class ErrorManager {
536
543
  declare class EventBusMysqlRepository implements EventBusRepository {
537
544
  private readonly connection;
538
545
  constructor(connection: DatabaseConnection);
539
- private eventToRowValues;
540
- create(event: OutboxRecord): Promise<void>;
541
- update(event: OutboxRecord): Promise<void>;
546
+ private recordToRowValues;
547
+ create(record: OutboxRecord): Promise<void>;
548
+ update(record: OutboxRecord): Promise<void>;
542
549
  listPending(limit: number): Promise<OutboxRecord[]>;
543
550
  }
544
551
 
@@ -558,7 +565,7 @@ declare class KafkaManager extends EventManager {
558
565
  private readonly producer;
559
566
  constructor(connection: EventManagerConnection);
560
567
  private run;
561
- send(topic: string, message: string): Promise<void>;
568
+ send(e: EventMessage): Promise<void>;
562
569
  start(autocommit?: boolean): Promise<void>;
563
570
  pause(): Promise<void>;
564
571
  restart(): Promise<void>;
package/dist/index.js CHANGED
@@ -103,6 +103,9 @@ var _DateTime = class _DateTime extends ValueObject {
103
103
  getWeekdayName(locale = "en") {
104
104
  return this._dt.setLocale(locale).toFormat("cccc");
105
105
  }
106
+ getDayName(locale = "en") {
107
+ return this._dt.setLocale(locale).toFormat("EEEE");
108
+ }
106
109
  format(format) {
107
110
  if (!_DateTime.FORMATS.hasOwnProperty(format)) {
108
111
  throw new Error(`Invalid format: ${format}`);
@@ -1087,9 +1090,9 @@ var EventManager = class {
1087
1090
  this._onCrash = null;
1088
1091
  this._onReconnect = null;
1089
1092
  }
1090
- async execRoute(topic, message) {
1093
+ async execRoute(topic, event) {
1091
1094
  if (this._callbackList[topic]) {
1092
- await this._callbackList[topic](message);
1095
+ await this._callbackList[topic](event);
1093
1096
  }
1094
1097
  }
1095
1098
  async execCallback(callback, data) {
@@ -1145,7 +1148,7 @@ var OutboxRecord = class _OutboxRecord {
1145
1148
  this._errorMessage = errorMessage;
1146
1149
  this._publishedAt = publishedAt;
1147
1150
  this._lastAttempt = lastAttempt;
1148
- this._createdAt = createdAt;
1151
+ this._createdAt = createdAt ?? DateTime.now();
1149
1152
  }
1150
1153
  get eventUuid() {
1151
1154
  return this._eventUuid;
@@ -1243,25 +1246,25 @@ var EventBusMysqlRepository = class {
1243
1246
  constructor(connection) {
1244
1247
  this.connection = connection;
1245
1248
  }
1246
- eventToRowValues(e) {
1249
+ recordToRowValues(record) {
1247
1250
  return [
1248
- e.eventUuid.value,
1249
- e.eventType,
1250
- e.tenantUuid.value,
1251
- e.aggregateUuid.value,
1252
- e.aggregateType,
1253
- e.topic,
1254
- e.payload,
1255
- e.status.value,
1256
- e.attempts,
1257
- e.errorMessage,
1258
- e.publishedAt?.value,
1259
- e.lastAttempt?.value,
1260
- e.createdAt.value
1251
+ record.eventUuid.value,
1252
+ record.eventType,
1253
+ record.tenantUuid.value,
1254
+ record.aggregateUuid.value,
1255
+ record.aggregateType,
1256
+ record.topic,
1257
+ record.payload,
1258
+ record.status.value,
1259
+ record.attempts,
1260
+ record.errorMessage,
1261
+ record.publishedAt?.value,
1262
+ record.lastAttempt?.value,
1263
+ record.createdAt.value
1261
1264
  ];
1262
1265
  }
1263
- async create(event) {
1264
- const values = this.eventToRowValues(event);
1266
+ async create(record) {
1267
+ const values = this.recordToRowValues(record);
1265
1268
  await this.connection.query(
1266
1269
  `INSERT INTO events_outbox (event_uuid, event_type, tenant_uuid, aggregate_uuid, aggregate_type, topic,
1267
1270
  payload, status, attempts, error_message, published_at, last_attempt, created_at)
@@ -1269,8 +1272,8 @@ var EventBusMysqlRepository = class {
1269
1272
  values
1270
1273
  );
1271
1274
  }
1272
- async update(event) {
1273
- const values = [event.status.value, event.attempts, event.errorMessage, event.publishedAt?.value, event.lastAttempt?.value, event.eventUuid.value];
1275
+ async update(record) {
1276
+ const values = [record.status.value, record.attempts, record.errorMessage, record.publishedAt?.value, record.lastAttempt?.value, record.eventUuid.value];
1274
1277
  await this.connection.query(
1275
1278
  `UPDATE events_outbox
1276
1279
  SET status = ?,
@@ -1284,7 +1287,7 @@ var EventBusMysqlRepository = class {
1284
1287
  }
1285
1288
  async listPending(limit) {
1286
1289
  const result = await this.connection.query(
1287
- `SELECT * FROM events_outbox WHERE status IN ('PENDING','FAILED') AND published_at IS NULL LIMIT 50`,
1290
+ `SELECT * FROM events_outbox WHERE status IN ('PENDING','FAILED') AND published_at IS NULL LIMIT ${limit}`,
1288
1291
  []
1289
1292
  );
1290
1293
  return result.length > 0 ? result.map((r) => OutboxRecord.reconstitute(r)) : [];
@@ -1481,7 +1484,13 @@ var KafkaManager = class extends EventManager {
1481
1484
  eachMessage: async ({ topic, partition, message, heartbeat }) => {
1482
1485
  try {
1483
1486
  await this.execCallback(this._onMessage, `[New message detected for ${topic}]: ${message.value?.toString()}`);
1484
- await this.execRoute(topic, String(message.value?.toString()));
1487
+ const json = JSON.parse(String(message.value?.toString()));
1488
+ await this.execRoute(topic, {
1489
+ topic: String(json.topic),
1490
+ producer: String(json.producer),
1491
+ tenant: UUID.create(String(json.tenant)),
1492
+ message: String(json.message)
1493
+ });
1485
1494
  const next = (BigInt(message.offset) + 1n).toString();
1486
1495
  await this.consumer.commitOffsets([{ topic, partition, offset: next }]);
1487
1496
  await heartbeat();
@@ -1497,11 +1506,12 @@ var KafkaManager = class extends EventManager {
1497
1506
  await this.execCallback(this._onError, new InternalError(ErrorManager.APP_ERRORS.PROCESS, error.toString()));
1498
1507
  }
1499
1508
  }
1500
- async send(topic, message) {
1509
+ async send(e) {
1501
1510
  const evt = {
1502
1511
  date: DateTime.now().value,
1503
- producer: process.env.NAME && process.env.ENVIRONMENT ? `${process.env.NAME}-${process.env.ENVIRONMENT}` : "unknown",
1504
- data: message
1512
+ tenant: e.tenant.value,
1513
+ producer: e.producer,
1514
+ data: e.message
1505
1515
  };
1506
1516
  try {
1507
1517
  if (!this.producer) {
@@ -1509,7 +1519,7 @@ var KafkaManager = class extends EventManager {
1509
1519
  }
1510
1520
  await this.producer.connect();
1511
1521
  await this.producer.send({
1512
- topic,
1522
+ topic: e.topic,
1513
1523
  messages: [{ value: JSON.stringify(evt) }]
1514
1524
  });
1515
1525
  await this.producer.disconnect();
@@ -1637,7 +1647,12 @@ var DefaultMysqlOutboxRunner = class {
1637
1647
  try {
1638
1648
  e.markProcessing();
1639
1649
  await eventBusRepository.update(e);
1640
- await this.eventManager.send(e.topic, e.payload);
1650
+ await this.eventManager.send({
1651
+ topic: e.topic,
1652
+ producer: process.env.NAME && process.env.ENVIRONMENT ? `${process.env.NAME}-${process.env.ENVIRONMENT}` : "unknown",
1653
+ tenant: e.tenantUuid,
1654
+ message: e.payload
1655
+ });
1641
1656
  e.markProcessed();
1642
1657
  } catch (error) {
1643
1658
  const type = String(error.type);