Files
aura-crm/production/backend/migrations/039_order_statuses.sql
T

48 lines
2.9 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- +goose Up
-- Кастомные статусы канбана — order_statuses replaces the fixed
-- orders_status_check enum with an owner-editable table. `key` is the
-- permanent identifier every existing piece of backend logic already keys
-- on (internal/order's "completed"/"cancelled"/"ready" transition side
-- effects, internal/scheduler's warranty reminder, internal/analytics'
-- turnaround calc, internal/booking's initial-status event log) — it is
-- deliberately NEVER exposed as editable. `label`/`color`/`sort_order` are
-- the owner-facing surface (rename, recolor, reorder) via internal/order's
-- new status CRUD. system_role is informational only (lets the UI flag
-- "Новая"/"Готово"/"Выдано"/"Отменено" as behaviourally special before an
-- owner deletes one) — nothing in Go queries it; the special-case logic
-- matches on `key` directly, same as before this migration, since key
-- never changes even when label does.
--
-- Deleting a status is only possible once no order references it anymore
-- (the FK below is RESTRICT, not CASCADE/SET NULL) — same "can't delete
-- something in use" behavior as every other reference table in this
-- schema, no extra guard needed even for the four system_role rows: an
-- owner who really wants to delete e.g. "Выдано" first has to move every
-- order out of it, at which point the handover side effects in
-- internal/order simply stop firing for future orders (no status carries
-- that key anymore) — a real capability tradeoff they're accepting on
-- purpose, not a bug.
CREATE TABLE order_statuses (
key TEXT PRIMARY KEY,
label TEXT NOT NULL,
color TEXT NOT NULL DEFAULT '#64748b',
sort_order INT NOT NULL,
system_role TEXT CHECK (system_role IN ('new', 'ready', 'completed', 'cancelled')),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE UNIQUE INDEX order_statuses_system_role_idx ON order_statuses (system_role) WHERE system_role IS NOT NULL;
INSERT INTO order_statuses (key, label, color, sort_order, system_role) VALUES
('new', 'Новая', '#f59e0b', 0, 'new'),
('waiting_documents', 'Ожидание документов', '#14b8a6', 1, NULL),
('diagnosing', 'Диагностика', '#0ea5e9', 2, NULL),
('in_repair', 'В ремонте', '#8b5cf6', 3, NULL),
('waiting_parts', 'Ждём запчасти', '#f97316', 4, NULL),
('ready', 'Готово', '#22c55e', 5, 'ready'),
('completed', 'Выдано', '#64748b', 6, 'completed'),
('cancelled', 'Отменено', '#ef4444', 7, 'cancelled');
ALTER TABLE orders DROP CONSTRAINT orders_status_check;
ALTER TABLE orders ADD CONSTRAINT orders_status_fkey FOREIGN KEY (status) REFERENCES order_statuses(key) ON DELETE RESTRICT;