Skip to main content

นิยามคุณสมบัติลูกค้า (Attribute Master)

ภาพรวม

Attribute Master คือจุดที่นิยาม custom attribute ของ LINE user แยกตาม LINE OA เช่น "ระดับสมาชิก" "วันเกิด" หรือ "สาขาที่ใช้บริการ" ทำหน้าที่เป็น schema ที่บอกระบบว่าแต่ละ attribute ชื่ออะไร เก็บข้อมูลชนิดใด นำไปใช้กรอง audience ได้หรือไม่ และแสดงในรายงานได้หรือไม่

attribute ที่นิยามไว้ที่นี่จะถูกนำไปใช้ต่อในหลายจุดทั่วระบบ ได้แก่ field ปลายทางของ Import Mapping, merge tag ของ Template Message, เงื่อนไขของ Audience Filter และคอลัมน์ของรายงานเพื่อนทั้งหมด

ฟีเจอร์ที่โดดเด่นคือ การนำเข้าจาก JSON schema ผู้ใช้วาง JSON ตัวอย่างเข้ามา แล้วระบบ parse ออกมาเป็นชุด attribute ให้ทันที ไม่ต้องนิยามทีละตัว

Business Flow

  1. ดูรายการGET /api/attribute-master คืน attribute ทั้งหมดแบบแบ่งหน้า กรองได้ตาม AttributeMasterQueryParamDto
  2. ดึงเฉพาะที่ใช้กรองได้GET /api/attribute-master/filterable คืนเฉพาะ attribute ที่ใช้เป็นเงื่อนไขกรอง audience ได้ ใช้โดยหน้า Audience Filter
  3. ดึงเฉพาะที่แสดงในรายงานได้GET /api/attribute-master/reportable คืนเฉพาะ attribute ที่แสดงเป็นคอลัมน์รายงานได้ ใช้โดยหน้ารายงาน
  4. สร้างใหม่POST /api/attribute-master สร้าง attribute ใหม่ โดยตรวจโควตา maxAttributes ตามแพ็กเกจก่อนเสมอ
  5. แปลง JSON เป็น attributePOST /api/attribute-master/parse-json รับ body ที่มี field json และ rootPath (ไม่บังคับ) แล้วคืน preview ว่าจะได้ attribute ใดบ้าง โดยยังไม่บันทึกลงฐานข้อมูล
  6. ยืนยันนำเข้าPOST /api/attribute-master/import-json สร้าง attribute จาก schema ที่ preview ไว้ทั้งชุดในครั้งเดียว
  7. ดูรายละเอียดและแก้ไขGET /api/attribute-master/:id และ PUT /api/attribute-master/:id
  8. ตรวจก่อนลบ — ควรเรียก GET /api/attribute-master/:id/check-attribute-usage?attributeKey=... ก่อนเสมอ เพื่อดูว่า attribute นี้ถูกใช้อยู่ที่ใดบ้าง ทั้งใน audience, รายงาน, ฟอร์ม และ import mapping จะได้ไม่ลบทิ้งจนทำให้ส่วนอื่นเสียหาย
  9. ลบDELETE /api/attribute-master/:id

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

โค้ดอยู่ที่ internal/modules/attributemaster/ ประกอบด้วย controller.go, service.go, parser.go, ordered.go, time.go, dto.go และ module.go

  • parser.go ทำหน้าที่ parse JSON schema เป็นชุด attribute
  • ordered.go รักษาลำดับ key เพื่อให้ผลลัพธ์ JSON ตรงกับระบบเดิม

Route ทั้งหมดลงทะเบียนบน authed.Group("/attribute-master") และครอบด้วย modulegate.ModuleGate(d, "attribute-master")

MethodRouteHandlerPolicy
GET/api/attribute-mastersvc.getListHandlerreadAll attribute-master
GET/api/attribute-master/filterablesvc.getFilterableHandlerread attribute-master
GET/api/attribute-master/reportablesvc.getReportableHandlerread attribute-master
GET/api/attribute-master/:id/check-attribute-usagesvc.checkAttributeUsageHandlerread attribute-master
GET/api/attribute-master/:idsvc.getByIDHandlerread attribute-master
POST/api/attribute-mastersvc.createHandlercreate attribute-master
POST/api/attribute-master/import-jsonsvc.importJsonHandlercreate attribute-master
POST/api/attribute-master/parse-jsonsvc.parseJsonHandlerread attribute-master
PUT/api/attribute-master/:idsvc.updateHandlerupdate attribute-master
DELETE/api/attribute-master/:idsvc.deleteHandlerdelete attribute-master

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

  • Permission — ต้องผ่าน ModuleGate("attribute-master") และมี PolicyModuleAttributeMaster เป็น metadata
  • ตารางที่เกี่ยวข้องattribute_master, line_user (ในส่วน custom attribute), audience, form_builder, import_mapping_job และ template_message
  • Cross-module — เรียกโมดูล Plan Limits เพื่อตรวจโควตา maxAttributes
  • Error handling — ใช้ throwInstanceofError ซึ่งปล่อย error 401, 404, 400 และ 409 ผ่านไปตามเดิม ส่วน error อื่นถูกแปลงเป็น 500 พร้อมรหัส APP_000
  • โมดูลที่เกี่ยวข้อง — System Attribute (attribute ระดับระบบ), LINE User Management, Audience Filter, Template Message, All Friend Listing และ Import Mapping