Add lookup tables, payment methods, tax exemptions, and processor link APIs
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.
This commit is contained in:
86
packages/backend/src/db/migrations/0007_accounts_lookups.sql
Normal file
86
packages/backend/src/db/migrations/0007_accounts_lookups.sql
Normal file
@@ -0,0 +1,86 @@
|
||||
-- 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");
|
||||
@@ -50,6 +50,13 @@
|
||||
"when": 1774653924179,
|
||||
"tag": "0006_add_consignment",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 7,
|
||||
"version": "7",
|
||||
"when": 1774662300000,
|
||||
"tag": "0007_accounts_lookups",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -6,12 +6,14 @@ import {
|
||||
jsonb,
|
||||
timestamp,
|
||||
boolean,
|
||||
integer,
|
||||
date,
|
||||
pgEnum,
|
||||
} from 'drizzle-orm/pg-core'
|
||||
import { companies } from './stores.js'
|
||||
|
||||
export const billingModeEnum = pgEnum('billing_mode', ['consolidated', 'split'])
|
||||
export const taxExemptStatusEnum = pgEnum('tax_exempt_status', ['none', 'pending', 'approved'])
|
||||
|
||||
export const accounts = pgTable('account', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
@@ -77,6 +79,54 @@ export const accountProcessorLinks = pgTable('account_processor_link', {
|
||||
export type AccountProcessorLink = typeof accountProcessorLinks.$inferSelect
|
||||
export type AccountProcessorLinkInsert = typeof accountProcessorLinks.$inferInsert
|
||||
|
||||
export const accountPaymentMethods = pgTable('account_payment_method', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
accountId: uuid('account_id')
|
||||
.notNull()
|
||||
.references(() => accounts.id),
|
||||
companyId: uuid('company_id')
|
||||
.notNull()
|
||||
.references(() => companies.id),
|
||||
processor: processorEnum('processor').notNull(),
|
||||
processorPaymentMethodId: varchar('processor_payment_method_id', { length: 255 }).notNull(),
|
||||
cardBrand: varchar('card_brand', { length: 50 }),
|
||||
lastFour: varchar('last_four', { length: 4 }),
|
||||
expMonth: integer('exp_month'),
|
||||
expYear: integer('exp_year'),
|
||||
isDefault: boolean('is_default').notNull().default(false),
|
||||
requiresUpdate: boolean('requires_update').notNull().default(false),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
})
|
||||
|
||||
export type AccountPaymentMethod = typeof accountPaymentMethods.$inferSelect
|
||||
export type AccountPaymentMethodInsert = typeof accountPaymentMethods.$inferInsert
|
||||
|
||||
export const taxExemptions = pgTable('tax_exemption', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
accountId: uuid('account_id')
|
||||
.notNull()
|
||||
.references(() => accounts.id),
|
||||
companyId: uuid('company_id')
|
||||
.notNull()
|
||||
.references(() => companies.id),
|
||||
status: taxExemptStatusEnum('status').notNull().default('pending'),
|
||||
certificateNumber: varchar('certificate_number', { length: 255 }).notNull(),
|
||||
certificateType: varchar('certificate_type', { length: 100 }),
|
||||
issuingState: varchar('issuing_state', { length: 2 }),
|
||||
expiresAt: date('expires_at'),
|
||||
approvedBy: uuid('approved_by'),
|
||||
approvedAt: timestamp('approved_at', { withTimezone: true }),
|
||||
revokedBy: uuid('revoked_by'),
|
||||
revokedAt: timestamp('revoked_at', { withTimezone: true }),
|
||||
revokedReason: text('revoked_reason'),
|
||||
notes: text('notes'),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
})
|
||||
|
||||
export type TaxExemption = typeof taxExemptions.$inferSelect
|
||||
export type TaxExemptionInsert = typeof taxExemptions.$inferInsert
|
||||
|
||||
export type Account = typeof accounts.$inferSelect
|
||||
export type AccountInsert = typeof accounts.$inferInsert
|
||||
export type Member = typeof members.$inferSelect
|
||||
|
||||
@@ -8,7 +8,6 @@ import {
|
||||
integer,
|
||||
numeric,
|
||||
date,
|
||||
pgEnum,
|
||||
} from 'drizzle-orm/pg-core'
|
||||
import { companies, locations } from './stores.js'
|
||||
|
||||
@@ -44,21 +43,9 @@ export const suppliers = pgTable('supplier', {
|
||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
})
|
||||
|
||||
export const conditionEnum = pgEnum('item_condition', [
|
||||
'new',
|
||||
'excellent',
|
||||
'good',
|
||||
'fair',
|
||||
'poor',
|
||||
])
|
||||
|
||||
export const unitStatusEnum = pgEnum('unit_status', [
|
||||
'available',
|
||||
'sold',
|
||||
'rented',
|
||||
'in_repair',
|
||||
'retired',
|
||||
])
|
||||
// NOTE: item_condition and unit_status pgEnums replaced by lookup tables.
|
||||
// See lookups.ts for inventory_unit_status and item_condition tables.
|
||||
// Columns below use varchar referencing the lookup slug.
|
||||
|
||||
export const products = pgTable('product', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
@@ -97,8 +84,8 @@ export const inventoryUnits = pgTable('inventory_unit', {
|
||||
.references(() => companies.id),
|
||||
locationId: uuid('location_id').references(() => locations.id),
|
||||
serialNumber: varchar('serial_number', { length: 255 }),
|
||||
condition: conditionEnum('condition').notNull().default('new'),
|
||||
status: unitStatusEnum('status').notNull().default('available'),
|
||||
condition: varchar('condition', { length: 100 }).notNull().default('new'),
|
||||
status: varchar('status', { length: 100 }).notNull().default('available'),
|
||||
purchaseDate: date('purchase_date'),
|
||||
purchaseCost: numeric('purchase_cost', { precision: 10, scale: 2 }),
|
||||
notes: text('notes'),
|
||||
|
||||
67
packages/backend/src/db/schema/lookups.ts
Normal file
67
packages/backend/src/db/schema/lookups.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { pgTable, uuid, varchar, text, timestamp, boolean, integer } from 'drizzle-orm/pg-core'
|
||||
import { companies } from './stores.js'
|
||||
|
||||
/**
|
||||
* Lookup tables replace hard-coded pgEnums for values that stores may want to customize.
|
||||
* System rows (is_system = true) are seeded per company and cannot be deleted or renamed.
|
||||
* Stores can add custom rows for informational/tracking purposes.
|
||||
*/
|
||||
|
||||
export const inventoryUnitStatuses = pgTable('inventory_unit_status', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
companyId: uuid('company_id')
|
||||
.notNull()
|
||||
.references(() => companies.id),
|
||||
name: varchar('name', { length: 100 }).notNull(),
|
||||
slug: varchar('slug', { length: 100 }).notNull(),
|
||||
description: text('description'),
|
||||
isSystem: boolean('is_system').notNull().default(false),
|
||||
sortOrder: integer('sort_order').notNull().default(0),
|
||||
isActive: boolean('is_active').notNull().default(true),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
})
|
||||
|
||||
export const itemConditions = pgTable('item_condition', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
companyId: uuid('company_id')
|
||||
.notNull()
|
||||
.references(() => companies.id),
|
||||
name: varchar('name', { length: 100 }).notNull(),
|
||||
slug: varchar('slug', { length: 100 }).notNull(),
|
||||
description: text('description'),
|
||||
isSystem: boolean('is_system').notNull().default(false),
|
||||
sortOrder: integer('sort_order').notNull().default(0),
|
||||
isActive: boolean('is_active').notNull().default(true),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
})
|
||||
|
||||
export type InventoryUnitStatus = typeof inventoryUnitStatuses.$inferSelect
|
||||
export type InventoryUnitStatusInsert = typeof inventoryUnitStatuses.$inferInsert
|
||||
export type ItemCondition = typeof itemConditions.$inferSelect
|
||||
export type ItemConditionInsert = typeof itemConditions.$inferInsert
|
||||
|
||||
/**
|
||||
* System-seeded inventory unit statuses.
|
||||
* Code references these slugs for business logic.
|
||||
*/
|
||||
export const SYSTEM_UNIT_STATUSES = [
|
||||
{ slug: 'available', name: 'Available', description: 'In stock, ready for sale or rental', sortOrder: 0 },
|
||||
{ slug: 'sold', name: 'Sold', description: 'Purchased by customer', sortOrder: 1 },
|
||||
{ slug: 'rented', name: 'Rented', description: 'Out on active rental contract', sortOrder: 2 },
|
||||
{ slug: 'on_trial', name: 'On Trial', description: 'Out with customer on in-home trial', sortOrder: 3 },
|
||||
{ slug: 'in_repair', name: 'In Repair', description: 'In repair shop for service', sortOrder: 4 },
|
||||
{ slug: 'layaway', name: 'Layaway', description: 'Reserved for layaway customer', sortOrder: 5 },
|
||||
{ slug: 'lost', name: 'Lost', description: 'Unrecovered from trial, rental, or discrepancy', sortOrder: 6 },
|
||||
{ slug: 'retired', name: 'Retired', description: 'Permanently removed from inventory', sortOrder: 7 },
|
||||
] as const
|
||||
|
||||
/**
|
||||
* System-seeded item conditions.
|
||||
*/
|
||||
export const SYSTEM_ITEM_CONDITIONS = [
|
||||
{ slug: 'new', name: 'New', description: 'Brand new, unopened or unused', sortOrder: 0 },
|
||||
{ slug: 'excellent', name: 'Excellent', description: 'Like new, minimal signs of use', sortOrder: 1 },
|
||||
{ slug: 'good', name: 'Good', description: 'Normal wear, fully functional', sortOrder: 2 },
|
||||
{ slug: 'fair', name: 'Fair', description: 'Noticeable wear, functional with minor issues', sortOrder: 3 },
|
||||
{ slug: 'poor', name: 'Poor', description: 'Heavy wear, may need repair', sortOrder: 4 },
|
||||
] as const
|
||||
Reference in New Issue
Block a user