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 = 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,8 +430,9 @@ var { CONFIG_ROUTER_SERVICE } = require_config();
|
|
|
392
430
|
var AlbumFileService = class extends BaseService {
|
|
393
431
|
constructor() {
|
|
394
432
|
super(CONFIG_ROUTER_SERVICE.Lms.AlbumFileService);
|
|
395
|
-
this.
|
|
396
|
-
this.
|
|
433
|
+
this.isCache = true;
|
|
434
|
+
this.module = "albumFileService";
|
|
435
|
+
this.service = "lms";
|
|
397
436
|
}
|
|
398
437
|
};
|
|
399
438
|
module.exports = AlbumFileService;
|
|
@@ -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,8 +430,9 @@ var { CONFIG_ROUTER_SERVICE } = require_config();
|
|
|
392
430
|
var AlbumService = class extends BaseService {
|
|
393
431
|
constructor() {
|
|
394
432
|
super(CONFIG_ROUTER_SERVICE.Lms.AlbumService);
|
|
395
|
-
this.
|
|
396
|
-
this.
|
|
433
|
+
this.isCache = true;
|
|
434
|
+
this.module = "albumService";
|
|
435
|
+
this.service = "lms";
|
|
397
436
|
}
|
|
398
437
|
async category(filter = {}) {
|
|
399
438
|
let url = CONFIG_ROUTER_SERVICE.BASE_URL + "/lms/cate_album";
|
|
@@ -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,8 +430,9 @@ var { CONFIG_ROUTER_SERVICE } = require_config();
|
|
|
392
430
|
var ClassScheduleService = class extends BaseService {
|
|
393
431
|
constructor() {
|
|
394
432
|
super(CONFIG_ROUTER_SERVICE.Lms.ClassScheduleService);
|
|
395
|
-
this.service = "Lms.ClassScheduleService";
|
|
396
433
|
this.isCache = true;
|
|
434
|
+
this.module = "classScheduleService";
|
|
435
|
+
this.service = "lms";
|
|
397
436
|
}
|
|
398
437
|
};
|
|
399
438
|
module.exports = ClassScheduleService;
|