From 493ffa4097071c924138ba11b88a270e1146aa4f Mon Sep 17 00:00:00 2001 From: grabowski Date: Fri, 17 Apr 2026 13:49:16 +0700 Subject: [PATCH] Add service accounts schema, enum, audit events, recurringBills FK Co-Authored-By: Claude Opus 4.6 (1M context) --- src/lib/server/db/schema.ts | 50 ++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/lib/server/db/schema.ts b/src/lib/server/db/schema.ts index 548f4f3..0a8eca6 100644 --- a/src/lib/server/db/schema.ts +++ b/src/lib/server/db/schema.ts @@ -974,6 +974,10 @@ export const recurringBills = pgTable( .references(() => companyAccounts.id, { onDelete: 'restrict' }), categoryId: uuid('category_id').references(() => categories.id, { onDelete: 'set null' }), partyId: uuid('party_id').references(() => parties.id, { onDelete: 'set null' }), + serviceAccountId: uuid('service_account_id').references( + (): any => companyServiceAccounts.id, + { onDelete: 'set null' } + ), name: text('name').notNull(), description: text('description'), cycle: recurringBillCycleEnum('cycle').notNull(), @@ -999,6 +1003,47 @@ export const recurringBills = pgTable( ] ); +// ── Service Accounts ─────────────────────────────────── + +export const serviceAccountTypeEnum = pgEnum('service_account_type', [ + 'electricity', + 'water', + 'gas', + 'internet', + 'phone', + 'shipping', + 'insurance', + 'tax_registration', + 'social_security', + 'customs', + 'other' +]); + +export const companyServiceAccounts = pgTable( + 'company_service_accounts', + { + id: uuid('id').primaryKey().defaultRandom(), + companyId: uuid('company_id') + .notNull() + .references(() => companies.id, { onDelete: 'cascade' }), + type: serviceAccountTypeEnum('type').notNull(), + providerName: text('provider_name').notNull(), + accountNumber: text('account_number').notNull(), + customLabel: text('custom_label'), + contactPhone: text('contact_phone'), + websiteUrl: text('website_url'), + notes: text('notes'), + isActive: boolean('is_active').notNull().default(true), + createdBy: text('created_by').references(() => users.id, { onDelete: 'set null' }), + deletedAt: timestamp('deleted_at', { withTimezone: true }), + createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(), + updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow() + }, + (table) => [ + index('company_service_accounts_company_type_idx').on(table.companyId, table.type) + ] +); + export const companyAddresses = pgTable( 'company_addresses', { @@ -1099,7 +1144,10 @@ export const companyLogEventEnum = pgEnum('company_log_event', [ 'recurring_bill_paused', 'recurring_bill_resumed', 'recurring_bill_skipped', - 'recurring_bill_posted' + 'recurring_bill_posted', + 'service_account_created', + 'service_account_updated', + 'service_account_deleted' ]); export const companyLog = pgTable(