| 1 | import { createId } from "@paralleldrive/cuid2"; |
| 2 | import { boolean, index |
| 3 | import { organizations, teams |
| 4 | |
| 5 | export * from "./auth-schema.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Projects owned by an organization (agency or individual freelancer). |
| 9 | */ |
| 10 | export const projects = |
| 11 | id: text("id |
| 12 | .primaryKey() |
| 13 | .$defaultFn(() => |
| 14 | name: text("name |
| 15 | description: text("description |
| 16 | organizationId: text("organization_id |
| 17 | onDelete: "cascade", |
| 18 | }), |
| 19 | createdAt: timestamp("created_at |
| 20 | updatedAt: timestamp("updated_at |
| 21 | }); |
| 22 | |
| 23 | /** |
| 24 | * Freelancers or freelancer teams assigned to a project, scoped to the owning org. |
| 25 | */ |
| 26 | export const projectMembers = |
| 27 | "project_members", |
| 28 | { |
| 29 | id: text("id |
| 30 | .primaryKey() |
| 31 | .$defaultFn(() => |
| 32 | projectId: text("project_id |
| 33 | .notNull() |
| 34 | .references(() => |
| 35 | organizationId: text("organization_id |
| 36 | .notNull() |
| 37 | .references(() => |
| 38 | teamId: text("team_id |
| 39 | onDelete: "set null", |
| 40 | }), |
| 41 | userId: text("user_id |
| 42 | onDelete: "set null", |
| 43 | }), |
| 44 | role: text("role |
| 45 | createdAt: timestamp("created_at |
| 46 | }, |
| 47 | (table) => |
| 48 | index("projectMembers_projectId_idx"). |
| 49 | index("projectMembers_orgId_idx"). |
| 50 | ] |
| 51 | ); |
| 52 | |
| 53 | /** |
| 54 | * Client parties (individual or team) attached to a project. |
| 55 | */ |
| 56 | export const projectClients = |
| 57 | "project_clients", |
| 58 | { |
| 59 | id: text("id |
| 60 | .primaryKey() |
| 61 | .$defaultFn(() => |
| 62 | projectId: text("project_id |
| 63 | .notNull() |
| 64 | .references(() => |
| 65 | clientUserId: text("client_user_id |
| 66 | onDelete: "set null", |
| 67 | }), |
| 68 | clientTeamId: text("client_team_id |
| 69 | onDelete: "set null", |
| 70 | }), |
| 71 | label: text("label |
| 72 | createdAt: timestamp("created_at |
| 73 | }, |
| 74 | (table) => |
| 75 | index("projectClients_projectId_idx"). |
| 76 | index("projectClients_clientUser_idx"). |
| 77 | index("projectClients_clientTeam_idx"). |
| 78 | ] |
| 79 | ); |
| 80 | |
| 81 | /** |
| 82 | * Requirements for a project, authored in markdown, with approval rule. |
| 83 | */ |
| 84 | export const requirements = |
| 85 | "requirements", |
| 86 | { |
| 87 | id: text("id |
| 88 | .primaryKey() |
| 89 | .$defaultFn(() => |
| 90 | projectId: text("project_id |
| 91 | .notNull() |
| 92 | .references(() => |
| 93 | title: text("title |
| 94 | body: text("body |
| 95 | approvalRule: text("approval_rule |
| 96 | status: text("status |
| 97 | createdAt: timestamp("created_at |
| 98 | updatedAt: timestamp("updated_at |
| 99 | createdByUserId: text("created_by_user_id |
| 100 | onDelete: "set null", |
| 101 | }), |
| 102 | }, |
| 103 | (table) => |
| 104 | ); |
| 105 | |
| 106 | /** |
| 107 | * Client recipients for a given requirement, tied to a project client party. |
| 108 | */ |
| 109 | export const requirementRecipients = |
| 110 | "requirement_recipients", |
| 111 | { |
| 112 | id: text("id |
| 113 | .primaryKey() |
| 114 | .$defaultFn(() => |
| 115 | requirementId: text("requirement_id |
| 116 | .notNull() |
| 117 | .references(() => |
| 118 | projectClientId: text("project_client_id |
| 119 | .notNull() |
| 120 | .references(() => |
| 121 | onDelete: "cascade", |
| 122 | }), |
| 123 | required: boolean("required |
| 124 | createdAt: timestamp("created_at |
| 125 | }, |
| 126 | (table) => |
| 127 | index("requirementRecipients_reqId_idx"). |
| 128 | index("requirementRecipients_client_idx"). |
| 129 | ] |
| 130 | ); |
| 131 | |
| 132 | /** |
| 133 | * Sign-off state for each requirement recipient. |
| 134 | */ |
| 135 | export const requirementSignoffs = |
| 136 | "requirement_signoffs", |
| 137 | { |
| 138 | id: text("id |
| 139 | .primaryKey() |
| 140 | .$defaultFn(() => |
| 141 | requirementId: text("requirement_id |
| 142 | .notNull() |
| 143 | .references(() => |
| 144 | recipientId: text("recipient_id |
| 145 | .notNull() |
| 146 | .references(() => |
| 147 | status: text("status |
| 148 | approvedAt: timestamp("approved_at |
| 149 | note: text("note |
| 150 | }, |
| 151 | (table) => |
| 152 | index("requirementSignoffs_reqId_idx"). |
| 153 | index("requirementSignoffs_recipientId_idx"). |
| 154 | ] |
| 155 | ); |
| 156 | |
| 157 | /** |
| 158 | * Discussion threads attached to a requirement. |
| 159 | */ |
| 160 | export const requirementThreads = |
| 161 | "requirement_threads", |
| 162 | { |
| 163 | id: text("id |
| 164 | .primaryKey() |
| 165 | .$defaultFn(() => |
| 166 | requirementId: text("requirement_id |
| 167 | .notNull() |
| 168 | .references(() => |
| 169 | createdByUserId: text("created_by_user_id |
| 170 | onDelete: "set null", |
| 171 | }), |
| 172 | createdAt: timestamp("created_at |
| 173 | }, |
| 174 | (table) => |
| 175 | index("requirementThreads_requirementId_idx"). |
| 176 | ] |
| 177 | ); |
| 178 | |
| 179 | /** |
| 180 | * Comments within requirement threads; supports replies via parentCommentId. |
| 181 | */ |
| 182 | export const requirementComments = |
| 183 | "requirement_comments", |
| 184 | { |
| 185 | id: text("id |
| 186 | .primaryKey() |
| 187 | .$defaultFn(() => |
| 188 | threadId: text("thread_id |
| 189 | .notNull() |
| 190 | .references(() => |
| 191 | authorUserId: text("author_user_id |
| 192 | onDelete: "set null", |
| 193 | }), |
| 194 | body: text("body |
| 195 | parentCommentId: text("parent_comment_id |
| 196 | createdAt: timestamp("created_at |
| 197 | updatedAt: timestamp("updated_at |
| 198 | }, |
| 199 | (table) => |
| 200 | threadIdx: index("requirementComments_threadId_idx |
| 201 | parentIdx: index("requirementComments_parentId_idx |
| 202 | table.parentCommentId |
| 203 | ), |
| 204 | }) |
| 205 | ); |