Skip to main content

แอปกระดานประกาศ

ภาพรวม

แอป bulletin คือกระดานประกาศและชุมชนย่อยในหน้า LINE ของแบรนด์ รองรับการโพสต์ประกาศ ให้ผู้ใช้คอมเมนต์และแสดงรีแอ็กชัน พร้อมระบบรายงานเนื้อหาไม่เหมาะสม การบล็อกผู้ใช้ที่ก่อกวน และ audit log ที่บันทึกทุกการกระทำของทีมงาน

โมดูลนี้เป็นโมดูลใหม่ที่เขียนขึ้นตาม design spec ไม่ได้ port มาจาก NestJS และมี กติกาความปลอดภัยของข้อมูล 6 ข้อ ที่โค้ดยึดอย่างเคร่งครัด ซึ่งเป็นสิ่งที่ต้องเข้าใจก่อนแก้ไขโค้ดส่วนนี้

Business Flow

กติกา 6 ข้อที่บังคับทั่วทั้งโมดูล

กติกาเหล่านี้ระบุไว้ใน doc comment ของไฟล์ service ทุกไฟล์

  1. ทุก query ต้องผ่าน repo.scoped หรือ scopedTx ซึ่งบังคับทั้ง organization_id และ line_oa_id
  2. id ที่ผู้เรียกส่งมาต้องถูกตรวจ ความเป็นเจ้าของ ไม่ใช่เพียงตรวจว่ามีอยู่จริง และหากไม่พบต้องตอบ 404 ไม่ใช่ 403 เพราะ 403 เท่ากับยืนยันว่าแถวนั้นมีอยู่ในองค์กรอื่น
  3. ทุก mutation ต้องอยู่ใน transaction เดียวกับการเขียนแถว audit log
  4. ห้ามเขียนคอลัมน์ counter ได้แก่ comment_count, reaction_count และ report_count เพราะ database trigger เป็นเจ้าของค่าเหล่านี้
  5. ห้าม mutate post และ comment ใน transaction เดียวกัน เพราะ trigger จับ advisory lock แยกตาม target (4201 สำหรับ post และ 4202 สำหรับ comment) การสลับลำดับจะทำให้เกิด deadlock
  6. การลบต้องเป็นการตั้ง status = 'deleted' พร้อม stamp deleted_date เสมอ ไม่มีการ hard delete

ตั้งค่าและหมวดหมู่

  1. GET /api/apps/bulletin/settings และ PUT /api/apps/bulletin/settings ตั้งค่ากระดาน เช่น เปิดคอมเมนต์หรือไม่ และต้องอนุมัติก่อนเผยแพร่หรือไม่
  2. GET /api/apps/bulletin/pending-count คืนจำนวนรายการที่รออนุมัติ ใช้แสดง badge บนเมนู
  3. หมวดหมู่จัดการผ่าน GET /api/apps/bulletin/categories, POST /api/apps/bulletin/categories, PUT /api/apps/bulletin/categories/:id และ DELETE /api/apps/bulletin/categories/:id

โพสต์และคอมเมนต์

  1. GET /api/apps/bulletin/posts และ POST /api/apps/bulletin/posts ดูรายการและสร้างโพสต์ ส่วน GET /api/apps/bulletin/posts/:id และ PUT /api/apps/bulletin/posts/:id ดูรายละเอียดและแก้ไข
  2. PUT /api/apps/bulletin/posts/:id/status เปลี่ยนสถานะโพสต์ เช่น เผยแพร่ ซ่อน หรือลบ
  3. PUT /api/apps/bulletin/posts/:id/pin ปักหมุดโพสต์
  4. PUT /api/apps/bulletin/posts/:id/comments-setting เปิดหรือปิดคอมเมนต์เฉพาะโพสต์นั้น
  5. GET /api/apps/bulletin/posts/:id/comments ดูคอมเมนต์ของโพสต์
  6. GET /api/apps/bulletin/comments ดูคิวคอมเมนต์ทั้งกระดาน และ PUT /api/apps/bulletin/comments/:id/status อนุมัติ ซ่อน หรือลบคอมเมนต์

ความปลอดภัยและการกำกับดูแล

  1. GET /api/apps/bulletin/reports ดูคิวรายงานเนื้อหา และ PUT /api/apps/bulletin/reports/:id/status จัดการหรือปัดตกรายงาน
    • แถว report จะไม่ถูกลบ เพราะ index idx_bul_report_once ใช้ป้องกันไม่ให้ผู้ใช้คนเดิมรายงานซ้ำ
  2. GET /api/apps/bulletin/blocks และ POST /api/apps/bulletin/blocks ดูและเพิ่มการบล็อกผู้ใช้ ส่วน DELETE /api/apps/bulletin/blocks/:id เป็นการ revoke ไม่ใช่การลบแถวจริง
  3. GET /api/apps/bulletin/audit-log ดูประวัติการกระทำของทีมงาน เป็นแบบอ่านอย่างเดียว

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

โค้ดอยู่ที่ internal/modules/bulletin/

ไฟล์บทบาท
controller.goลงทะเบียน route โดยทุก route มี apps.AppEnabledGuard(d)
scope.gorepo.scoped และ scopedTx ซึ่งบังคับ tenant scope
service_board.gosettings, categories และ pending-count
service_moderation.goposts, comments, การเปลี่ยนสถานะ และการปักหมุด
service_safety.goreports, blocks และการอ่าน audit log
audit.goเขียน audit log
dto.goDTO

endpoint ทั้งหมดอยู่ใต้ /api/apps/bulletin/ บน group authed โดยลำดับ middleware คือ appEnabled แล้วตามด้วย policy และ handler

กลุ่มRoutesHandler
settingsGET /settings, PUT /settings, GET /pending-countct.getSettings, ct.updateSettings, ct.pendingCount
categoriesGET /categories, POST /categories, PUT /categories/:id, DELETE /categories/:idct.listCategories, ct.createCategory, ct.updateCategory, ct.deleteCategory
postsGET /posts, POST /posts, GET /posts/:id, PUT /posts/:id, PUT /posts/:id/status, PUT /posts/:id/pin, PUT /posts/:id/comments-setting, GET /posts/:id/commentsct.listPosts, ct.createPost, ct.getPost, ct.updatePost, ct.setPostStatus, ct.setPostPinned, ct.setPostCommentsSetting, ct.listPostComments
commentsGET /comments, PUT /comments/:id/statusct.listComments, ct.setCommentStatus
reportsGET /reports, PUT /reports/:id/statusct.listReports, ct.setReportStatus
blocksGET /blocks, POST /blocks, DELETE /blocks/:idct.listBlocks, ct.createBlock, ct.revokeBlock
auditGET /audit-logct.listAuditLog

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

  • สิทธิ์การเข้าถึง — ทุก route ผ่าน apps.AppEnabledGuard(d) แอปต้องถูกเปิดให้องค์กรก่อน (ดู ระบบ Apps เสริม) ส่วน policy metadata ใช้ PolicyModuleLineOa ในระดับ read, readAll, create, update และ delete
  • ตารางที่เกี่ยวข้อง (schema bulletin) — bulletin_board, bulletin_post, bulletin_engagement, bulletin_audit หรือ audit_log รวมถึงตาราง comment, report และ block
  • Database trigger — เป็นเจ้าของคอลัมน์ counter และใช้ advisory lock หมายเลข 4201 กับ 4202
  • CLSorganizationId และ lineOaId ส่งผ่าน repo.scoped
  • โมดูลที่เกี่ยวข้องระบบ Apps เสริม, แอปสะสมแต้ม และ แอปจองคิว/นัดหมาย