npm_microservice 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/config.js +95 -0
  2. package/dist/services/BaseService.js +232 -0
  3. package/dist/services/Online/CourseLessonService.js +254 -0
  4. package/dist/services/Online/CourseService.js +271 -0
  5. package/dist/services/Online/StudentService.js +262 -0
  6. package/dist/services/SettingAppService.js +249 -0
  7. package/dist/services/SettingService.js +286 -0
  8. package/dist/services/app/CategoryService.js +248 -0
  9. package/dist/services/app/NewService.js +248 -0
  10. package/dist/services/auth.service.js +122 -0
  11. package/dist/services/call_center/CategoryService.js +251 -0
  12. package/dist/services/call_center/TicketActivityService.js +251 -0
  13. package/dist/services/call_center/TicketService.js +286 -0
  14. package/dist/services/call_center/TicketTemplateService.js +251 -0
  15. package/dist/services/call_center/TicketTopicService.js +254 -0
  16. package/dist/services/crm/ContactsService.js +251 -0
  17. package/dist/services/hr/AllowanceService.js +247 -0
  18. package/dist/services/hr/AnnualLeaveService.js +247 -0
  19. package/dist/services/hr/CategoryService.js +248 -0
  20. package/dist/services/hr/DepartmentService.js +247 -0
  21. package/dist/services/hr/DeviceService.js +252 -0
  22. package/dist/services/hr/DocumentService.js +248 -0
  23. package/dist/services/hr/EmployeeExtendService.js +247 -0
  24. package/dist/services/hr/EmployeeExtendTempService.js +247 -0
  25. package/dist/services/hr/EmployeeLeaveService.js +247 -0
  26. package/dist/services/hr/EmployeeQuitService.js +247 -0
  27. package/dist/services/hr/EmployeeService.js +247 -0
  28. package/dist/services/hr/EmployeeShiftService.js +264 -0
  29. package/dist/services/hr/HolidayService.js +248 -0
  30. package/dist/services/hr/JobTitleService.js +247 -0
  31. package/dist/services/hr/PositionService.js +247 -0
  32. package/dist/services/hr/ReasonTypeService.js +247 -0
  33. package/dist/services/hr/ResignationService.js +247 -0
  34. package/dist/services/hr/SalaryService.js +248 -0
  35. package/dist/services/hr/ScheduleOnlineService.js +248 -0
  36. package/dist/services/hr/ScheduleService.js +256 -0
  37. package/dist/services/hr/SettingDocumentService.js +248 -0
  38. package/dist/services/hr/ShiftService.js +252 -0
  39. package/dist/services/hr/ShiftsCustomerCareService.js +264 -0
  40. package/dist/services/hr/TicketExtendService.js +247 -0
  41. package/dist/services/hr/TicketService.js +251 -0
  42. package/dist/services/hr/TicketTypeService.js +247 -0
  43. package/dist/services/hr/TrackingConfirmService.js +252 -0
  44. package/dist/services/hr/TrackingErrorService.js +248 -0
  45. package/dist/services/inventory/InventoryService.js +258 -0
  46. package/dist/services/inventory/LogService.js +256 -0
  47. package/dist/services/inventory/ProductService.js +254 -0
  48. package/dist/services/inventory/ProductShipmentService.js +254 -0
  49. package/dist/services/inventory/StoreService.js +254 -0
  50. package/dist/services/inventory/SupplierService.js +254 -0
  51. package/dist/services/inventory/TransactionDetailService.js +254 -0
  52. package/dist/services/inventory/TransactionService.js +254 -0
  53. package/dist/services/notification/AppService.js +249 -0
  54. package/dist/services/org/BranchRequestService.js +248 -0
  55. package/dist/services/org/BranchService.js +252 -0
  56. package/dist/services/org/BranchToMacsService.js +248 -0
  57. package/dist/services/org/BrandService.js +248 -0
  58. package/dist/services/org/CitiesService.js +248 -0
  59. package/dist/services/org/LegalEntityService.js +248 -0
  60. package/dist/services/pm/NoteService.js +249 -0
  61. package/package.json +32 -0
package/dist/config.js ADDED
@@ -0,0 +1,95 @@
1
+ // src/config.js
2
+ require("dotenv").config();
3
+ var Redis = require("ioredis");
4
+ var redis = new Redis({
5
+ host: process.env.REDIS_HOST || "128.199.195.180",
6
+ port: process.env.REDIS_PORT || 6379,
7
+ password: process.env.REDIS_PASSWORD || "WZyB5Z7BRRgvrzuGzRwB"
8
+ });
9
+ var baseUrl = process.env.API_MICROSERVICE_URL;
10
+ var configRouteService = {
11
+ BASE_URL: baseUrl,
12
+ CallCenter: {
13
+ CategoryService: `${baseUrl}/call-center/ticket-categories`,
14
+ TicketService: `${baseUrl}/call-center/ticket`,
15
+ TicketActivityService: `${baseUrl}/call-center/ticket-activities`,
16
+ TicketTemplateService: `${baseUrl}/call-center/ticket-template`,
17
+ TicketTopicService: `${baseUrl}/call-center/ticket-topics`,
18
+ CallService: `${baseUrl}/call-center/calls`
19
+ },
20
+ Inventory: {
21
+ InventoryService: `${baseUrl}/inventory/inventory-products`,
22
+ LogService: `${baseUrl}/inventory/logs`,
23
+ ProductService: `${baseUrl}/inventory/products`,
24
+ ProductShipmentService: `${baseUrl}/inventory/products_shipment`,
25
+ StoreService: `${baseUrl}/inventory/stores`,
26
+ SupplierService: `${baseUrl}/inventory/supplier`,
27
+ TransactionService: `${baseUrl}/inventory/transaction`,
28
+ TransactionDetailService: `${baseUrl}/inventory/transaction_detail`
29
+ },
30
+ App: {
31
+ CategoryService: `${baseUrl}/app/category`,
32
+ NewService: `${baseUrl}/app/news`
33
+ },
34
+ Pm: {
35
+ NoteService: `${baseUrl}/pm/notes`
36
+ },
37
+ Crm: {
38
+ ContactService: `${baseUrl}/crm/contacts`
39
+ },
40
+ Notification: {
41
+ AppService: `${baseUrl}/notification/type/app`,
42
+ MailService: `${baseUrl}/notification/type/mail`,
43
+ SmsService: `${baseUrl}/notification/type/sms`,
44
+ ZnsService: `${baseUrl}/notification/type/zns`,
45
+ NotificationContactService: `${baseUrl}/notification/notification_contact`,
46
+ NotificationEmployeeService: `${baseUrl}/notification/notification_employee`,
47
+ TemplateService: `${baseUrl}/notification/template`
48
+ },
49
+ Org: {
50
+ BranchService: `${baseUrl}/org/branch`,
51
+ BranchRequestService: `${baseUrl}/org/branch_request`,
52
+ BranchToMacsService: `${baseUrl}/org/branch_to_mac`,
53
+ BrandService: `${baseUrl}/org/brand`,
54
+ CitiesService: `${baseUrl}/org/city`,
55
+ LegalEntityService: `${baseUrl}/org/legal_entities`
56
+ },
57
+ Hr: {
58
+ AllowanceService: `${baseUrl}/hr/allowance`,
59
+ CategoryService: `${baseUrl}/hr/setting/category`,
60
+ AnnualLeaveService: `${baseUrl}/hr/annual_leave`,
61
+ EmployeeService: `${baseUrl}/hr/employees`,
62
+ JobTitleService: `${baseUrl}/hr/setting/job_title`,
63
+ DepartmentService: `${baseUrl}/hr/departments`,
64
+ DeviceService: `${baseUrl}/hr/device`,
65
+ DocumentService: `${baseUrl}/hr/documents`,
66
+ EmployeeExtendService: `${baseUrl}/hr/employees/extend`,
67
+ EmployeeExtendTempService: `${baseUrl}/hr/employees/extend/temp`,
68
+ EmployeeLeaveService: `${baseUrl}/hr/employee_leave`,
69
+ EmployeeNotificationService: `${baseUrl}/hr/notification-employee`,
70
+ EmployeeQuitService: `${baseUrl}/hr/employee_quit`,
71
+ EmployeeShiftService: `${baseUrl}/hr/employee_shift`,
72
+ HolidayService: `${baseUrl}/hr/holidays`,
73
+ PositionService: `${baseUrl}/hr/positions`,
74
+ ReasonTypeService: `${baseUrl}/hr/setting/reason_type`,
75
+ ResignationService: `${baseUrl}/hr/resignations`,
76
+ SalaryService: `${baseUrl}/hr/employee_salary`,
77
+ ScheduleService: `${baseUrl}/hr/schedule`,
78
+ ScheduleOnlineService: `${baseUrl}/hr/schedule_online`,
79
+ ShiftService: `${baseUrl}/hr/setting/shifts`,
80
+ ShiftsCustomerCareService: `${baseUrl}/hr/setting/shifts-customer-care`,
81
+ TicketService: `${baseUrl}/hr/ticket`,
82
+ TicketExtendService: `${baseUrl}/hr/ticket/extend`,
83
+ TicketTypeService: `${baseUrl}/hr/ticket-type`,
84
+ TrackingConfirmService: `${baseUrl}/hr/tracking_confirm`,
85
+ TrackingErrorService: `${baseUrl}/hr/tracking-error`,
86
+ SettingDocumentService: `${baseUrl}/hr/setting/profile`
87
+ }
88
+ };
89
+ module.exports = {
90
+ AUTHENTICATOR_PUBLIC_KEY: process.env.AUTHENTICATOR_PUBLIC_KEY || "",
91
+ API_MICROSERVICE_URL: process.env.API_MICROSERVICE_URL || "",
92
+ API_MICROSERVICE_TOKEN: process.env.API_MICROSERVICE_TOKEN || "",
93
+ CONFIG_ROUTER_SERVICE: configRouteService,
94
+ CONFIG_REDIS: redis
95
+ };
@@ -0,0 +1,232 @@
1
+ var __getOwnPropNames = Object.getOwnPropertyNames;
2
+ var __commonJS = (cb, mod) => function __require() {
3
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
4
+ };
5
+
6
+ // src/config.js
7
+ var require_config = __commonJS({
8
+ "src/config.js"(exports2, module2) {
9
+ require("dotenv").config();
10
+ var Redis = require("ioredis");
11
+ var redis2 = new Redis({
12
+ host: process.env.REDIS_HOST || "128.199.195.180",
13
+ port: process.env.REDIS_PORT || 6379,
14
+ password: process.env.REDIS_PASSWORD || "WZyB5Z7BRRgvrzuGzRwB"
15
+ });
16
+ var baseUrl = process.env.API_MICROSERVICE_URL;
17
+ var configRouteService = {
18
+ BASE_URL: baseUrl,
19
+ CallCenter: {
20
+ CategoryService: `${baseUrl}/call-center/ticket-categories`,
21
+ TicketService: `${baseUrl}/call-center/ticket`,
22
+ TicketActivityService: `${baseUrl}/call-center/ticket-activities`,
23
+ TicketTemplateService: `${baseUrl}/call-center/ticket-template`,
24
+ TicketTopicService: `${baseUrl}/call-center/ticket-topics`,
25
+ CallService: `${baseUrl}/call-center/calls`
26
+ },
27
+ Inventory: {
28
+ InventoryService: `${baseUrl}/inventory/inventory-products`,
29
+ LogService: `${baseUrl}/inventory/logs`,
30
+ ProductService: `${baseUrl}/inventory/products`,
31
+ ProductShipmentService: `${baseUrl}/inventory/products_shipment`,
32
+ StoreService: `${baseUrl}/inventory/stores`,
33
+ SupplierService: `${baseUrl}/inventory/supplier`,
34
+ TransactionService: `${baseUrl}/inventory/transaction`,
35
+ TransactionDetailService: `${baseUrl}/inventory/transaction_detail`
36
+ },
37
+ App: {
38
+ CategoryService: `${baseUrl}/app/category`,
39
+ NewService: `${baseUrl}/app/news`
40
+ },
41
+ Pm: {
42
+ NoteService: `${baseUrl}/pm/notes`
43
+ },
44
+ Crm: {
45
+ ContactService: `${baseUrl}/crm/contacts`
46
+ },
47
+ Notification: {
48
+ AppService: `${baseUrl}/notification/type/app`,
49
+ MailService: `${baseUrl}/notification/type/mail`,
50
+ SmsService: `${baseUrl}/notification/type/sms`,
51
+ ZnsService: `${baseUrl}/notification/type/zns`,
52
+ NotificationContactService: `${baseUrl}/notification/notification_contact`,
53
+ NotificationEmployeeService: `${baseUrl}/notification/notification_employee`,
54
+ TemplateService: `${baseUrl}/notification/template`
55
+ },
56
+ Org: {
57
+ BranchService: `${baseUrl}/org/branch`,
58
+ BranchRequestService: `${baseUrl}/org/branch_request`,
59
+ BranchToMacsService: `${baseUrl}/org/branch_to_mac`,
60
+ BrandService: `${baseUrl}/org/brand`,
61
+ CitiesService: `${baseUrl}/org/city`,
62
+ LegalEntityService: `${baseUrl}/org/legal_entities`
63
+ },
64
+ Hr: {
65
+ AllowanceService: `${baseUrl}/hr/allowance`,
66
+ CategoryService: `${baseUrl}/hr/setting/category`,
67
+ AnnualLeaveService: `${baseUrl}/hr/annual_leave`,
68
+ EmployeeService: `${baseUrl}/hr/employees`,
69
+ JobTitleService: `${baseUrl}/hr/setting/job_title`,
70
+ DepartmentService: `${baseUrl}/hr/departments`,
71
+ DeviceService: `${baseUrl}/hr/device`,
72
+ DocumentService: `${baseUrl}/hr/documents`,
73
+ EmployeeExtendService: `${baseUrl}/hr/employees/extend`,
74
+ EmployeeExtendTempService: `${baseUrl}/hr/employees/extend/temp`,
75
+ EmployeeLeaveService: `${baseUrl}/hr/employee_leave`,
76
+ EmployeeNotificationService: `${baseUrl}/hr/notification-employee`,
77
+ EmployeeQuitService: `${baseUrl}/hr/employee_quit`,
78
+ EmployeeShiftService: `${baseUrl}/hr/employee_shift`,
79
+ HolidayService: `${baseUrl}/hr/holidays`,
80
+ PositionService: `${baseUrl}/hr/positions`,
81
+ ReasonTypeService: `${baseUrl}/hr/setting/reason_type`,
82
+ ResignationService: `${baseUrl}/hr/resignations`,
83
+ SalaryService: `${baseUrl}/hr/employee_salary`,
84
+ ScheduleService: `${baseUrl}/hr/schedule`,
85
+ ScheduleOnlineService: `${baseUrl}/hr/schedule_online`,
86
+ ShiftService: `${baseUrl}/hr/setting/shifts`,
87
+ ShiftsCustomerCareService: `${baseUrl}/hr/setting/shifts-customer-care`,
88
+ TicketService: `${baseUrl}/hr/ticket`,
89
+ TicketExtendService: `${baseUrl}/hr/ticket/extend`,
90
+ TicketTypeService: `${baseUrl}/hr/ticket-type`,
91
+ TrackingConfirmService: `${baseUrl}/hr/tracking_confirm`,
92
+ TrackingErrorService: `${baseUrl}/hr/tracking-error`,
93
+ SettingDocumentService: `${baseUrl}/hr/setting/profile`
94
+ }
95
+ };
96
+ module2.exports = {
97
+ AUTHENTICATOR_PUBLIC_KEY: process.env.AUTHENTICATOR_PUBLIC_KEY || "",
98
+ API_MICROSERVICE_URL: process.env.API_MICROSERVICE_URL || "",
99
+ API_MICROSERVICE_TOKEN: process.env.API_MICROSERVICE_TOKEN || "",
100
+ CONFIG_ROUTER_SERVICE: configRouteService,
101
+ CONFIG_REDIS: redis2
102
+ };
103
+ }
104
+ });
105
+
106
+ // src/services/BaseService.js
107
+ var axios = require("axios");
108
+ var qs = require("qs");
109
+ var { API_MICROSERVICE_TOKEN, CONFIG_REDIS } = require_config();
110
+ var BaseService = class {
111
+ baseUrl = "";
112
+ headers = {};
113
+ isCache = false;
114
+ service = null;
115
+ // nhớ set ở subclass
116
+ timeCache = 300;
117
+ // 300s = 5 phút
118
+ constructor(baseUrl = "", defaultHeaders = {}) {
119
+ this.baseUrl = baseUrl;
120
+ this.setHeader();
121
+ this.http = axios.create({
122
+ baseURL: this.baseUrl,
123
+ headers: this.headers,
124
+ paramsSerializer: (params) => qs.stringify(params, { arrayFormat: "brackets", encode: true })
125
+ });
126
+ }
127
+ // Nhận: object headers, hoặc trực tiếp token string ("Bearer x"), hoặc không truyền gì (auto từ ENV)
128
+ setHeader(headers = {}) {
129
+ var Authorization = `Bearer ${process.env.API_MICROSERVICE_TOKEN}`;
130
+ if (headers && headers.Authorization) Authorization = headers.Authorization;
131
+ if (headers && headers.authorization) Authorization = headers.authorization;
132
+ this.headers = {
133
+ ...this.headers || {},
134
+ Authorization
135
+ };
136
+ return this;
137
+ }
138
+ buildCacheKey(path, params = {}) {
139
+ const q = qs.stringify(params, { arrayFormat: "brackets", encode: true });
140
+ const full = `${path}${q ? "?" + q : ""}`;
141
+ const service = this.service || "unknown";
142
+ return `gateway.${service}.urls:${full}`;
143
+ }
144
+ async request(method, url, options = {}, cacheKey = null) {
145
+ var _a, _b, _c;
146
+ const isGet = method.toLowerCase() === "get";
147
+ if (isGet && this.isCache) {
148
+ cacheKey = cacheKey ? cacheKey : this.buildCacheKey(url, options.params);
149
+ const cached = await redis.get(cacheKey);
150
+ if (cached) {
151
+ console.log(`[CACHE HIT] ${cacheKey}`);
152
+ return JSON.parse(cached);
153
+ }
154
+ }
155
+ try {
156
+ if (method.toLowerCase() === "get") {
157
+ let q = qs.stringify(options.params || {}, {
158
+ arrayFormat: "brackets",
159
+ encode: true
160
+ });
161
+ console.log(`[HTTP GET] ${url}${q ? "?" + q : ""}`);
162
+ } else {
163
+ console.log(`[HTTP ${method.toUpperCase()}] ${url}`);
164
+ }
165
+ if (options.params) console.log("Query:", options.params);
166
+ if (options.data) console.log("Body:", options.data);
167
+ const res = await this.http.request({
168
+ method,
169
+ url,
170
+ // dùng path vì đã set baseURL
171
+ headers: this.headers,
172
+ ...options
173
+ });
174
+ const data = res.data;
175
+ if (isGet && this.isCache) {
176
+ cacheKey = cacheKey ? cacheKey : this.buildCacheKey(url, options.params);
177
+ await redis.set(cacheKey, JSON.stringify(data), "EX", this.timeCache);
178
+ console.log(`[CACHE SET] ${cacheKey} - time ${this.timeCache}s`);
179
+ }
180
+ return data;
181
+ } catch (error) {
182
+ var safeError = {
183
+ // message: error.message || "Request failed",
184
+ statusCode: ((_a = error.response) == null ? void 0 : _a.status) || 500,
185
+ ...(_b = error.response) == null ? void 0 : _b.data
186
+ };
187
+ if (safeError.statusCode == 404) {
188
+ let q = qs.stringify(options.params || {}, {
189
+ arrayFormat: "brackets",
190
+ encode: true
191
+ });
192
+ return {
193
+ statusCode: 404,
194
+ status: "error",
195
+ messag: `[HTTP GET] ${url}${q ? "?" + q : ""} not found`
196
+ };
197
+ }
198
+ console.error(
199
+ `[${method.toUpperCase()}] ${url} failed:`,
200
+ safeError,
201
+ (_c = error.response) == null ? void 0 : _c.data
202
+ );
203
+ return {
204
+ error: true,
205
+ ...safeError
206
+ };
207
+ }
208
+ }
209
+ async all(query = {}) {
210
+ if (query.order_by && Array.isArray(query.order_by)) {
211
+ query.order_by = query.order_by.slice(0, 2);
212
+ if (query.order_by[1] && !["asc", "desc"].includes(query.order_by[1])) {
213
+ query.order_by[1] = "asc";
214
+ }
215
+ }
216
+ return this.request("get", this.baseUrl, { params: query });
217
+ }
218
+ async detail(id) {
219
+ const cacheKey = this.isCache ? `gateway.${this.service}.${id}` : null;
220
+ return this.request("get", `${this.baseUrl}/${id}`);
221
+ }
222
+ async create(data) {
223
+ return this.request("post", this.baseUrl, { data });
224
+ }
225
+ async update(id, data) {
226
+ return this.request("put", `${this.baseUrl}/${id}`, { data });
227
+ }
228
+ async delete(id) {
229
+ return this.request("delete", `${this.baseUrl}/${id}`);
230
+ }
231
+ };
232
+ module.exports = BaseService;
@@ -0,0 +1,254 @@
1
+ var __getOwnPropNames = Object.getOwnPropertyNames;
2
+ var __commonJS = (cb, mod) => function __require() {
3
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
4
+ };
5
+
6
+ // src/config.js
7
+ var require_config = __commonJS({
8
+ "src/config.js"(exports2, module2) {
9
+ require("dotenv").config();
10
+ var Redis = require("ioredis");
11
+ var redis2 = new Redis({
12
+ host: process.env.REDIS_HOST || "128.199.195.180",
13
+ port: process.env.REDIS_PORT || 6379,
14
+ password: process.env.REDIS_PASSWORD || "WZyB5Z7BRRgvrzuGzRwB"
15
+ });
16
+ var baseUrl = process.env.API_MICROSERVICE_URL;
17
+ var configRouteService = {
18
+ BASE_URL: baseUrl,
19
+ CallCenter: {
20
+ CategoryService: `${baseUrl}/call-center/ticket-categories`,
21
+ TicketService: `${baseUrl}/call-center/ticket`,
22
+ TicketActivityService: `${baseUrl}/call-center/ticket-activities`,
23
+ TicketTemplateService: `${baseUrl}/call-center/ticket-template`,
24
+ TicketTopicService: `${baseUrl}/call-center/ticket-topics`,
25
+ CallService: `${baseUrl}/call-center/calls`
26
+ },
27
+ Inventory: {
28
+ InventoryService: `${baseUrl}/inventory/inventory-products`,
29
+ LogService: `${baseUrl}/inventory/logs`,
30
+ ProductService: `${baseUrl}/inventory/products`,
31
+ ProductShipmentService: `${baseUrl}/inventory/products_shipment`,
32
+ StoreService: `${baseUrl}/inventory/stores`,
33
+ SupplierService: `${baseUrl}/inventory/supplier`,
34
+ TransactionService: `${baseUrl}/inventory/transaction`,
35
+ TransactionDetailService: `${baseUrl}/inventory/transaction_detail`
36
+ },
37
+ App: {
38
+ CategoryService: `${baseUrl}/app/category`,
39
+ NewService: `${baseUrl}/app/news`
40
+ },
41
+ Pm: {
42
+ NoteService: `${baseUrl}/pm/notes`
43
+ },
44
+ Crm: {
45
+ ContactService: `${baseUrl}/crm/contacts`
46
+ },
47
+ Notification: {
48
+ AppService: `${baseUrl}/notification/type/app`,
49
+ MailService: `${baseUrl}/notification/type/mail`,
50
+ SmsService: `${baseUrl}/notification/type/sms`,
51
+ ZnsService: `${baseUrl}/notification/type/zns`,
52
+ NotificationContactService: `${baseUrl}/notification/notification_contact`,
53
+ NotificationEmployeeService: `${baseUrl}/notification/notification_employee`,
54
+ TemplateService: `${baseUrl}/notification/template`
55
+ },
56
+ Org: {
57
+ BranchService: `${baseUrl}/org/branch`,
58
+ BranchRequestService: `${baseUrl}/org/branch_request`,
59
+ BranchToMacsService: `${baseUrl}/org/branch_to_mac`,
60
+ BrandService: `${baseUrl}/org/brand`,
61
+ CitiesService: `${baseUrl}/org/city`,
62
+ LegalEntityService: `${baseUrl}/org/legal_entities`
63
+ },
64
+ Hr: {
65
+ AllowanceService: `${baseUrl}/hr/allowance`,
66
+ CategoryService: `${baseUrl}/hr/setting/category`,
67
+ AnnualLeaveService: `${baseUrl}/hr/annual_leave`,
68
+ EmployeeService: `${baseUrl}/hr/employees`,
69
+ JobTitleService: `${baseUrl}/hr/setting/job_title`,
70
+ DepartmentService: `${baseUrl}/hr/departments`,
71
+ DeviceService: `${baseUrl}/hr/device`,
72
+ DocumentService: `${baseUrl}/hr/documents`,
73
+ EmployeeExtendService: `${baseUrl}/hr/employees/extend`,
74
+ EmployeeExtendTempService: `${baseUrl}/hr/employees/extend/temp`,
75
+ EmployeeLeaveService: `${baseUrl}/hr/employee_leave`,
76
+ EmployeeNotificationService: `${baseUrl}/hr/notification-employee`,
77
+ EmployeeQuitService: `${baseUrl}/hr/employee_quit`,
78
+ EmployeeShiftService: `${baseUrl}/hr/employee_shift`,
79
+ HolidayService: `${baseUrl}/hr/holidays`,
80
+ PositionService: `${baseUrl}/hr/positions`,
81
+ ReasonTypeService: `${baseUrl}/hr/setting/reason_type`,
82
+ ResignationService: `${baseUrl}/hr/resignations`,
83
+ SalaryService: `${baseUrl}/hr/employee_salary`,
84
+ ScheduleService: `${baseUrl}/hr/schedule`,
85
+ ScheduleOnlineService: `${baseUrl}/hr/schedule_online`,
86
+ ShiftService: `${baseUrl}/hr/setting/shifts`,
87
+ ShiftsCustomerCareService: `${baseUrl}/hr/setting/shifts-customer-care`,
88
+ TicketService: `${baseUrl}/hr/ticket`,
89
+ TicketExtendService: `${baseUrl}/hr/ticket/extend`,
90
+ TicketTypeService: `${baseUrl}/hr/ticket-type`,
91
+ TrackingConfirmService: `${baseUrl}/hr/tracking_confirm`,
92
+ TrackingErrorService: `${baseUrl}/hr/tracking-error`,
93
+ SettingDocumentService: `${baseUrl}/hr/setting/profile`
94
+ }
95
+ };
96
+ module2.exports = {
97
+ AUTHENTICATOR_PUBLIC_KEY: process.env.AUTHENTICATOR_PUBLIC_KEY || "",
98
+ API_MICROSERVICE_URL: process.env.API_MICROSERVICE_URL || "",
99
+ API_MICROSERVICE_TOKEN: process.env.API_MICROSERVICE_TOKEN || "",
100
+ CONFIG_ROUTER_SERVICE: configRouteService,
101
+ CONFIG_REDIS: redis2
102
+ };
103
+ }
104
+ });
105
+
106
+ // src/services/BaseService.js
107
+ var require_BaseService = __commonJS({
108
+ "src/services/BaseService.js"(exports2, module2) {
109
+ var axios = require("axios");
110
+ var qs = require("qs");
111
+ var { API_MICROSERVICE_TOKEN: API_MICROSERVICE_TOKEN2, CONFIG_REDIS } = require_config();
112
+ var BaseService2 = class {
113
+ baseUrl = "";
114
+ headers = {};
115
+ isCache = false;
116
+ service = null;
117
+ // nhớ set ở subclass
118
+ timeCache = 300;
119
+ // 300s = 5 phút
120
+ constructor(baseUrl = "", defaultHeaders = {}) {
121
+ this.baseUrl = baseUrl;
122
+ this.setHeader();
123
+ this.http = axios.create({
124
+ baseURL: this.baseUrl,
125
+ headers: this.headers,
126
+ paramsSerializer: (params) => qs.stringify(params, { arrayFormat: "brackets", encode: true })
127
+ });
128
+ }
129
+ // Nhận: object headers, hoặc trực tiếp token string ("Bearer x"), hoặc không truyền gì (auto từ ENV)
130
+ setHeader(headers = {}) {
131
+ var Authorization = `Bearer ${process.env.API_MICROSERVICE_TOKEN}`;
132
+ if (headers && headers.Authorization) Authorization = headers.Authorization;
133
+ if (headers && headers.authorization) Authorization = headers.authorization;
134
+ this.headers = {
135
+ ...this.headers || {},
136
+ Authorization
137
+ };
138
+ return this;
139
+ }
140
+ buildCacheKey(path, params = {}) {
141
+ const q = qs.stringify(params, { arrayFormat: "brackets", encode: true });
142
+ const full = `${path}${q ? "?" + q : ""}`;
143
+ const service = this.service || "unknown";
144
+ return `gateway.${service}.urls:${full}`;
145
+ }
146
+ async request(method, url, options = {}, cacheKey = null) {
147
+ var _a, _b, _c;
148
+ const isGet = method.toLowerCase() === "get";
149
+ if (isGet && this.isCache) {
150
+ cacheKey = cacheKey ? cacheKey : this.buildCacheKey(url, options.params);
151
+ const cached = await redis.get(cacheKey);
152
+ if (cached) {
153
+ console.log(`[CACHE HIT] ${cacheKey}`);
154
+ return JSON.parse(cached);
155
+ }
156
+ }
157
+ try {
158
+ if (method.toLowerCase() === "get") {
159
+ let q = qs.stringify(options.params || {}, {
160
+ arrayFormat: "brackets",
161
+ encode: true
162
+ });
163
+ console.log(`[HTTP GET] ${url}${q ? "?" + q : ""}`);
164
+ } else {
165
+ console.log(`[HTTP ${method.toUpperCase()}] ${url}`);
166
+ }
167
+ if (options.params) console.log("Query:", options.params);
168
+ if (options.data) console.log("Body:", options.data);
169
+ const res = await this.http.request({
170
+ method,
171
+ url,
172
+ // dùng path vì đã set baseURL
173
+ headers: this.headers,
174
+ ...options
175
+ });
176
+ const data = res.data;
177
+ if (isGet && this.isCache) {
178
+ cacheKey = cacheKey ? cacheKey : this.buildCacheKey(url, options.params);
179
+ await redis.set(cacheKey, JSON.stringify(data), "EX", this.timeCache);
180
+ console.log(`[CACHE SET] ${cacheKey} - time ${this.timeCache}s`);
181
+ }
182
+ return data;
183
+ } catch (error) {
184
+ var safeError = {
185
+ // message: error.message || "Request failed",
186
+ statusCode: ((_a = error.response) == null ? void 0 : _a.status) || 500,
187
+ ...(_b = error.response) == null ? void 0 : _b.data
188
+ };
189
+ if (safeError.statusCode == 404) {
190
+ let q = qs.stringify(options.params || {}, {
191
+ arrayFormat: "brackets",
192
+ encode: true
193
+ });
194
+ return {
195
+ statusCode: 404,
196
+ status: "error",
197
+ messag: `[HTTP GET] ${url}${q ? "?" + q : ""} not found`
198
+ };
199
+ }
200
+ console.error(
201
+ `[${method.toUpperCase()}] ${url} failed:`,
202
+ safeError,
203
+ (_c = error.response) == null ? void 0 : _c.data
204
+ );
205
+ return {
206
+ error: true,
207
+ ...safeError
208
+ };
209
+ }
210
+ }
211
+ async all(query = {}) {
212
+ if (query.order_by && Array.isArray(query.order_by)) {
213
+ query.order_by = query.order_by.slice(0, 2);
214
+ if (query.order_by[1] && !["asc", "desc"].includes(query.order_by[1])) {
215
+ query.order_by[1] = "asc";
216
+ }
217
+ }
218
+ return this.request("get", this.baseUrl, { params: query });
219
+ }
220
+ async detail(id) {
221
+ const cacheKey = this.isCache ? `gateway.${this.service}.${id}` : null;
222
+ return this.request("get", `${this.baseUrl}/${id}`);
223
+ }
224
+ async create(data) {
225
+ return this.request("post", this.baseUrl, { data });
226
+ }
227
+ async update(id, data) {
228
+ return this.request("put", `${this.baseUrl}/${id}`, { data });
229
+ }
230
+ async delete(id) {
231
+ return this.request("delete", `${this.baseUrl}/${id}`);
232
+ }
233
+ };
234
+ module2.exports = BaseService2;
235
+ }
236
+ });
237
+
238
+ // src/services/Online/CourseLessonService.js
239
+ var BaseService = require_BaseService();
240
+ var { CONFIG_ROUTER_SERVICE, API_MICROSERVICE_TOKEN } = require_config();
241
+ var CourseLessonService = class extends BaseService {
242
+ constructor() {
243
+ super(CONFIG_ROUTER_SERVICE.CourseOnlineLessonService, {
244
+ Authorization: `Bearer ${API_MICROSERVICE_TOKEN}`
245
+ });
246
+ this.service = "course_online_lesson";
247
+ }
248
+ // có thể thêm method riêng cho user nếu cần
249
+ async category(filter = {}) {
250
+ let url = CONFIG_ROUTER_SERVICE.BASE_URL + "/course/category";
251
+ return this.request("get", url, { params: filter });
252
+ }
253
+ };
254
+ module.exports = CourseLessonService;