| 1 | import { and, asc, desc, eq, getTableColumns, inArray, not } from 'drizzle-orm' |
| 2 | import type { ReadonlyHeaders |
| 3 | import { getCachedActiveOrgMember } |
| 4 | import { db } |
| 5 | import { |
| 6 | media as mediaTable, |
| 7 | members as membersTable, |
| 8 | requirementChangeRequests as requirementChangeRequestsTable, |
| 9 | requirementRecipients, |
| 10 | requirementRecipients as requirementRecipientsTable, |
| 11 | requirementSignatures as requirementSignaturesTable, |
| 12 | requirements, |
| 13 | threadMessages as threadMessagesTable, |
| 14 | threads as threadsTable, |
| 15 | users, |
| 16 | } from '@/server/db/schema' |
| 17 | |
| 18 | export const REQUIREMENTS_CACHE_TAG = |
| 19 | |
| 20 | const listByProject = async |
| 21 | const activeMember = |
| 22 | if (activeMember.role |
| 23 | return db |
| 24 | .select(getTableColumns(requirements |
| 25 | .from(requirements) |
| 26 | .where( |
| 27 | and( |
| 28 | eq(requirements.projectId |
| 29 | not(eq(requirements |
| 30 | ) |
| 31 | ) |
| 32 | .innerJoin( |
| 33 | requirementRecipients, |
| 34 | and( |
| 35 | eq(requirementRecipients.requirementId |
| 36 | eq(requirementRecipients.clientMemberId |
| 37 | ) |
| 38 | ) |
| 39 | .orderBy(desc(requirements |
| 40 | } |
| 41 | return db |
| 42 | .select() |
| 43 | .from(requirements) |
| 44 | .where(eq(requirements |
| 45 | .orderBy(desc(requirements |
| 46 | } |
| 47 | |
| 48 | const getById = async |
| 49 | requirementId: string, |
| 50 | projectId: string, |
| 51 | headers: ReadonlyHeaders |
| 52 | ) => { |
| 53 | const [requirement] |
| 54 | .select() |
| 55 | .from(requirements) |
| 56 | .where( |
| 57 | and( |
| 58 | eq(requirements.id |
| 59 | eq(requirements.projectId |
| 60 | ) |
| 61 | ) |
| 62 | |
| 63 | if (!requirement) |
| 64 | return null |
| 65 | } |
| 66 | |
| 67 | const member = |
| 68 | if (member.role |
| 69 | return null |
| 70 | } |
| 71 | |
| 72 | return requirement |
| 73 | } |
| 74 | |
| 75 | const getBySlug = async |
| 76 | projectId: string, |
| 77 | slug: string, |
| 78 | headers: ReadonlyHeaders |
| 79 | ) => { |
| 80 | const activeMember = |
| 81 | let requirement: |
| 82 | if (activeMember.role |
| 83 | const requiredRequirements = |
| 84 | .select(getTableColumns(requirements |
| 85 | .from(requirements) |
| 86 | .where( |
| 87 | and(eq(requirements |
| 88 | ) |
| 89 | .innerJoin( |
| 90 | requirementRecipients, |
| 91 | and( |
| 92 | eq(requirementRecipients.requirementId |
| 93 | eq(requirementRecipients.clientMemberId |
| 94 | ) |
| 95 | ) |
| 96 | requirement = requiredRequirements[ |
| 97 | } else { |
| 98 | const requiredRequirements = |
| 99 | .select() |
| 100 | .from(requirements) |
| 101 | .where( |
| 102 | and(eq(requirements |
| 103 | ) |
| 104 | requirement = requiredRequirements[ |
| 105 | } |
| 106 | |
| 107 | return requirement ?? |
| 108 | } |
| 109 | |
| 110 | const getThreads = async |
| 111 | const rows = |
| 112 | .select() |
| 113 | .from(threadsTable) |
| 114 | .where( |
| 115 | and( |
| 116 | eq(threadsTable.projectId |
| 117 | eq(threadsTable.entityId |
| 118 | ) |
| 119 | ) |
| 120 | .orderBy(asc(threadsTable |
| 121 | |
| 122 | const threadIds = |
| 123 | if (threadIds.length |
| 124 | return [] |
| 125 | } |
| 126 | |
| 127 | const messages = |
| 128 | .select({ |
| 129 | id: threadMessagesTable.id |
| 130 | threadId: threadMessagesTable.threadId |
| 131 | authorMemberId: threadMessagesTable.authorMemberId |
| 132 | authorName: users.name |
| 133 | authorImage: users.image |
| 134 | body: threadMessagesTable.body |
| 135 | createdAt: threadMessagesTable.createdAt |
| 136 | }) |
| 137 | .from(threadMessagesTable) |
| 138 | .leftJoin( |
| 139 | membersTable, |
| 140 | eq(threadMessagesTable.authorMemberId |
| 141 | ) |
| 142 | .leftJoin(users, |
| 143 | .where(inArray(threadMessagesTable |
| 144 | .orderBy(asc(threadMessagesTable |
| 145 | |
| 146 | const messagesByThread = |
| 147 | for (const msg |
| 148 | const list = |
| 149 | list.push(msg |
| 150 | messagesByThread.set(msg |
| 151 | } |
| 152 | |
| 153 | // Get creator info |
| 154 | const creatorIds = |
| 155 | .map((t) |
| 156 | .filter((id) |
| 157 | const creators = |
| 158 | creatorIds.length > 0 |
| 159 | ? await db |
| 160 | .select({ |
| 161 | memberId: membersTable.id |
| 162 | name: users.name |
| 163 | image: users.image |
| 164 | }) |
| 165 | .from(membersTable) |
| 166 | .leftJoin(users, |
| 167 | .where(inArray(membersTable |
| 168 | : [] |
| 169 | |
| 170 | const creatorMap = |
| 171 | |
| 172 | return rows.map |
| 173 | const creator = |
| 174 | ? creatorMap.get( |
| 175 | : null |
| 176 | return { |
| 177 | id: t.id |
| 178 | selectedText: t.selectedText |
| 179 | status: t.status |
| 180 | createdByMemberId: t.createdByMemberId |
| 181 | createdByName: creator?.name |
| 182 | createdByImage: creator?.image |
| 183 | messages: messagesByThread.get |
| 184 | createdAt: t.createdAt |
| 185 | } |
| 186 | }) |
| 187 | } |
| 188 | |
| 189 | const getRecipients = async |
| 190 | return await db |
| 191 | .select() |
| 192 | .from(requirementRecipientsTable) |
| 193 | .where(eq(requirementRecipientsTable |
| 194 | } |
| 195 | |
| 196 | const getSignatures = async |
| 197 | return await db |
| 198 | .select({ |
| 199 | id: requirementSignaturesTable.id |
| 200 | requirementId: requirementSignaturesTable.requirementId |
| 201 | clientMemberId: requirementSignaturesTable.clientMemberId |
| 202 | signedAt: requirementSignaturesTable.signedAt |
| 203 | mediaId: requirementSignaturesTable.mediaId |
| 204 | mediaFileName: mediaTable.name |
| 205 | signerName: users.name |
| 206 | signerImage: users.image |
| 207 | }) |
| 208 | .from(requirementSignaturesTable) |
| 209 | .leftJoin(mediaTable, |
| 210 | .leftJoin( |
| 211 | membersTable, |
| 212 | eq(requirementSignaturesTable.clientMemberId |
| 213 | ) |
| 214 | .leftJoin(users, |
| 215 | .where(eq(requirementSignaturesTable |
| 216 | } |
| 217 | |
| 218 | const getChangeRequests = async |
| 219 | const rows = |
| 220 | .select({ |
| 221 | id: requirementChangeRequestsTable.id |
| 222 | description: requirementChangeRequestsTable.description |
| 223 | referencedThreadIds: requirementChangeRequestsTable.referencedThreadIds |
| 224 | status: requirementChangeRequestsTable.status |
| 225 | createdAt: requirementChangeRequestsTable.createdAt |
| 226 | resolvedAt: requirementChangeRequestsTable.resolvedAt |
| 227 | requestedByMemberId: requirementChangeRequestsTable.requestedByMemberId |
| 228 | requestedByName: users.name |
| 229 | requestedByImage: users.image |
| 230 | }) |
| 231 | .from(requirementChangeRequestsTable) |
| 232 | .leftJoin( |
| 233 | membersTable, |
| 234 | eq(requirementChangeRequestsTable.requestedByMemberId |
| 235 | ) |
| 236 | .leftJoin(users, |
| 237 | .where(eq(requirementChangeRequestsTable |
| 238 | .orderBy(desc(requirementChangeRequestsTable |
| 239 | |
| 240 | return rows |
| 241 | } |
| 242 | |
| 243 | export const getChangeRequestById = |
| 244 | changeRequestId: string, |
| 245 | requirementId: string |
| 246 | ) => { |
| 247 | const [changeRequest] |
| 248 | .select() |
| 249 | .from(requirementChangeRequestsTable) |
| 250 | .where( |
| 251 | and( |
| 252 | eq(requirementChangeRequestsTable.id |
| 253 | eq(requirementChangeRequestsTable.requirementId |
| 254 | ) |
| 255 | ) |
| 256 | |
| 257 | return changeRequest ?? |
| 258 | } |
| 259 | |
| 260 | export const getThreadById = |
| 261 | const [thread] |
| 262 | .select() |
| 263 | .from(threadsTable) |
| 264 | .where( |
| 265 | and(eq(threadsTable |
| 266 | ) |
| 267 | return thread ?? |
| 268 | } |
| 269 | |
| 270 | const getSignatureMediaForMember = async |
| 271 | return await db |
| 272 | .select({ |
| 273 | id: mediaTable.id |
| 274 | name: mediaTable.name |
| 275 | contentType: mediaTable.contentType |
| 276 | createdAt: mediaTable.createdAt |
| 277 | }) |
| 278 | .from(requirementSignaturesTable) |
| 279 | .innerJoin( |
| 280 | mediaTable, |
| 281 | eq(requirementSignaturesTable.mediaId |
| 282 | ) |
| 283 | .where(eq(requirementSignaturesTable |
| 284 | .groupBy(mediaTable.id |
| 285 | .orderBy(desc(mediaTable |
| 286 | } |
| 287 | |
| 288 | export const requirementsService = |
| 289 | listByProject, |
| 290 | getById, |
| 291 | getBySlug, |
| 292 | getThreads, |
| 293 | getRecipients, |
| 294 | getSignatures, |
| 295 | getSignatureMediaForMember, |
| 296 | getChangeRequests, |
| 297 | getChangeRequestById, |
| 298 | getThreadById, |
| 299 | } |