Skip to content

1 · Repository map — chat-center-api

โครงสร้างไฟล์ทั้งหมดอ้างอิงจาก branch prod (~68 ไฟล์ tracked ไม่รวม node_modules, .git, dist)


Tree

text
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.json

Module breakdown

Moduleลงทะเบียนที่Controller pathหน้าที่หลัก
AppModule (root)AppController ที่ @Controller()webhook LINE, createChatHistory, chatHistory, imageProfile
ChatModuleapp.module.ts:26@Controller('chat')รายการห้อง, อัปเดตห้อง, ประวัติแชท+โน้ต, notification, สถานะ
ChatGatewayproviders ของ AppModule (app.module.ts:58) — ไม่ได้อยู่ใน ChatModule.providersWS events chat, chatNoterealtime ข้อความ + push LINE + บันทึก Conversation/ChatNote
CronJobsModuleapp.module.ts:23@Controller() ไม่มี HTTP routeลบ conversation/note เก่า 90 วัน; เตือนห้องค้าง 24 ชม.
AppointmentModuleapp.module.ts:24@Controller('appointment')CRUD นัดหมาย + dropdown เวลาแจ้งเตือน
SatisfactionSurveyModuleapp.module.ts:25@Controller('satisfactionsurvey')gen/save/report survey (หมายเหตุ: path สะกด satisfacedurvey)
TagsModuleapp.module.ts:27@Controller('tags')CRUD tag + setting isShow
TeamAdminModuleapp.module.ts:28@Controller('teamAdmin')ทีม super admin/staff, สมาชิก, care taker ตาม tag

ข้อสังเกตการลงทะเบียน gatewayChatGateway ถูกประกาศเป็น 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)

MethodPathAuthHandlerหลักฐาน
POST/webhook@Public()AppService.actionsapp.controller.ts:18-22
POST/createChatHistoryJWTAppService.createChatHistoryapp.controller.ts:24-27
GET/chatHistoryJWTAppService.chatHistoryapp.controller.ts:29-32
GET/imageProfileJWTAppService.imageProfileapp.controller.ts:34-37
POST/imageProfileMultiJWTAppService.imageProfileMultiapp.controller.ts:39-42
GET/test@Public()AppService.test (ว่าง)app.controller.ts:44-48

Chat (ChatControllerchat/...)

MethodPathHandlerหลักฐาน
GET/chat/roomsChatService.roomschat.controller.ts:18-21
PUT/chat/:idChatService.updateRoomschat.controller.ts:23-26
GET/chat/chatRoomHistoryChatService.chatRoomHistorychat.controller.ts:28-31
GET/chat/listNotiChatService.listNotichat.controller.ts:33-36
PUT/chat/Noti/:idChatService.updateNotichat.controller.ts:38-41
GET/chat/roomStatusHistoryChatService.roomStatusHistorychat.controller.ts:43-46
PUT/chat/open/:idChatService.updateChatchat.controller.ts:48-51

Appointment

MethodPathAuthHandlerหลักฐาน
POST/appointmentJWTcreateAppointmentappointment.controller.ts:20-23
PUT/appointment/:idJWTupdateAppointmentappointment.controller.ts:25-28
GET/appointmentJWTlistAppointmentappointment.controller.ts:30-33
GET/appointment/dropDown@Public()คืน appointmentDD จาก configappointment.controller.ts:35-39

Satisfaction survey (path: satisfacedurvey)

MethodPathAuthHandlerหลักฐาน
POST/satisfacedurvey/public@Public()saveSurveyPublicsatisfactionsurvey.controller.ts:23-27
POST/satisfacedurvey/genSurveyJWTgenSurveysatisfedurvey.controller.ts:29-32
GET/satisfacedurvey/reportServeyJWTreportServeysatisfedurvey.controller.ts:34-37
POST/satisfacedurvey/checkKey@Public()checkKeysatisfedurvey.controller.ts:39-43

Tags

MethodPathHandlerหลักฐาน
POST/tagscreateOnetags.controller.ts:18-21
PUT/tags/:idupdateOnetags.controller.ts:23-26
GET/tags/listlisttags.controller.ts:28-31
POST/tags/settingsettingtags.controller.ts:33-36

Team admin

MethodPathHandlerหลักฐาน
POST/teamAdmincreateOneteamadmin.controller.ts:17-20
POST/teamAdmin/onlySuperAdminonlySuperAdminteamadmin.controller.ts:22-25
PUT/teamAdmin/:idupdateOneteamadmin.controller.ts:27-30
GET/teamAdmin/listlistteamadmin.controller.ts:32-35
GET/teamAdmin/detail/:iddetailteamadmin.controller.ts:37-40
GET/teamAdmin/listMemberlistMember (query HRMI)teamadmin.controller.ts:42-45
POST/teamAdmin/listCareTakerlistCareTakerteamadmin.controller.ts:47-50
GET/teamAdmin/listCareTakergetListCareTakerteamadmin.controller.ts:52-55

WebSocket events

Gateway config (chat-center-api/src/modules/chat/chat.gateway.ts (branch: prod):19-26):

typescript
@WebSocketGateway({
  cors: { origin: '*' },
  path: '/chat-center-ws',
  transports: ['websocket'],
  maxHttpBufferSize: 1e8
})
Event (subscribe)Methodทิศทางหลักฐาน
chathandleMessageclient → server; server emit ตาม replyToken / lineTokenchat.gateway.ts:49-230
chatNotesaveChatNoteclient → server; server emit ตาม roomEmpCodechat.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)

MethodExpressionService method
clearTransactionEVERY_DAY_AT_1AMลบ Conversation + ChatNote ที่ createdAt เก่ากว่า 90 วัน
checkStatusAnd24HEVERY_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-37db.synchronize: true และ mapping DB_HRMI_* → connection center
src/app.service.ts:29-186LINE webhook state machine ทั้งก้อน (สร้างห้อง, ติดต่อแอดมิน, เลือก tag, แชทเปิดอยู่)
src/modules/chat/chat.gateway.ts:49-230push LINE + เขียนไฟล์ + บันทึก Mongo — ไม่มี auth
src/modules/chat/chat.service.ts:103-157raw SQL string interpolation จาก query param (SEC-CHAT-01)
src/app.service.ts_bkbackup ประวัติศาสตร์ — ห้ามแก้/ลบโดยไม่ตรวจสอบ

Dependencies ที่ import แต่ไม่ได้ใช้ใน flow หลัก

  • @nestjsx/crud / @nestjsx/crud-typeorm — อยู่ใน package.json และ TeamAdminService เคย import TypeOrmCrudService แต่ class ไม่ extends มันแล้ว (teamadmin.service.ts:8 ยัง import อยู่) — scaffolding ค้าง
  • src/dto/cats.dto.ts — ชื่อจาก NestJS tutorial ไม่ถูก wire เข้า controller
  • CENTRALIZE_SERVER ใน .env — ไม่พบการอ้างอิงใน src/ หรือ libs/ (vestigial env)