| 1 | import { createId } from '@paralleldrive/cuid2' |
| 2 | import { |
| 3 | boolean, |
| 4 | index, |
| 5 | integer, |
| 6 | pgEnum, |
| 7 | pgTable, |
| 8 | text, |
| 9 | timestamp, |
| 10 | unique, |
| 11 | } from 'drizzle-orm/pg-core' |
| 12 | import { members, organizations |
| 13 | import { statusEnum } |
| 14 | import { invoices } |
| 15 | import { media } |
| 16 | import { milestones } |
| 17 | import { projects } |
| 18 | |
| 19 | export const expenseCategories = |
| 20 | id: text('id |
| 21 | .primaryKey() |
| 22 | .$defaultFn(() => |
| 23 | organizationId: text('organization_id |
| 24 | .references(() => |
| 25 | .notNull(), |
| 26 | name: text('name |
| 27 | color: text('color |
| 28 | isArchived: boolean('is_archived |
| 29 | sortOrder: integer('sort_order |
| 30 | createdAt: timestamp('created_at |
| 31 | updatedAt: timestamp('updated_at |
| 32 | .defaultNow() |
| 33 | .$onUpdate(() => |
| 34 | .notNull(), |
| 35 | }) |
| 36 | |
| 37 | export const expenses = |
| 38 | 'expenses', |
| 39 | { |
| 40 | id: text('id |
| 41 | .primaryKey() |
| 42 | .$defaultFn(() => |
| 43 | projectId: text('project_id |
| 44 | .references(() => |
| 45 | .notNull(), |
| 46 | memberId: text('member_id |
| 47 | .references(() => |
| 48 | .notNull(), |
| 49 | categoryId: text('category_id |
| 50 | .references(() => |
| 51 | .notNull(), |
| 52 | milestoneId: text('milestone_id |
| 53 | onDelete: 'set null', |
| 54 | }), |
| 55 | title: text('title |
| 56 | description: text('description |
| 57 | amountCents: integer('amount_cents |
| 58 | currency: text('currency |
| 59 | date: timestamp().notNull |
| 60 | billable: boolean().default |
| 61 | status: statusEnum().notNull |
| 62 | rejectReason: text('reject_reason |
| 63 | receiptMediaId: text('receipt_media_id |
| 64 | onDelete: 'set null', |
| 65 | }), |
| 66 | invoiceId: text('invoice_id |
| 67 | onDelete: 'set null', |
| 68 | }), |
| 69 | createdAt: timestamp('created_at |
| 70 | updatedAt: timestamp('updated_at |
| 71 | .defaultNow() |
| 72 | .$onUpdate(() => |
| 73 | .notNull(), |
| 74 | }, |
| 75 | (table) => |
| 76 | index('expenses_project_id_status_idx'). |
| 77 | index('expenses_member_id_idx'). |
| 78 | ] |
| 79 | ) |
| 80 | |
| 81 | export const expenseRecipientStatusEnum = |
| 82 | 'pending', |
| 83 | 'approved', |
| 84 | 'rejected', |
| 85 | ]) |
| 86 | |
| 87 | export const expenseRecipients = |
| 88 | 'expense_recipients', |
| 89 | { |
| 90 | id: text('id |
| 91 | .primaryKey() |
| 92 | .$defaultFn(() => |
| 93 | expenseId: text('expense_id |
| 94 | .references(() => |
| 95 | .notNull(), |
| 96 | clientMemberId: text('client_member_id |
| 97 | .references(() => |
| 98 | .notNull(), |
| 99 | status: expenseRecipientStatusEnum('status |
| 100 | rejectReason: text('reject_reason |
| 101 | respondedAt: timestamp('responded_at |
| 102 | createdAt: timestamp('created_at |
| 103 | updatedAt: timestamp('updated_at |
| 104 | .defaultNow() |
| 105 | .$onUpdate(() => |
| 106 | .notNull(), |
| 107 | }, |
| 108 | (table) => |
| 109 | unique().on(table |
| 110 | index('expense_recipients_expense_id_idx'). |
| 111 | ] |
| 112 | ) |