npm_microservice 1.0.18 → 1.0.19

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 (106) hide show
  1. package/dist/service.js +104 -48
  2. package/dist/services/BaseService.js +73 -20
  3. package/dist/services/CacheService.js +55 -0
  4. package/dist/services/SettingService.js +73 -20
  5. package/dist/services/app/AppTestService.js +75 -22
  6. package/dist/services/app/BannerService.js +74 -21
  7. package/dist/services/app/CategoryService.js +74 -21
  8. package/dist/services/app/NewService.js +73 -20
  9. package/dist/services/app/RecommendedCourseService.js +73 -20
  10. package/dist/services/app/SettingAppService.js +73 -20
  11. package/dist/services/app/TargetService.js +73 -20
  12. package/dist/services/call_center/CategoryService.js +73 -20
  13. package/dist/services/call_center/TicketActivityService.js +73 -20
  14. package/dist/services/call_center/TicketService.js +77 -24
  15. package/dist/services/call_center/TicketTemplateService.js +73 -20
  16. package/dist/services/call_center/TicketTopicService.js +73 -20
  17. package/dist/services/course/CategoryService.js +73 -20
  18. package/dist/services/course/CourseLessonService.js +73 -20
  19. package/dist/services/course/StudentService.js +73 -20
  20. package/dist/services/crm/ContactsService.js +83 -27
  21. package/dist/services/crm/DeviceService.js +73 -20
  22. package/dist/services/finance/InvoicesCancelService.js +73 -20
  23. package/dist/services/finance/InvoicesDetailService.js +73 -20
  24. package/dist/services/finance/InvoicesLogsAssignService.js +73 -20
  25. package/dist/services/finance/InvoicesService.js +73 -20
  26. package/dist/services/finance/PaymentRequestService.js +73 -20
  27. package/dist/services/finance/PaymentService.js +73 -20
  28. package/dist/services/finance/VouchersCategoryService.js +73 -20
  29. package/dist/services/finance/VouchersService.js +73 -20
  30. package/dist/services/finance/WalletTransactionService.js +73 -20
  31. package/dist/services/finance/WalletsService.js +73 -20
  32. package/dist/services/hr/AllowanceService.js +73 -20
  33. package/dist/services/hr/AnnualLeaveService.js +73 -20
  34. package/dist/services/hr/CategoryService.js +73 -20
  35. package/dist/services/hr/DepartmentService.js +73 -20
  36. package/dist/services/hr/DeviceService.js +73 -20
  37. package/dist/services/hr/DocumentService.js +73 -20
  38. package/dist/services/hr/EmployeeExtendService.js +73 -20
  39. package/dist/services/hr/EmployeeExtendTempService.js +73 -20
  40. package/dist/services/hr/EmployeeLeaveService.js +73 -20
  41. package/dist/services/hr/EmployeeQuitService.js +73 -20
  42. package/dist/services/hr/EmployeeService.js +73 -20
  43. package/dist/services/hr/EmployeeShiftService.js +73 -20
  44. package/dist/services/hr/HolidayService.js +73 -20
  45. package/dist/services/hr/JobTitleService.js +73 -20
  46. package/dist/services/hr/PositionService.js +73 -20
  47. package/dist/services/hr/ReasonTypeService.js +73 -20
  48. package/dist/services/hr/ResignationService.js +73 -20
  49. package/dist/services/hr/SalaryService.js +73 -20
  50. package/dist/services/hr/ScheduleOnlineService.js +73 -20
  51. package/dist/services/hr/ScheduleService.js +73 -20
  52. package/dist/services/hr/SettingDocumentService.js +73 -20
  53. package/dist/services/hr/ShiftService.js +73 -20
  54. package/dist/services/hr/ShiftsCustomerCareService.js +73 -20
  55. package/dist/services/hr/TicketExtendService.js +73 -20
  56. package/dist/services/hr/TicketService.js +73 -20
  57. package/dist/services/hr/TicketTypeService.js +73 -20
  58. package/dist/services/hr/TrackingConfirmService.js +73 -20
  59. package/dist/services/hr/TrackingErrorService.js +73 -20
  60. package/dist/services/inventory/InventoryService.js +73 -20
  61. package/dist/services/inventory/LogService.js +73 -20
  62. package/dist/services/inventory/ProductService.js +73 -20
  63. package/dist/services/inventory/ProductShipmentService.js +73 -20
  64. package/dist/services/inventory/StoreService.js +73 -20
  65. package/dist/services/inventory/SupplierService.js +73 -20
  66. package/dist/services/inventory/TransactionDetailService.js +73 -20
  67. package/dist/services/inventory/TransactionService.js +73 -20
  68. package/dist/services/lms/AlbumCategoryService.js +73 -20
  69. package/dist/services/lms/AlbumFileService.js +73 -20
  70. package/dist/services/lms/AlbumService.js +74 -21
  71. package/dist/services/lms/ClassScheduleService.js +73 -20
  72. package/dist/services/lms/ClassesService.js +73 -20
  73. package/dist/services/lms/CourseLessonService.js +73 -20
  74. package/dist/services/lms/CoursePriceService.js +73 -20
  75. package/dist/services/lms/CourseProductService.js +73 -20
  76. package/dist/services/lms/CourseService.js +73 -20
  77. package/dist/services/lms/DocumentCategoryService.js +73 -20
  78. package/dist/services/lms/DocumentService.js +77 -24
  79. package/dist/services/lms/LicenseService.js +73 -20
  80. package/dist/services/lms/PracticeResultService.js +73 -20
  81. package/dist/services/lms/PracticeService.js +73 -20
  82. package/dist/services/lms/StudentAgainService.js +73 -20
  83. package/dist/services/lms/StudentDocumentService.js +73 -20
  84. package/dist/services/lms/StudentExamTimeService.js +73 -20
  85. package/dist/services/lms/StudentReviewService.js +73 -20
  86. package/dist/services/lms/StudentScoreService.js +73 -20
  87. package/dist/services/lms/StudentService.js +73 -20
  88. package/dist/services/lms/StudentSettingReviewService.js +73 -20
  89. package/dist/services/lms/StudentTrackingService.js +73 -20
  90. package/dist/services/lms/StudentTrialService.js +73 -20
  91. package/dist/services/notification/AppService.js +73 -20
  92. package/dist/services/notification/NotificationService.js +79 -26
  93. package/dist/services/online/CourseLessonService.js +73 -20
  94. package/dist/services/online/CourseService.js +73 -20
  95. package/dist/services/online/StudentService.js +77 -20
  96. package/dist/services/org/BranchRequestService.js +73 -20
  97. package/dist/services/org/BranchService.js +73 -20
  98. package/dist/services/org/BranchToMacsService.js +73 -20
  99. package/dist/services/org/BrandService.js +73 -20
  100. package/dist/services/org/CitiesService.js +73 -20
  101. package/dist/services/org/LegalEntityService.js +73 -20
  102. package/dist/services/pm/NoteService.js +73 -20
  103. package/dist/services/pm/TaskService.js +73 -20
  104. package/dist/services/support/CommentService.js +75 -22
  105. package/dist/services/test/EduTestService.js +73 -20
  106. package/package.json +1 -1
package/dist/service.js CHANGED
@@ -7,8 +7,8 @@ var __commonJS = (cb, mod) => function __require() {
7
7
  var require_config = __commonJS({
8
8
  "src/config.js"(exports2, module2) {
9
9
  require("dotenv").config({ quiet: true, override: true });
10
- var Redis = require("ioredis");
11
- var redis = new Redis({
10
+ var Redis2 = require("ioredis");
11
+ var redis = new Redis2({
12
12
  host: process.env.REDIS_HOST || "128.199.195.180",
13
13
  port: process.env.REDIS_PORT || 6379,
14
14
  password: process.env.REDIS_PASSWORD || "WZyB5Z7BRRgvrzuGzRwB"
@@ -178,13 +178,74 @@ var require_config = __commonJS({
178
178
  }
179
179
  });
180
180
 
181
+ // src/services/CacheService.js
182
+ var require_CacheService = __commonJS({
183
+ "src/services/CacheService.js"(exports2, module2) {
184
+ var qs = require("qs");
185
+ var CacheService = class {
186
+ isCache = false;
187
+ // bật/tắt cache mặc định
188
+ service = null;
189
+ // nhớ gán ở subclass
190
+ module = null;
191
+ // module
192
+ configService = "gateway";
193
+ // module
194
+ timeCache = 3600;
195
+ // cache 60p
196
+ redis = null;
197
+ constructor() {
198
+ this.redis = new Redis();
199
+ }
200
+ buildCacheKeyUrl(path, params = {}) {
201
+ const q = qs.stringify(params, { arrayFormat: "brackets", encode: true });
202
+ const full = `${path}${q ? "?" + q : ""}`;
203
+ return `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.urls:${full}`;
204
+ }
205
+ // Lấy cache theo id
206
+ async getCacheByKey(key) {
207
+ const data = await this.redis.get(key);
208
+ return data ? JSON.parse(data) : null;
209
+ }
210
+ // Lấy cache theo id
211
+ async getCacheById(id) {
212
+ if (!this.isCache || !this.redis) return null;
213
+ const key = `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`;
214
+ const data = await this.redis.get(key);
215
+ return data ? JSON.parse(data) : null;
216
+ }
217
+ // Lấy cache theo nhiều ids
218
+ async getCacheByIds(ids = []) {
219
+ if (!this.isCache || !this.redis) return [];
220
+ const keys = ids.map(
221
+ (id) => `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`
222
+ );
223
+ const results = await this.redis.mget(keys);
224
+ return results.map((r) => r ? JSON.parse(r) : null);
225
+ }
226
+ // Set cache
227
+ async setCache(key, value, ttl = this.timeCache) {
228
+ if (!this.isCache || !this.redis) return;
229
+ await this.redis.set(key, JSON.stringify(value), "EX", ttl);
230
+ }
231
+ // Ví dụ set cache theo id
232
+ async setCacheById(id, value, ttl) {
233
+ const key = `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`;
234
+ await this.setCache(key, value, ttl);
235
+ }
236
+ };
237
+ module2.exports = CacheService;
238
+ }
239
+ });
240
+
181
241
  // src/services/BaseService.js
182
242
  var require_BaseService = __commonJS({
183
243
  "src/services/BaseService.js"(exports2, module2) {
184
244
  var axios = require("axios");
185
245
  var qs = require("qs");
186
246
  var { API_MICROSERVICE_TOKEN, CONFIG_REDIS } = require_config();
187
- var BaseService = class {
247
+ var CacheService = require_CacheService();
248
+ var BaseService = class extends CacheService {
188
249
  baseUrl = "";
189
250
  headers = {};
190
251
  isCache = false;
@@ -196,6 +257,7 @@ var require_BaseService = __commonJS({
196
257
  constructor(baseUrl = "", defaultHeaders = {}) {
197
258
  this.baseUrl = baseUrl;
198
259
  this.setHeader(defaultHeaders);
260
+ this.redis = CONFIG_REDIS;
199
261
  this.http = axios.create({
200
262
  baseURL: this.baseUrl,
201
263
  headers: this.headers,
@@ -221,23 +283,14 @@ var require_BaseService = __commonJS({
221
283
  };
222
284
  return this;
223
285
  }
224
- buildCacheKey(path, params = {}) {
225
- const q = qs.stringify(params, { arrayFormat: "brackets", encode: true });
226
- const full = `${path}${q ? "?" + q : ""}`;
227
- const service = this.service || "unknown";
228
- return `gateway.${service}.urls:${full}`;
229
- }
230
- async request(method, url, options = {}, cacheKey = null, cacheOption = {}) {
286
+ async request(method, url, options = {}, cacheKey2 = null, cacheOption = {}) {
231
287
  var _a, _b;
232
288
  const isGet = method.toLowerCase() === "get";
233
289
  const enableCache = cacheOption.isCache ?? this.isCache;
234
290
  if (isGet && enableCache) {
235
- cacheKey = cacheKey || this.buildCacheKey(url, options.params);
236
- const cached = await CONFIG_REDIS.get(cacheKey);
237
- if (cached) {
238
- console.log(`[CACHE HIT] ${cacheKey}`);
239
- return JSON.parse(cached);
240
- }
291
+ cacheKey2 = cacheKey2 || this.buildCacheKeyUrl(url, options.params);
292
+ const cached = await this.getCacheByKey(cacheKey2);
293
+ if (cached) return JSON.parse(cached);
241
294
  }
242
295
  try {
243
296
  if (isGet) {
@@ -252,10 +305,10 @@ var require_BaseService = __commonJS({
252
305
  });
253
306
  const data = res.data;
254
307
  if (isGet && enableCache) {
255
- cacheKey = cacheKey || this.buildCacheKey(url, options.params);
308
+ cacheKey2 = cacheKey2 || this.buildCacheKeyUrl(url, options.params);
256
309
  const ttl = cacheOption.timeCache || this.timeCache;
257
- await CONFIG_REDIS.set(cacheKey, JSON.stringify(data), "EX", ttl);
258
- console.log(`[CACHE SET] ${cacheKey} - TTL ${ttl}s`);
310
+ await CONFIG_REDIS.set(cacheKey2, JSON.stringify(data), "EX", ttl);
311
+ console.log(`[CACHE SET] ${cacheKey2} - TTL ${ttl}s`);
259
312
  }
260
313
  const duration = (Date.now() - startTime) / 1e3;
261
314
  if (duration > 5) {
@@ -310,7 +363,7 @@ var require_BaseService = __commonJS({
310
363
  }
311
364
  async detail(id, cacheOption = {}) {
312
365
  const enableCache = cacheOption.isCache ?? this.isCache;
313
- const cacheKey = enableCache ? `gateway.${this.service}.${id}` : null;
366
+ if (enableCache && this.getCacheById(id)) return this.getCacheById(id);
314
367
  return this.request(
315
368
  "get",
316
369
  `${this.baseUrl}/${id}`,
@@ -395,7 +448,7 @@ var require_BannerService = __commonJS({
395
448
  var BannerService = class extends BaseService {
396
449
  constructor() {
397
450
  super(CONFIG_ROUTER_SERVICE.App.BannerService);
398
- this.service = "App.BannerService";
451
+ this.service = "AppBannerService";
399
452
  this.isCache = true;
400
453
  }
401
454
  };
@@ -411,7 +464,7 @@ var require_CategoryService = __commonJS({
411
464
  var CategoryService = class extends BaseService {
412
465
  constructor() {
413
466
  super(CONFIG_ROUTER_SERVICE.App.CategoryService);
414
- this.service = "App.CategoryService";
467
+ this.service = "AppCategoryService";
415
468
  this.isCache = true;
416
469
  }
417
470
  };
@@ -508,7 +561,7 @@ var require_AppTestService = __commonJS({
508
561
  var AppTestService = class extends BaseService {
509
562
  constructor() {
510
563
  super(CONFIG_ROUTER_SERVICE.App.AppTestService);
511
- this.service = "app_test";
564
+ this.service = "AppTestService";
512
565
  }
513
566
  async category(filter = {}) {
514
567
  this.service = "app_category";
@@ -517,7 +570,7 @@ var require_AppTestService = __commonJS({
517
570
  }
518
571
  async count(filter = {}) {
519
572
  let url = CONFIG_ROUTER_SERVICE.BASE_URL + "/app/tests/count";
520
- this.service = "app_tests";
573
+ this.service = "AppTestService";
521
574
  return this.request("get", url, { params: filter });
522
575
  }
523
576
  // có thể thêm method riêng cho user nếu cần
@@ -637,10 +690,10 @@ var require_TicketService = __commonJS({
637
690
  this.service = "ticket-topics";
638
691
  return this.request("get", url);
639
692
  }
640
- // async close(params) {
641
- // const url = `${CONFIG_ROUTER_SERVICE.BASE_URL}/call-center/ticket/${params.ticket_id}/close`;
642
- // return this.request("post", url, { data: params });
643
- // }
693
+ async close(params) {
694
+ const url = `${CONFIG_ROUTER_SERVICE.BASE_URL}/call-center/ticket/${params.ticket_id}/close`;
695
+ return this.request("post", url, { data: params });
696
+ }
644
697
  async create(params) {
645
698
  const url = `${CONFIG_ROUTER_SERVICE.BASE_URL}/call-center/ticket`;
646
699
  return this.request("post", url, { data: params });
@@ -722,19 +775,22 @@ var require_ContactsService = __commonJS({
722
775
  this.service = "contacts";
723
776
  }
724
777
  async findEmailOrPhone(input) {
725
- const options = { limit: 1 };
726
- let query = {};
778
+ let filter = {};
727
779
  if (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(input)) {
728
- query = { emailphone: input };
780
+ filter = { emailphone: input };
729
781
  } else {
730
782
  const normalizedPhone = input.replace(/^0/, "+84");
731
- query = { emailphone: normalizedPhone };
783
+ filter = { emailphone: normalizedPhone };
732
784
  }
733
- return await this.all(query);
785
+ return await this.all({ filter });
734
786
  }
735
- async sendOtp(params) {
787
+ async sendOtp(data) {
736
788
  let url = CONFIG_ROUTER_SERVICE.BASE_URL + "/lms/contact-request";
737
- return this.request("post", url, params);
789
+ return this.request("post", url, { data });
790
+ }
791
+ async getContactRequestDetail(id) {
792
+ let url = CONFIG_ROUTER_SERVICE.BASE_URL + "/lms/contact-request/" + id;
793
+ return this.request("get", url);
738
794
  }
739
795
  // có thể thêm method riêng cho user nếu cần
740
796
  };
@@ -1624,7 +1680,7 @@ var require_AlbumService = __commonJS({
1624
1680
  var AlbumService = class extends BaseService {
1625
1681
  constructor() {
1626
1682
  super(CONFIG_ROUTER_SERVICE.Lms.AlbumService);
1627
- this.service = "Lms.AlbumService";
1683
+ this.service = "albumService";
1628
1684
  this.isCache = false;
1629
1685
  }
1630
1686
  async category(filter = {}) {
@@ -1780,14 +1836,14 @@ var require_DocumentService2 = __commonJS({
1780
1836
  this.service = "document_categories";
1781
1837
  return this.request("get", url, { params: filter });
1782
1838
  }
1783
- async favorite(filter = {}) {
1839
+ async favorite(data = {}) {
1784
1840
  let url = CONFIG_ROUTER_SERVICE.BASE_URL + "/lms/student-documents";
1785
1841
  this.service = "student_documents";
1786
- return this.request("post", url, { params: filter });
1842
+ return this.request("post", url, { data });
1787
1843
  }
1788
- async view(id, params) {
1844
+ async view(id, data) {
1789
1845
  const url = `${CONFIG_ROUTER_SERVICE.BASE_URL}/lms/documents/${id}/view`;
1790
- return this.request("post", url, params);
1846
+ return this.request("post", url, data);
1791
1847
  }
1792
1848
  async count(filter = {}) {
1793
1849
  let url = CONFIG_ROUTER_SERVICE.BASE_URL + "/lms/documents/count";
@@ -2037,9 +2093,9 @@ var require_NotificationService = __commonJS({
2037
2093
  this.service = "notifications";
2038
2094
  return this.request("get", url, { params });
2039
2095
  }
2040
- async read(id, params) {
2096
+ async read(id, data) {
2041
2097
  const url = CONFIG_ROUTER_SERVICE.BASE_URL + `/notification/app/is_read/${id}`;
2042
- return this.request("post", url, params);
2098
+ return this.request("post", url, { data });
2043
2099
  }
2044
2100
  async getDetailNotiContact(id) {
2045
2101
  const url = `${CONFIG_ROUTER_SERVICE.BASE_URL}/notification/notification_contact/${id}`;
@@ -2056,13 +2112,13 @@ var require_NotificationService = __commonJS({
2056
2112
  this.service = "devices";
2057
2113
  return this.request("get", url, { params });
2058
2114
  }
2059
- async createMail(params) {
2115
+ async createMail(data) {
2060
2116
  let url = CONFIG_ROUTER_SERVICE.BASE_URL + "/notification/type/mail";
2061
- return this.request("post", url, params);
2117
+ return this.request("post", url, { data });
2062
2118
  }
2063
- async createSms(params) {
2119
+ async createSms(data) {
2064
2120
  let url = CONFIG_ROUTER_SERVICE.BASE_URL + "/notification/type/sms";
2065
- return this.request("post", url, params);
2121
+ return this.request("post", url, { data });
2066
2122
  }
2067
2123
  // có thể thêm method riêng cho user nếu cần
2068
2124
  };
@@ -2378,9 +2434,9 @@ var require_CommentService = __commonJS({
2378
2434
  let url = CONFIG_ROUTER_SERVICE.BASE_URL + "/support/likes/count";
2379
2435
  return this.request("get", url, { params: filter });
2380
2436
  }
2381
- async likes(params) {
2437
+ async likes(data) {
2382
2438
  const url = `${CONFIG_ROUTER_SERVICE.BASE_URL}/support/likes`;
2383
- return this.request("post", url, { data: params });
2439
+ return this.request("post", url, { data });
2384
2440
  }
2385
2441
  async getComment(filter = {}) {
2386
2442
  let url = CONFIG_ROUTER_SERVICE.BASE_URL + "/support/comment/render_comment";
@@ -7,8 +7,8 @@ var __commonJS = (cb, mod) => function __require() {
7
7
  var require_config = __commonJS({
8
8
  "src/config.js"(exports2, module2) {
9
9
  require("dotenv").config({ quiet: true, override: true });
10
- var Redis = require("ioredis");
11
- var redis = new Redis({
10
+ var Redis2 = require("ioredis");
11
+ var redis = new Redis2({
12
12
  host: process.env.REDIS_HOST || "128.199.195.180",
13
13
  port: process.env.REDIS_PORT || 6379,
14
14
  password: process.env.REDIS_PASSWORD || "WZyB5Z7BRRgvrzuGzRwB"
@@ -178,11 +178,72 @@ var require_config = __commonJS({
178
178
  }
179
179
  });
180
180
 
181
+ // src/services/CacheService.js
182
+ var require_CacheService = __commonJS({
183
+ "src/services/CacheService.js"(exports2, module2) {
184
+ var qs2 = require("qs");
185
+ var CacheService2 = class {
186
+ isCache = false;
187
+ // bật/tắt cache mặc định
188
+ service = null;
189
+ // nhớ gán ở subclass
190
+ module = null;
191
+ // module
192
+ configService = "gateway";
193
+ // module
194
+ timeCache = 3600;
195
+ // cache 60p
196
+ redis = null;
197
+ constructor() {
198
+ this.redis = new Redis();
199
+ }
200
+ buildCacheKeyUrl(path, params = {}) {
201
+ const q = qs2.stringify(params, { arrayFormat: "brackets", encode: true });
202
+ const full = `${path}${q ? "?" + q : ""}`;
203
+ return `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.urls:${full}`;
204
+ }
205
+ // Lấy cache theo id
206
+ async getCacheByKey(key) {
207
+ const data = await this.redis.get(key);
208
+ return data ? JSON.parse(data) : null;
209
+ }
210
+ // Lấy cache theo id
211
+ async getCacheById(id) {
212
+ if (!this.isCache || !this.redis) return null;
213
+ const key = `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`;
214
+ const data = await this.redis.get(key);
215
+ return data ? JSON.parse(data) : null;
216
+ }
217
+ // Lấy cache theo nhiều ids
218
+ async getCacheByIds(ids = []) {
219
+ if (!this.isCache || !this.redis) return [];
220
+ const keys = ids.map(
221
+ (id) => `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`
222
+ );
223
+ const results = await this.redis.mget(keys);
224
+ return results.map((r) => r ? JSON.parse(r) : null);
225
+ }
226
+ // Set cache
227
+ async setCache(key, value, ttl = this.timeCache) {
228
+ if (!this.isCache || !this.redis) return;
229
+ await this.redis.set(key, JSON.stringify(value), "EX", ttl);
230
+ }
231
+ // Ví dụ set cache theo id
232
+ async setCacheById(id, value, ttl) {
233
+ const key = `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`;
234
+ await this.setCache(key, value, ttl);
235
+ }
236
+ };
237
+ module2.exports = CacheService2;
238
+ }
239
+ });
240
+
181
241
  // src/services/BaseService.js
182
242
  var axios = require("axios");
183
243
  var qs = require("qs");
184
244
  var { API_MICROSERVICE_TOKEN, CONFIG_REDIS } = require_config();
185
- var BaseService = class {
245
+ var CacheService = require_CacheService();
246
+ var BaseService = class extends CacheService {
186
247
  baseUrl = "";
187
248
  headers = {};
188
249
  isCache = false;
@@ -194,6 +255,7 @@ var BaseService = class {
194
255
  constructor(baseUrl = "", defaultHeaders = {}) {
195
256
  this.baseUrl = baseUrl;
196
257
  this.setHeader(defaultHeaders);
258
+ this.redis = CONFIG_REDIS;
197
259
  this.http = axios.create({
198
260
  baseURL: this.baseUrl,
199
261
  headers: this.headers,
@@ -219,23 +281,14 @@ var BaseService = class {
219
281
  };
220
282
  return this;
221
283
  }
222
- buildCacheKey(path, params = {}) {
223
- const q = qs.stringify(params, { arrayFormat: "brackets", encode: true });
224
- const full = `${path}${q ? "?" + q : ""}`;
225
- const service = this.service || "unknown";
226
- return `gateway.${service}.urls:${full}`;
227
- }
228
- async request(method, url, options = {}, cacheKey = null, cacheOption = {}) {
284
+ async request(method, url, options = {}, cacheKey2 = null, cacheOption = {}) {
229
285
  var _a, _b;
230
286
  const isGet = method.toLowerCase() === "get";
231
287
  const enableCache = cacheOption.isCache ?? this.isCache;
232
288
  if (isGet && enableCache) {
233
- cacheKey = cacheKey || this.buildCacheKey(url, options.params);
234
- const cached = await CONFIG_REDIS.get(cacheKey);
235
- if (cached) {
236
- console.log(`[CACHE HIT] ${cacheKey}`);
237
- return JSON.parse(cached);
238
- }
289
+ cacheKey2 = cacheKey2 || this.buildCacheKeyUrl(url, options.params);
290
+ const cached = await this.getCacheByKey(cacheKey2);
291
+ if (cached) return JSON.parse(cached);
239
292
  }
240
293
  try {
241
294
  if (isGet) {
@@ -250,10 +303,10 @@ var BaseService = class {
250
303
  });
251
304
  const data = res.data;
252
305
  if (isGet && enableCache) {
253
- cacheKey = cacheKey || this.buildCacheKey(url, options.params);
306
+ cacheKey2 = cacheKey2 || this.buildCacheKeyUrl(url, options.params);
254
307
  const ttl = cacheOption.timeCache || this.timeCache;
255
- await CONFIG_REDIS.set(cacheKey, JSON.stringify(data), "EX", ttl);
256
- console.log(`[CACHE SET] ${cacheKey} - TTL ${ttl}s`);
308
+ await CONFIG_REDIS.set(cacheKey2, JSON.stringify(data), "EX", ttl);
309
+ console.log(`[CACHE SET] ${cacheKey2} - TTL ${ttl}s`);
257
310
  }
258
311
  const duration = (Date.now() - startTime) / 1e3;
259
312
  if (duration > 5) {
@@ -308,7 +361,7 @@ var BaseService = class {
308
361
  }
309
362
  async detail(id, cacheOption = {}) {
310
363
  const enableCache = cacheOption.isCache ?? this.isCache;
311
- const cacheKey = enableCache ? `gateway.${this.service}.${id}` : null;
364
+ if (enableCache && this.getCacheById(id)) return this.getCacheById(id);
312
365
  return this.request(
313
366
  "get",
314
367
  `${this.baseUrl}/${id}`,
@@ -0,0 +1,55 @@
1
+ // src/services/CacheService.js
2
+ var qs = require("qs");
3
+ var CacheService = class {
4
+ isCache = false;
5
+ // bật/tắt cache mặc định
6
+ service = null;
7
+ // nhớ gán ở subclass
8
+ module = null;
9
+ // module
10
+ configService = "gateway";
11
+ // module
12
+ timeCache = 3600;
13
+ // cache 60p
14
+ redis = null;
15
+ constructor() {
16
+ this.redis = new Redis();
17
+ }
18
+ buildCacheKeyUrl(path, params = {}) {
19
+ const q = qs.stringify(params, { arrayFormat: "brackets", encode: true });
20
+ const full = `${path}${q ? "?" + q : ""}`;
21
+ return `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.urls:${full}`;
22
+ }
23
+ // Lấy cache theo id
24
+ async getCacheByKey(key) {
25
+ const data = await this.redis.get(key);
26
+ return data ? JSON.parse(data) : null;
27
+ }
28
+ // Lấy cache theo id
29
+ async getCacheById(id) {
30
+ if (!this.isCache || !this.redis) return null;
31
+ const key = `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`;
32
+ const data = await this.redis.get(key);
33
+ return data ? JSON.parse(data) : null;
34
+ }
35
+ // Lấy cache theo nhiều ids
36
+ async getCacheByIds(ids = []) {
37
+ if (!this.isCache || !this.redis) return [];
38
+ const keys = ids.map(
39
+ (id) => `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`
40
+ );
41
+ const results = await this.redis.mget(keys);
42
+ return results.map((r) => r ? JSON.parse(r) : null);
43
+ }
44
+ // Set cache
45
+ async setCache(key, value, ttl = this.timeCache) {
46
+ if (!this.isCache || !this.redis) return;
47
+ await this.redis.set(key, JSON.stringify(value), "EX", ttl);
48
+ }
49
+ // Ví dụ set cache theo id
50
+ async setCacheById(id, value, ttl) {
51
+ const key = `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`;
52
+ await this.setCache(key, value, ttl);
53
+ }
54
+ };
55
+ module.exports = CacheService;
@@ -7,8 +7,8 @@ var __commonJS = (cb, mod) => function __require() {
7
7
  var require_config = __commonJS({
8
8
  "src/config.js"(exports2, module2) {
9
9
  require("dotenv").config({ quiet: true, override: true });
10
- var Redis = require("ioredis");
11
- var redis = new Redis({
10
+ var Redis2 = require("ioredis");
11
+ var redis = new Redis2({
12
12
  host: process.env.REDIS_HOST || "128.199.195.180",
13
13
  port: process.env.REDIS_PORT || 6379,
14
14
  password: process.env.REDIS_PASSWORD || "WZyB5Z7BRRgvrzuGzRwB"
@@ -178,13 +178,74 @@ var require_config = __commonJS({
178
178
  }
179
179
  });
180
180
 
181
+ // src/services/CacheService.js
182
+ var require_CacheService = __commonJS({
183
+ "src/services/CacheService.js"(exports2, module2) {
184
+ var qs = require("qs");
185
+ var CacheService = class {
186
+ isCache = false;
187
+ // bật/tắt cache mặc định
188
+ service = null;
189
+ // nhớ gán ở subclass
190
+ module = null;
191
+ // module
192
+ configService = "gateway";
193
+ // module
194
+ timeCache = 3600;
195
+ // cache 60p
196
+ redis = null;
197
+ constructor() {
198
+ this.redis = new Redis();
199
+ }
200
+ buildCacheKeyUrl(path, params = {}) {
201
+ const q = qs.stringify(params, { arrayFormat: "brackets", encode: true });
202
+ const full = `${path}${q ? "?" + q : ""}`;
203
+ return `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.urls:${full}`;
204
+ }
205
+ // Lấy cache theo id
206
+ async getCacheByKey(key) {
207
+ const data = await this.redis.get(key);
208
+ return data ? JSON.parse(data) : null;
209
+ }
210
+ // Lấy cache theo id
211
+ async getCacheById(id) {
212
+ if (!this.isCache || !this.redis) return null;
213
+ const key = `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`;
214
+ const data = await this.redis.get(key);
215
+ return data ? JSON.parse(data) : null;
216
+ }
217
+ // Lấy cache theo nhiều ids
218
+ async getCacheByIds(ids = []) {
219
+ if (!this.isCache || !this.redis) return [];
220
+ const keys = ids.map(
221
+ (id) => `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`
222
+ );
223
+ const results = await this.redis.mget(keys);
224
+ return results.map((r) => r ? JSON.parse(r) : null);
225
+ }
226
+ // Set cache
227
+ async setCache(key, value, ttl = this.timeCache) {
228
+ if (!this.isCache || !this.redis) return;
229
+ await this.redis.set(key, JSON.stringify(value), "EX", ttl);
230
+ }
231
+ // Ví dụ set cache theo id
232
+ async setCacheById(id, value, ttl) {
233
+ const key = `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`;
234
+ await this.setCache(key, value, ttl);
235
+ }
236
+ };
237
+ module2.exports = CacheService;
238
+ }
239
+ });
240
+
181
241
  // src/services/BaseService.js
182
242
  var require_BaseService = __commonJS({
183
243
  "src/services/BaseService.js"(exports2, module2) {
184
244
  var axios = require("axios");
185
245
  var qs = require("qs");
186
246
  var { API_MICROSERVICE_TOKEN: API_MICROSERVICE_TOKEN2, CONFIG_REDIS } = require_config();
187
- var BaseService2 = class {
247
+ var CacheService = require_CacheService();
248
+ var BaseService2 = class extends CacheService {
188
249
  baseUrl = "";
189
250
  headers = {};
190
251
  isCache = false;
@@ -196,6 +257,7 @@ var require_BaseService = __commonJS({
196
257
  constructor(baseUrl = "", defaultHeaders = {}) {
197
258
  this.baseUrl = baseUrl;
198
259
  this.setHeader(defaultHeaders);
260
+ this.redis = CONFIG_REDIS;
199
261
  this.http = axios.create({
200
262
  baseURL: this.baseUrl,
201
263
  headers: this.headers,
@@ -221,23 +283,14 @@ var require_BaseService = __commonJS({
221
283
  };
222
284
  return this;
223
285
  }
224
- buildCacheKey(path, params = {}) {
225
- const q = qs.stringify(params, { arrayFormat: "brackets", encode: true });
226
- const full = `${path}${q ? "?" + q : ""}`;
227
- const service = this.service || "unknown";
228
- return `gateway.${service}.urls:${full}`;
229
- }
230
- async request(method, url, options = {}, cacheKey = null, cacheOption = {}) {
286
+ async request(method, url, options = {}, cacheKey2 = null, cacheOption = {}) {
231
287
  var _a, _b;
232
288
  const isGet = method.toLowerCase() === "get";
233
289
  const enableCache = cacheOption.isCache ?? this.isCache;
234
290
  if (isGet && enableCache) {
235
- cacheKey = cacheKey || this.buildCacheKey(url, options.params);
236
- const cached = await CONFIG_REDIS.get(cacheKey);
237
- if (cached) {
238
- console.log(`[CACHE HIT] ${cacheKey}`);
239
- return JSON.parse(cached);
240
- }
291
+ cacheKey2 = cacheKey2 || this.buildCacheKeyUrl(url, options.params);
292
+ const cached = await this.getCacheByKey(cacheKey2);
293
+ if (cached) return JSON.parse(cached);
241
294
  }
242
295
  try {
243
296
  if (isGet) {
@@ -252,10 +305,10 @@ var require_BaseService = __commonJS({
252
305
  });
253
306
  const data = res.data;
254
307
  if (isGet && enableCache) {
255
- cacheKey = cacheKey || this.buildCacheKey(url, options.params);
308
+ cacheKey2 = cacheKey2 || this.buildCacheKeyUrl(url, options.params);
256
309
  const ttl = cacheOption.timeCache || this.timeCache;
257
- await CONFIG_REDIS.set(cacheKey, JSON.stringify(data), "EX", ttl);
258
- console.log(`[CACHE SET] ${cacheKey} - TTL ${ttl}s`);
310
+ await CONFIG_REDIS.set(cacheKey2, JSON.stringify(data), "EX", ttl);
311
+ console.log(`[CACHE SET] ${cacheKey2} - TTL ${ttl}s`);
259
312
  }
260
313
  const duration = (Date.now() - startTime) / 1e3;
261
314
  if (duration > 5) {
@@ -310,7 +363,7 @@ var require_BaseService = __commonJS({
310
363
  }
311
364
  async detail(id, cacheOption = {}) {
312
365
  const enableCache = cacheOption.isCache ?? this.isCache;
313
- const cacheKey = enableCache ? `gateway.${this.service}.${id}` : null;
366
+ if (enableCache && this.getCacheById(id)) return this.getCacheById(id);
314
367
  return this.request(
315
368
  "get",
316
369
  `${this.baseUrl}/${id}`,