exodus-framework 2.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.
Files changed (155) hide show
  1. package/build/@types/app.d.ts +5 -0
  2. package/build/@types/app.d.ts.map +1 -0
  3. package/build/@types/app.js +5 -0
  4. package/build/@types/console.d.ts +2 -0
  5. package/build/@types/console.d.ts.map +1 -0
  6. package/build/@types/console.js +5 -0
  7. package/build/@types/entity.d.ts +42 -0
  8. package/build/@types/entity.d.ts.map +1 -0
  9. package/build/@types/entity.js +5 -0
  10. package/build/@types/express.d.js +5 -0
  11. package/build/@types/http.d.ts +62 -0
  12. package/build/@types/http.d.ts.map +1 -0
  13. package/build/@types/http.js +50 -0
  14. package/build/@types/index.d.ts +10 -0
  15. package/build/@types/index.d.ts.map +1 -0
  16. package/build/@types/index.js +93 -0
  17. package/build/@types/messaging.d.ts +22 -0
  18. package/build/@types/messaging.d.ts.map +1 -0
  19. package/build/@types/messaging.js +13 -0
  20. package/build/@types/service.d.ts +4 -0
  21. package/build/@types/service.d.ts.map +1 -0
  22. package/build/@types/service.js +5 -0
  23. package/build/@types/session.d.ts +9 -0
  24. package/build/@types/session.d.ts.map +1 -0
  25. package/build/@types/session.js +5 -0
  26. package/build/@types/singleton.d.ts +10 -0
  27. package/build/@types/singleton.d.ts.map +1 -0
  28. package/build/@types/singleton.js +5 -0
  29. package/build/@types/socket.d.ts +20 -0
  30. package/build/@types/socket.d.ts.map +1 -0
  31. package/build/@types/socket.js +11 -0
  32. package/build/app/app.d.ts +16 -0
  33. package/build/app/app.d.ts.map +1 -0
  34. package/build/app/app.js +68 -0
  35. package/build/app/controller.d.ts +28 -0
  36. package/build/app/controller.d.ts.map +1 -0
  37. package/build/app/controller.js +94 -0
  38. package/build/app/core.d.ts +7 -0
  39. package/build/app/core.d.ts.map +1 -0
  40. package/build/app/core.js +61 -0
  41. package/build/app/index.d.ts +8 -0
  42. package/build/app/index.d.ts.map +1 -0
  43. package/build/app/index.js +70 -0
  44. package/build/app/service.d.ts +6 -0
  45. package/build/app/service.d.ts.map +1 -0
  46. package/build/app/service.js +24 -0
  47. package/build/app/singleton.d.ts +9 -0
  48. package/build/app/singleton.d.ts.map +1 -0
  49. package/build/app/singleton.js +29 -0
  50. package/build/config/app.d.ts +16 -0
  51. package/build/config/app.d.ts.map +1 -0
  52. package/build/config/app.js +25 -0
  53. package/build/config/auth.d.ts +5 -0
  54. package/build/config/auth.d.ts.map +1 -0
  55. package/build/config/auth.js +11 -0
  56. package/build/config/cache.d.ts +7 -0
  57. package/build/config/cache.d.ts.map +1 -0
  58. package/build/config/cache.js +12 -0
  59. package/build/config/db.d.ts +13 -0
  60. package/build/config/db.d.ts.map +1 -0
  61. package/build/config/db.js +17 -0
  62. package/build/config/http.d.ts +5 -0
  63. package/build/config/http.d.ts.map +1 -0
  64. package/build/config/http.js +10 -0
  65. package/build/config/index.d.ts +9 -0
  66. package/build/config/index.d.ts.map +1 -0
  67. package/build/config/index.js +93 -0
  68. package/build/config/logger.d.ts +7 -0
  69. package/build/config/logger.d.ts.map +1 -0
  70. package/build/config/logger.js +33 -0
  71. package/build/config/messaging.d.ts +7 -0
  72. package/build/config/messaging.d.ts.map +1 -0
  73. package/build/config/messaging.js +12 -0
  74. package/build/config/task.d.ts +6 -0
  75. package/build/config/task.d.ts.map +1 -0
  76. package/build/config/task.js +13 -0
  77. package/build/controllers/api/file.d.ts +8 -0
  78. package/build/controllers/api/file.d.ts.map +1 -0
  79. package/build/controllers/api/file.js +24 -0
  80. package/build/controllers/api/index.d.ts +2 -0
  81. package/build/controllers/api/index.d.ts.map +1 -0
  82. package/build/controllers/api/index.js +16 -0
  83. package/build/controllers/index.d.ts +2 -0
  84. package/build/controllers/index.d.ts.map +1 -0
  85. package/build/controllers/index.js +16 -0
  86. package/build/index.d.ts +8 -0
  87. package/build/index.d.ts.map +1 -0
  88. package/build/index.js +82 -0
  89. package/build/middlewares/access.d.ts +8 -0
  90. package/build/middlewares/access.d.ts.map +1 -0
  91. package/build/middlewares/access.js +21 -0
  92. package/build/middlewares/authentication.d.ts +7 -0
  93. package/build/middlewares/authentication.d.ts.map +1 -0
  94. package/build/middlewares/authentication.js +22 -0
  95. package/build/middlewares/file.d.ts +10 -0
  96. package/build/middlewares/file.d.ts.map +1 -0
  97. package/build/middlewares/file.js +41 -0
  98. package/build/middlewares/index.d.ts +4 -0
  99. package/build/middlewares/index.d.ts.map +1 -0
  100. package/build/middlewares/index.js +38 -0
  101. package/build/models/Connection.d.ts +11 -0
  102. package/build/models/Connection.d.ts.map +1 -0
  103. package/build/models/Connection.js +50 -0
  104. package/build/models/index.d.ts +5 -0
  105. package/build/models/index.d.ts.map +1 -0
  106. package/build/models/index.js +14 -0
  107. package/build/server.d.ts +2 -0
  108. package/build/server.d.ts.map +1 -0
  109. package/build/server.js +33 -0
  110. package/build/services/error.d.ts +21 -0
  111. package/build/services/error.d.ts.map +1 -0
  112. package/build/services/error.js +55 -0
  113. package/build/services/express.d.ts +21 -0
  114. package/build/services/express.d.ts.map +1 -0
  115. package/build/services/express.js +142 -0
  116. package/build/services/file.d.ts +12 -0
  117. package/build/services/file.d.ts.map +1 -0
  118. package/build/services/file.js +54 -0
  119. package/build/services/index.d.ts +9 -0
  120. package/build/services/index.d.ts.map +1 -0
  121. package/build/services/index.js +93 -0
  122. package/build/services/rabitmq.d.ts +19 -0
  123. package/build/services/rabitmq.d.ts.map +1 -0
  124. package/build/services/rabitmq.js +87 -0
  125. package/build/services/redis.d.ts +14 -0
  126. package/build/services/redis.d.ts.map +1 -0
  127. package/build/services/redis.js +60 -0
  128. package/build/services/sequelize.d.ts +20 -0
  129. package/build/services/sequelize.d.ts.map +1 -0
  130. package/build/services/sequelize.js +193 -0
  131. package/build/services/socket.d.ts +18 -0
  132. package/build/services/socket.d.ts.map +1 -0
  133. package/build/services/socket.js +56 -0
  134. package/build/services/task.d.ts +53 -0
  135. package/build/services/task.d.ts.map +1 -0
  136. package/build/services/task.js +160 -0
  137. package/build/utils/api.d.ts +15 -0
  138. package/build/utils/api.d.ts.map +1 -0
  139. package/build/utils/api.js +50 -0
  140. package/build/utils/database.d.ts +4 -0
  141. package/build/utils/database.d.ts.map +1 -0
  142. package/build/utils/database.js +36 -0
  143. package/build/utils/date.d.ts +2 -0
  144. package/build/utils/date.d.ts.map +1 -0
  145. package/build/utils/date.js +28 -0
  146. package/build/utils/index.d.ts +6 -0
  147. package/build/utils/index.d.ts.map +1 -0
  148. package/build/utils/index.js +60 -0
  149. package/build/utils/logger.d.ts +3 -0
  150. package/build/utils/logger.d.ts.map +1 -0
  151. package/build/utils/logger.js +45 -0
  152. package/build/utils/session.d.ts +10 -0
  153. package/build/utils/session.d.ts.map +1 -0
  154. package/build/utils/session.js +21 -0
  155. package/package.json +80 -0
@@ -0,0 +1,193 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _service = _interopRequireDefault(require("../app/service"));
8
+ var _db = require("../config/db");
9
+ var _Connection = require("@models/Connection");
10
+ var _index = require("@models/index");
11
+ var _database = require("../utils/database");
12
+ var _logger = _interopRequireDefault(require("../utils/logger"));
13
+ var _sequelize = require("sequelize");
14
+ var _error = require("./error");
15
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
16
+ /**
17
+ * Serviço de gerênciamento do banco de dados
18
+ *
19
+ * @class SequelizeService
20
+ * @extends {Service}
21
+ * @implements {IService}
22
+ */
23
+ class SequelizeService extends _service.default {
24
+ serviceDB;
25
+ masterDB;
26
+ models;
27
+ connections;
28
+
29
+ //# Initialization
30
+ constructor() {
31
+ super();
32
+ }
33
+ async init() {
34
+ this.serviceDB = _database.serviceDB;
35
+ this.masterDB = _database.masterDB;
36
+ this.models = _index.Models;
37
+ this.connections = new Map();
38
+ await this.connectMainDatabase();
39
+ }
40
+ async connectMainDatabase() {
41
+ return new Promise(resolve => {
42
+ this.masterDB.sync({
43
+ force: false
44
+ }).then(() => {
45
+ this.log('✅ Connected master database sucessfully', 'success');
46
+ this.serviceDB.sync({
47
+ force: false
48
+ }).then(() => {
49
+ this.log('✅ Connected service database sucessfully', 'success');
50
+ resolve(true);
51
+ }).catch(reason => {
52
+ new _error.ApplicationException('Não foi possível inicializar a conexão com o ServiceDB', reason);
53
+ });
54
+ }).catch(reason => {
55
+ new _error.ApplicationException('Não foi possível inicializar a conexão com MasterDB', reason);
56
+ });
57
+ });
58
+ }
59
+
60
+ //# Common
61
+ reconnect(db, attempts = 2) {
62
+ db.sync().then(() => {
63
+ this.log('Database pronto', 'success');
64
+ }).catch(reason => {
65
+ this.log('Attemping to connect master database', 'warning');
66
+ (0, _logger.default)().warn('Erro ao inicializar o banco de dados, tentando novamente...', reason);
67
+ if (attempts > 0) {
68
+ setTimeout(() => this.reconnect(db, attempts - 1), 5000);
69
+ } else {
70
+ new _error.ApplicationException('Error on connecting master database', reason);
71
+ }
72
+ });
73
+ }
74
+ getMasterConnection() {
75
+ return this.masterDB;
76
+ }
77
+ async createDB(connection_uuid, name) {
78
+ const connData = await _Connection.Connection.findByPk(connection_uuid);
79
+ if (!connData) {
80
+ this.log('Connection data not found: ' + connection_uuid);
81
+ return false;
82
+ }
83
+ const dbQueryName = `${_db.DatabaseConfig.service.database}_${name}`;
84
+ const sequelize = new _sequelize.Sequelize({
85
+ dialect: connData.dialect,
86
+ username: connData.username,
87
+ password: connData.password,
88
+ define: {
89
+ timestamps: true
90
+ },
91
+ timezone: '-03:00',
92
+ logging(sql, timing) {
93
+ (0, _logger.default)().trace('DATABASE', sql, timing);
94
+ }
95
+ });
96
+ return new Promise((resolve, reject) => {
97
+ sequelize.authenticate().then(async () => {
98
+ const [results] = await sequelize.query(`SHOW DATABASES LIKE '${dbQueryName}';`);
99
+ if (results.length === 0) {
100
+ // Se o banco de dados não existir, cria-o
101
+ await sequelize.query(`CREATE DATABASE ${dbQueryName};`);
102
+ this.log(`Database "${dbQueryName}" has been created.`);
103
+ resolve(true);
104
+ } else {
105
+ this.log(`Database "${dbQueryName}" already exists.`);
106
+ resolve(false);
107
+ }
108
+ }).catch(reason => {
109
+ new _error.ApplicationException('Error while checking/creating database:', reason);
110
+ reject(reason);
111
+ }).finally(() => {
112
+ sequelize.close();
113
+ });
114
+ });
115
+ }
116
+ async deleteDB(connection_uuid, name) {
117
+ const dbQueryName = `${_db.DatabaseConfig.service.database}_${name}`;
118
+ const connData = await _Connection.Connection.findByPk(connection_uuid);
119
+ if (!connData) {
120
+ this.log('Connection data not found: ' + connection_uuid);
121
+ return false;
122
+ }
123
+ const sequelize = new _sequelize.Sequelize({
124
+ dialect: connData.dialect,
125
+ username: connData.username,
126
+ password: connData.password,
127
+ define: {
128
+ timestamps: true
129
+ },
130
+ timezone: '-03:00',
131
+ logging(sql, timing) {
132
+ (0, _logger.default)().trace('DATABASE', sql, timing);
133
+ }
134
+ });
135
+ return new Promise((resolve, reject) => {
136
+ sequelize.authenticate().then(async () => {
137
+ // Se o banco de dados não existir, cria-o
138
+ await sequelize.query(`DROP DATABASE IF EXISTS ${dbQueryName};`);
139
+ this.log(`Database "${dbQueryName}" has been deleted.`);
140
+ resolve(true);
141
+ }).catch(reason => {
142
+ new _error.ApplicationException('Error while checking/deleting database:', reason);
143
+ reject(reason);
144
+ }).finally(() => {
145
+ sequelize.close();
146
+ });
147
+ });
148
+ }
149
+ async getDB(uuid, name) {
150
+ const dbQueryName = `${_db.DatabaseConfig.service.database}_${name}`;
151
+ const queryId = `${uuid}@${dbQueryName}`;
152
+ return this.connections.get(queryId);
153
+ }
154
+ async initDB(uuid, name) {
155
+ const dbQueryName = `${_db.DatabaseConfig.service.database}_${name}`;
156
+ const queryId = `${uuid}@${dbQueryName}`;
157
+ if (this.connections.get(queryId)) {
158
+ return false;
159
+ }
160
+ const connData = await _Connection.Connection.findByPk(uuid);
161
+ if (!connData) {
162
+ this.log('Connection data not found: ' + uuid);
163
+ return false;
164
+ }
165
+ const conn = new _sequelize.Sequelize({
166
+ database: dbQueryName,
167
+ dialect: connData.dialect,
168
+ username: connData.username,
169
+ password: connData.password,
170
+ define: {
171
+ timestamps: true
172
+ },
173
+ timezone: '-03:00',
174
+ logging(sql, timing) {
175
+ (0, _logger.default)().trace('DATABASE', sql, timing);
176
+ }
177
+ });
178
+ return new Promise((resolve, reject) => {
179
+ conn.sync({
180
+ force: false
181
+ }).then(() => {
182
+ this.log(`✅ Connected ${dbQueryName} database sucessfully`, 'success');
183
+ this.connections.set(queryId, conn);
184
+ resolve(true);
185
+ }).catch(reason => {
186
+ this.log(`error trying to connect: ${dbQueryName}, UUID: ${uuid}`);
187
+ reject(reason);
188
+ new _error.ApplicationException(`error trying to connect: ${dbQueryName}, UUID: ${uuid}`, reason);
189
+ });
190
+ });
191
+ }
192
+ }
193
+ var _default = exports.default = SequelizeService;
@@ -0,0 +1,18 @@
1
+ import Service from '@app/service';
2
+ import { TSocketEventList } from '../@types';
3
+ import { CorsOptions, CorsOptionsDelegate } from 'cors';
4
+ import { Server as HttpServer } from 'http';
5
+ import { Server } from 'socket.io';
6
+ import { IService } from '../@types/service';
7
+ declare class SocketIOService extends Service implements IService {
8
+ server: Server;
9
+ private mainRouter;
10
+ constructor();
11
+ init(): Promise<void>;
12
+ private onConnection;
13
+ private bindEvents;
14
+ createSocket(app: HttpServer, cors?: CorsOptions | CorsOptionsDelegate): void;
15
+ registerRouter(router: TSocketEventList): void;
16
+ }
17
+ export default SocketIOService;
18
+ //# sourceMappingURL=socket.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"socket.d.ts","sourceRoot":"","sources":["../../src/services/socket.ts"],"names":[],"mappings":"AACA,OAAO,OAAO,MAAM,cAAc,CAAC;AACnC,OAAO,EAAgC,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC3E,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,MAAM,CAAC;AACxD,OAAO,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,MAAM,CAAC;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAW7C,cAAM,eAAgB,SAAQ,OAAQ,YAAW,QAAQ;IAChD,MAAM,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,UAAU,CAAmB;;IAK/B,IAAI;IAQV,OAAO,CAAC,YAAY;IAMpB,OAAO,CAAC,UAAU;IAQlB,YAAY,CAAC,GAAG,EAAE,UAAU,EAAE,IAAI,GAAE,WAAW,GAAG,mBAAqC;IAIvF,cAAc,CAAC,MAAM,EAAE,gBAAgB;CAGxC;AAED,eAAe,eAAe,CAAC"}
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _controller = require("../app/controller");
8
+ var _service = _interopRequireDefault(require("../app/service"));
9
+ var _socket = require("socket.io");
10
+ var _express = _interopRequireDefault(require("./express"));
11
+ var _error = require("./error");
12
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
+ /**
14
+ * Serviço de gerênciamento de comunicação WS/WSS Socket
15
+ *
16
+ * @class SocketIOService
17
+ * @extends {Service}
18
+ * @implements {IService}
19
+ */
20
+ class SocketIOService extends _service.default {
21
+ server;
22
+ mainRouter;
23
+ constructor() {
24
+ super();
25
+ }
26
+ async init() {
27
+ if (!this.mainRouter) throw new _error.ApplicationException('Need router'); //!sem router
28
+ const server = _express.default.singleton().getHttpServer();
29
+ this.createSocket(server);
30
+ this.server.on('connection', this.onConnection.bind(this));
31
+ this.log('Socket server created');
32
+ }
33
+ onConnection(socket) {
34
+ this.log(`${socket.id} conectado`);
35
+ socket.on('disconnect', () => this.log(`${socket.id} desconectado`));
36
+ this.bindEvents(socket);
37
+ }
38
+ bindEvents(socket) {
39
+ Object.entries(this.mainRouter).forEach(([key, handle]) => {
40
+ socket.on(key, (...args) => {
41
+ handle(new _controller.SocketRequest(socket, key), ...args);
42
+ });
43
+ });
44
+ }
45
+ createSocket(app, cors = {
46
+ origin: '*'
47
+ }) {
48
+ this.server = new _socket.Server(app, {
49
+ cors
50
+ });
51
+ }
52
+ registerRouter(router) {
53
+ this.mainRouter = router;
54
+ }
55
+ }
56
+ var _default = exports.default = SocketIOService;
@@ -0,0 +1,53 @@
1
+ import Service from '@app/service';
2
+ import Singleton from '@app/singleton';
3
+ import type { Job } from 'bullmq';
4
+ import { Queue, Worker } from 'bullmq';
5
+ import { IService } from '../@types/service';
6
+ declare abstract class TaskServiceBase extends Service implements IService {
7
+ abstract init(): Promise<void>;
8
+ abstract registerJob(...args: any[]): Promise<void>;
9
+ }
10
+ export declare class TaskSchedulingService extends TaskServiceBase {
11
+ private jobsPath;
12
+ private jobs;
13
+ init(): Promise<void>;
14
+ registerJob(job: SchedulingTask): Promise<void>;
15
+ private importAllJobs;
16
+ private processAllJobs;
17
+ }
18
+ export declare abstract class SchedulingTask extends Singleton {
19
+ protected abstract cron: string;
20
+ protected abstract name: string;
21
+ protected abstract active: boolean;
22
+ abstract handler(): Promise<void>;
23
+ getJobTime(): string;
24
+ getActive(): boolean;
25
+ getJobName(): string;
26
+ }
27
+ export declare class TaskQueueService extends TaskServiceBase {
28
+ private jobsPath;
29
+ private jobs;
30
+ private connection;
31
+ init(): Promise<void>;
32
+ registerJob(job: QueueTask<any>): Promise<void>;
33
+ private importAllJobs;
34
+ private processAllJobs;
35
+ }
36
+ export declare abstract class QueueTask<DataType, ResultType = any, NameType extends string = string> extends Singleton {
37
+ protected queue: Queue;
38
+ protected worker: Worker;
39
+ protected abstract active: boolean;
40
+ protected abstract queueName: string;
41
+ initialize(): void;
42
+ abstract init(): Promise<void>;
43
+ addJob(data: DataType): Promise<void>;
44
+ getQueue(): Queue<any, any, string>;
45
+ setQueue(queue: Queue): void;
46
+ getWorker(): Worker<any, any, string>;
47
+ setWorker(worker: Worker): void;
48
+ getActive(): boolean;
49
+ getQueueName(): string;
50
+ abstract handle(_job: Job<DataType, ResultType, NameType>, _token?: string): Promise<ResultType>;
51
+ }
52
+ export {};
53
+ //# sourceMappingURL=task.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"task.d.ts","sourceRoot":"","sources":["../../src/services/task.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,cAAc,CAAC;AACnC,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAEvC,OAAO,KAAK,EAAqB,GAAG,EAAE,MAAM,QAAQ,CAAC;AACrD,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAGvC,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAK7C,uBAAe,eAAgB,SAAQ,OAAQ,YAAW,QAAQ;IAChE,QAAQ,CAAC,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAC9B,QAAQ,CAAC,WAAW,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;CACpD;AAGD,qBAAa,qBAAsB,SAAQ,eAAe;IACxD,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,IAAI,CAAmB;IAEzB,IAAI;IAUG,WAAW,CAAC,GAAG,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;YAG9C,aAAa;YAcb,cAAc;CAO7B;AACD,8BAAsB,cAAe,SAAQ,SAAS;IACpD,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAChC,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAChC,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IAEnC,QAAQ,CAAC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAE1B,UAAU;IAGV,SAAS;IAGT,UAAU;CAGlB;AAGD,qBAAa,gBAAiB,SAAQ,eAAe;IACnD,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,IAAI,CAAmB;IAC/B,OAAO,CAAC,UAAU,CAAoB;IAEhC,IAAI;IAgBG,WAAW,CAAC,GAAG,EAAE,SAAS,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;YAI9C,aAAa;YAUb,cAAc;CAmB7B;AACD,8BAAsB,SAAS,CAC7B,QAAQ,EACR,UAAU,GAAG,GAAG,EAChB,QAAQ,SAAS,MAAM,GAAG,MAAM,CAChC,SAAQ,SAAS;IACjB,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC;IACvB,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IACzB,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACnC,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAE9B,UAAU;IAoBjB,QAAQ,CAAC,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAEjB,MAAM,CAAC,IAAI,EAAE,QAAQ;IAI3B,QAAQ;IAGR,QAAQ,CAAC,KAAK,EAAE,KAAK;IAGrB,SAAS;IAGT,SAAS,CAAC,MAAM,EAAE,MAAM;IAGxB,SAAS;IAIT,YAAY,IAAI,MAAM;IAG7B,QAAQ,CAAC,MAAM,CAEb,IAAI,EAAE,GAAG,CAAC,QAAQ,EAAE,UAAU,EAAE,QAAQ,CAAC,EAEzC,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,UAAU,CAAC;CACvB"}
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.TaskSchedulingService = exports.TaskQueueService = exports.SchedulingTask = exports.QueueTask = void 0;
7
+ var _service = _interopRequireDefault(require("../app/service"));
8
+ var _singleton = _interopRequireDefault(require("../app/singleton"));
9
+ var _task = _interopRequireDefault(require("../config/task"));
10
+ var _bullmq = require("bullmq");
11
+ var _fs = require("fs");
12
+ var _nodeSchedule = require("node-schedule");
13
+ var _cache = _interopRequireDefault(require("../config/cache"));
14
+ var _error = require("./error");
15
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
16
+ //* Base
17
+ class TaskServiceBase extends _service.default {}
18
+
19
+ //* Scheduler
20
+ class TaskSchedulingService extends TaskServiceBase {
21
+ jobsPath;
22
+ jobs;
23
+ async init() {
24
+ this.jobsPath = _task.default.schedulerPath;
25
+ this.jobs = [];
26
+ this.log('Initializing jobs...', 'info');
27
+ await this.importAllJobs();
28
+ await this.processAllJobs();
29
+ this.log('All jobs initializeds...', 'info');
30
+ }
31
+ async registerJob(job) {
32
+ this.jobs.push(job);
33
+ }
34
+ async importAllJobs() {
35
+ if (!(0, _fs.existsSync)(this.jobsPath)) (0, _fs.mkdirSync)(this.jobsPath, {
36
+ recursive: true
37
+ });
38
+ for (const handlerFile of (0, _fs.readdirSync)(this.jobsPath)) {
39
+ try {
40
+ const job = require(`${this.jobsPath}/${handlerFile}`).default;
41
+ const jobInstance = job.singleton();
42
+ this.jobs.push(jobInstance);
43
+ } catch (error) {
44
+ new _error.ApplicationException(`Falha ao iniciar um ou mais jobs: ${handlerFile}`, error);
45
+ }
46
+ }
47
+ }
48
+ async processAllJobs() {
49
+ for (const job of this.jobs) {
50
+ if (!job.getActive()) continue;
51
+ (0, _nodeSchedule.scheduleJob)(job.getJobTime(), job.handler.bind(job));
52
+ this.log(`Job '${job.getJobName()}' started...`, 'warning');
53
+ }
54
+ }
55
+ }
56
+ exports.TaskSchedulingService = TaskSchedulingService;
57
+ class SchedulingTask extends _singleton.default {
58
+ getJobTime() {
59
+ return this.cron;
60
+ }
61
+ getActive() {
62
+ return this.active;
63
+ }
64
+ getJobName() {
65
+ return this.name;
66
+ }
67
+ }
68
+
69
+ //* Queue
70
+ exports.SchedulingTask = SchedulingTask;
71
+ class TaskQueueService extends TaskServiceBase {
72
+ jobsPath;
73
+ jobs;
74
+ connection;
75
+ async init() {
76
+ this.jobsPath = _task.default.queuePath;
77
+ this.jobs = [];
78
+ this.connection = {
79
+ host: _cache.default.host,
80
+ port: Number(_cache.default.port),
81
+ password: _cache.default.password
82
+ };
83
+ this.log('Initializing jobs...', 'info');
84
+
85
+ // await this.importAllJobs();
86
+ await this.processAllJobs();
87
+ this.log('All jobs initializeds...', 'info');
88
+ }
89
+ async registerJob(job) {
90
+ this.jobs.push(job);
91
+ }
92
+ async importAllJobs() {
93
+ if (!(0, _fs.existsSync)(this.jobsPath)) (0, _fs.mkdirSync)(this.jobsPath, {
94
+ recursive: true
95
+ });
96
+ for (const handlerFile of (0, _fs.readdirSync)(this.jobsPath)) {
97
+ const job = require(`${this.jobsPath}/${handlerFile}`).default;
98
+ const jobInstance = job.singleton();
99
+ this.jobs.push(jobInstance);
100
+ }
101
+ }
102
+ async processAllJobs() {
103
+ for (const job of this.jobs) {
104
+ if (!job.getActive()) continue;
105
+ const queue = new _bullmq.Queue(job.getQueueName(), {
106
+ connection: this.connection
107
+ });
108
+ const worker = new _bullmq.Worker(job.getQueueName(), job.handle.bind(job), {
109
+ connection: this.connection
110
+ });
111
+ job.setQueue(queue);
112
+ job.setWorker(worker);
113
+ job.init();
114
+ this.log(`Job '${job.getQueueName()}' started...`, 'warning');
115
+ }
116
+ }
117
+ }
118
+ exports.TaskQueueService = TaskQueueService;
119
+ class QueueTask extends _singleton.default {
120
+ queue;
121
+ worker;
122
+ initialize() {
123
+ this.queue = new _bullmq.Queue(this.getQueueName(), {
124
+ // Configurações do Redis
125
+ connection: {
126
+ host: 'localhost',
127
+ port: 6379
128
+ }
129
+ });
130
+ this.worker = new _bullmq.Worker(this.getQueueName(), this.handle.bind(this), {
131
+ connection: {
132
+ host: 'localhost',
133
+ port: 6379
134
+ }
135
+ });
136
+ }
137
+ async addJob(data) {
138
+ this.log('adicionando à fila...');
139
+ await this.queue.add(this.getQueueName(), data);
140
+ }
141
+ getQueue() {
142
+ return this.queue;
143
+ }
144
+ setQueue(queue) {
145
+ this.queue = queue;
146
+ }
147
+ getWorker() {
148
+ return this.worker;
149
+ }
150
+ setWorker(worker) {
151
+ this.worker = worker;
152
+ }
153
+ getActive() {
154
+ return this.active;
155
+ }
156
+ getQueueName() {
157
+ return this.queueName;
158
+ }
159
+ }
160
+ exports.QueueTask = QueueTask;
@@ -0,0 +1,15 @@
1
+ import Core from '@app/core';
2
+ import { AxiosHeaders, AxiosInstance, Method } from 'axios';
3
+ import { IApiResponse } from '../@types/http';
4
+ declare abstract class Api extends Core {
5
+ axios: AxiosInstance;
6
+ abstract host: string;
7
+ abstract headers: Record<string, any>;
8
+ constructor();
9
+ init(): Promise<void>;
10
+ abstract validateStatus(status: number): boolean;
11
+ request<T>(endpoint: string, method: Method, data?: any, params?: any, headers?: AxiosHeaders): Promise<IApiResponse<T>>;
12
+ protected getLogicalSecurityId(): string;
13
+ }
14
+ export default Api;
15
+ //# sourceMappingURL=api.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/utils/api.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAc,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAEnE,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,uBAAe,GAAI,SAAQ,IAAI;IAC7B,KAAK,EAAE,aAAa,CAAC;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;;IAOhC,IAAI;IAWV,QAAQ,CAAC,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAE1C,OAAO,CAAC,CAAC,EACb,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,IAAI,CAAC,EAAE,GAAG,EACV,MAAM,CAAC,EAAE,GAAG,EACZ,OAAO,CAAC,EAAE,YAAY;IAgBxB,SAAS,CAAC,oBAAoB;CAI/B;AAED,eAAe,GAAG,CAAC"}
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _core = _interopRequireDefault(require("../app/core"));
8
+ var _error = require("../services/error");
9
+ var _axios = _interopRequireDefault(require("axios"));
10
+ var _dateFns = require("date-fns");
11
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
12
+ class Api extends _core.default {
13
+ axios;
14
+ constructor() {
15
+ super();
16
+ this.init();
17
+ }
18
+ async init() {
19
+ this.axios = _axios.default.create({
20
+ baseURL: this.host,
21
+ headers: {
22
+ ...this.headers,
23
+ 'Content-Type': 'application/json'
24
+ },
25
+ validateStatus: this.validateStatus
26
+ });
27
+ }
28
+ async request(endpoint, method, data, params, headers) {
29
+ try {
30
+ const res = await this.axios({
31
+ method,
32
+ url: endpoint,
33
+ data,
34
+ params,
35
+ headers: {
36
+ ...headers,
37
+ ...this.headers
38
+ }
39
+ });
40
+ return res.data;
41
+ } catch (error) {
42
+ new _error.ApplicationException('não foi possível realizar uma requisição', error);
43
+ }
44
+ }
45
+ getLogicalSecurityId() {
46
+ const date = (0, _dateFns.format)(new Date(), 'yyyy-MM-dd');
47
+ return String(Date.parse(date) - 5000);
48
+ }
49
+ }
50
+ var _default = exports.default = Api;
@@ -0,0 +1,4 @@
1
+ import { Sequelize } from 'sequelize';
2
+ export declare const serviceDB: Sequelize;
3
+ export declare const masterDB: Sequelize;
4
+ //# sourceMappingURL=database.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"database.d.ts","sourceRoot":"","sources":["../../src/utils/database.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAGtC,eAAO,MAAM,SAAS,WAYpB,CAAC;AAEH,eAAO,MAAM,QAAQ,WAYnB,CAAC"}
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.serviceDB = exports.masterDB = void 0;
7
+ var _db = require("../config/db");
8
+ var _sequelize = require("sequelize");
9
+ var _logger = _interopRequireDefault(require("./logger"));
10
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
+ const serviceDB = exports.serviceDB = new _sequelize.Sequelize({
12
+ database: _db.DatabaseConfig.service.database,
13
+ dialect: _db.DatabaseConfig.master.dialect,
14
+ username: _db.DatabaseConfig.master.username,
15
+ password: _db.DatabaseConfig.master.password,
16
+ define: {
17
+ timestamps: true
18
+ },
19
+ timezone: '-03:00',
20
+ logging(sql, timing) {
21
+ (0, _logger.default)().trace('DATABASE', sql, timing);
22
+ }
23
+ });
24
+ const masterDB = exports.masterDB = new _sequelize.Sequelize({
25
+ database: _db.DatabaseConfig.master.database,
26
+ dialect: _db.DatabaseConfig.master.dialect,
27
+ username: _db.DatabaseConfig.master.username,
28
+ password: _db.DatabaseConfig.master.password,
29
+ define: {
30
+ timestamps: true
31
+ },
32
+ timezone: '-03:00',
33
+ logging(sql, timing) {
34
+ (0, _logger.default)().trace('DATABASE', sql, timing);
35
+ }
36
+ });
@@ -0,0 +1,2 @@
1
+ export declare function timestampDifference(timestamp1: number, timestamp2: number, type?: string): number;
2
+ //# sourceMappingURL=date.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"date.d.ts","sourceRoot":"","sources":["../../src/utils/date.ts"],"names":[],"mappings":"AAAA,wBAAgB,mBAAmB,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,IAAI,SAAM,UAyBrF"}
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.timestampDifference = timestampDifference;
7
+ function timestampDifference(timestamp1, timestamp2, type = 'd') {
8
+ const difference = timestamp1 - timestamp2;
9
+ let dvrs;
10
+ switch (type) {
11
+ case 'd':
12
+ dvrs = difference / 1000 / 60 / 60 / 24;
13
+ break;
14
+ case 'h':
15
+ dvrs = difference / 1000 / 60 / 60;
16
+ break;
17
+ case 'm':
18
+ dvrs = difference / 1000 / 60;
19
+ break;
20
+ case 's':
21
+ dvrs = difference / 1000;
22
+ break;
23
+ default:
24
+ dvrs = difference / 1000 / 60 / 60 / 24;
25
+ break;
26
+ }
27
+ return Math.round(dvrs * -1 * 100) / 100;
28
+ }
@@ -0,0 +1,6 @@
1
+ export * from './api';
2
+ export * from './database';
3
+ export * from './date';
4
+ export * from './logger';
5
+ export * from './session';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,YAAY,CAAC;AAC3B,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC"}