From 6e5362fa2c17e803a8e4a3d31c99b5eb53378661 Mon Sep 17 00:00:00 2001 From: ngqile Date: Wed, 2 Dec 2026 00:53:04 +0800 Subject: [PATCH] feat: WhatsApp inbox schema and modules Adds schema service-whatsapp (wa_conversation, wa_message, wa_read, wa_template), modules BO_WHATSAPP and BO_WHATSAPP_ADMIN, the rollback, and the local seed mirror. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM --- .../specs/2026-12-02-whatsapp-inbox-design.md | 2 +- local/seed/00-schemas.sql | 1 + local/seed/03-colite.sql | 81 +++++++++++++++++++ .../sql/01-whatsapp-schema.sql | 78 ++++++++++++++++++ .../sql/02-whatsapp-modules.sql | 9 +++ .../add-whatsapp-inbox/sql/99-rollback.sql | 6 ++ 6 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 openspec/changes/add-whatsapp-inbox/sql/01-whatsapp-schema.sql create mode 100644 openspec/changes/add-whatsapp-inbox/sql/02-whatsapp-modules.sql create mode 100644 openspec/changes/add-whatsapp-inbox/sql/99-rollback.sql diff --git a/docs/superpowers/specs/2026-12-02-whatsapp-inbox-design.md b/docs/superpowers/specs/2026-12-02-whatsapp-inbox-design.md index d71f079..02b77b2 100644 --- a/docs/superpowers/specs/2026-12-02-whatsapp-inbox-design.md +++ b/docs/superpowers/specs/2026-12-02-whatsapp-inbox-design.md @@ -82,7 +82,7 @@ All of these come from the environment, with none committed. Each company has it My call: settings live in configuration, not a table. They change only when a number changes, they are deployed with the secrets, and they need no screen. They are keyed by company code, which is stable (codes are uppercase identifiers; a rename would need the settings renamed too). A company without settings has WhatsApp switched off: its inbox says "WhatsApp is not configured", and the rest of Co-Lite still starts. -### Data (schema `service-whatsapp`, every table carries `company_id`) +### Data (schema `service-whatsapp`; conversations and templates carry `company_id`, messages and read markers inherit it through `conversation_id`) - **`wa_conversation`** - Columns: `id`, `uuid`, `company_id`, `phone_number_id`, `contact_wa_id` (E.164 digits), `contact_name` (WhatsApp profile name), `lead_uuid` (nullable), `owner_user_id` (nullable), `owner_manual` (boolean), `last_inbound_at` (sets the 24-hour window), `last_message_at`, `last_message_preview` (first 80 characters), `created_at`. diff --git a/local/seed/00-schemas.sql b/local/seed/00-schemas.sql index 30d2787..a9920ef 100644 --- a/local/seed/00-schemas.sql +++ b/local/seed/00-schemas.sql @@ -4,3 +4,4 @@ CREATE DATABASE IF NOT EXISTS `service-counter` CHARACTER SET utf8mb4; CREATE DATABASE IF NOT EXISTS `service-member` CHARACTER SET utf8mb4; CREATE DATABASE IF NOT EXISTS `service-log` CHARACTER SET utf8mb4; CREATE DATABASE IF NOT EXISTS `service-email` CHARACTER SET utf8mb4; +CREATE DATABASE IF NOT EXISTS `service-whatsapp` CHARACTER SET utf8mb4; diff --git a/local/seed/03-colite.sql b/local/seed/03-colite.sql index 99553eb..98a43a9 100644 --- a/local/seed/03-colite.sql +++ b/local/seed/03-colite.sql @@ -347,3 +347,84 @@ INSERT INTO `module` (`id`, `name`, `desc`, `area`, `type`, `parent`, `url`, `ic -- polish-e2e-followups/sql/01-soft-delete.sql ALTER TABLE `service-counter`.`interest` ADD COLUMN `deleted_at` datetime NULL; ALTER TABLE `service-system`.`role` ADD COLUMN `deleted_at` datetime NULL; +-- add-whatsapp-inbox/sql/01-whatsapp-schema.sql +CREATE DATABASE IF NOT EXISTS `service-whatsapp` CHARACTER SET utf8mb4; +USE `service-whatsapp`; + +CREATE TABLE `wa_conversation` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `uuid` varchar(36) NOT NULL, + `company_id` bigint NOT NULL, + `phone_number_id` varchar(32) NOT NULL, + `contact_wa_id` varchar(20) NOT NULL COMMENT 'E.164 digits, no leading +', + `contact_name` varchar(255) NULL, + `lead_uuid` bigint NULL, + `owner_user_id` bigint NULL, + `owner_manual` boolean NOT NULL DEFAULT false, + `last_inbound_at` datetime NULL, + `last_message_at` datetime NULL, + `last_message_preview` varchar(80) NULL, + `created_at` datetime NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `uk_wa_conversation_uuid` (`uuid`), + UNIQUE KEY `uk_wa_conversation_contact` (`company_id`, `contact_wa_id`), + KEY `idx_wa_conversation_owner` (`company_id`, `owner_user_id`, `last_message_at`), + KEY `idx_wa_conversation_last_message` (`company_id`, `last_message_at`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE `wa_message` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `uuid` varchar(36) NOT NULL, + `conversation_id` bigint NOT NULL, + `direction` varchar(3) NOT NULL COMMENT 'IN | OUT', + `wamid` varchar(128) NULL COMMENT 'NULL while an OUT message is pending, before Meta answers', + `type` varchar(16) NOT NULL COMMENT 'text | template | image | document | audio | video | sticker | location | unsupported', + `body` text NULL, + `template_name` varchar(255) NULL, + `media_id` varchar(128) NULL, + `media_mime` varchar(128) NULL, + `media_filename` varchar(255) NULL, + `location_lat` decimal(10,7) NULL, + `location_lng` decimal(10,7) NULL, + `location_name` varchar(255) NULL, + `status` varchar(16) NULL COMMENT 'OUT only: pending | sent | delivered | read | failed', + `error_code` varchar(32) NULL, + `error_title` varchar(255) NULL, + `sent_by_user_id` bigint NULL, + `created_at` datetime NOT NULL, + `status_at` datetime NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `uk_wa_message_uuid` (`uuid`), + UNIQUE KEY `uk_wa_message_wamid` (`wamid`), + KEY `idx_wa_message_conversation` (`conversation_id`, `id`), + CONSTRAINT `fk_wa_message_conversation` FOREIGN KEY (`conversation_id`) REFERENCES `wa_conversation` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE `wa_read` ( + `conversation_id` bigint NOT NULL, + `user_id` bigint NOT NULL, + `last_read_message_id` bigint NOT NULL, + PRIMARY KEY (`conversation_id`, `user_id`), + CONSTRAINT `fk_wa_read_conversation` FOREIGN KEY (`conversation_id`) REFERENCES `wa_conversation` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE `wa_template` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `company_id` bigint NOT NULL, + `name` varchar(255) NOT NULL, + `language` varchar(16) NOT NULL, + `category` varchar(32) NULL, + `status` varchar(32) NOT NULL, + `body_text` text NULL, + `param_count` int NOT NULL DEFAULT 0, + `synced_at` datetime NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `uk_wa_template` (`company_id`, `name`, `language`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +-- add-whatsapp-inbox/sql/02-whatsapp-modules.sql +USE `service-system`; + +INSERT INTO `module` (`id`, `name`, `desc`, `area`, `type`, `parent`, `url`, `icon_name`) VALUES + (41200000, 'BO_WHATSAPP', 'WhatsApp', 'BO', 'M', 40000000, '/whatsapp', 'MessageCircle'), + (41300000, 'BO_WHATSAPP_ADMIN', 'WhatsApp admin', 'BO', 'M', 40000000, NULL, NULL); diff --git a/openspec/changes/add-whatsapp-inbox/sql/01-whatsapp-schema.sql b/openspec/changes/add-whatsapp-inbox/sql/01-whatsapp-schema.sql new file mode 100644 index 0000000..d6a10a1 --- /dev/null +++ b/openspec/changes/add-whatsapp-inbox/sql/01-whatsapp-schema.sql @@ -0,0 +1,78 @@ +-- Co-Lite change add-whatsapp-inbox: the WhatsApp inbox schema. +-- Target: new schema `service-whatsapp`. Additive only. Run once, after the release package. +-- DDL: run with a user that may create databases and tables, not the services' user. +-- company_id, owner_user_id, sent_by_user_id and user_id point at `service-system`; lead_uuid at `service-counter`.`lead`.`uuid`; no foreign keys across schemas. +-- direction (IN | OUT), type and status are varchar, not ENUM, like the rest of the repo. +CREATE DATABASE IF NOT EXISTS `service-whatsapp` CHARACTER SET utf8mb4; +USE `service-whatsapp`; + +CREATE TABLE `wa_conversation` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `uuid` varchar(36) NOT NULL, + `company_id` bigint NOT NULL, + `phone_number_id` varchar(32) NOT NULL, + `contact_wa_id` varchar(20) NOT NULL COMMENT 'E.164 digits, no leading +', + `contact_name` varchar(255) NULL, + `lead_uuid` bigint NULL, + `owner_user_id` bigint NULL, + `owner_manual` boolean NOT NULL DEFAULT false, + `last_inbound_at` datetime NULL, + `last_message_at` datetime NULL, + `last_message_preview` varchar(80) NULL, + `created_at` datetime NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `uk_wa_conversation_uuid` (`uuid`), + UNIQUE KEY `uk_wa_conversation_contact` (`company_id`, `contact_wa_id`), + KEY `idx_wa_conversation_owner` (`company_id`, `owner_user_id`, `last_message_at`), + KEY `idx_wa_conversation_last_message` (`company_id`, `last_message_at`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE `wa_message` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `uuid` varchar(36) NOT NULL, + `conversation_id` bigint NOT NULL, + `direction` varchar(3) NOT NULL COMMENT 'IN | OUT', + `wamid` varchar(128) NULL COMMENT 'NULL while an OUT message is pending, before Meta answers', + `type` varchar(16) NOT NULL COMMENT 'text | template | image | document | audio | video | sticker | location | unsupported', + `body` text NULL, + `template_name` varchar(255) NULL, + `media_id` varchar(128) NULL, + `media_mime` varchar(128) NULL, + `media_filename` varchar(255) NULL, + `location_lat` decimal(10,7) NULL, + `location_lng` decimal(10,7) NULL, + `location_name` varchar(255) NULL, + `status` varchar(16) NULL COMMENT 'OUT only: pending | sent | delivered | read | failed', + `error_code` varchar(32) NULL, + `error_title` varchar(255) NULL, + `sent_by_user_id` bigint NULL, + `created_at` datetime NOT NULL, + `status_at` datetime NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `uk_wa_message_uuid` (`uuid`), + UNIQUE KEY `uk_wa_message_wamid` (`wamid`), + KEY `idx_wa_message_conversation` (`conversation_id`, `id`), + CONSTRAINT `fk_wa_message_conversation` FOREIGN KEY (`conversation_id`) REFERENCES `wa_conversation` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE `wa_read` ( + `conversation_id` bigint NOT NULL, + `user_id` bigint NOT NULL, + `last_read_message_id` bigint NOT NULL, + PRIMARY KEY (`conversation_id`, `user_id`), + CONSTRAINT `fk_wa_read_conversation` FOREIGN KEY (`conversation_id`) REFERENCES `wa_conversation` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE `wa_template` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `company_id` bigint NOT NULL, + `name` varchar(255) NOT NULL, + `language` varchar(16) NOT NULL, + `category` varchar(32) NULL, + `status` varchar(32) NOT NULL, + `body_text` text NULL, + `param_count` int NOT NULL DEFAULT 0, + `synced_at` datetime NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `uk_wa_template` (`company_id`, `name`, `language`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; diff --git a/openspec/changes/add-whatsapp-inbox/sql/02-whatsapp-modules.sql b/openspec/changes/add-whatsapp-inbox/sql/02-whatsapp-modules.sql new file mode 100644 index 0000000..ed20464 --- /dev/null +++ b/openspec/changes/add-whatsapp-inbox/sql/02-whatsapp-modules.sql @@ -0,0 +1,9 @@ +-- Co-Lite change add-whatsapp-inbox: the WhatsApp inbox and WhatsApp admin back-office modules (area BO). +-- Target: `service-system`.`module`. Additive only. Run once, after the release package. +-- No role grant: super admins see every module; they grant BO_WHATSAPP and BO_WHATSAPP_ADMIN to other roles under Roles. +-- BO_WHATSAPP_ADMIN has no url and no icon (like BO_EVENT_GUESTS), so it stays out of the sidebar and only gates actions. +USE `service-system`; + +INSERT INTO `module` (`id`, `name`, `desc`, `area`, `type`, `parent`, `url`, `icon_name`) VALUES + (41200000, 'BO_WHATSAPP', 'WhatsApp', 'BO', 'M', 40000000, '/whatsapp', 'MessageCircle'), + (41300000, 'BO_WHATSAPP_ADMIN', 'WhatsApp admin', 'BO', 'M', 40000000, NULL, NULL); diff --git a/openspec/changes/add-whatsapp-inbox/sql/99-rollback.sql b/openspec/changes/add-whatsapp-inbox/sql/99-rollback.sql new file mode 100644 index 0000000..e641053 --- /dev/null +++ b/openspec/changes/add-whatsapp-inbox/sql/99-rollback.sql @@ -0,0 +1,6 @@ +-- Co-Lite change add-whatsapp-inbox: rollback of 01-02. Destroys every stored WhatsApp conversation and message. +USE `service-system`; +DELETE FROM `modules_roles` WHERE `module_id` IN (41200000, 41300000); +DELETE FROM `module` WHERE `id` IN (41200000, 41300000); + +DROP DATABASE IF EXISTS `service-whatsapp`;