Replace unit_status and item_condition pgEnums with company-scoped lookup tables that support custom values. Add account_payment_method table, tax_exemption table with approve/revoke workflow, and CRUD routes for processor links. Validate inventory unit status/condition against lookup tables at service layer.
87 lines
3.5 KiB
SQL
87 lines
3.5 KiB
SQL
-- Migration: Add lookup tables, account payment methods, tax exemptions
|
|
-- Replaces unit_status and item_condition pgEnums with lookup tables
|
|
|
|
-- 1. Drop defaults that reference old enums
|
|
ALTER TABLE "inventory_unit" ALTER COLUMN "status" DROP DEFAULT;
|
|
ALTER TABLE "inventory_unit" ALTER COLUMN "condition" DROP DEFAULT;
|
|
|
|
-- 2. Migrate inventory_unit columns from enum to varchar
|
|
ALTER TABLE "inventory_unit" ALTER COLUMN "status" TYPE varchar(100) USING "status"::text;
|
|
ALTER TABLE "inventory_unit" ALTER COLUMN "condition" TYPE varchar(100) USING "condition"::text;
|
|
|
|
-- 3. Re-add defaults as varchar values
|
|
ALTER TABLE "inventory_unit" ALTER COLUMN "status" SET DEFAULT 'available';
|
|
ALTER TABLE "inventory_unit" ALTER COLUMN "condition" SET DEFAULT 'new';
|
|
|
|
-- 4. Drop old enums (must happen before creating tables with same names)
|
|
DROP TYPE IF EXISTS "unit_status";
|
|
DROP TYPE IF EXISTS "item_condition";
|
|
|
|
-- 3. Create new enum for tax exemptions
|
|
CREATE TYPE "tax_exempt_status" AS ENUM ('none', 'pending', 'approved');
|
|
|
|
-- 4. Create lookup tables (item_condition name is now free)
|
|
CREATE TABLE IF NOT EXISTS "inventory_unit_status" (
|
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
"company_id" uuid NOT NULL REFERENCES "company"("id"),
|
|
"name" varchar(100) NOT NULL,
|
|
"slug" varchar(100) NOT NULL,
|
|
"description" text,
|
|
"is_system" boolean NOT NULL DEFAULT false,
|
|
"sort_order" integer NOT NULL DEFAULT 0,
|
|
"is_active" boolean NOT NULL DEFAULT true,
|
|
"created_at" timestamp with time zone NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS "item_condition" (
|
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
"company_id" uuid NOT NULL REFERENCES "company"("id"),
|
|
"name" varchar(100) NOT NULL,
|
|
"slug" varchar(100) NOT NULL,
|
|
"description" text,
|
|
"is_system" boolean NOT NULL DEFAULT false,
|
|
"sort_order" integer NOT NULL DEFAULT 0,
|
|
"is_active" boolean NOT NULL DEFAULT true,
|
|
"created_at" timestamp with time zone NOT NULL DEFAULT now()
|
|
);
|
|
|
|
-- 5. Create account_payment_method table
|
|
CREATE TABLE IF NOT EXISTS "account_payment_method" (
|
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
"account_id" uuid NOT NULL REFERENCES "account"("id"),
|
|
"company_id" uuid NOT NULL REFERENCES "company"("id"),
|
|
"processor" "payment_processor" NOT NULL,
|
|
"processor_payment_method_id" varchar(255) NOT NULL,
|
|
"card_brand" varchar(50),
|
|
"last_four" varchar(4),
|
|
"exp_month" integer,
|
|
"exp_year" integer,
|
|
"is_default" boolean NOT NULL DEFAULT false,
|
|
"requires_update" boolean NOT NULL DEFAULT false,
|
|
"created_at" timestamp with time zone NOT NULL DEFAULT now()
|
|
);
|
|
|
|
-- 6. Create tax_exemption table
|
|
CREATE TABLE IF NOT EXISTS "tax_exemption" (
|
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
"account_id" uuid NOT NULL REFERENCES "account"("id"),
|
|
"company_id" uuid NOT NULL REFERENCES "company"("id"),
|
|
"status" "tax_exempt_status" NOT NULL DEFAULT 'pending',
|
|
"certificate_number" varchar(255) NOT NULL,
|
|
"certificate_type" varchar(100),
|
|
"issuing_state" varchar(2),
|
|
"expires_at" date,
|
|
"approved_by" uuid,
|
|
"approved_at" timestamp with time zone,
|
|
"revoked_by" uuid,
|
|
"revoked_at" timestamp with time zone,
|
|
"revoked_reason" text,
|
|
"notes" text,
|
|
"created_at" timestamp with time zone NOT NULL DEFAULT now(),
|
|
"updated_at" timestamp with time zone NOT NULL DEFAULT now()
|
|
);
|
|
|
|
-- 7. Add unique constraint on lookup slugs per company
|
|
CREATE UNIQUE INDEX "inventory_unit_status_company_slug" ON "inventory_unit_status" ("company_id", "slug");
|
|
CREATE UNIQUE INDEX "item_condition_company_slug" ON "item_condition" ("company_id", "slug");
|