2way-batch — Persistence & Integrations
บทนี้อธิบายชั้นข้อมูลและการเชื่อมต่อภายนอกของ 2way-batch (branch: master) — MSSQL ผ่าน TypeORM, ตารางที่ cron อ้างถึง, และการยิง LINE ผ่าน HTTP hardcode
ดู wiring ที่ 01-repository-map และ cron flow ที่ 02-core-pipeline
Database connection
Connection เดียวชื่อ 'main':
| รายการ | ค่า | อ้างอิง |
|---|---|---|
| Driver | mssql | app.module.ts:21 |
| Credentials | DB_HOST / DB_PORT / DB_USER / DB_PASSWORD / DB_NAME | configuration.ts:4-10 → app.module.ts:22-26 |
| synchronize | false | app.module.ts:27 |
| logging | ['error','query'] | app.module.ts:28 |
| TLS encrypt | false | app.module.ts:32 |
| trustServerCertificate | true | app.module.ts:31 |
ไม่มีหลักฐานใน git ว่า DB_NAME production คืออะไร — ไม่มี .env.example
TypeORM โหลด entity ทุกไฟล์ใต้ src/**/*.entity{.ts,.js} (app.module.ts:29) — รวม complaint/announce entities ที่ cron ไม่แตะ
Entity inventory (25 ไฟล์)
| ไฟล์ | ตาราง / หมายเหตุ | ใช้โดย cron path? |
|---|---|---|
t_content.entity.ts | t_content | ใช่ (query หลัก) |
t_content_assign.entity.ts | t_content_assign | ใช่ |
t_content_status.entity.ts | t_content_status | ใช่ |
t_content_flow_history.entity.ts | t_content_flow_history | ใช่ |
m_content_type.entity.ts | m_content_type | ใช่ |
m_content_flow.entity.ts | m_content_flow | ใช่ |
m_status.entity.ts | m_status | ใช่ (+ initializer) |
m_config.entity.ts | m_config | ใช่ (list.empCode.hrAdmin) |
userLine.entity.ts | userLine | ใช่ (LINE id lookup) |
m_error_message.entity.ts | error messages | initializer ของ SurveysUtil |
t_send_notify.entity.ts | notify | ไม่ใน cron methods ปัจจุบัน |
t_content_manager.entity.ts / m_user_manager.entity.ts / m_topic.entity.ts / m_role.entity.ts | manager/topic/role | ไม่ใน cron |
*_complaint*.entity.ts (หลายไฟล์) | complaint domain | ไม่ — module ไม่ mount |
base.ts | BaseEntity | ใช้ร่วม |
TContent (ตัวอย่างคอลัมน์หลัก)
topic_name, offer_date, link_url, tot_assign, relations ไป type/assign/status/flow (t_content.entity.ts:10-50)
UserLine
ตาราง userLine — คอลัมน์ userID, lineID, richMenuID, isActive (userLine.entity.ts:3-24)
Lookup ใน cron: where userID = :empCode (surveys.repository.ts:591-593)
MConfig
ตาราง m_config — module, name, value (m_config.entity.ts:4-16)
HR admin list: findOne where name = 'list.empCode.hrAdmin' (surveys.repository.ts:587-589, surveys.constant.ts:10) — ค่าเป็น string คั่นด้วย comma แล้ว split ใน cron (survey.task.service.ts:143)
Survey flow short names (constants + config maps)
จาก SurveysConstant (surveys.constant.ts:2-10):
| Constant | ค่า | ความหมายในโค้ด |
|---|---|---|
CONTENT_TYPE | survey | filter m_content_type.name_en |
SHORT_NAME_IN_SCOPE | ACS | create in-scope (stub method ชื่ออ้าง) |
SHORT_NAME_OUT_SCOPE | ACD | used by fetchCreateOutScope |
SHORT_NAME_HR_ADMIN_APPROVED | HAP | used by fetchApproval |
CREATE_CODE | S00 | status code filter out-scope path |
configuration.ts มี map contentStatus.survey.* ซ้ำความหมายเดียวกันแบบ static (configuration.ts:19-40) — cron อ่านจาก SurveysConstant + DB maps ของ SurveysUtil เป็นหลัก ไม่ได้อ่าน object นี้ตรง ๆ ใน survey.task.service.ts
SurveysUtil initializer — in-memory maps
เมื่อ SurveysService ถูกสร้าง (ผ่าน SurveyTaskModule providers):
constructor(..., private readonly dao: SurveysRepository) {
SurveysUtil.initializer(entityManager);
}แหล่ง: surveys.service.ts:21-27
initializer โหลด (surveys.util.ts:25-62):
MContentFlowshort_nameACS/ACD- ทุก
MStatusที่group_name = 'survey'และis_active = 1→ map ตามmappingและcode - ทุก
MErrorMessageที่ active - ทุก
MContentFlowที่group_name = 'Survey'
getActionCodeByName คืน .code จาก map (surveys.util.ts:125-128) — ใช้ใน dead cron methods และใน updateSurveys
ถ้า DB ยังไม่พร้อมตอน boot — map ว่าง → การเปิด fetchApproval จะ fail เมื่อเรียก getActionCodeByName
Repository helpers ที่ cron พึ่งพา
| Method | SQL / behavior | อ้างอิง |
|---|---|---|
findAllContentAssignBySurveyId | TContentAssign where content_id | surveys.repository.ts:663-665 |
findLineIDAsEmpCode | UserLine where userID | surveys.repository.ts:591-593 |
findEmployeeCodeOfHrAdmin | MConfig where name key | surveys.repository.ts:587-589 |
updateSurvey (ผ่าน service) | insert flow history + update status | เรียกจาก updateSurveys |
หมายเหตุ: findByConditionV2 ใน repository มี raw SQL string ที่ interpolate dto.empCode (surveys.repository.ts:70) — ไม่ถูกเรียกจาก cron และ controller surveys ไม่ได้ mount — แต่เป็น debt ถ้า uncomment SurveysModule (QUAL-2WAYBATCH-04)
Outbound integration — LINE via hardcoded HTTP
Batch ไม่ได้เรียก https://api.line.me โดยตรงใน LineService
SurveyTaskService
→ LineService.pushMassage
→ POST http://10.188.128.126:9340/2way-batch/api/v1/lineแหล่ง: line.service.ts:17-41
| รายการ | ค่าที่พิสูจน์ได้ |
|---|---|
| Host | 10.188.128.126 (hardcode) |
| Port | 9340 |
| Path | /2way-batch/api/v1/line |
| Body | { replyToken, messages } |
| Success status expected | 201 |
| Env override | ไม่มี |
เปรียบเทียบกับ 2way-line-service ที่เรียก LINE API จริงด้วย LINE_AUTH_TOKEN (ดูเอกสาร volume ของ line-service) — คนละ hop
config.lineToken / LINE_AUTH_TOKEN ใน configuration.ts:11 ไม่ได้ถูกอ่าน ใน 2way-batch/src/modules/line/line.service.ts
Inbound — nginx path vs Nest prefix
| Layer | Path | อ้างอิง |
|---|---|---|
| nginx location | /2way-batch/ → strip ไป :9340/ | nginx/conf.d/default.conf:3-8 |
| Nest global prefix | ${BASE_PATH}api/v1 | main.ts:23-25 |
| Hardcoded push URL | รวม /2way-batch/api/v1/line | line.service.ts:26 |
ถ้า BASE_PATH ที่ process จริงว่าง หรือเป็นค่าอื่น — path ที่ hardcode อาจไม่ตรงกับ route จริงของตัวเองหรือของ gateway อื่น เป็น UNVERIFIED ที่ runtime
สิ่งที่ไม่มีใน repo นี้
| รายการ | สถานะ |
|---|---|
.env.example | ไม่มี |
docker-compose.yml | ไม่มี |
การเรียก CENTRALIZE_SERVER / cu-central / vtrc-api | ไม่พบในซอร์ส cron/line ของ batch |
| Direct LINE Messaging API | ไม่มีใน LineService ของ batch |
สิ่งที่อยู่นอกขอบเขตบทนี้
- Cron stub vs dead methods → 02-core-pipeline
- Debt register → 04-scorecard