@seidor-cloud-produtos/orbit-backend-lib 2.0.96 → 2.0.98

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.
@@ -0,0 +1,8 @@
1
+ import { AuthorizerHeaders } from '../../infra/http/handle-authorizer-headers';
2
+ import { Entity } from '../../domain/entities/entity';
3
+ import ConsistencyEvent from '../../domain/events/consistency-event';
4
+ export declare abstract class ConsistencyEventDispatcher {
5
+ dispatch(entity: Entity<any>, authorizerHeaders: AuthorizerHeaders, eventName: string): Promise<void>;
6
+ dispatchBatch(entities: Entity<any>[], authorizerHeaders: AuthorizerHeaders, eventName: string): Promise<void>;
7
+ abstract publish(event: ConsistencyEvent): Promise<void>;
8
+ }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConsistencyEventDispatcher = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const consistency_event_1 = tslib_1.__importDefault(require("../../domain/events/consistency-event"));
6
+ class ConsistencyEventDispatcher {
7
+ async dispatch(entity, authorizerHeaders, eventName) {
8
+ const event = new consistency_event_1.default(entity, authorizerHeaders, eventName);
9
+ await this.publish(event);
10
+ }
11
+ async dispatchBatch(entities, authorizerHeaders, eventName) {
12
+ const events = entities.map(entity => new consistency_event_1.default(entity, authorizerHeaders, eventName));
13
+ await Promise.all(events.map(event => this.publish(event)));
14
+ }
15
+ }
16
+ exports.ConsistencyEventDispatcher = ConsistencyEventDispatcher;
@@ -10,7 +10,7 @@ class BaseAuditLogger {
10
10
  this.gateway = gateway;
11
11
  }
12
12
  buildParams(type, authorizerHeaders, trace) {
13
- trace?.ids.push(authorizerHeaders.traceId);
13
+ trace.ids.push(authorizerHeaders.traceId);
14
14
  return {
15
15
  type,
16
16
  trace,
@@ -8,4 +8,5 @@ export declare abstract class Entity<Props> {
8
8
  equals(entity: Entity<any>): boolean;
9
9
  private serializeValue;
10
10
  toObject(): Record<string, unknown>;
11
+ toEventDispatchObject(): Record<string, unknown>;
11
12
  }
@@ -52,5 +52,8 @@ class Entity {
52
52
  toObject() {
53
53
  return this.serializeValue(this.props);
54
54
  }
55
+ toEventDispatchObject() {
56
+ return this.toObject();
57
+ }
55
58
  }
56
59
  exports.Entity = Entity;
@@ -22,5 +22,5 @@ export declare abstract class WatchedList<T> {
22
22
  add(item: T): void;
23
23
  remove(item: T): void;
24
24
  update(items: T[]): void;
25
- toObject(): (Record<string, unknown> | T)[];
25
+ toObject(): (T | Record<string, unknown>)[];
26
26
  }
@@ -0,0 +1,9 @@
1
+ import { AuthorizerHeaders } from '../../infra/http/handle-authorizer-headers';
2
+ import { Entity } from '../entities/entity';
3
+ import DomainEvent from './domain-event';
4
+ export default class ConsistencyEvent implements DomainEvent {
5
+ readonly eventDate: Date;
6
+ readonly name: string;
7
+ readonly input: Record<string, unknown>;
8
+ constructor(entity: Entity<any>, authorizerHeaders: AuthorizerHeaders, eventName: string);
9
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class ConsistencyEvent {
4
+ eventDate = new Date();
5
+ name;
6
+ input;
7
+ constructor(entity, authorizerHeaders, eventName) {
8
+ this.name = eventName;
9
+ this.input = {
10
+ origin: authorizerHeaders,
11
+ payload: entity.toEventDispatchObject(),
12
+ };
13
+ }
14
+ }
15
+ exports.default = ConsistencyEvent;
@@ -0,0 +1,7 @@
1
+ import { ConsistencyEventDispatcher } from '../../application/consistency-event-dispatcher/consistency-event-dispatcher';
2
+ import ConsistencyEvent from '../../domain/events/consistency-event';
3
+ export declare class InMemoryConsistencyEventDispatcher extends ConsistencyEventDispatcher {
4
+ protected events: ConsistencyEvent[];
5
+ constructor(events?: ConsistencyEvent[]);
6
+ publish(event: ConsistencyEvent): Promise<void>;
7
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InMemoryConsistencyEventDispatcher = void 0;
4
+ const consistency_event_dispatcher_1 = require("../../application/consistency-event-dispatcher/consistency-event-dispatcher");
5
+ class InMemoryConsistencyEventDispatcher extends consistency_event_dispatcher_1.ConsistencyEventDispatcher {
6
+ events;
7
+ constructor(events = []) {
8
+ super();
9
+ this.events = events;
10
+ }
11
+ async publish(event) {
12
+ this.events.push(event);
13
+ }
14
+ }
15
+ exports.InMemoryConsistencyEventDispatcher = InMemoryConsistencyEventDispatcher;
@@ -0,0 +1,8 @@
1
+ import { ConsistencyEventDispatcher } from '../../application/consistency-event-dispatcher/consistency-event-dispatcher';
2
+ import AmqpQueue from '../queue/rabbitmq/amqp-lib';
3
+ import ConsistencyEvent from '../../domain/events/consistency-event';
4
+ export declare class AmqpConsistencyEventDispatcher extends ConsistencyEventDispatcher {
5
+ protected amqp: AmqpQueue;
6
+ constructor(amqp: AmqpQueue);
7
+ publish(event: ConsistencyEvent): Promise<void>;
8
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AmqpConsistencyEventDispatcher = void 0;
4
+ const consistency_event_dispatcher_1 = require("../../application/consistency-event-dispatcher/consistency-event-dispatcher");
5
+ class AmqpConsistencyEventDispatcher extends consistency_event_dispatcher_1.ConsistencyEventDispatcher {
6
+ amqp;
7
+ constructor(amqp) {
8
+ super();
9
+ this.amqp = amqp;
10
+ }
11
+ async publish(event) {
12
+ await this.amqp.publish(event.name, event, { storeMessageOnError: false });
13
+ }
14
+ }
15
+ exports.AmqpConsistencyEventDispatcher = AmqpConsistencyEventDispatcher;
@@ -4,24 +4,16 @@ import QueueConnection from '../../../application/queue/queue-connection';
4
4
  import DomainEvent from '../../../domain/events/domain-event';
5
5
  import { AmqpQueueGetResult } from './types';
6
6
  import { Logger } from '../../../application/logger';
7
- interface SocketOptions {
8
- retry?: {
9
- maxCount: number;
10
- intervalMs: number;
11
- };
12
- timeoutSeconds?: number;
13
- }
14
7
  /**
15
8
  * Opções de socket/conexão para o AMQP.
16
9
  */
17
10
  interface SocketOptions {
18
- /**
19
- * Política simples de retry para a conexão inicial.
20
- */
21
11
  retry?: {
22
12
  maxCount: number;
23
13
  intervalMs: number;
24
14
  };
15
+ timeoutSeconds?: number;
16
+ maxStoredMessagesOnError?: number;
25
17
  }
26
18
  /**
27
19
  * Implementação de fila baseada em AMQP (RabbitMQ) usando `amqplib`.
@@ -32,7 +32,10 @@ class AmqpQueue {
32
32
  * Define (ou atualiza) as opções de conexão.
33
33
  */
34
34
  setSocketOptions(socketOptions) {
35
- this.socketOptions = socketOptions;
35
+ this.socketOptions = {
36
+ ...(socketOptions || {}),
37
+ maxStoredMessagesOnError: 500,
38
+ };
36
39
  return this;
37
40
  }
38
41
  /**
@@ -194,15 +197,20 @@ class AmqpQueue {
194
197
  return result;
195
198
  }
196
199
  catch (err) {
200
+ this.logger.info({
201
+ message: `⛔ Error to publish messages to: ${domainEvent.name}.`,
202
+ error: err,
203
+ });
204
+ if (buildedConfigs.storeMessageOnError === false ||
205
+ (this.socketOptions?.maxStoredMessagesOnError || 0) <
206
+ this.messagesInMemory.length) {
207
+ return;
208
+ }
197
209
  this.messagesInMemory.push({
198
210
  exchangeName,
199
211
  domainEvent,
200
212
  configs: buildedConfigs,
201
213
  });
202
- this.logger.info({
203
- message: `⛔ Error to publish messages to: ${domainEvent.name}. Publish in memory!`,
204
- error: err,
205
- });
206
214
  }
207
215
  }
208
216
  async publishToServer(exchangeName, domainEvent, buildedConfigs) {
@@ -74,11 +74,11 @@ export declare const traceSchema: z.ZodObject<{
74
74
  "x-trace-id"?: string | undefined;
75
75
  }>;
76
76
  export declare const tenantSchema: z.ZodObject<{
77
- tenantname: z.ZodString;
77
+ tenantname: z.ZodOptional<z.ZodString>;
78
78
  }, "strip", z.ZodTypeAny, {
79
- tenantname: string;
79
+ tenantname?: string | undefined;
80
80
  }, {
81
- tenantname: string;
81
+ tenantname?: string | undefined;
82
82
  }>;
83
83
  export declare const integrationHeaders: z.ZodObject<z.objectUtil.extendShape<{
84
84
  tenantid: z.ZodString;
@@ -100,21 +100,21 @@ export declare const authorizerHeaders: z.ZodUnion<[z.ZodObject<z.objectUtil.ext
100
100
  }, {
101
101
  "x-trace-id": z.ZodDefault<z.ZodString>;
102
102
  }>>, {
103
- tenantname: z.ZodString;
103
+ tenantname: z.ZodOptional<z.ZodString>;
104
104
  }>, "strip", z.ZodTypeAny, {
105
105
  "x-trace-id": string;
106
106
  username: string;
107
107
  tenantid: string;
108
108
  useremail: string;
109
- tenantname: string;
110
109
  userid?: string | undefined;
110
+ tenantname?: string | undefined;
111
111
  }, {
112
112
  username: string;
113
113
  tenantid: string;
114
114
  useremail: string;
115
- tenantname: string;
116
115
  "x-trace-id"?: string | undefined;
117
116
  userid?: string | undefined;
117
+ tenantname?: string | undefined;
118
118
  }>, z.ZodObject<z.objectUtil.extendShape<z.objectUtil.extendShape<{
119
119
  "app-name": z.ZodString;
120
120
  "app-id": z.ZodString;
@@ -123,19 +123,19 @@ export declare const authorizerHeaders: z.ZodUnion<[z.ZodObject<z.objectUtil.ext
123
123
  }, {
124
124
  "x-trace-id": z.ZodDefault<z.ZodString>;
125
125
  }>>, {
126
- tenantname: z.ZodString;
126
+ tenantname: z.ZodOptional<z.ZodString>;
127
127
  }>, "strip", z.ZodTypeAny, {
128
128
  "app-id": string;
129
129
  "app-name": string;
130
130
  "x-trace-id": string;
131
131
  tenantid: string;
132
- tenantname: string;
132
+ tenantname?: string | undefined;
133
133
  }, {
134
134
  "app-id": string;
135
135
  "app-name": string;
136
136
  tenantid: string;
137
- tenantname: string;
138
137
  "x-trace-id"?: string | undefined;
138
+ tenantname?: string | undefined;
139
139
  }>]>;
140
140
  export declare const authorizerHeadersWithFiscalIds: z.ZodUnion<[z.ZodObject<z.objectUtil.extendShape<z.objectUtil.extendShape<{
141
141
  username: z.ZodString;
@@ -49,7 +49,7 @@ const fiscalIdsSchema = zod_1.z.object({
49
49
  }))),
50
50
  });
51
51
  exports.tenantSchema = zod_1.z.object({
52
- tenantname: zod_1.z.string(),
52
+ tenantname: zod_1.z.string().optional(),
53
53
  });
54
54
  exports.integrationHeaders = exports.tenantIdSchema.merge(exports.traceSchema);
55
55
  exports.authorizerHeaders = zod_1.z.union([
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seidor-cloud-produtos/orbit-backend-lib",
3
- "version": "2.0.96",
3
+ "version": "2.0.98",
4
4
  "description": "Internal lib for backend components",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",