1 · Repository map — chat-center-api
โครงสร้างไฟล์ทั้งหมดอ้างอิงจาก branch prod (~68 ไฟล์ tracked ไม่รวม node_modules, .git, dist)
Tree
chat-center-api/
├── config/
│ └── configuration.ts รวม env → object เดียว (db, dbCenter, mongoDB, line*, appointmentDD)
├── docs/
│ └── document.config.ts Swagger DocumentBuilder (title "Vtrc RestFull")
├── libs/ boilerplate copy จาก template เดียวกับ Domain Microservices อื่น
│ ├── common/
│ │ ├── base64-to-file.ts
│ │ ├── socket-io.adapter.ts
│ │ └── validator-date.ts
│ ├── exception/http-exception.filter.ts
│ ├── jwt/
│ │ ├── jwt-auth.guard.ts JwtAuthGuard + @Public() decorator
│ │ └── jwt.strategy.ts passport-jwt, secret จาก jwtSecretKey
│ └── log/logs.middleware.ts I3GatewayLogger — console + ./logs/{DD-MM-YYYY}.log
├── src/
│ ├── main.ts entry point
│ ├── app.module.ts root — TypeORM main/center + Mongoose + ทุก feature module
│ ├── app.controller.ts LINE webhook + chat history + image profile (root controller)
│ ├── app.service.ts logic เบื้องหลัง AppController (webhook actions)
│ ├── app.service.ts_bk backup เก่า — ห้ามแก้/ลบตาม workspace convention
│ ├── dto/ class-validator DTO ต่อ domain
│ │ ├── appointment.dto.ts
│ │ ├── cats.dto.ts (ชื่อค้างจาก NestJS cats tutorial — ไม่ถูกใช้ใน controller จริง)
│ │ ├── chat.dto.ts
│ │ ├── conversation.dto.ts LineBody / Event interfaces
│ │ ├── satisfactionSurvey.dto.ts
│ │ ├── tags.dto.ts
│ │ ├── teamAdmin.dto.ts
│ │ └── user.dto.ts UserType / ProfileType จาก JWT payload
│ ├── entities/ TypeORM entity บน connection 'main' (MSSQL `2way`)
│ │ ├── base.ts BaseEntity (id, createdAt/By, updatedAt/By)
│ │ ├── chatRoom.entity.ts
│ │ ├── chatRoomStatusHistory.entity.ts
│ │ ├── appointment.entity.ts
│ │ ├── notifications.entity.ts
│ │ ├── satisfactionSurvey.entity.ts
│ │ ├── tags.entity.ts
│ │ ├── teamAdmin.entity.ts
│ │ ├── teamMember.entity.ts
│ │ └── teamTags.entity.ts
│ ├── schemas/ Mongoose schema บน MongoDB `chatDB`
│ │ ├── conversation.schema.ts
│ │ └── chatNote.schema.ts
│ └── modules/
│ ├── chat/ ChatController + ChatService + (ChatGateway ลงทะเบียนที่ AppModule)
│ ├── cronJobs/ ScheduleModule + 2 daily cron
│ ├── appointment/ นัดหมาย + in-memory CronJob
│ ├── satisfactionSurvey/ แบบสำรวจ (มี @Public endpoints)
│ ├── tags/
│ └── teamAdmin/
├── test/ e2e scaffold เดียว (app.e2e-spec.ts) — ไม่มี unit test ต่อ module
├── Dockerfile node:18-alpine, yarn build, start:prod
├── .env / .env.prod / .env.uat credential commit เข้า git (ดู SEC-CHAT-04)
├── package.json name: "rest-structure"
└── nest-cli.jsonModule breakdown
| Module | ลงทะเบียนที่ | Controller path | หน้าที่หลัก |
|---|---|---|---|
AppModule (root) | — | AppController ที่ @Controller() | webhook LINE, createChatHistory, chatHistory, imageProfile |
ChatModule | app.module.ts:26 | @Controller('chat') | รายการห้อง, อัปเดตห้อง, ประวัติแชท+โน้ต, notification, สถานะ |
ChatGateway | providers ของ AppModule (app.module.ts:58) — ไม่ได้อยู่ใน ChatModule.providers | WS events chat, chatNote | realtime ข้อความ + push LINE + บันทึก Conversation/ChatNote |
CronJobsModule | app.module.ts:23 | @Controller() ไม่มี HTTP route | ลบ conversation/note เก่า 90 วัน; เตือนห้องค้าง 24 ชม. |
AppointmentModule | app.module.ts:24 | @Controller('appointment') | CRUD นัดหมาย + dropdown เวลาแจ้งเตือน |
SatisfactionSurveyModule | app.module.ts:25 | @Controller('satisfactionsurvey') | gen/save/report survey (หมายเหตุ: path สะกด satisfacedurvey) |
TagsModule | app.module.ts:27 | @Controller('tags') | CRUD tag + setting isShow |
TeamAdminModule | app.module.ts:28 | @Controller('teamAdmin') | ทีม super admin/staff, สมาชิก, care taker ตาม tag |
ข้อสังเกตการลงทะเบียน gateway — ChatGateway ถูกประกาศเป็น provider ของ AppModule โดยตรง (chat-center-api/src/app.module.ts (branch: prod):58) และ AppService inject ChatGateway (chat-center-api/src/app.service.ts (branch: prod):26) ส่วน ChatModule มีแค่ ChatController + ChatService (chat-center-api/src/modules/chat/chat.module.ts (branch: prod):20-21) — ไม่มี ChatGateway ใน providers ของ module ย่อย
HTTP endpoint inventory
Prefix จริง: /chat-center-api/api/v1 (จาก BASE_PATH + api/v1 ใน main.ts:25-27)
Root (AppController)
| Method | Path | Auth | Handler | หลักฐาน |
|---|---|---|---|---|
| POST | /webhook | @Public() | AppService.actions | app.controller.ts:18-22 |
| POST | /createChatHistory | JWT | AppService.createChatHistory | app.controller.ts:24-27 |
| GET | /chatHistory | JWT | AppService.chatHistory | app.controller.ts:29-32 |
| GET | /imageProfile | JWT | AppService.imageProfile | app.controller.ts:34-37 |
| POST | /imageProfileMulti | JWT | AppService.imageProfileMulti | app.controller.ts:39-42 |
| GET | /test | @Public() | AppService.test (ว่าง) | app.controller.ts:44-48 |
Chat (ChatController → chat/...)
| Method | Path | Handler | หลักฐาน |
|---|---|---|---|
| GET | /chat/rooms | ChatService.rooms | chat.controller.ts:18-21 |
| PUT | /chat/:id | ChatService.updateRooms | chat.controller.ts:23-26 |
| GET | /chat/chatRoomHistory | ChatService.chatRoomHistory | chat.controller.ts:28-31 |
| GET | /chat/listNoti | ChatService.listNoti | chat.controller.ts:33-36 |
| PUT | /chat/Noti/:id | ChatService.updateNoti | chat.controller.ts:38-41 |
| GET | /chat/roomStatusHistory | ChatService.roomStatusHistory | chat.controller.ts:43-46 |
| PUT | /chat/open/:id | ChatService.updateChat | chat.controller.ts:48-51 |
Appointment
| Method | Path | Auth | Handler | หลักฐาน |
|---|---|---|---|---|
| POST | /appointment | JWT | createAppointment | appointment.controller.ts:20-23 |
| PUT | /appointment/:id | JWT | updateAppointment | appointment.controller.ts:25-28 |
| GET | /appointment | JWT | listAppointment | appointment.controller.ts:30-33 |
| GET | /appointment/dropDown | @Public() | คืน appointmentDD จาก config | appointment.controller.ts:35-39 |
Satisfaction survey (path: satisfacedurvey)
| Method | Path | Auth | Handler | หลักฐาน |
|---|---|---|---|---|
| POST | /satisfacedurvey/public | @Public() | saveSurveyPublic | satisfactionsurvey.controller.ts:23-27 |
| POST | /satisfacedurvey/genSurvey | JWT | genSurvey | satisfedurvey.controller.ts:29-32 |
| GET | /satisfacedurvey/reportServey | JWT | reportServey | satisfedurvey.controller.ts:34-37 |
| POST | /satisfacedurvey/checkKey | @Public() | checkKey | satisfedurvey.controller.ts:39-43 |
Tags
| Method | Path | Handler | หลักฐาน |
|---|---|---|---|
| POST | /tags | createOne | tags.controller.ts:18-21 |
| PUT | /tags/:id | updateOne | tags.controller.ts:23-26 |
| GET | /tags/list | list | tags.controller.ts:28-31 |
| POST | /tags/setting | setting | tags.controller.ts:33-36 |
Team admin
| Method | Path | Handler | หลักฐาน |
|---|---|---|---|
| POST | /teamAdmin | createOne | teamadmin.controller.ts:17-20 |
| POST | /teamAdmin/onlySuperAdmin | onlySuperAdmin | teamadmin.controller.ts:22-25 |
| PUT | /teamAdmin/:id | updateOne | teamadmin.controller.ts:27-30 |
| GET | /teamAdmin/list | list | teamadmin.controller.ts:32-35 |
| GET | /teamAdmin/detail/:id | detail | teamadmin.controller.ts:37-40 |
| GET | /teamAdmin/listMember | listMember (query HRMI) | teamadmin.controller.ts:42-45 |
| POST | /teamAdmin/listCareTaker | listCareTaker | teamadmin.controller.ts:47-50 |
| GET | /teamAdmin/listCareTaker | getListCareTaker | teamadmin.controller.ts:52-55 |
WebSocket events
Gateway config (chat-center-api/src/modules/chat/chat.gateway.ts (branch: prod):19-26):
@WebSocketGateway({
cors: { origin: '*' },
path: '/chat-center-ws',
transports: ['websocket'],
maxHttpBufferSize: 1e8
})| Event (subscribe) | Method | ทิศทาง | หลักฐาน |
|---|---|---|---|
chat | handleMessage | client → server; server emit ตาม replyToken / lineToken | chat.gateway.ts:49-230 |
chatNote | saveChatNote | client → server; server emit ตาม roomEmpCode | chat.gateway.ts:232-299 |
Server ยัง emit ไปที่ room key ที่เป็น LINE user ID (message.replyToken หรือ message.lineToken) เพื่อให้หน้าเว็บที่ subscribe ชื่อเดียวกันรับข้อความ realtime
Cron jobs (ไม่มี HTTP)
ลงทะเบียนผ่าน @Cron บน CronJobsController (chat-center-api/src/modules/cronJobs/cronjobs.controller.ts (branch: prod):13-21) และ ScheduleModule.forRoot() ใน CronJobsModule (cronjobs.module.ts:16)
| Method | Expression | Service method |
|---|---|---|
clearTransaction | EVERY_DAY_AT_1AM | ลบ Conversation + ChatNote ที่ createdAt เก่ากว่า 90 วัน |
checkStatusAnd24H | EVERY_DAY_AT_MIDNIGHT | หาสถานะห้องล่าสุดค้าง ≥24 ชม. → สร้าง NotificationsEntity |
นอกจากนี้ AppointmentService.createAppointment สร้าง in-memory CronJob จาก package cron ต่อรายการนัดหมาย (appointment.service.ts:37-67) — ไม่ใช่ @nestjs/schedule และไม่ persist
ไฟล์ที่ควรรู้จักก่อนแก้โค้ด
| ไฟล์ | ทำไมสำคัญ |
|---|---|
src/app.module.ts:33-55 | จุดเดียวที่ประกาศ connection ทั้งสาม — module ที่ inject @InjectEntityManager('center') พึ่ง root นี้ |
config/configuration.ts:7-37 | db.synchronize: true และ mapping DB_HRMI_* → connection center |
src/app.service.ts:29-186 | LINE webhook state machine ทั้งก้อน (สร้างห้อง, ติดต่อแอดมิน, เลือก tag, แชทเปิดอยู่) |
src/modules/chat/chat.gateway.ts:49-230 | push LINE + เขียนไฟล์ + บันทึก Mongo — ไม่มี auth |
src/modules/chat/chat.service.ts:103-157 | raw SQL string interpolation จาก query param (SEC-CHAT-01) |
src/app.service.ts_bk | backup ประวัติศาสตร์ — ห้ามแก้/ลบโดยไม่ตรวจสอบ |
Dependencies ที่ import แต่ไม่ได้ใช้ใน flow หลัก
@nestjsx/crud/@nestjsx/crud-typeorm— อยู่ในpackage.jsonและTeamAdminServiceเคย importTypeOrmCrudServiceแต่ class ไม่ extends มันแล้ว (teamadmin.service.ts:8ยัง import อยู่) — scaffolding ค้างsrc/dto/cats.dto.ts— ชื่อจาก NestJS tutorial ไม่ถูก wire เข้า controllerCENTRALIZE_SERVERใน.env— ไม่พบการอ้างอิงในsrc/หรือlibs/(vestigial env)