Skip to main content

จัดการ LINE Official Account (LINE OA)

ภาพรวม​

LINE OA เป็นหน่วยของ tenant ในระบบนี้ ข้อมูลแทบทุกตารางถูก scope ด้วย line_oa_id โมดูลนี้จึงเป็นแกนกลางของทั้งระบบ ครอบคลุมการสร้าง แก้ไข และลบ OA, การเก็บ channel credential (channel id, channel secret, access token), การจัดการ webhook URL, การตั้งค่า audience auto-refresh, การตั้งค่า GA tracking, การตั้งค่าการรับส่งข้อความ ตลอดจนการเชื่อมต่อกับระบบ mbox (live chat) ภายนอก

route ส่วนใหญ่อยู่บน group public คู่กับ JwtLoginAuth เพราะต้องเรียกได้ก่อนที่ token จะมี lineOaId เนื่องจากหน้าเลือก OA หลัง login จำเป็นต้องดึงรายการ OA มาแสดงก่อน

Business Flow​

การเชื่อม OA เข้าระบบ

  1. ผู้ใช้กรอก channel id, channel secret และ access token แล้วเรียก POST /api/line-oa/get-oa-info ระบบจะยิงถาม LINE API เพื่อยืนยันว่า credential ใช้งานได้ พร้อมดึงชื่อและรูปของ OA มาแสดง
  2. POST /api/line-oa สร้างแถวใน line_oa โดยตรวจโควตา maxChannels จากกลไก plan limits พร้อม generate ค่า webhook_id และ line_oa_hash
  3. ผู้ใช้คัดลอก webhook URL ที่ระบบ generate ไปตั้งค่าใน LINE Developer Console โดย GET /api/line-oa/webhook/:webhookId ใช้ค้นย้อนกลับจาก webhook id ไปยัง OA
  4. PUT /api/line-oa/:id/regenerate-webhook ออก webhook id ใหม่ในกรณีที่ค่าเดิมรั่วไหล

การใช้งานประจำวัน

  1. GET /api/line-oa แสดงรายการแบบแบ่งหน้า โดยคืนโครงสร้าง {data, total, totalOverAll}
  2. GET /api/line-oa/find-all-object ซึ่งไม่มี guard เลย คืนรูปแบบ dropdown, dropdown-group หรือ object ตามค่าที่ระบุใน query ?format=
  3. PUT /api/line-oa/:id แก้ไขข้อมูล และ PUT /api/line-oa/:id/status เปิด/ปิดการใช้งาน
  4. DELETE /api/line-oa/:id ลบแบบ soft delete ส่วน DELETE /api/line-oa/:id/hard ลบข้อมูลจริง ต้องเป็น super admin และส่ง ?confirm= กำกับ

การตั้งค่าเฉพาะทาง

  1. audience auto-refresh ผ่าน GET /api/line-oa/:id/audience-refresh-settings และ PUT /api/line-oa/:id/audience-refresh-settings กำหนดว่า cron ที่ refresh audience ของ OA นี้จะทำงานอย่างไร
  2. GA tracking ผ่าน GET /api/line-oa/:id/ga-tracking-settings และ PUT /api/line-oa/:id/ga-tracking-settings เก็บ measurement id และ api secret สำหรับส่ง event ไป Google Analytics
  3. message handling ผ่าน PUT /api/line-oa/:id/message-handling เลือกว่าข้อความขาเข้า จะให้ auto-response, workflow หรือ mbox ตัวใดเป็นผู้รับ
  4. mbox ผ่าน POST /api/line-oa/:id/mbox/test-connection สำหรับทดสอบ baseUrl, apiToken และ accountId รวมถึง GET /api/line-oa/:id/mbox/teams สำหรับดึงรายชื่อทีมมาผูก

สิทธิ์เข้าถึง OA เฉพาะ super admin

  1. GET /api/line-oa/:id/user-access และ PUT /api/line-oa/:id/user-access กำหนดว่าผู้ใช้คนใดเข้าถึง OA นี้ได้ โดยเขียนลง user_line_oa route คู่นี้อยู่บน group authed ซึ่งต่างจาก route อื่นที่ port มา เพราะต้องให้ CLS ถูกเติมค่าก่อนที่ SuperAdmin() จะอ่าน

ไฟล์และฟังก์ชันหลัก​

โค้ดอยู่ที่ internal/modules/lineoa/ ประกอบด้วย controller.go, service.go, dto.go, responses.go, js.go และ user_access.go

MethodRouteHandlerGuard / Policy
GET/api/line-oact.findAllJwtLogin + readAll line-oa
GET/api/line-oa/find-all-objectct.findAllObjectไม่มี guard
GET/api/line-oa/:idct.findByIDJwtLogin + read
GET/api/line-oa/webhook/:webhookIdct.findByWebhookIDJwtLogin + read
POST/api/line-oact.createJwtLogin + create
POST/api/line-oa/get-oa-infoct.getOaInfo—
POST/api/line-oa/validate-loginct.validateLogin—
PUT/api/line-oa/:idct.updateJwtLogin + update
PUT/api/line-oa/:id/statusct.updateStatusJwtLogin + update
PUT/api/line-oa/:id/regenerate-webhookct.regenerateWebhookJwtLogin + update
GET / PUT/api/line-oa/:id/audience-refresh-settingsct.getAudienceRefreshSettings / ct.updateAudienceRefreshSettingsJwtLogin + read/update
GET / PUT/api/line-oa/:id/ga-tracking-settingsct.getGaTrackingSettings / ct.updateGaTrackingSettingsJwtLogin + read/update
PUT/api/line-oa/:id/message-handlingct.updateMessageHandlingJwtLogin + update
POST/api/line-oa/:id/mbox/test-connectionct.testMboxConnectionJwtLogin + update
GET/api/line-oa/:id/mbox/teamsct.getMboxTeamsJwtLogin + read
DELETE/api/line-oa/:idct.deleteJwtLogin + delete
DELETE/api/line-oa/:id/hardct.hardDeleteauth.SuperAdmin()
GET / PUT/api/line-oa/:id/user-accessct.getUserAccess / ct.updateUserAccessauthed + auth.SuperAdmin()

จุดเชื่อมต่อกับ Service อื่น​

  • Permission — policy metadata PolicyModuleLineOa ยังไม่บังคับใช้ สิ่งที่บังคับจริงคือ auth.SuperAdmin() บน hard delete และ user-access
  • ตารางที่เกี่ยวข้อง — line_oa, line_oa_app, user_line_oa, organization
  • Redis — cache ข้อมูล OA และ LINE access token
  • RabbitMQ — publish ลงคิว line_sync_follower_user เมื่อสั่ง sync follower
  • External — LINE Messaging API ผ่าน internal/externals/lineapi และระบบ mbox ผ่าน HTTP
  • Cross-module — planlimits สำหรับโควตา maxChannels และ auth.GenerateLineOaHash
  • แทบทุก feature ในระบบพึ่งพา lineOaId ที่มาจากโมดูลนี้