| 1 | import { createId } from '@paralleldrive/cuid2' |
| 2 | import { sql } |
| 3 | import { |
| 4 | index, |
| 5 | integer, |
| 6 | jsonb, |
| 7 | pgEnum, |
| 8 | pgTable, |
| 9 | text, |
| 10 | timestamp, |
| 11 | uniqueIndex, |
| 12 | } from 'drizzle-orm/pg-core' |
| 13 | import { organizations } |
| 14 | import { projects } |
| 15 | |
| 16 | export const timesheetDurationEnum = |
| 17 | 'weekly', |
| 18 | 'biweekly', |
| 19 | 'monthly', |
| 20 | ]) |
| 21 | |
| 22 | export const settings = |
| 23 | 'settings', |
| 24 | { |
| 25 | id: text('id |
| 26 | .primaryKey() |
| 27 | .$defaultFn(() => |
| 28 | organizationId: text('organization_id |
| 29 | .references(() => |
| 30 | .notNull(), |
| 31 | projectId: text('project_id |
| 32 | onDelete: 'cascade', |
| 33 | }), |
| 34 | memberRate: integer('member_rate |
| 35 | currency: text('currency |
| 36 | invoiceNumberTemplate: text('invoice_number_template |
| 37 | .default('INV-%year(short)%month(num)-%seq(4)') |
| 38 | .notNull(), |
| 39 | timesheetDuration: timesheetDurationEnum('timesheet_duration |
| 40 | .default('weekly') |
| 41 | .notNull(), |
| 42 | clientInvolvement: jsonb('client_involvement |
| 43 | .$type<{ |
| 44 | proposals: 'on |
| 45 | requirements: 'on |
| 46 | milestones: 'on |
| 47 | timesheets: 'on |
| 48 | expenses: 'on |
| 49 | invoices: 'on |
| 50 | }>() |
| 51 | .$defaultFn(() => |
| 52 | proposals: 'on', |
| 53 | requirements: 'on', |
| 54 | milestones: 'on', |
| 55 | timesheets: 'on', |
| 56 | expenses: 'on', |
| 57 | invoices: 'on', |
| 58 | })) |
| 59 | .notNull(), |
| 60 | createdAt: timestamp('created_at |
| 61 | updatedAt: timestamp('updated_at |
| 62 | .defaultNow() |
| 63 | .$onUpdate(() => |
| 64 | .notNull(), |
| 65 | }, |
| 66 | (table) => |
| 67 | uniqueIndex('org_settings_org_project_unique'). |
| 68 | table.organizationId, |
| 69 | table.projectId |
| 70 | ), |
| 71 | uniqueIndex('org_settings_org_only_unique') |
| 72 | .on(table.organizationId |
| 73 | .where(sql`${ |
| 74 | index('org_settings_org_id_idx'). |
| 75 | ] |
| 76 | ) |