| 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 | uniqueIndex, |
| 12 | } from 'drizzle-orm/pg-core' |
| 13 | import { members } |
| 14 | import { statusEnum } |
| 15 | import { invoices } |
| 16 | import { projects } |
| 17 | import { requirements } |
| 18 | |
| 19 | export const timeEntries = |
| 20 | 'time_entries', |
| 21 | { |
| 22 | id: text('id |
| 23 | .primaryKey() |
| 24 | .$defaultFn(() => |
| 25 | projectId: text('project_id |
| 26 | .references(() => |
| 27 | .notNull(), |
| 28 | requirementId: text('requirement_id |
| 29 | onDelete: 'set null', |
| 30 | }), |
| 31 | memberId: text('member_id |
| 32 | .references(() => |
| 33 | .notNull(), |
| 34 | description: text('description |
| 35 | date: timestamp('date |
| 36 | durationMinutes: integer('duration_minutes |
| 37 | billable: boolean('billable |
| 38 | status: statusEnum('status |
| 39 | rejectReason: text('reject_reason |
| 40 | invoiceId: text('invoice_id |
| 41 | onDelete: 'set null', |
| 42 | }), |
| 43 | createdAt: timestamp('created_at |
| 44 | updatedAt: timestamp('updated_at |
| 45 | .defaultNow() |
| 46 | .$onUpdate(() => |
| 47 | .notNull(), |
| 48 | }, |
| 49 | (table) => |
| 50 | index('time_entries_project_id_status_idx'). |
| 51 | table.projectId, |
| 52 | table.status |
| 53 | ), |
| 54 | index('time_entries_member_id_idx'). |
| 55 | ] |
| 56 | ) |
| 57 | |
| 58 | export const memberRates = |
| 59 | 'member_rates', |
| 60 | { |
| 61 | id: text('id |
| 62 | .primaryKey() |
| 63 | .$defaultFn(() => |
| 64 | memberId: text('member_id |
| 65 | .references(() => |
| 66 | .notNull(), |
| 67 | projectId: text('project_id |
| 68 | onDelete: 'cascade', |
| 69 | }), |
| 70 | hourlyRate: integer('hourly_rate |
| 71 | currency: text('currency |
| 72 | effectiveFrom: timestamp('effective_from |
| 73 | createdAt: timestamp('created_at |
| 74 | updatedAt: timestamp('updated_at |
| 75 | .defaultNow() |
| 76 | .$onUpdate(() => |
| 77 | .notNull(), |
| 78 | }, |
| 79 | (table) => |
| 80 | uniqueIndex('member_rate_effective_unique'). |
| 81 | table.memberId, |
| 82 | table.projectId, |
| 83 | table.effectiveFrom |
| 84 | ), |
| 85 | ] |
| 86 | ) |
| 87 | |
| 88 | export const timesheetReportStatusEnum = |
| 89 | 'draft', |
| 90 | 'sent', |
| 91 | 'approved', |
| 92 | 'disputed', |
| 93 | ]) |
| 94 | |
| 95 | export const timesheetReports = |
| 96 | id: text('id |
| 97 | .primaryKey() |
| 98 | .$defaultFn(() => |
| 99 | projectId: text('project_id |
| 100 | .references(() => |
| 101 | .notNull(), |
| 102 | title: text('title |
| 103 | status: timesheetReportStatusEnum('status |
| 104 | totalMinutes: integer('total_minutes |
| 105 | totalAmount: integer('total_amount_cents |
| 106 | currency: text('currency |
| 107 | sentByMemberId: text('sent_by_member_id |
| 108 | onDelete: 'set null', |
| 109 | }), |
| 110 | disputeReason: text('dispute_reason |
| 111 | sentAt: timestamp('sent_at |
| 112 | respondedAt: timestamp('responded_at |
| 113 | createdAt: timestamp('created_at |
| 114 | updatedAt: timestamp('updated_at |
| 115 | .defaultNow() |
| 116 | .$onUpdate(() => |
| 117 | .notNull(), |
| 118 | }) |
| 119 | |
| 120 | export const timesheetReportEntries = |
| 121 | 'timesheet_report_entries', |
| 122 | { |
| 123 | id: text('id |
| 124 | .primaryKey() |
| 125 | .$defaultFn(() => |
| 126 | reportId: text('report_id |
| 127 | .references(() => |
| 128 | .notNull(), |
| 129 | timeEntryId: text('time_entry_id |
| 130 | .references(() => |
| 131 | .notNull(), |
| 132 | |
| 133 | createdAt: timestamp('created_at |
| 134 | updatedAt: timestamp('updated_at |
| 135 | .defaultNow() |
| 136 | .$onUpdate(() => |
| 137 | .notNull(), |
| 138 | }, |
| 139 | (table) => |
| 140 | index('timesheet_report_entries_report_id_idx'). |
| 141 | ] |
| 142 | ) |
| 143 | |
| 144 | export const timesheetReportRecipientStatusEnum = |
| 145 | 'timesheet_report_recipient_status', |
| 146 | ['pending', 'approved' |
| 147 | ) |
| 148 | |
| 149 | export const timesheetReportRecipients = |
| 150 | 'timesheet_report_recipients', |
| 151 | { |
| 152 | id: text('id |
| 153 | .primaryKey() |
| 154 | .$defaultFn(() => |
| 155 | reportId: text('report_id |
| 156 | .references(() => |
| 157 | .notNull(), |
| 158 | clientMemberId: text('client_member_id |
| 159 | .references(() => |
| 160 | .notNull(), |
| 161 | status: timesheetReportRecipientStatusEnum('status |
| 162 | .default('pending') |
| 163 | .notNull(), |
| 164 | disputeReason: text('dispute_reason |
| 165 | respondedAt: timestamp('responded_at |
| 166 | createdAt: timestamp('created_at |
| 167 | updatedAt: timestamp('updated_at |
| 168 | .defaultNow() |
| 169 | .$onUpdate(() => |
| 170 | .notNull(), |
| 171 | }, |
| 172 | (table) => |
| 173 | index('timesheet_report_recipients_report_id_idx'). |
| 174 | unique().on(table |
| 175 | ] |
| 176 | ) |
| 177 | |
| 178 | export const pendingMemberRates = |
| 179 | 'pending_member_rates', |
| 180 | { |
| 181 | id: text('id |
| 182 | .primaryKey() |
| 183 | .$defaultFn(() => |
| 184 | invitationId: text('invitation_id |
| 185 | organizationId: text('organization_id |
| 186 | email: text('email |
| 187 | hourlyRate: integer('hourly_rate |
| 188 | currency: text('currency |
| 189 | createdAt: timestamp('created_at |
| 190 | updatedAt: timestamp('updated_at |
| 191 | .defaultNow() |
| 192 | .$onUpdate(() => |
| 193 | .notNull(), |
| 194 | }, |
| 195 | (table) => |
| 196 | index('pending_member_rates_org_email_idx'). |
| 197 | table.organizationId, |
| 198 | table.email |
| 199 | ), |
| 200 | ] |
| 201 | ) |
| 202 | |
| 203 | export const projectBudgets = |
| 204 | id: text('id |
| 205 | .primaryKey() |
| 206 | .$defaultFn(() => |
| 207 | projectId: text('project_id |
| 208 | .references(() => |
| 209 | .notNull() |
| 210 | .unique(), |
| 211 | budgetMinutes: integer('budget_minutes |
| 212 | alertThreshold: integer('alert_threshold |
| 213 | createdAt: timestamp('created_at |
| 214 | updatedAt: timestamp('updated_at |
| 215 | .defaultNow() |
| 216 | .$onUpdate(() => |
| 217 | .notNull(), |
| 218 | }) |