npm_microservice 1.0.19 → 1.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/service.js +221 -115
- package/dist/services/BaseService.js +75 -37
- package/dist/services/CacheService.js +19 -4
- package/dist/services/SettingService.js +75 -37
- package/dist/services/app/AppTestService.js +77 -38
- package/dist/services/app/BannerService.js +77 -38
- package/dist/services/app/CategoryService.js +77 -38
- package/dist/services/app/NewService.js +77 -38
- package/dist/services/app/RecommendedCourseService.js +77 -38
- package/dist/services/app/SettingAppService.js +77 -37
- package/dist/services/app/TargetService.js +76 -38
- package/dist/services/call_center/CategoryService.js +77 -38
- package/dist/services/call_center/TicketActivityService.js +81 -39
- package/dist/services/call_center/TicketService.js +77 -43
- package/dist/services/call_center/TicketTemplateService.js +77 -38
- package/dist/services/call_center/TicketTopicService.js +77 -38
- package/dist/services/course/CategoryService.js +77 -38
- package/dist/services/course/CourseLessonService.js +78 -38
- package/dist/services/course/StudentService.js +81 -38
- package/dist/services/crm/ContactsService.js +77 -38
- package/dist/services/crm/DeviceService.js +77 -38
- package/dist/services/finance/InvoicesCancelService.js +75 -37
- package/dist/services/finance/InvoicesDetailService.js +75 -37
- package/dist/services/finance/InvoicesLogsAssignService.js +75 -37
- package/dist/services/finance/InvoicesService.js +75 -37
- package/dist/services/finance/PaymentRequestService.js +75 -37
- package/dist/services/finance/PaymentService.js +75 -37
- package/dist/services/finance/VouchersCategoryService.js +75 -37
- package/dist/services/finance/VouchersService.js +75 -37
- package/dist/services/finance/WalletTransactionService.js +75 -37
- package/dist/services/finance/WalletsService.js +75 -37
- package/dist/services/hr/AllowanceService.js +75 -37
- package/dist/services/hr/AnnualLeaveService.js +75 -37
- package/dist/services/hr/CategoryService.js +75 -37
- package/dist/services/hr/DepartmentService.js +75 -37
- package/dist/services/hr/DeviceService.js +75 -37
- package/dist/services/hr/DocumentService.js +75 -37
- package/dist/services/hr/EmployeeExtendService.js +75 -37
- package/dist/services/hr/EmployeeExtendTempService.js +75 -37
- package/dist/services/hr/EmployeeLeaveService.js +75 -37
- package/dist/services/hr/EmployeeQuitService.js +75 -37
- package/dist/services/hr/EmployeeService.js +78 -39
- package/dist/services/hr/EmployeeShiftService.js +80 -40
- package/dist/services/hr/HolidayService.js +77 -38
- package/dist/services/hr/JobTitleService.js +78 -38
- package/dist/services/hr/PositionService.js +78 -39
- package/dist/services/hr/ReasonTypeService.js +78 -38
- package/dist/services/hr/ResignationService.js +75 -37
- package/dist/services/hr/SalaryService.js +75 -37
- package/dist/services/hr/ScheduleOnlineService.js +75 -37
- package/dist/services/hr/ScheduleService.js +75 -37
- package/dist/services/hr/SettingDocumentService.js +77 -38
- package/dist/services/hr/ShiftService.js +77 -38
- package/dist/services/hr/ShiftsCustomerCareService.js +80 -41
- package/dist/services/hr/TicketExtendService.js +78 -38
- package/dist/services/hr/TicketService.js +77 -38
- package/dist/services/hr/TicketTypeService.js +78 -38
- package/dist/services/hr/TrackingConfirmService.js +75 -37
- package/dist/services/hr/TrackingErrorService.js +75 -37
- package/dist/services/inventory/InventoryService.js +75 -37
- package/dist/services/inventory/LogService.js +75 -37
- package/dist/services/inventory/ProductService.js +75 -37
- package/dist/services/inventory/ProductShipmentService.js +75 -37
- package/dist/services/inventory/StoreService.js +75 -37
- package/dist/services/inventory/SupplierService.js +75 -37
- package/dist/services/inventory/TransactionDetailService.js +75 -37
- package/dist/services/inventory/TransactionService.js +75 -37
- package/dist/services/lms/AlbumCategoryService.js +77 -38
- package/dist/services/lms/AlbumFileService.js +78 -39
- package/dist/services/lms/AlbumService.js +78 -39
- package/dist/services/lms/ClassScheduleService.js +77 -38
- package/dist/services/lms/ClassesService.js +77 -38
- package/dist/services/lms/CourseLessonService.js +78 -39
- package/dist/services/lms/CoursePriceService.js +78 -39
- package/dist/services/lms/CourseProductService.js +78 -39
- package/dist/services/lms/CourseService.js +78 -39
- package/dist/services/lms/DocumentCategoryService.js +78 -39
- package/dist/services/lms/DocumentService.js +78 -39
- package/dist/services/lms/LicenseService.js +78 -39
- package/dist/services/lms/PracticeResultService.js +77 -38
- package/dist/services/lms/PracticeService.js +77 -38
- package/dist/services/lms/StudentAgainService.js +77 -38
- package/dist/services/lms/StudentDocumentService.js +77 -38
- package/dist/services/lms/StudentExamTimeService.js +77 -38
- package/dist/services/lms/StudentReviewService.js +77 -38
- package/dist/services/lms/StudentScoreService.js +77 -38
- package/dist/services/lms/StudentService.js +77 -38
- package/dist/services/lms/StudentSettingReviewService.js +77 -38
- package/dist/services/lms/StudentTrackingService.js +77 -38
- package/dist/services/lms/StudentTrialService.js +77 -38
- package/dist/services/notification/AppService.js +77 -38
- package/dist/services/notification/NotificationService.js +76 -37
- package/dist/services/online/CourseLessonService.js +75 -37
- package/dist/services/online/CourseService.js +75 -37
- package/dist/services/online/StudentService.js +75 -37
- package/dist/services/org/BranchRequestService.js +77 -38
- package/dist/services/org/BranchService.js +77 -38
- package/dist/services/org/BranchToMacsService.js +77 -38
- package/dist/services/org/BrandService.js +77 -38
- package/dist/services/org/CitiesService.js +77 -38
- package/dist/services/org/LegalEntityService.js +77 -38
- package/dist/services/pm/NoteService.js +77 -38
- package/dist/services/pm/TaskService.js +77 -38
- package/dist/services/support/CommentService.js +75 -37
- package/dist/services/test/EduTestService.js +77 -38
- package/package.json +1 -1
|
@@ -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
|
|
11
|
-
var redis = new
|
|
10
|
+
var Redis = require("ioredis");
|
|
11
|
+
var redis = new Redis({
|
|
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"
|
|
@@ -189,14 +189,11 @@ var require_CacheService = __commonJS({
|
|
|
189
189
|
// nhớ gán ở subclass
|
|
190
190
|
module = null;
|
|
191
191
|
// module
|
|
192
|
-
configService = "
|
|
192
|
+
configService = "microservice";
|
|
193
193
|
// module
|
|
194
194
|
timeCache = 3600;
|
|
195
195
|
// cache 60p
|
|
196
196
|
redis = null;
|
|
197
|
-
constructor() {
|
|
198
|
-
this.redis = new Redis();
|
|
199
|
-
}
|
|
200
197
|
buildCacheKeyUrl(path, params = {}) {
|
|
201
198
|
const q = qs2.stringify(params, { arrayFormat: "brackets", encode: true });
|
|
202
199
|
const full = `${path}${q ? "?" + q : ""}`;
|
|
@@ -233,6 +230,24 @@ var require_CacheService = __commonJS({
|
|
|
233
230
|
const key = `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`;
|
|
234
231
|
await this.setCache(key, value, ttl);
|
|
235
232
|
}
|
|
233
|
+
async deleteById(id) {
|
|
234
|
+
const key = `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`;
|
|
235
|
+
await this.deleteCache(key);
|
|
236
|
+
}
|
|
237
|
+
async deleteByIds(ids = []) {
|
|
238
|
+
if (!this.isCache || !this.redis) return;
|
|
239
|
+
const keys = ids.map(
|
|
240
|
+
(id) => `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`
|
|
241
|
+
);
|
|
242
|
+
if (keys.length) {
|
|
243
|
+
await this.redis.del(...keys);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
// Xóa cache theo key
|
|
247
|
+
async deleteCache(key) {
|
|
248
|
+
if (!this.isCache || !this.redis) return;
|
|
249
|
+
await this.redis.del(key);
|
|
250
|
+
}
|
|
236
251
|
};
|
|
237
252
|
module2.exports = CacheService2;
|
|
238
253
|
}
|
|
@@ -246,13 +261,8 @@ var CacheService = require_CacheService();
|
|
|
246
261
|
var BaseService = class extends CacheService {
|
|
247
262
|
baseUrl = "";
|
|
248
263
|
headers = {};
|
|
249
|
-
isCache = false;
|
|
250
|
-
// bật/tắt cache mặc định
|
|
251
|
-
service = null;
|
|
252
|
-
// nhớ gán ở subclass
|
|
253
|
-
timeCache = 300;
|
|
254
|
-
// 300s = 5 phút
|
|
255
264
|
constructor(baseUrl = "", defaultHeaders = {}) {
|
|
265
|
+
super(baseUrl, defaultHeaders);
|
|
256
266
|
this.baseUrl = baseUrl;
|
|
257
267
|
this.setHeader(defaultHeaders);
|
|
258
268
|
this.redis = CONFIG_REDIS;
|
|
@@ -281,14 +291,14 @@ var BaseService = class extends CacheService {
|
|
|
281
291
|
};
|
|
282
292
|
return this;
|
|
283
293
|
}
|
|
284
|
-
async request(method, url, options = {},
|
|
294
|
+
async request(method, url, options = {}, cacheOption = {}) {
|
|
285
295
|
var _a, _b;
|
|
286
296
|
const isGet = method.toLowerCase() === "get";
|
|
287
|
-
const enableCache = cacheOption.isCache ??
|
|
297
|
+
const enableCache = cacheOption.isCache ?? false;
|
|
288
298
|
if (isGet && enableCache) {
|
|
289
|
-
|
|
290
|
-
const cached = await this.getCacheByKey(
|
|
291
|
-
if (cached) return
|
|
299
|
+
let cacheKey = (cacheOption == null ? void 0 : cacheOption.cacheKey) ?? this.buildCacheKeyUrl(url, options.params);
|
|
300
|
+
const cached = await this.getCacheByKey(cacheKey);
|
|
301
|
+
if (cached) return cached;
|
|
292
302
|
}
|
|
293
303
|
try {
|
|
294
304
|
if (isGet) {
|
|
@@ -303,10 +313,10 @@ var BaseService = class extends CacheService {
|
|
|
303
313
|
});
|
|
304
314
|
const data = res.data;
|
|
305
315
|
if (isGet && enableCache) {
|
|
306
|
-
|
|
316
|
+
let cacheKey = cacheOption.cacheKey ?? this.buildCacheKeyUrl(url, options.params);
|
|
307
317
|
const ttl = cacheOption.timeCache || this.timeCache;
|
|
308
|
-
await
|
|
309
|
-
console.log(`[CACHE SET] ${
|
|
318
|
+
await this.setCache(cacheKey, data, ttl);
|
|
319
|
+
console.log(`[CACHE SET] ${cacheKey} - TTL ${ttl}s`);
|
|
310
320
|
}
|
|
311
321
|
const duration = (Date.now() - startTime) / 1e3;
|
|
312
322
|
if (duration > 5) {
|
|
@@ -345,39 +355,67 @@ var BaseService = class extends CacheService {
|
|
|
345
355
|
return `${url}${queryString ? "?" + queryString : ""}`;
|
|
346
356
|
}
|
|
347
357
|
async all(query = {}, cacheOption = {}) {
|
|
358
|
+
let { filter = {} } = query;
|
|
359
|
+
const useCache = this.isCache && filter && filter._id && Object.keys(filter).length === 1;
|
|
360
|
+
if (useCache) {
|
|
361
|
+
let ids = Array.isArray(filter._id) ? filter._id : [filter._id];
|
|
362
|
+
return this.details(ids);
|
|
363
|
+
}
|
|
348
364
|
if (query.order_by && Array.isArray(query.order_by)) {
|
|
349
365
|
query.order_by = query.order_by.slice(0, 2);
|
|
350
366
|
if (query.order_by[1] && !["asc", "desc"].includes(query.order_by[1])) {
|
|
351
367
|
query.order_by[1] = "asc";
|
|
352
368
|
}
|
|
353
369
|
}
|
|
354
|
-
return this.request(
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
370
|
+
return this.request("get", this.baseUrl, { params: query }, cacheOption);
|
|
371
|
+
}
|
|
372
|
+
async details(ids = [], options = {}, cacheOption = {}) {
|
|
373
|
+
const enableCache = cacheOption.isCache ?? this.isCache;
|
|
374
|
+
let dataCache = [];
|
|
375
|
+
if (enableCache) {
|
|
376
|
+
dataCache = await this.getCacheByIds(ids);
|
|
377
|
+
const cachedIds = dataCache.filter((item) => item && Object.keys(item).length > 0).map((item) => item._id);
|
|
378
|
+
ids = ids.filter((id) => !cachedIds.includes(id));
|
|
379
|
+
}
|
|
380
|
+
if (ids.length === 0) return dataCache;
|
|
381
|
+
let params = { params: { filter: { _id: ids }, limit: ids.length } };
|
|
382
|
+
const datas = await this.request("get", this.baseUrl, params, cacheOption);
|
|
383
|
+
const rows = [...dataCache];
|
|
384
|
+
if (enableCache && Array.isArray(datas)) {
|
|
385
|
+
for (const item of datas) {
|
|
386
|
+
if (item._id) {
|
|
387
|
+
await this.setCacheById(item._id, item, this.timeCache);
|
|
388
|
+
rows.push(item);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return rows;
|
|
361
393
|
}
|
|
362
394
|
async detail(id, cacheOption = {}) {
|
|
363
395
|
const enableCache = cacheOption.isCache ?? this.isCache;
|
|
364
|
-
if (enableCache
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
cacheOption
|
|
371
|
-
);
|
|
396
|
+
if (enableCache) {
|
|
397
|
+
let data = await this.getCacheById(id);
|
|
398
|
+
if (data) return data;
|
|
399
|
+
}
|
|
400
|
+
let url = `${this.baseUrl}/${id}`;
|
|
401
|
+
return this.request("get", url, {}, cacheOption);
|
|
372
402
|
}
|
|
373
403
|
async create(data) {
|
|
374
404
|
return this.request("post", this.baseUrl, { data });
|
|
375
405
|
}
|
|
376
406
|
async update(id, data) {
|
|
377
|
-
|
|
407
|
+
let response = this.request("put", `${this.baseUrl}/${id}`, { data });
|
|
408
|
+
if (response && (response == null ? void 0 : response.status) == "success" && this.isCache) {
|
|
409
|
+
this.deleteById(id);
|
|
410
|
+
}
|
|
411
|
+
return response;
|
|
378
412
|
}
|
|
379
413
|
async delete(id) {
|
|
380
|
-
|
|
414
|
+
let response = this.request("delete", `${this.baseUrl}/${id}`);
|
|
415
|
+
if (response && (response == null ? void 0 : response.status) == "success" && this.isCache) {
|
|
416
|
+
this.deleteById(id);
|
|
417
|
+
}
|
|
418
|
+
return response;
|
|
381
419
|
}
|
|
382
420
|
};
|
|
383
421
|
module.exports = BaseService;
|
|
@@ -7,14 +7,11 @@ var CacheService = class {
|
|
|
7
7
|
// nhớ gán ở subclass
|
|
8
8
|
module = null;
|
|
9
9
|
// module
|
|
10
|
-
configService = "
|
|
10
|
+
configService = "microservice";
|
|
11
11
|
// module
|
|
12
12
|
timeCache = 3600;
|
|
13
13
|
// cache 60p
|
|
14
14
|
redis = null;
|
|
15
|
-
constructor() {
|
|
16
|
-
this.redis = new Redis();
|
|
17
|
-
}
|
|
18
15
|
buildCacheKeyUrl(path, params = {}) {
|
|
19
16
|
const q = qs.stringify(params, { arrayFormat: "brackets", encode: true });
|
|
20
17
|
const full = `${path}${q ? "?" + q : ""}`;
|
|
@@ -51,5 +48,23 @@ var CacheService = class {
|
|
|
51
48
|
const key = `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`;
|
|
52
49
|
await this.setCache(key, value, ttl);
|
|
53
50
|
}
|
|
51
|
+
async deleteById(id) {
|
|
52
|
+
const key = `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`;
|
|
53
|
+
await this.deleteCache(key);
|
|
54
|
+
}
|
|
55
|
+
async deleteByIds(ids = []) {
|
|
56
|
+
if (!this.isCache || !this.redis) return;
|
|
57
|
+
const keys = ids.map(
|
|
58
|
+
(id) => `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`
|
|
59
|
+
);
|
|
60
|
+
if (keys.length) {
|
|
61
|
+
await this.redis.del(...keys);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// Xóa cache theo key
|
|
65
|
+
async deleteCache(key) {
|
|
66
|
+
if (!this.isCache || !this.redis) return;
|
|
67
|
+
await this.redis.del(key);
|
|
68
|
+
}
|
|
54
69
|
};
|
|
55
70
|
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
|
|
11
|
-
var redis = new
|
|
10
|
+
var Redis = require("ioredis");
|
|
11
|
+
var redis = new Redis({
|
|
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"
|
|
@@ -189,14 +189,11 @@ var require_CacheService = __commonJS({
|
|
|
189
189
|
// nhớ gán ở subclass
|
|
190
190
|
module = null;
|
|
191
191
|
// module
|
|
192
|
-
configService = "
|
|
192
|
+
configService = "microservice";
|
|
193
193
|
// module
|
|
194
194
|
timeCache = 3600;
|
|
195
195
|
// cache 60p
|
|
196
196
|
redis = null;
|
|
197
|
-
constructor() {
|
|
198
|
-
this.redis = new Redis();
|
|
199
|
-
}
|
|
200
197
|
buildCacheKeyUrl(path, params = {}) {
|
|
201
198
|
const q = qs.stringify(params, { arrayFormat: "brackets", encode: true });
|
|
202
199
|
const full = `${path}${q ? "?" + q : ""}`;
|
|
@@ -233,6 +230,24 @@ var require_CacheService = __commonJS({
|
|
|
233
230
|
const key = `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`;
|
|
234
231
|
await this.setCache(key, value, ttl);
|
|
235
232
|
}
|
|
233
|
+
async deleteById(id) {
|
|
234
|
+
const key = `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`;
|
|
235
|
+
await this.deleteCache(key);
|
|
236
|
+
}
|
|
237
|
+
async deleteByIds(ids = []) {
|
|
238
|
+
if (!this.isCache || !this.redis) return;
|
|
239
|
+
const keys = ids.map(
|
|
240
|
+
(id) => `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`
|
|
241
|
+
);
|
|
242
|
+
if (keys.length) {
|
|
243
|
+
await this.redis.del(...keys);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
// Xóa cache theo key
|
|
247
|
+
async deleteCache(key) {
|
|
248
|
+
if (!this.isCache || !this.redis) return;
|
|
249
|
+
await this.redis.del(key);
|
|
250
|
+
}
|
|
236
251
|
};
|
|
237
252
|
module2.exports = CacheService;
|
|
238
253
|
}
|
|
@@ -248,13 +263,8 @@ var require_BaseService = __commonJS({
|
|
|
248
263
|
var BaseService2 = class extends CacheService {
|
|
249
264
|
baseUrl = "";
|
|
250
265
|
headers = {};
|
|
251
|
-
isCache = false;
|
|
252
|
-
// bật/tắt cache mặc định
|
|
253
|
-
service = null;
|
|
254
|
-
// nhớ gán ở subclass
|
|
255
|
-
timeCache = 300;
|
|
256
|
-
// 300s = 5 phút
|
|
257
266
|
constructor(baseUrl = "", defaultHeaders = {}) {
|
|
267
|
+
super(baseUrl, defaultHeaders);
|
|
258
268
|
this.baseUrl = baseUrl;
|
|
259
269
|
this.setHeader(defaultHeaders);
|
|
260
270
|
this.redis = CONFIG_REDIS;
|
|
@@ -283,14 +293,14 @@ var require_BaseService = __commonJS({
|
|
|
283
293
|
};
|
|
284
294
|
return this;
|
|
285
295
|
}
|
|
286
|
-
async request(method, url, options = {},
|
|
296
|
+
async request(method, url, options = {}, cacheOption = {}) {
|
|
287
297
|
var _a, _b;
|
|
288
298
|
const isGet = method.toLowerCase() === "get";
|
|
289
|
-
const enableCache = cacheOption.isCache ??
|
|
299
|
+
const enableCache = cacheOption.isCache ?? false;
|
|
290
300
|
if (isGet && enableCache) {
|
|
291
|
-
|
|
292
|
-
const cached = await this.getCacheByKey(
|
|
293
|
-
if (cached) return
|
|
301
|
+
let cacheKey = (cacheOption == null ? void 0 : cacheOption.cacheKey) ?? this.buildCacheKeyUrl(url, options.params);
|
|
302
|
+
const cached = await this.getCacheByKey(cacheKey);
|
|
303
|
+
if (cached) return cached;
|
|
294
304
|
}
|
|
295
305
|
try {
|
|
296
306
|
if (isGet) {
|
|
@@ -305,10 +315,10 @@ var require_BaseService = __commonJS({
|
|
|
305
315
|
});
|
|
306
316
|
const data = res.data;
|
|
307
317
|
if (isGet && enableCache) {
|
|
308
|
-
|
|
318
|
+
let cacheKey = cacheOption.cacheKey ?? this.buildCacheKeyUrl(url, options.params);
|
|
309
319
|
const ttl = cacheOption.timeCache || this.timeCache;
|
|
310
|
-
await
|
|
311
|
-
console.log(`[CACHE SET] ${
|
|
320
|
+
await this.setCache(cacheKey, data, ttl);
|
|
321
|
+
console.log(`[CACHE SET] ${cacheKey} - TTL ${ttl}s`);
|
|
312
322
|
}
|
|
313
323
|
const duration = (Date.now() - startTime) / 1e3;
|
|
314
324
|
if (duration > 5) {
|
|
@@ -347,39 +357,67 @@ var require_BaseService = __commonJS({
|
|
|
347
357
|
return `${url}${queryString ? "?" + queryString : ""}`;
|
|
348
358
|
}
|
|
349
359
|
async all(query = {}, cacheOption = {}) {
|
|
360
|
+
let { filter = {} } = query;
|
|
361
|
+
const useCache = this.isCache && filter && filter._id && Object.keys(filter).length === 1;
|
|
362
|
+
if (useCache) {
|
|
363
|
+
let ids = Array.isArray(filter._id) ? filter._id : [filter._id];
|
|
364
|
+
return this.details(ids);
|
|
365
|
+
}
|
|
350
366
|
if (query.order_by && Array.isArray(query.order_by)) {
|
|
351
367
|
query.order_by = query.order_by.slice(0, 2);
|
|
352
368
|
if (query.order_by[1] && !["asc", "desc"].includes(query.order_by[1])) {
|
|
353
369
|
query.order_by[1] = "asc";
|
|
354
370
|
}
|
|
355
371
|
}
|
|
356
|
-
return this.request(
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
372
|
+
return this.request("get", this.baseUrl, { params: query }, cacheOption);
|
|
373
|
+
}
|
|
374
|
+
async details(ids = [], options = {}, cacheOption = {}) {
|
|
375
|
+
const enableCache = cacheOption.isCache ?? this.isCache;
|
|
376
|
+
let dataCache = [];
|
|
377
|
+
if (enableCache) {
|
|
378
|
+
dataCache = await this.getCacheByIds(ids);
|
|
379
|
+
const cachedIds = dataCache.filter((item) => item && Object.keys(item).length > 0).map((item) => item._id);
|
|
380
|
+
ids = ids.filter((id) => !cachedIds.includes(id));
|
|
381
|
+
}
|
|
382
|
+
if (ids.length === 0) return dataCache;
|
|
383
|
+
let params = { params: { filter: { _id: ids }, limit: ids.length } };
|
|
384
|
+
const datas = await this.request("get", this.baseUrl, params, cacheOption);
|
|
385
|
+
const rows = [...dataCache];
|
|
386
|
+
if (enableCache && Array.isArray(datas)) {
|
|
387
|
+
for (const item of datas) {
|
|
388
|
+
if (item._id) {
|
|
389
|
+
await this.setCacheById(item._id, item, this.timeCache);
|
|
390
|
+
rows.push(item);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
return rows;
|
|
363
395
|
}
|
|
364
396
|
async detail(id, cacheOption = {}) {
|
|
365
397
|
const enableCache = cacheOption.isCache ?? this.isCache;
|
|
366
|
-
if (enableCache
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
cacheOption
|
|
373
|
-
);
|
|
398
|
+
if (enableCache) {
|
|
399
|
+
let data = await this.getCacheById(id);
|
|
400
|
+
if (data) return data;
|
|
401
|
+
}
|
|
402
|
+
let url = `${this.baseUrl}/${id}`;
|
|
403
|
+
return this.request("get", url, {}, cacheOption);
|
|
374
404
|
}
|
|
375
405
|
async create(data) {
|
|
376
406
|
return this.request("post", this.baseUrl, { data });
|
|
377
407
|
}
|
|
378
408
|
async update(id, data) {
|
|
379
|
-
|
|
409
|
+
let response = this.request("put", `${this.baseUrl}/${id}`, { data });
|
|
410
|
+
if (response && (response == null ? void 0 : response.status) == "success" && this.isCache) {
|
|
411
|
+
this.deleteById(id);
|
|
412
|
+
}
|
|
413
|
+
return response;
|
|
380
414
|
}
|
|
381
415
|
async delete(id) {
|
|
382
|
-
|
|
416
|
+
let response = this.request("delete", `${this.baseUrl}/${id}`);
|
|
417
|
+
if (response && (response == null ? void 0 : response.status) == "success" && this.isCache) {
|
|
418
|
+
this.deleteById(id);
|
|
419
|
+
}
|
|
420
|
+
return response;
|
|
383
421
|
}
|
|
384
422
|
};
|
|
385
423
|
module2.exports = BaseService2;
|
|
@@ -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
|
|
11
|
-
var redis = new
|
|
10
|
+
var Redis = require("ioredis");
|
|
11
|
+
var redis = new Redis({
|
|
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"
|
|
@@ -189,14 +189,11 @@ var require_CacheService = __commonJS({
|
|
|
189
189
|
// nhớ gán ở subclass
|
|
190
190
|
module = null;
|
|
191
191
|
// module
|
|
192
|
-
configService = "
|
|
192
|
+
configService = "microservice";
|
|
193
193
|
// module
|
|
194
194
|
timeCache = 3600;
|
|
195
195
|
// cache 60p
|
|
196
196
|
redis = null;
|
|
197
|
-
constructor() {
|
|
198
|
-
this.redis = new Redis();
|
|
199
|
-
}
|
|
200
197
|
buildCacheKeyUrl(path, params = {}) {
|
|
201
198
|
const q = qs.stringify(params, { arrayFormat: "brackets", encode: true });
|
|
202
199
|
const full = `${path}${q ? "?" + q : ""}`;
|
|
@@ -233,6 +230,24 @@ var require_CacheService = __commonJS({
|
|
|
233
230
|
const key = `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`;
|
|
234
231
|
await this.setCache(key, value, ttl);
|
|
235
232
|
}
|
|
233
|
+
async deleteById(id) {
|
|
234
|
+
const key = `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`;
|
|
235
|
+
await this.deleteCache(key);
|
|
236
|
+
}
|
|
237
|
+
async deleteByIds(ids = []) {
|
|
238
|
+
if (!this.isCache || !this.redis) return;
|
|
239
|
+
const keys = ids.map(
|
|
240
|
+
(id) => `${this.configService}.${this.service || "unknown"}.${this.module || "unknown"}.id:${id}`
|
|
241
|
+
);
|
|
242
|
+
if (keys.length) {
|
|
243
|
+
await this.redis.del(...keys);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
// Xóa cache theo key
|
|
247
|
+
async deleteCache(key) {
|
|
248
|
+
if (!this.isCache || !this.redis) return;
|
|
249
|
+
await this.redis.del(key);
|
|
250
|
+
}
|
|
236
251
|
};
|
|
237
252
|
module2.exports = CacheService;
|
|
238
253
|
}
|
|
@@ -248,13 +263,8 @@ var require_BaseService = __commonJS({
|
|
|
248
263
|
var BaseService2 = class extends CacheService {
|
|
249
264
|
baseUrl = "";
|
|
250
265
|
headers = {};
|
|
251
|
-
isCache = false;
|
|
252
|
-
// bật/tắt cache mặc định
|
|
253
|
-
service = null;
|
|
254
|
-
// nhớ gán ở subclass
|
|
255
|
-
timeCache = 300;
|
|
256
|
-
// 300s = 5 phút
|
|
257
266
|
constructor(baseUrl = "", defaultHeaders = {}) {
|
|
267
|
+
super(baseUrl, defaultHeaders);
|
|
258
268
|
this.baseUrl = baseUrl;
|
|
259
269
|
this.setHeader(defaultHeaders);
|
|
260
270
|
this.redis = CONFIG_REDIS;
|
|
@@ -283,14 +293,14 @@ var require_BaseService = __commonJS({
|
|
|
283
293
|
};
|
|
284
294
|
return this;
|
|
285
295
|
}
|
|
286
|
-
async request(method, url, options = {},
|
|
296
|
+
async request(method, url, options = {}, cacheOption = {}) {
|
|
287
297
|
var _a, _b;
|
|
288
298
|
const isGet = method.toLowerCase() === "get";
|
|
289
|
-
const enableCache = cacheOption.isCache ??
|
|
299
|
+
const enableCache = cacheOption.isCache ?? false;
|
|
290
300
|
if (isGet && enableCache) {
|
|
291
|
-
|
|
292
|
-
const cached = await this.getCacheByKey(
|
|
293
|
-
if (cached) return
|
|
301
|
+
let cacheKey = (cacheOption == null ? void 0 : cacheOption.cacheKey) ?? this.buildCacheKeyUrl(url, options.params);
|
|
302
|
+
const cached = await this.getCacheByKey(cacheKey);
|
|
303
|
+
if (cached) return cached;
|
|
294
304
|
}
|
|
295
305
|
try {
|
|
296
306
|
if (isGet) {
|
|
@@ -305,10 +315,10 @@ var require_BaseService = __commonJS({
|
|
|
305
315
|
});
|
|
306
316
|
const data = res.data;
|
|
307
317
|
if (isGet && enableCache) {
|
|
308
|
-
|
|
318
|
+
let cacheKey = cacheOption.cacheKey ?? this.buildCacheKeyUrl(url, options.params);
|
|
309
319
|
const ttl = cacheOption.timeCache || this.timeCache;
|
|
310
|
-
await
|
|
311
|
-
console.log(`[CACHE SET] ${
|
|
320
|
+
await this.setCache(cacheKey, data, ttl);
|
|
321
|
+
console.log(`[CACHE SET] ${cacheKey} - TTL ${ttl}s`);
|
|
312
322
|
}
|
|
313
323
|
const duration = (Date.now() - startTime) / 1e3;
|
|
314
324
|
if (duration > 5) {
|
|
@@ -347,39 +357,67 @@ var require_BaseService = __commonJS({
|
|
|
347
357
|
return `${url}${queryString ? "?" + queryString : ""}`;
|
|
348
358
|
}
|
|
349
359
|
async all(query = {}, cacheOption = {}) {
|
|
360
|
+
let { filter = {} } = query;
|
|
361
|
+
const useCache = this.isCache && filter && filter._id && Object.keys(filter).length === 1;
|
|
362
|
+
if (useCache) {
|
|
363
|
+
let ids = Array.isArray(filter._id) ? filter._id : [filter._id];
|
|
364
|
+
return this.details(ids);
|
|
365
|
+
}
|
|
350
366
|
if (query.order_by && Array.isArray(query.order_by)) {
|
|
351
367
|
query.order_by = query.order_by.slice(0, 2);
|
|
352
368
|
if (query.order_by[1] && !["asc", "desc"].includes(query.order_by[1])) {
|
|
353
369
|
query.order_by[1] = "asc";
|
|
354
370
|
}
|
|
355
371
|
}
|
|
356
|
-
return this.request(
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
372
|
+
return this.request("get", this.baseUrl, { params: query }, cacheOption);
|
|
373
|
+
}
|
|
374
|
+
async details(ids = [], options = {}, cacheOption = {}) {
|
|
375
|
+
const enableCache = cacheOption.isCache ?? this.isCache;
|
|
376
|
+
let dataCache = [];
|
|
377
|
+
if (enableCache) {
|
|
378
|
+
dataCache = await this.getCacheByIds(ids);
|
|
379
|
+
const cachedIds = dataCache.filter((item) => item && Object.keys(item).length > 0).map((item) => item._id);
|
|
380
|
+
ids = ids.filter((id) => !cachedIds.includes(id));
|
|
381
|
+
}
|
|
382
|
+
if (ids.length === 0) return dataCache;
|
|
383
|
+
let params = { params: { filter: { _id: ids }, limit: ids.length } };
|
|
384
|
+
const datas = await this.request("get", this.baseUrl, params, cacheOption);
|
|
385
|
+
const rows = [...dataCache];
|
|
386
|
+
if (enableCache && Array.isArray(datas)) {
|
|
387
|
+
for (const item of datas) {
|
|
388
|
+
if (item._id) {
|
|
389
|
+
await this.setCacheById(item._id, item, this.timeCache);
|
|
390
|
+
rows.push(item);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
return rows;
|
|
363
395
|
}
|
|
364
396
|
async detail(id, cacheOption = {}) {
|
|
365
397
|
const enableCache = cacheOption.isCache ?? this.isCache;
|
|
366
|
-
if (enableCache
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
cacheOption
|
|
373
|
-
);
|
|
398
|
+
if (enableCache) {
|
|
399
|
+
let data = await this.getCacheById(id);
|
|
400
|
+
if (data) return data;
|
|
401
|
+
}
|
|
402
|
+
let url = `${this.baseUrl}/${id}`;
|
|
403
|
+
return this.request("get", url, {}, cacheOption);
|
|
374
404
|
}
|
|
375
405
|
async create(data) {
|
|
376
406
|
return this.request("post", this.baseUrl, { data });
|
|
377
407
|
}
|
|
378
408
|
async update(id, data) {
|
|
379
|
-
|
|
409
|
+
let response = this.request("put", `${this.baseUrl}/${id}`, { data });
|
|
410
|
+
if (response && (response == null ? void 0 : response.status) == "success" && this.isCache) {
|
|
411
|
+
this.deleteById(id);
|
|
412
|
+
}
|
|
413
|
+
return response;
|
|
380
414
|
}
|
|
381
415
|
async delete(id) {
|
|
382
|
-
|
|
416
|
+
let response = this.request("delete", `${this.baseUrl}/${id}`);
|
|
417
|
+
if (response && (response == null ? void 0 : response.status) == "success" && this.isCache) {
|
|
418
|
+
this.deleteById(id);
|
|
419
|
+
}
|
|
420
|
+
return response;
|
|
383
421
|
}
|
|
384
422
|
};
|
|
385
423
|
module2.exports = BaseService2;
|
|
@@ -392,7 +430,8 @@ var { CONFIG_ROUTER_SERVICE } = require_config();
|
|
|
392
430
|
var AppTestService = class extends BaseService {
|
|
393
431
|
constructor() {
|
|
394
432
|
super(CONFIG_ROUTER_SERVICE.App.AppTestService);
|
|
395
|
-
this.
|
|
433
|
+
this.module = "testService";
|
|
434
|
+
this.service = "app";
|
|
396
435
|
}
|
|
397
436
|
async category(filter = {}) {
|
|
398
437
|
this.service = "app_category";
|