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:
Ryan Moon
2026-03-27 20:53:30 -05:00
parent e7853f59f2
commit 0a2d6e23af
17 changed files with 1431 additions and 28 deletions

View File

@@ -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

View File

@@ -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'),

View 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