@seidor-cloud-produtos/orbit-backend-lib 0.0.98 → 0.0.12327

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.
Files changed (237) hide show
  1. package/README.md +0 -0
  2. package/dist/clean-arch/application/cache/cache.d.ts +8 -2
  3. package/dist/clean-arch/application/cache/cache.js +81 -17
  4. package/dist/clean-arch/application/cache/client.d.ts +5 -0
  5. package/dist/clean-arch/application/cache/client.js +5 -1
  6. package/dist/clean-arch/application/logger/index.d.ts +29 -9
  7. package/dist/clean-arch/application/logger/index.js +13 -22
  8. package/dist/clean-arch/application/orbit-http-client/orbit-http-client-dto.d.ts +0 -0
  9. package/dist/clean-arch/application/orbit-http-client/orbit-http-client-dto.js +0 -0
  10. package/dist/clean-arch/application/orbit-http-client/orbit-http-client.d.ts +0 -0
  11. package/dist/clean-arch/application/orbit-http-client/orbit-http-client.js +0 -0
  12. package/dist/clean-arch/application/oribit-notification-client/orbit-notification-client.d.ts +0 -0
  13. package/dist/clean-arch/application/oribit-notification-client/orbit-notification-client.js +0 -0
  14. package/dist/clean-arch/application/queue/handler.d.ts +0 -0
  15. package/dist/clean-arch/application/queue/handler.js +0 -0
  16. package/dist/clean-arch/application/queue/messages-in-memory.d.ts +0 -0
  17. package/dist/clean-arch/application/queue/messages-in-memory.js +0 -0
  18. package/dist/clean-arch/application/queue/queue-connection.d.ts +0 -0
  19. package/dist/clean-arch/application/queue/queue-connection.js +0 -0
  20. package/dist/clean-arch/application/queue/queue.d.ts +1 -0
  21. package/dist/clean-arch/application/queue/queue.js +0 -0
  22. package/dist/clean-arch/domain/entities/aggregate-root.d.ts +0 -0
  23. package/dist/clean-arch/domain/entities/aggregate-root.js +0 -0
  24. package/dist/clean-arch/domain/entities/common-dto.d.ts +0 -0
  25. package/dist/clean-arch/domain/entities/common-dto.js +0 -0
  26. package/dist/clean-arch/domain/entities/entity.d.ts +0 -0
  27. package/dist/clean-arch/domain/entities/entity.js +0 -0
  28. package/dist/clean-arch/domain/entities/unique-entity-id.d.ts +0 -0
  29. package/dist/clean-arch/domain/entities/unique-entity-id.js +0 -0
  30. package/dist/clean-arch/domain/entities/value-object.d.ts +0 -0
  31. package/dist/clean-arch/domain/entities/value-object.js +0 -0
  32. package/dist/clean-arch/domain/entities/value-objects/cnpj.d.ts +17 -0
  33. package/dist/clean-arch/domain/entities/value-objects/cnpj.js +61 -0
  34. package/dist/clean-arch/domain/entities/value-objects/cnpj.spec.d.ts +1 -0
  35. package/dist/clean-arch/domain/entities/value-objects/cnpj.spec.js +57 -0
  36. package/dist/clean-arch/domain/entities/watched-list.d.ts +0 -0
  37. package/dist/clean-arch/domain/entities/watched-list.js +0 -0
  38. package/dist/clean-arch/domain/entities/watched-list.spec.d.ts +0 -0
  39. package/dist/clean-arch/domain/entities/watched-list.spec.js +0 -0
  40. package/dist/clean-arch/domain/errors/application-error.d.ts +0 -0
  41. package/dist/clean-arch/domain/errors/application-error.js +0 -0
  42. package/dist/clean-arch/domain/errors/conflict-error.d.ts +10 -0
  43. package/dist/clean-arch/domain/errors/conflict-error.js +12 -0
  44. package/dist/clean-arch/domain/errors/domain-error.d.ts +0 -0
  45. package/dist/clean-arch/domain/errors/domain-error.js +0 -0
  46. package/dist/clean-arch/domain/errors/http-error.d.ts +0 -0
  47. package/dist/clean-arch/domain/errors/http-error.js +0 -0
  48. package/dist/clean-arch/domain/errors/invalid-cnpj-error.d.ts +4 -0
  49. package/dist/clean-arch/domain/errors/invalid-cnpj-error.js +11 -0
  50. package/dist/clean-arch/domain/errors/invalid-password-algorithm-error.d.ts +0 -0
  51. package/dist/clean-arch/domain/errors/invalid-password-algorithm-error.js +0 -0
  52. package/dist/clean-arch/domain/errors/invalid-password-length-error.d.ts +0 -0
  53. package/dist/clean-arch/domain/errors/invalid-password-length-error.js +0 -0
  54. package/dist/clean-arch/domain/errors/log-error.d.ts +4 -0
  55. package/dist/clean-arch/domain/errors/log-error.js +11 -0
  56. package/dist/clean-arch/domain/errors/validation-error.d.ts +0 -0
  57. package/dist/clean-arch/domain/errors/validation-error.js +0 -0
  58. package/dist/clean-arch/domain/events/domain-event.d.ts +0 -0
  59. package/dist/clean-arch/domain/events/domain-event.js +0 -0
  60. package/dist/clean-arch/domain/factories/create-password-factory.d.ts +0 -0
  61. package/dist/clean-arch/domain/factories/create-password-factory.js +0 -0
  62. package/dist/clean-arch/domain/factories/random-password-temporary-factory.d.ts +0 -0
  63. package/dist/clean-arch/domain/factories/random-password-temporary-factory.js +0 -0
  64. package/dist/clean-arch/domain/interfaces/address-type.d.ts +0 -0
  65. package/dist/clean-arch/domain/interfaces/address-type.js +0 -0
  66. package/dist/clean-arch/domain/interfaces/city.d.ts +0 -0
  67. package/dist/clean-arch/domain/interfaces/city.js +0 -0
  68. package/dist/clean-arch/domain/interfaces/country.d.ts +0 -0
  69. package/dist/clean-arch/domain/interfaces/country.js +0 -0
  70. package/dist/clean-arch/domain/interfaces/ddd.d.ts +0 -0
  71. package/dist/clean-arch/domain/interfaces/ddd.js +0 -0
  72. package/dist/clean-arch/domain/interfaces/state.d.ts +0 -0
  73. package/dist/clean-arch/domain/interfaces/state.js +0 -0
  74. package/dist/clean-arch/domain/types/optional.d.ts +0 -0
  75. package/dist/clean-arch/domain/types/optional.js +0 -0
  76. package/dist/clean-arch/domain/value-objects/password.d.ts +0 -0
  77. package/dist/clean-arch/domain/value-objects/password.js +0 -0
  78. package/dist/clean-arch/domain/value-objects/pbkdf2-password.d.ts +0 -0
  79. package/dist/clean-arch/domain/value-objects/pbkdf2-password.js +0 -0
  80. package/dist/clean-arch/domain/value-objects/sha1-password.d.ts +0 -0
  81. package/dist/clean-arch/domain/value-objects/sha1-password.js +0 -0
  82. package/dist/clean-arch/infra/adapters/express-adapter.d.ts +0 -0
  83. package/dist/clean-arch/infra/adapters/express-adapter.js +1 -1
  84. package/dist/clean-arch/infra/adapters/fastify-adapter.d.ts +0 -0
  85. package/dist/clean-arch/infra/adapters/fastify-adapter.js +1 -1
  86. package/dist/clean-arch/infra/authorizations/authorization.d.ts +0 -0
  87. package/dist/clean-arch/infra/authorizations/authorization.js +0 -0
  88. package/dist/clean-arch/infra/authorizations/authorizer.d.ts +0 -0
  89. package/dist/clean-arch/infra/authorizations/authorizer.js +0 -0
  90. package/dist/clean-arch/infra/authorizations/authorizer.spec.d.ts +0 -0
  91. package/dist/clean-arch/infra/authorizations/authorizer.spec.js +0 -0
  92. package/dist/clean-arch/infra/authorizations/not-allowed.d.ts +0 -0
  93. package/dist/clean-arch/infra/authorizations/not-allowed.js +0 -0
  94. package/dist/clean-arch/infra/cache/clients/node-cache.d.ts +0 -0
  95. package/dist/clean-arch/infra/cache/clients/node-cache.js +0 -0
  96. package/dist/clean-arch/infra/cache/clients/redis.d.ts +8 -3
  97. package/dist/clean-arch/infra/cache/clients/redis.js +16 -14
  98. package/dist/clean-arch/infra/environment/env.d.ts +2 -3
  99. package/dist/clean-arch/infra/environment/env.js +10 -9
  100. package/dist/clean-arch/infra/environment/{environment.d.ts → types.d.ts} +6 -0
  101. package/dist/clean-arch/infra/environment/{environment.js → types.js} +8 -1
  102. package/dist/clean-arch/infra/errors/infra-error.d.ts +0 -0
  103. package/dist/clean-arch/infra/errors/infra-error.js +0 -0
  104. package/dist/clean-arch/infra/http/controller.d.ts +0 -0
  105. package/dist/clean-arch/infra/http/controller.js +11 -0
  106. package/dist/clean-arch/infra/http/controller.spec.d.ts +1 -0
  107. package/dist/clean-arch/infra/http/controller.spec.js +60 -0
  108. package/dist/clean-arch/infra/http/handle-bool-filter.d.ts +0 -0
  109. package/dist/clean-arch/infra/http/handle-bool-filter.js +0 -0
  110. package/dist/clean-arch/infra/http/handle-is-active-query.d.ts +0 -0
  111. package/dist/clean-arch/infra/http/handle-is-active-query.js +0 -0
  112. package/dist/clean-arch/infra/http/handle-sort-param-query.d.ts +0 -0
  113. package/dist/clean-arch/infra/http/handle-sort-param-query.js +0 -0
  114. package/dist/clean-arch/infra/http/handle-user-headers.d.ts +0 -0
  115. package/dist/clean-arch/infra/http/handle-user-headers.js +0 -0
  116. package/dist/clean-arch/infra/http/health-connections.d.ts +0 -0
  117. package/dist/clean-arch/infra/http/health-connections.js +0 -0
  118. package/dist/clean-arch/infra/http/http.d.ts +0 -0
  119. package/dist/clean-arch/infra/http/http.js +0 -0
  120. package/dist/clean-arch/infra/http/net.d.ts +5 -0
  121. package/dist/clean-arch/infra/http/net.js +31 -0
  122. package/dist/clean-arch/infra/logger/logger-in-memory.d.ts +10 -0
  123. package/dist/clean-arch/infra/logger/logger-in-memory.js +36 -0
  124. package/dist/clean-arch/infra/logger/logger-orbit.d.ts +21 -0
  125. package/dist/clean-arch/infra/logger/logger-orbit.js +77 -0
  126. package/dist/clean-arch/infra/orbit-http-client/orbit-axios-client/orbit-axios-client.d.ts +0 -0
  127. package/dist/clean-arch/infra/orbit-http-client/orbit-axios-client/orbit-axios-client.js +0 -0
  128. package/dist/clean-arch/infra/orbit-notification-client/discord-client.d.ts +0 -0
  129. package/dist/clean-arch/infra/orbit-notification-client/discord-client.js +0 -0
  130. package/dist/clean-arch/infra/queue/create-consumers.d.ts +0 -0
  131. package/dist/clean-arch/infra/queue/create-consumers.js +0 -0
  132. package/dist/clean-arch/infra/queue/in-memory/in-memory-queue.d.ts +0 -0
  133. package/dist/clean-arch/infra/queue/in-memory/in-memory-queue.js +0 -0
  134. package/dist/clean-arch/infra/queue/queue-controller.d.ts +0 -0
  135. package/dist/clean-arch/infra/queue/queue-controller.js +0 -0
  136. package/dist/clean-arch/infra/queue/rabbitmq/amqp-lib.d.ts +9 -2
  137. package/dist/clean-arch/infra/queue/rabbitmq/amqp-lib.js +53 -7
  138. package/dist/clean-arch/infra/queue/rabbitmq/amqp-lib.spec.d.ts +0 -0
  139. package/dist/clean-arch/infra/queue/rabbitmq/amqp-lib.spec.js +71 -1
  140. package/dist/clean-arch/infra/queue/rabbitmq/types.d.ts +5 -0
  141. package/dist/clean-arch/infra/queue/rabbitmq/types.js +2 -0
  142. package/dist/clean-arch/infra/queue/worker.d.ts +0 -0
  143. package/dist/clean-arch/infra/queue/worker.js +0 -0
  144. package/dist/clean-arch/infra/scaledjob/runner.d.ts +19 -0
  145. package/dist/clean-arch/infra/scaledjob/runner.js +108 -0
  146. package/dist/clean-arch/infra/scaledjob/runner.spec.d.ts +1 -0
  147. package/dist/clean-arch/infra/scaledjob/runner.spec.js +127 -0
  148. package/dist/clean-arch/infra/scaledjob/types.d.ts +8 -0
  149. package/dist/clean-arch/infra/scaledjob/types.js +2 -0
  150. package/dist/clean-arch/infra/validations/validation-http.d.ts +0 -0
  151. package/dist/clean-arch/infra/validations/validation-http.js +0 -0
  152. package/dist/clean-arch/infra/validations/zod/schemas/common-validation.d.ts +18 -2
  153. package/dist/clean-arch/infra/validations/zod/schemas/common-validation.js +7 -1
  154. package/dist/clean-arch/infra/validations/zod/validation-zod-error.d.ts +1 -1
  155. package/dist/clean-arch/infra/validations/zod/validation-zod-error.js +1 -1
  156. package/dist/clean-arch/infra/validations/zod/validation-zod.d.ts +2 -2
  157. package/dist/clean-arch/infra/validations/zod/validation-zod.js +0 -0
  158. package/dist/clean-arch/infra/validations/zod/validation-zod.spec.d.ts +0 -0
  159. package/dist/clean-arch/infra/validations/zod/validation-zod.spec.js +0 -0
  160. package/dist/clean-arch/shared/pagination/dto-response.d.ts +0 -0
  161. package/dist/clean-arch/shared/pagination/dto-response.js +0 -0
  162. package/dist/clean-arch/shared/pagination/get-take-and-skip.d.ts +0 -0
  163. package/dist/clean-arch/shared/pagination/get-take-and-skip.js +0 -0
  164. package/dist/clean-arch/shared/pagination/pagination.d.ts +0 -0
  165. package/dist/clean-arch/shared/pagination/pagination.js +0 -0
  166. package/dist/frameworks/express/api-gateway/mapping-model.d.ts +0 -0
  167. package/dist/frameworks/express/api-gateway/mapping-model.js +0 -0
  168. package/dist/frameworks/express/api-gateway/mapping-model.spec.d.ts +0 -0
  169. package/dist/frameworks/express/api-gateway/mapping-model.spec.js +0 -0
  170. package/dist/frameworks/express/api-gateway/middleware-express.d.ts +0 -0
  171. package/dist/frameworks/express/api-gateway/middleware-express.js +0 -0
  172. package/dist/frameworks/express/authorizations/authorization-express.d.ts +0 -0
  173. package/dist/frameworks/express/authorizations/authorization-express.js +0 -0
  174. package/dist/frameworks/express/authorizations/authorization-express.spec.d.ts +0 -0
  175. package/dist/frameworks/express/authorizations/authorization-express.spec.js +0 -0
  176. package/dist/frameworks/express/authorizations/middleware-express.d.ts +0 -0
  177. package/dist/frameworks/express/authorizations/middleware-express.js +0 -0
  178. package/dist/frameworks/express/compression/middleware-express.d.ts +0 -0
  179. package/dist/frameworks/express/compression/middleware-express.js +0 -0
  180. package/dist/frameworks/express/types.d.ts +0 -0
  181. package/dist/frameworks/express/types.js +0 -0
  182. package/dist/frameworks/nest/authorizations/authorization-nest.d.ts +0 -0
  183. package/dist/frameworks/nest/authorizations/authorization-nest.js +0 -0
  184. package/dist/frameworks/nest/authorizations/guard-nest.d.ts +0 -0
  185. package/dist/frameworks/nest/authorizations/guard-nest.js +0 -0
  186. package/dist/frameworks/nest/authorizations/not-allowed-nest.d.ts +0 -0
  187. package/dist/frameworks/nest/authorizations/not-allowed-nest.js +0 -0
  188. package/dist/frameworks/nest/errors/orbit-error.d.ts +0 -0
  189. package/dist/frameworks/nest/errors/orbit-error.js +0 -0
  190. package/dist/frameworks/nest/exception-filters/orbit-exception-filter.d.ts +0 -0
  191. package/dist/frameworks/nest/exception-filters/orbit-exception-filter.js +0 -0
  192. package/dist/index.d.ts +0 -0
  193. package/dist/index.js +0 -0
  194. package/dist/infra/authorizations/auth-matcher.d.ts +0 -0
  195. package/dist/infra/authorizations/auth-matcher.js +0 -0
  196. package/dist/infra/authorizations/auth-matcher.spec.d.ts +0 -0
  197. package/dist/infra/authorizations/auth-matcher.spec.js +0 -0
  198. package/dist/infra/authorizations/validator/api/api-validator.d.ts +0 -0
  199. package/dist/infra/authorizations/validator/api/api-validator.js +0 -0
  200. package/dist/infra/authorizations/validator/api/factories/unauthorized-response-factory.d.ts +0 -0
  201. package/dist/infra/authorizations/validator/api/factories/unauthorized-response-factory.js +0 -0
  202. package/dist/infra/authorizations/validator/api/factories/unauthorized-response-factory.spec.d.ts +0 -0
  203. package/dist/infra/authorizations/validator/api/factories/unauthorized-response-factory.spec.js +0 -0
  204. package/dist/infra/authorizations/validator/auth-validator.d.ts +0 -0
  205. package/dist/infra/authorizations/validator/auth-validator.js +4 -1
  206. package/dist/infra/authorizations/validator/auth-validator.spec.d.ts +0 -0
  207. package/dist/infra/authorizations/validator/auth-validator.spec.js +0 -0
  208. package/dist/infra/http/api-gateway/mapping-model.d.ts +0 -0
  209. package/dist/infra/http/api-gateway/mapping-model.js +5 -0
  210. package/dist/infra/http/api-gateway/mapping-model.spec.d.ts +0 -0
  211. package/dist/infra/http/api-gateway/mapping-model.spec.js +2 -0
  212. package/dist/infra/http/compression/filter.d.ts +0 -0
  213. package/dist/infra/http/compression/filter.js +0 -0
  214. package/dist/infra/http/errors/api-common-error.d.ts +0 -0
  215. package/dist/infra/http/errors/api-common-error.js +0 -0
  216. package/dist/infra/http/errors/api-error.d.ts +0 -0
  217. package/dist/infra/http/errors/api-error.js +0 -0
  218. package/dist/infra/http/errors/unauthorized-response.d.ts +0 -0
  219. package/dist/infra/http/errors/unauthorized-response.js +0 -0
  220. package/dist/static/address-types.d.ts +0 -0
  221. package/dist/static/address-types.js +714 -5
  222. package/dist/static/cities.d.ts +0 -0
  223. package/dist/static/cities.js +44562 -3
  224. package/dist/static/cnaes.d.ts +1 -0
  225. package/dist/static/cnaes.js +729 -0
  226. package/dist/static/countries.d.ts +0 -0
  227. package/dist/static/countries.js +1002 -3
  228. package/dist/static/ddd.d.ts +0 -0
  229. package/dist/static/ddd.js +28012 -3
  230. package/dist/static/states.d.ts +0 -0
  231. package/dist/static/states.js +137 -3
  232. package/package.json +4 -3
  233. package/dist/static/address-types.json +0 -714
  234. package/dist/static/cities.json +0 -44562
  235. package/dist/static/countries.json +0 -1002
  236. package/dist/static/ddd.json +0 -28012
  237. package/dist/static/states.json +0 -137
package/README.md CHANGED
File without changes
@@ -4,13 +4,17 @@ export type MethodOptions = {
4
4
  };
5
5
  export type CacheOptions = {
6
6
  throwOnError?: boolean;
7
+ contingencyClients?: CacheClient[];
7
8
  };
8
9
  export declare abstract class Cache {
9
- protected abstract client: CacheClient;
10
+ protected client: CacheClient;
11
+ protected elegibleClients: CacheClient[];
10
12
  protected DEFAULT_EXPIRATION_SECONDS: number;
11
13
  private options?;
12
14
  constructor(options?: CacheOptions);
15
+ private setElegibleClients;
13
16
  private setOptions;
17
+ private setRunningClient;
14
18
  start(options?: MethodOptions): Promise<void>;
15
19
  get(keyCache: string, options?: MethodOptions): Promise<any>;
16
20
  set(keyCache: string, data: any, expiration?: number, options?: MethodOptions): Promise<void>;
@@ -18,6 +22,8 @@ export declare abstract class Cache {
18
22
  getKeys(pattern?: string, options?: MethodOptions): Promise<string[] | null>;
19
23
  delBatch(patterns: string[], options?: MethodOptions): Promise<void>;
20
24
  flush(): Promise<void>;
21
- isRunning(options?: MethodOptions): Promise<any>;
25
+ isRunning(options?: MethodOptions, isContingency?: boolean): Promise<boolean>;
22
26
  close(options?: MethodOptions): Promise<void>;
27
+ protected handleException(e: Error): Promise<void>;
28
+ getOrFetchAndCache<T>(keyCache: string, expiration: number | undefined, promise: () => Promise<T>, options?: MethodOptions): Promise<T | undefined>;
23
29
  }
@@ -2,14 +2,25 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Cache = void 0;
4
4
  const tslib_1 = require("tslib");
5
+ const logger_in_memory_1 = tslib_1.__importDefault(require("../../infra/logger/logger-in-memory"));
5
6
  const env_1 = require("../../infra/environment/env");
6
- const logger_1 = tslib_1.__importDefault(require("../logger"));
7
+ const types_1 = require("../../infra/environment/types");
7
8
  class Cache {
8
- DEFAULT_EXPIRATION_SECONDS = env_1.env.DEFAULT_CACHE_EXPIRATION;
9
+ client;
10
+ elegibleClients;
11
+ DEFAULT_EXPIRATION_SECONDS = env_1.env?.DEFAULT_CACHE_EXPIRATION || 360;
9
12
  options;
10
13
  constructor(options) {
11
- if (options)
14
+ if (options) {
12
15
  this.setOptions(options);
16
+ this.setElegibleClients(options.contingencyClients);
17
+ }
18
+ }
19
+ setElegibleClients(contingencyClients) {
20
+ this.elegibleClients = [
21
+ ...(contingencyClients || []),
22
+ this.client,
23
+ ];
13
24
  }
14
25
  setOptions(options) {
15
26
  this.options = {
@@ -17,17 +28,45 @@ class Cache {
17
28
  throwOnError: options.throwOnError === undefined ? true : options.throwOnError,
18
29
  };
19
30
  }
31
+ async setRunningClient() {
32
+ if (this.elegibleClients.length === 1) {
33
+ return this;
34
+ }
35
+ let isRunning = false;
36
+ for (let i = 0; i < this.elegibleClients.length; i++) {
37
+ const currentClient = this.elegibleClients[i];
38
+ try {
39
+ await currentClient.start();
40
+ isRunning = await currentClient.isRunning();
41
+ }
42
+ catch { }
43
+ if (isRunning) {
44
+ try {
45
+ await this.client.close();
46
+ }
47
+ catch { }
48
+ this.client = currentClient;
49
+ break;
50
+ }
51
+ currentClient?.close();
52
+ }
53
+ await logger_in_memory_1.default.log(`Changed cache client to ${this.client.constructor.name}`, { level: types_1.LOG_LEVEL.warn });
54
+ return this;
55
+ }
20
56
  async start(options) {
21
57
  try {
22
58
  await this.client.start();
23
59
  }
24
60
  catch (e) {
25
61
  if (options?.throwOnError || this.options?.throwOnError) {
26
- throw e;
62
+ await this.handleException(e);
63
+ }
64
+ else {
65
+ await this.setRunningClient();
27
66
  }
28
67
  return;
29
68
  }
30
- logger_1.default.debug()?.info('💾 Cache Connected');
69
+ await logger_in_memory_1.default.debug()?.log('💾 Cache Connected');
31
70
  }
32
71
  async get(keyCache, options) {
33
72
  try {
@@ -36,7 +75,7 @@ class Cache {
36
75
  }
37
76
  catch (e) {
38
77
  if (options?.throwOnError || this.options?.throwOnError) {
39
- throw e;
78
+ await this.handleException(e);
40
79
  }
41
80
  return null;
42
81
  }
@@ -47,7 +86,7 @@ class Cache {
47
86
  }
48
87
  catch (e) {
49
88
  if (options?.throwOnError || this.options?.throwOnError) {
50
- throw e;
89
+ await this.handleException(e);
51
90
  }
52
91
  return;
53
92
  }
@@ -58,7 +97,7 @@ class Cache {
58
97
  }
59
98
  catch (e) {
60
99
  if (options?.throwOnError || this.options?.throwOnError) {
61
- throw e;
100
+ await this.handleException(e);
62
101
  }
63
102
  return;
64
103
  }
@@ -69,7 +108,7 @@ class Cache {
69
108
  }
70
109
  catch (e) {
71
110
  if (options?.throwOnError || this.options?.throwOnError) {
72
- throw e;
111
+ await this.handleException(e);
73
112
  }
74
113
  return null;
75
114
  }
@@ -80,36 +119,61 @@ class Cache {
80
119
  }
81
120
  catch (e) {
82
121
  if (options?.throwOnError || this.options?.throwOnError) {
83
- throw e;
122
+ await this.handleException(e);
84
123
  }
85
124
  return;
86
125
  }
87
126
  }
88
127
  async flush() {
89
- return await this.client.flush();
128
+ try {
129
+ return await this.client.flush();
130
+ }
131
+ catch (e) {
132
+ await this.handleException(e);
133
+ }
90
134
  }
91
- async isRunning(options) {
135
+ async isRunning(options, isContingency = false) {
136
+ let isRunning = false;
92
137
  try {
93
- return await this.client.isRunning();
138
+ isRunning = await this.client.isRunning();
94
139
  }
95
140
  catch (e) {
96
141
  if (options?.throwOnError || this.options?.throwOnError) {
97
- throw e;
142
+ await this.handleException(e);
98
143
  }
99
- return false;
100
144
  }
145
+ if (!isRunning && !isContingency) {
146
+ await this.setRunningClient();
147
+ return await this.isRunning(options, true);
148
+ }
149
+ return isRunning;
101
150
  }
102
151
  async close(options) {
103
- logger_1.default.debug()?.info('💾 Cache closed');
152
+ await logger_in_memory_1.default.debug()?.log('💾 Cache closed');
104
153
  try {
105
154
  return await this.client.close();
106
155
  }
107
156
  catch (e) {
108
157
  if (options?.throwOnError || this.options?.throwOnError) {
109
- throw e;
158
+ await this.handleException(e);
110
159
  }
111
160
  return;
112
161
  }
113
162
  }
163
+ async handleException(e) {
164
+ try {
165
+ await logger_in_memory_1.default.log(JSON.stringify(e), { level: types_1.LOG_LEVEL.error });
166
+ }
167
+ catch { }
168
+ throw e;
169
+ }
170
+ async getOrFetchAndCache(keyCache, expiration = this.DEFAULT_EXPIRATION_SECONDS, promise, options) {
171
+ let data = await this.get(keyCache, options);
172
+ if (!data) {
173
+ data = await promise();
174
+ this.set(keyCache, data, expiration, options);
175
+ }
176
+ return data;
177
+ }
114
178
  }
115
179
  exports.Cache = Cache;
@@ -1,4 +1,9 @@
1
+ export interface CacheClientOptions {
2
+ timeout?: number;
3
+ }
1
4
  export declare abstract class CacheClient {
5
+ protected connection?: CacheClientOptions | undefined;
6
+ constructor(connection?: CacheClientOptions | undefined);
2
7
  abstract start(): Promise<void>;
3
8
  abstract close(): Promise<void>;
4
9
  abstract get(keyCache: string): Promise<any>;
@@ -3,9 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CacheClient = void 0;
4
4
  const unique_entity_id_1 = require("../../domain/entities/unique-entity-id");
5
5
  class CacheClient {
6
+ connection;
7
+ constructor(connection) {
8
+ this.connection = connection;
9
+ }
6
10
  async isRunning() {
7
11
  const key = new unique_entity_id_1.UniqueEntityId().toValue();
8
- await this.set(key, JSON.stringify({ ping: true }), 60);
12
+ await this.set(key, JSON.stringify({ ping: true }), this.connection?.timeout || 60);
9
13
  const cachedData = await this.get(key);
10
14
  return cachedData?.ping || false;
11
15
  }
@@ -1,11 +1,31 @@
1
- export declare enum LOG_LEVEL {
2
- debug = "DEBUG",
3
- info = "INFO"
1
+ import { LOG_LEVEL } from '../../infra/environment/types';
2
+ export declare enum ActorEnum {
3
+ SYSTEM = "system",
4
+ USER = "user"
4
5
  }
5
- export declare class Logger {
6
- debug(): this | undefined;
7
- info(info: any): void;
8
- error(error: any): void;
6
+ export declare enum CategoryEnum {
7
+ BUSINESS = "business",
8
+ TECHNICAL = "technical"
9
+ }
10
+ export type LogProperties = {
11
+ id?: string;
12
+ actorType: ActorEnum;
13
+ category: CategoryEnum;
14
+ level: LOG_LEVEL;
15
+ actor: string;
16
+ type: string;
17
+ tenantId?: string;
18
+ dateTime?: Date;
19
+ expirationHours?: number;
20
+ trace?: {
21
+ mainId?: string;
22
+ ids: string[];
23
+ };
24
+ };
25
+ export interface LogOptions {
26
+ throwOnError?: boolean;
27
+ retryStrategy?: (data: any, props?: LogProperties, options?: LogOptions) => Promise<void>;
28
+ }
29
+ export default abstract class LoggerGateway {
30
+ abstract log(data: any, props?: LogProperties, options?: LogOptions): Promise<void>;
9
31
  }
10
- declare const logger: Logger;
11
- export default logger;
@@ -1,25 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Logger = exports.LOG_LEVEL = void 0;
4
- const env_1 = require("../../infra/environment/env");
5
- var LOG_LEVEL;
6
- (function (LOG_LEVEL) {
7
- LOG_LEVEL["debug"] = "DEBUG";
8
- LOG_LEVEL["info"] = "INFO";
9
- })(LOG_LEVEL || (exports.LOG_LEVEL = LOG_LEVEL = {}));
10
- class Logger {
11
- debug() {
12
- if (env_1.env.LOG_LEVEL === LOG_LEVEL.debug)
13
- return this;
14
- return undefined;
15
- }
16
- info(info) {
17
- console.info(info);
18
- }
19
- error(error) {
20
- console.error(error);
21
- }
3
+ exports.CategoryEnum = exports.ActorEnum = void 0;
4
+ var ActorEnum;
5
+ (function (ActorEnum) {
6
+ ActorEnum["SYSTEM"] = "system";
7
+ ActorEnum["USER"] = "user";
8
+ })(ActorEnum || (exports.ActorEnum = ActorEnum = {}));
9
+ var CategoryEnum;
10
+ (function (CategoryEnum) {
11
+ CategoryEnum["BUSINESS"] = "business";
12
+ CategoryEnum["TECHNICAL"] = "technical";
13
+ })(CategoryEnum || (exports.CategoryEnum = CategoryEnum = {}));
14
+ class LoggerGateway {
22
15
  }
23
- exports.Logger = Logger;
24
- const logger = new Logger();
25
- exports.default = logger;
16
+ exports.default = LoggerGateway;
File without changes
File without changes
@@ -3,4 +3,5 @@ import Handler from './handler';
3
3
  export default interface Queue {
4
4
  on(queueName: string, callback: Handler): Promise<void>;
5
5
  publish(exchangeName: string, domainEvent: DomainEvent, configs?: Record<string, any>): Promise<void>;
6
+ get?(queueName: string, options?: any): Promise<unknown>;
6
7
  }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,17 @@
1
+ export default class Cnpj {
2
+ readonly value: string;
3
+ private constructor();
4
+ static create(cnpj: string): Cnpj;
5
+ static createCorrectly(cnpj: string): Cnpj;
6
+ private static tamanhoCNPJSemDV;
7
+ private static regexCNPJSemDV;
8
+ private static regexCNPJ;
9
+ private static regexMascara;
10
+ private static regexNaoPermitidos;
11
+ private static valorBase;
12
+ private static pesosDV;
13
+ private static cnpjZerado;
14
+ private static clear;
15
+ static isValid(input: string): boolean;
16
+ private static calculaDV;
17
+ }
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ /* eslint-disable no-useless-escape */
5
+ const invalid_cnpj_error_1 = tslib_1.__importDefault(require("../../../domain/errors/invalid-cnpj-error"));
6
+ class Cnpj {
7
+ value;
8
+ constructor(cnpj) {
9
+ this.value = cnpj;
10
+ }
11
+ static create(cnpj) {
12
+ if (!this.isValid(cnpj)) {
13
+ throw new invalid_cnpj_error_1.default(cnpj);
14
+ }
15
+ return new Cnpj(this.clear(cnpj));
16
+ }
17
+ static createCorrectly(cnpj) {
18
+ return new Cnpj(this.clear(cnpj));
19
+ }
20
+ static tamanhoCNPJSemDV = 12;
21
+ static regexCNPJSemDV = /^[A-Z0-9]{12}$/;
22
+ static regexCNPJ = /^[A-Z0-9]{12}\d{2}$/;
23
+ static regexMascara = /[.\/-]/g;
24
+ static regexNaoPermitidos = /[^A-Z0-9.\/-]/i;
25
+ static valorBase = '0'.charCodeAt(0);
26
+ static pesosDV = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
27
+ static cnpjZerado = '0'.repeat(14);
28
+ static clear(input) {
29
+ return input.replace(this.regexMascara, '').toUpperCase();
30
+ }
31
+ static isValid(input) {
32
+ if (this.regexNaoPermitidos.test(input))
33
+ return false;
34
+ const raw = this.clear(input);
35
+ if (raw.length !== 14 || raw === this.cnpjZerado)
36
+ return false;
37
+ if (!this.regexCNPJ.test(raw))
38
+ return false;
39
+ const base = raw.substring(0, this.tamanhoCNPJSemDV);
40
+ const dvInformado = raw.substring(this.tamanhoCNPJSemDV);
41
+ const dvCalculado = this.calculaDV(base);
42
+ return dvInformado === dvCalculado;
43
+ }
44
+ static calculaDV(base) {
45
+ if (!this.regexCNPJSemDV.test(base)) {
46
+ throw new Error('Base do CNPJ inválida para cálculo de DV');
47
+ }
48
+ let soma1 = 0;
49
+ let soma2 = 0;
50
+ for (let i = 0; i < this.tamanhoCNPJSemDV; i++) {
51
+ const valor = base.charCodeAt(i) - this.valorBase;
52
+ soma1 += valor * this.pesosDV[i + 1];
53
+ soma2 += valor * this.pesosDV[i];
54
+ }
55
+ const dv1 = soma1 % 11 < 2 ? 0 : 11 - (soma1 % 11);
56
+ soma2 += dv1 * this.pesosDV[this.tamanhoCNPJSemDV];
57
+ const dv2 = soma2 % 11 < 2 ? 0 : 11 - (soma2 % 11);
58
+ return `${dv1}${dv2}`;
59
+ }
60
+ }
61
+ exports.default = Cnpj;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const invalid_cnpj_error_1 = tslib_1.__importDefault(require("../../errors/invalid-cnpj-error"));
5
+ const cnpj_1 = tslib_1.__importDefault(require("./cnpj"));
6
+ describe('Value Object - Cnpj', () => {
7
+ it('should be able to crete a new cnpj (numeric)', () => {
8
+ const cnpj = cnpj_1.default.create('46230439000101');
9
+ expect(cnpj.value).toBe('46230439000101');
10
+ });
11
+ it('should be able to return error when cnpj is wrong', () => {
12
+ expect(() => cnpj_1.default.create('12344556')).toThrow(invalid_cnpj_error_1.default);
13
+ });
14
+ it('should be able to return error when cnpj only has one number', () => {
15
+ expect(() => cnpj_1.default.create('11111111111111')).toThrow(invalid_cnpj_error_1.default);
16
+ });
17
+ it('should be able to return error when digit 0 is diff', () => {
18
+ expect(() => cnpj_1.default.create('46230439000111')).toThrow(invalid_cnpj_error_1.default);
19
+ });
20
+ it('should be able to create correctly without validation', () => {
21
+ const cnpj = cnpj_1.default.createCorrectly('46230439000101');
22
+ expect(cnpj.value).toBe('46230439000101');
23
+ });
24
+ // --- cenários alfanuméricos existentes ---
25
+ it('should be able to create a valid alphanumeric cnpj (plain)', () => {
26
+ const raw = '12ABC34501DE35';
27
+ const cnpj = cnpj_1.default.create(raw);
28
+ expect(cnpj.value).toBe(raw);
29
+ });
30
+ it('should be able to create a valid alphanumeric cnpj (formatted)', () => {
31
+ const formatted = '12.ABC.345/01DE-35';
32
+ const cnpj = cnpj_1.default.create(formatted);
33
+ expect(cnpj.value).toBe('12ABC34501DE35');
34
+ });
35
+ it('should throw for alphanumeric cnpj with wrong check digit', () => {
36
+ expect(() => cnpj_1.default.create('12ABC34501DE36')).toThrow(invalid_cnpj_error_1.default);
37
+ });
38
+ it('should throw for alphanumeric cnpj with invalid character', () => {
39
+ expect(() => cnpj_1.default.create('12AB@34501DE35')).toThrow(invalid_cnpj_error_1.default);
40
+ });
41
+ it('should throw for alphanumeric cnpj too short', () => {
42
+ expect(() => cnpj_1.default.create('12ABC34501D')).toThrow(invalid_cnpj_error_1.default);
43
+ });
44
+ it('should throw for cnpj all zeros', () => {
45
+ expect(() => cnpj_1.default.create('00000000000000')).toThrow(invalid_cnpj_error_1.default);
46
+ });
47
+ it('should accept lowercase alphanumeric cnpj and normalize to uppercase', () => {
48
+ const lower = '12abc34501de35';
49
+ const cnpj = cnpj_1.default.create(lower);
50
+ expect(cnpj.value).toBe('12ABC34501DE35');
51
+ });
52
+ it('createCorrectly should remove mask from formatted alphanumeric cnpj without validating', () => {
53
+ const formatted = '12.ABC.345/01DE-XX'; // invalid DV/characters but mask is removed
54
+ const cnpj = cnpj_1.default.createCorrectly(formatted);
55
+ expect(cnpj.value).toBe('12ABC34501DEXX');
56
+ });
57
+ });
File without changes
File without changes
@@ -0,0 +1,10 @@
1
+ type ConflictProps = {
2
+ id: string;
3
+ [key: string]: any;
4
+ };
5
+ export default class ConflictError<T extends ConflictProps> extends Error {
6
+ code: number;
7
+ props: T | T[];
8
+ constructor(props: T | T[]);
9
+ }
10
+ export {};
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class ConflictError extends Error {
4
+ code;
5
+ props;
6
+ constructor(props) {
7
+ super('Entity already exists.');
8
+ this.code = 409;
9
+ this.props = props;
10
+ }
11
+ }
12
+ exports.default = ConflictError;
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,4 @@
1
+ import DomainError from './domain-error';
2
+ export default class InvalidCnpjError extends DomainError {
3
+ constructor(cnpj: string);
4
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const domain_error_1 = tslib_1.__importDefault(require("./domain-error"));
5
+ class InvalidCnpjError extends domain_error_1.default {
6
+ constructor(cnpj) {
7
+ super(`The cnpj "${cnpj}" is invalid.`, 400);
8
+ this.name = 'InvalidCnpjError';
9
+ }
10
+ }
11
+ exports.default = InvalidCnpjError;
@@ -0,0 +1,4 @@
1
+ import DomainError from './domain-error';
2
+ export default class LogError extends DomainError {
3
+ constructor();
4
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const domain_error_1 = tslib_1.__importDefault(require("./domain-error"));
5
+ class LogError extends domain_error_1.default {
6
+ constructor() {
7
+ super('Cannot log', 500);
8
+ this.name = 'LogError';
9
+ }
10
+ }
11
+ exports.default = LogError;
File without changes
File without changes
File without changes