| 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 | import { billingFrequencyEnum } |
| 16 | |
| 17 | export const timesheetDurationEnum = |
| 18 | 'weekly', |
| 19 | 'biweekly', |
| 20 | 'monthly', |
| 21 | ]) |
| 22 | |
| 23 | export const invoiceTimeUnitEnum = |
| 24 | 'hours', |
| 25 | 'minutes', |
| 26 | ]) |
| 27 | |
| 28 | export const settings = |
| 29 | 'settings', |
| 30 | { |
| 31 | id: text('id |
| 32 | .primaryKey() |
| 33 | .$defaultFn(() => |
| 34 | organizationId: text('organization_id |
| 35 | .references(() => |
| 36 | .notNull(), |
| 37 | projectId: text('project_id |
| 38 | onDelete: 'cascade', |
| 39 | }), |
| 40 | invoiceNumberTemplate: text('invoice_number_template |
| 41 | .default('INV-%year(short)%month(num)-%seq(4)') |
| 42 | .notNull(), |
| 43 | timesheetDuration: timesheetDurationEnum('timesheet_duration |
| 44 | .default('weekly') |
| 45 | .notNull(), |
| 46 | invoiceTimeUnit: invoiceTimeUnitEnum('invoice_time_unit |
| 47 | .default('hours') |
| 48 | .notNull(), |
| 49 | clientInvolvement: jsonb('client_involvement |
| 50 | .$type<{ |
| 51 | proposals: 'on |
| 52 | requirements: 'on |
| 53 | milestones: 'on |
| 54 | timesheets: 'on |
| 55 | expenses: 'on |
| 56 | invoices: 'on |
| 57 | }>() |
| 58 | .$defaultFn(() => |
| 59 | proposals: 'on', |
| 60 | requirements: 'on', |
| 61 | milestones: 'on', |
| 62 | timesheets: 'on', |
| 63 | expenses: 'on', |
| 64 | invoices: 'on', |
| 65 | })) |
| 66 | .notNull(), |
| 67 | currency: text('currency |
| 68 | |
| 69 | billingRate: integer('billing_rate |
| 70 | billingCurrency: text('billing_currency |
| 71 | billingFrequency: |
| 72 | billingFrequencyEnum('billing_frequency'). |
| 73 | payRate: integer('pay_rate |
| 74 | payCurrency: text('pay_currency |
| 75 | payFrequency: billingFrequencyEnum('pay_frequency |
| 76 | .default('hourly') |
| 77 | .notNull(), |
| 78 | |
| 79 | invoiceFromName: text('invoice_from_name |
| 80 | invoiceFromAddress: text('invoice_from_address |
| 81 | /// Project only fields |
| 82 | invoiceToName: text('invoice_to_name |
| 83 | invoiceToAddress: text('invoice_to_address |
| 84 | createdAt: timestamp('created_at |
| 85 | updatedAt: timestamp('updated_at |
| 86 | .defaultNow() |
| 87 | .$onUpdate(() => |
| 88 | .notNull(), |
| 89 | }, |
| 90 | (table) => |
| 91 | uniqueIndex('org_settings_org_project_unique'). |
| 92 | table.organizationId, |
| 93 | table.projectId |
| 94 | ), |
| 95 | uniqueIndex('org_settings_org_only_unique') |
| 96 | .on(table.organizationId |
| 97 | .where(sql`${ |
| 98 | index('org_settings_org_id_idx'). |
| 99 | ] |
| 100 | ) |