import type { UserRole } from '$lib/types/types'; import { boolean, foreignKey, integer, jsonb, numeric, pgTable, serial, text, timestamp } from 'drizzle-orm/pg-core'; import type { ClothingMeasurements } from '../../types/measurements'; export const user = pgTable('user', { id: text('id').primaryKey(), name: text('name').notNull(), email: text('email').notNull().unique(), emailVerified: boolean('email_verified').notNull(), image: text('image'), createdAt: timestamp('created_at').notNull(), updatedAt: timestamp('updated_at').notNull() }); export const session = pgTable('session', { id: text('id').primaryKey(), expiresAt: timestamp('expires_at').notNull(), token: text('token').notNull().unique(), createdAt: timestamp('created_at').notNull(), updatedAt: timestamp('updated_at').notNull(), ipAddress: text('ip_address'), userAgent: text('user_agent'), userId: text('user_id') .notNull() .references(() => user.id, { onDelete: 'cascade' }) }); export const account = pgTable('account', { id: text('id').primaryKey(), accountId: text('account_id').notNull(), providerId: text('provider_id').notNull(), userId: text('user_id') .notNull() .references(() => user.id, { onDelete: 'cascade' }), accessToken: text('access_token'), refreshToken: text('refresh_token'), idToken: text('id_token'), accessTokenExpiresAt: timestamp('access_token_expires_at'), refreshTokenExpiresAt: timestamp('refresh_token_expires_at'), scope: text('scope'), password: text('password'), role: text('role').default('super_admin'), createdAt: timestamp('created_at').notNull(), updatedAt: timestamp('updated_at').notNull() }); export const verification = pgTable('verification', { id: text('id').primaryKey(), identifier: text('identifier').notNull(), value: text('value').notNull(), expiresAt: timestamp('expires_at').notNull(), createdAt: timestamp('created_at'), updatedAt: timestamp('updated_at') }); export const customers = pgTable('customers', { id: text('id').primaryKey(), name: text('name').notNull(), phone: text('phone'), email: text('email'), avatar: text('avatar'), address: text('address'), notes: text('notes'), measurements: jsonb('measurements').$type().notNull().default({}), createdAt: timestamp('created_at', { withTimezone: true, mode: 'string' }).notNull(), updatedAt: timestamp('updated_at', { withTimezone: true, mode: 'string' }).notNull() }); export interface BusinessDetails { address: { street: string; city: string; state: string; zip: string; country: string; }; phone: string; paymentDetails: { accountName: string; accountNumber: string; bankName: string; }; } export const invoices = pgTable('invoices', { id: text('id').primaryKey(), customerId: text('customer_id') .notNull() .references(() => customers.id, { onDelete: 'cascade' }), date: timestamp('date', { withTimezone: true, mode: 'date' }).notNull(), dueDate: timestamp('due_date', { withTimezone: true, mode: 'date' }).notNull(), number: serial('number').notNull(), note: text('note'), businessDetails: jsonb('business_details').$type().notNull(), createdAt: timestamp('created_at', { withTimezone: true, mode: 'string' }).notNull().defaultNow(), updatedAt: timestamp('updated_at', { withTimezone: true, mode: 'string' }).notNull().defaultNow() }); export const invoiceLineItems = pgTable( 'invoice_line_items', { id: text('id').primaryKey(), invoiceId: text('invoice_id').notNull(), name: text('name').notNull(), productCode: text('product_code'), price: numeric('price', { precision: 19, scale: 4, mode: 'number' }).notNull(), quantity: integer('quantity').notNull(), discount: numeric('discount', { precision: 19, scale: 4, mode: 'number' }), photos: jsonb('photos').$type().default([]), description: text('description') }, (table) => ({ invoiceIdFk: foreignKey({ name: 'invoice_line_items_invoice_id_fk', columns: [table.invoiceId], foreignColumns: [invoices.id] }).onDelete('cascade') }) ); export type PaymentMethod = 'bank_transfer' | 'cash' | 'check' | 'credit_card'; export const payments = pgTable('payments', { id: text('id').primaryKey(), customerId: text('customer_id') .notNull() .references(() => customers.id, { onDelete: 'cascade' }), invoiceId: text('invoice_id') .notNull() .references(() => invoices.id, { onDelete: 'cascade' }), amountPaid: numeric('amount_paid', { precision: 19, scale: 4, mode: 'number' }).notNull(), paymentDate: timestamp('payment_date', { withTimezone: true, mode: 'string' }).notNull(), paymentMethod: text('payment_method').notNull().$type(), note: text('note'), date: timestamp('date', { withTimezone: true, mode: 'string' }).notNull(), createdAt: timestamp('created_at', { withTimezone: true, mode: 'string' }).notNull().defaultNow(), updatedAt: timestamp('updated_at', { withTimezone: true, mode: 'string' }).notNull().defaultNow() }); export const settings = pgTable('settings', { id: text('id').primaryKey(), defaultBusinessDetails: jsonb('default_business_details').$type().notNull(), defaultPaymentMethod: text('default_payment_method').notNull().$type(), defaultTaxRate: numeric('default_tax_rate', { precision: 19, scale: 4, mode: 'number' }).notNull(), createdAt: timestamp('created_at', { withTimezone: true, mode: 'string' }).notNull().defaultNow(), updatedAt: timestamp('updated_at', { withTimezone: true, mode: 'string' }).notNull().defaultNow() }); export const invites = pgTable('invites', { id: text('id').primaryKey(), email: text('email').notNull(), role: text('role').notNull().$type(), createdAt: timestamp('created_at', { withTimezone: true, mode: 'string' }).notNull().defaultNow(), updatedAt: timestamp('updated_at', { withTimezone: true, mode: 'string' }).notNull().defaultNow() });