| 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 | proposalDeliverables, |
| 9 | proposalRecipients, |
| 10 | proposalSignatures, |
| 11 | proposals, |
| 12 | threadMessages, |
| 13 | threads, |
| 14 | users, |
| 15 | } from '@/server/db/schema' |
| 16 | |
| 17 | export const PROPOSALS_CACHE_TAG = |
| 18 | |
| 19 | const listByProject = async |
| 20 | const activeMember = |
| 21 | if (activeMember.role |
| 22 | return db |
| 23 | .select(getTableColumns(proposals |
| 24 | .from(proposals) |
| 25 | .where( |
| 26 | and( |
| 27 | eq(proposals.projectId |
| 28 | not(eq(proposals |
| 29 | ) |
| 30 | ) |
| 31 | .innerJoin( |
| 32 | proposalRecipients, |
| 33 | and( |
| 34 | eq(proposalRecipients.proposalId |
| 35 | eq(proposalRecipients.clientMemberId |
| 36 | ) |
| 37 | ) |
| 38 | .orderBy(desc(proposals |
| 39 | } |
| 40 | return db |
| 41 | .select() |
| 42 | .from(proposals) |
| 43 | .where(eq(proposals |
| 44 | .orderBy(desc(proposals |
| 45 | } |
| 46 | |
| 47 | const getBySlug = async |
| 48 | projectId: string, |
| 49 | slug: string, |
| 50 | headers: ReadonlyHeaders |
| 51 | ) => { |
| 52 | const activeMember = |
| 53 | let proposal: |
| 54 | if (activeMember.role |
| 55 | const proposalArray = |
| 56 | .select(getTableColumns(proposals |
| 57 | .from(proposals) |
| 58 | .where(and(eq |
| 59 | .innerJoin( |
| 60 | proposalRecipients, |
| 61 | and( |
| 62 | eq(proposalRecipients.proposalId |
| 63 | eq(proposalRecipients.clientMemberId |
| 64 | ) |
| 65 | ) |
| 66 | proposal = proposalArray[ |
| 67 | } else { |
| 68 | const proposalArray = |
| 69 | .select() |
| 70 | .from(proposals) |
| 71 | .where(and(eq |
| 72 | proposal = proposalArray[ |
| 73 | } |
| 74 | return proposal ?? |
| 75 | } |
| 76 | |
| 77 | const getDeliverables = async |
| 78 | return await db |
| 79 | .select() |
| 80 | .from(proposalDeliverables) |
| 81 | .where(eq(proposalDeliverables |
| 82 | .orderBy(asc(proposalDeliverables |
| 83 | } |
| 84 | |
| 85 | const getThreads = async |
| 86 | const rows = |
| 87 | .select() |
| 88 | .from(threads) |
| 89 | .where( |
| 90 | and(eq(threads |
| 91 | ) |
| 92 | .orderBy(asc(threads |
| 93 | |
| 94 | const threadIds = |
| 95 | if (threadIds.length |
| 96 | return [] |
| 97 | } |
| 98 | |
| 99 | const messages = |
| 100 | .select({ |
| 101 | id: threadMessages.id |
| 102 | threadId: threadMessages.threadId |
| 103 | authorMemberId: threadMessages.authorMemberId |
| 104 | authorName: users.name |
| 105 | authorImage: users.image |
| 106 | body: threadMessages.body |
| 107 | createdAt: threadMessages.createdAt |
| 108 | }) |
| 109 | .from(threadMessages) |
| 110 | .leftJoin(membersTable, |
| 111 | .leftJoin(users, |
| 112 | .where(inArray(threadMessages |
| 113 | .orderBy(asc(threadMessages |
| 114 | |
| 115 | const messagesByThread = |
| 116 | for (const msg |
| 117 | const list = |
| 118 | list.push(msg |
| 119 | messagesByThread.set(msg |
| 120 | } |
| 121 | |
| 122 | const creatorIds = |
| 123 | .map((t) |
| 124 | .filter((id) |
| 125 | const creators = |
| 126 | creatorIds.length > 0 |
| 127 | ? await db |
| 128 | .select({ |
| 129 | memberId: membersTable.id |
| 130 | name: users.name |
| 131 | image: users.image |
| 132 | }) |
| 133 | .from(membersTable) |
| 134 | .leftJoin(users, |
| 135 | .where(inArray(membersTable |
| 136 | : [] |
| 137 | |
| 138 | const creatorMap = |
| 139 | |
| 140 | return rows.map |
| 141 | const creator = |
| 142 | ? creatorMap.get( |
| 143 | : null |
| 144 | return { |
| 145 | id: t.id |
| 146 | selectedText: t.selectedText |
| 147 | status: t.status |
| 148 | createdByMemberId: t.createdByMemberId |
| 149 | createdByName: creator?.name |
| 150 | createdByImage: creator?.image |
| 151 | messages: messagesByThread.get |
| 152 | createdAt: t.createdAt |
| 153 | } |
| 154 | }) |
| 155 | } |
| 156 | const getRecipients = async |
| 157 | return await db |
| 158 | .select() |
| 159 | .from(proposalRecipients) |
| 160 | .where(eq(proposalRecipients |
| 161 | } |
| 162 | |
| 163 | const getSignatures = async |
| 164 | return await db |
| 165 | .select({ |
| 166 | id: proposalSignatures.id |
| 167 | proposalId: proposalSignatures.proposalId |
| 168 | clientMemberId: proposalSignatures.clientMemberId |
| 169 | signedAt: proposalSignatures.signedAt |
| 170 | mediaId: proposalSignatures.mediaId |
| 171 | signerName: users.name |
| 172 | signerImage: users.image |
| 173 | }) |
| 174 | .from(proposalSignatures) |
| 175 | .leftJoin(mediaTable, |
| 176 | .leftJoin( |
| 177 | membersTable, |
| 178 | eq(proposalSignatures.clientMemberId |
| 179 | ) |
| 180 | .leftJoin(users, |
| 181 | .where(eq(proposalSignatures |
| 182 | } |
| 183 | |
| 184 | const getSignatureMediaForMember = async |
| 185 | return await db |
| 186 | .select({ |
| 187 | id: mediaTable.id |
| 188 | name: mediaTable.name |
| 189 | contentType: mediaTable.contentType |
| 190 | createdAt: mediaTable.createdAt |
| 191 | }) |
| 192 | .from(proposalSignatures) |
| 193 | .innerJoin(mediaTable, |
| 194 | .where(eq(proposalSignatures |
| 195 | .groupBy(mediaTable.id |
| 196 | .orderBy(desc(mediaTable |
| 197 | } |
| 198 | |
| 199 | export const proposalsService = |
| 200 | listByProject, |
| 201 | getBySlug, |
| 202 | getThreads, |
| 203 | getRecipients, |
| 204 | getSignatures, |
| 205 | getSignatureMediaForMember, |
| 206 | getDeliverables, |
| 207 | } |