Skip to main content

เพื่อน LINE (LINE User)

ภาพรวม

line_user คือ ผู้ใช้ปลายทางที่เป็นเพื่อนของ LINE OA ซึ่งเป็นคนละกลุ่มกับผู้ใช้ CMS ตารางนี้เป็นตารางที่ใหญ่ที่สุดและถูกอ่านเขียนบ่อยที่สุดในระบบ ข้อมูลเข้ามาจาก 3 ทาง ได้แก่ webhook (follow event), การ sync รายชื่อ follower จาก LINE และการ import CSV

นอกจากโปรไฟล์พื้นฐานจาก LINE แล้ว ตารางนี้ยังเก็บ custom attribute ซึ่งเป็นข้อมูลลูกค้าแบบยืดหยุ่นในรูป JSONB, สถานะการติดตาม, ข้อมูลกิจกรรมล่าสุด และรายการ audience ที่ผู้ใช้คนนี้เป็นสมาชิกอยู่

โครงสร้างข้อมูลหลัก

line_user (model LineUser)

ใช้ UUID เป็น primary key (สร้างด้วย gen_random_uuid())

ตัวตนจาก LINEuser_id (LINE userId ตัวจริง), display_name, picture_url, user_info JSONB และ language

ข้อมูลที่กรอกเพิ่มภายหลังfirstname, lastname, email, mobile_no

สถานะการติดตามfollow (enum CommonConfirm คือ yes/no), registered_date, unfollow_date และ user_type (enum LineUserType: guest หรือ member โดย member หมายถึงลงทะเบียนแล้ว)

กิจกรรมล่าสุดlast_activity_type (ActivityType), last_activity_period (ActivityPeriod: morning / afternoon / evening / night), last_activity_date และ last_activity_status

ข้อมูลแบบยืดหยุ่นcustom_attribute JSONB default {} โดยคีย์ที่ใช้ได้นิยามไว้ใน นิยาม Attribute

สมาชิกกลุ่มaudience_ids JSONB พร้อม GIN index เก็บรายการ audience ที่ผู้ใช้สังกัด ออกแบบเป็น denormalize เพื่อให้ query ได้เร็ว

Rich menurich_menu_id FK ไปยัง rich_menu.id และ rich_menu_archive_id FK ไปยัง rich_menu_archive.id ระบุ rich menu ที่ผูกอยู่กับผู้ใช้คนนี้

คีย์ธุรกิจตัวจริงคือ unique constraint บน (user_id, line_oa_id, organization_id) ซึ่ง tracking_log ใช้อ้างอิงเป็น composite FK ส่วน index อื่นคือ (line_oa_id) และ GIN บน audience_ids

Trigger trg_line_user_attribute_change

Trigger แบบ AFTER UPDATE ที่เรียกฟังก์ชัน notify_attribute_change() เพื่อส่ง pg_notify ไปยังช่อง attribute_changed พร้อมรายชื่อคีย์ที่มีการเปลี่ยนแปลง ครอบคลุมทั้งคอลัมน์คงที่ (เช่น display_name, email, mobile_no, follow) และคีย์ภายใน custom_attribute ที่อ้างในรูป custom.<key>

มีสวิตช์ current_setting('app.skip_triggers')='true' ไว้ปิดการทำงานชั่วคราวระหว่างทำ bulk update เพื่อไม่ให้ trigger ยิงถี่เกินไป รายละเอียดเพิ่มเติมอยู่ที่ Event Outbox

ไฟล์ที่เกี่ยวข้อง

  • prisma/schema.prisma:492 — model LineUser
  • manual-sql/1.notify_attribute_change.sql — trigger รุ่นแรก ครอบคลุมเฉพาะ custom_attribute
  • manual-sql/3.bank.sql — trigger รุ่นที่สอง เพิ่มคอลัมน์คงที่และสวิตช์ app.skip_triggers
  • manual-sql/5.sql — รุ่นล่าสุด เพิ่มการเขียนลง event_outbox
  • seed-data/07.line_user.sql — seed เพื่อนตัวอย่าง 2 คน
  • schema-dumps/2026-07-24/schema.sql:2243

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

  • webhook-go — สร้างและอัปเดตแถวเมื่อเกิด follow, unfollow หรือ message event
  • worker-go — sync follower จาก LINE, ประมวลผลงาน import, รีเฟรช audience_ids และ listen ช่อง attribute_changed เพื่อจุดชนวน trigger rule และ workflow
  • cms-api-go — ขับเคลื่อนหน้า "เพื่อนทั้งหมด" รวมถึงการค้นหาและกรองด้วย custom attribute และการ export
  • client-api-go — ให้ LIFF อ่านและอัปเดตโปรไฟล์ของผู้ใช้เอง และผูกกับ loyalty, bulletin, appointment ผ่าน LINE userId
  • เชื่อมกับ กลุ่มเป้าหมาย, นิยาม Attribute, Rich Menu และ Tracking