| 1 | import { |
| 2 | and, |
| 3 | asc, |
| 4 | count, |
| 5 | desc, |
| 6 | eq, |
| 7 | getTableColumns, |
| 8 | inArray, |
| 9 | or, |
| 10 | } from 'drizzle-orm' |
| 11 | import type { ReadonlyHeaders |
| 12 | import { getCachedActiveOrgMember } |
| 13 | |
| 14 | import { db } |
| 15 | import { |
| 16 | invoiceItems, |
| 17 | invoiceRecipients, |
| 18 | invoiceRequirements, |
| 19 | invoices, |
| 20 | media, |
| 21 | members, |
| 22 | projectClientAssignments, |
| 23 | projects, |
| 24 | requirements, |
| 25 | users, |
| 26 | } from '@/server/db/schema' |
| 27 | |
| 28 | export type InvoiceWithMedia |
| 29 | senderLogoObject?: typeof |
| 30 | senderSignatureObject?: typeof |
| 31 | } |
| 32 | |
| 33 | const listByProject = async |
| 34 | const activeMember = |
| 35 | let invoicesList: |
| 36 | if (activeMember?.role |
| 37 | invoicesList = await |
| 38 | .select(getTableColumns(invoices |
| 39 | .from(invoices) |
| 40 | .where( |
| 41 | and( |
| 42 | eq(invoices.projectId |
| 43 | inArray(invoices.status |
| 44 | ) |
| 45 | ) |
| 46 | .innerJoin( |
| 47 | invoiceRecipients, |
| 48 | and( |
| 49 | eq(invoiceRecipients.invoiceId |
| 50 | eq(invoiceRecipients.memberId |
| 51 | ) |
| 52 | ) |
| 53 | .orderBy(desc(invoices |
| 54 | } else if (activeMember |
| 55 | // Members see all client-recipient invoices in the project, plus |
| 56 | // member-recipient invoices addressed to them. |
| 57 | const memberRecipientInvoiceIds = |
| 58 | .select({ id: |
| 59 | .from(invoiceRecipients) |
| 60 | .where(eq(invoiceRecipients |
| 61 | invoicesList = await |
| 62 | .select() |
| 63 | .from(invoices) |
| 64 | .where( |
| 65 | and( |
| 66 | eq(invoices.projectId |
| 67 | or( |
| 68 | eq(invoices.recipient |
| 69 | inArray(invoices.id |
| 70 | ) |
| 71 | ) |
| 72 | ) |
| 73 | .orderBy(desc(invoices |
| 74 | } else { |
| 75 | invoicesList = await |
| 76 | .select() |
| 77 | .from(invoices) |
| 78 | .where(eq(invoices |
| 79 | .orderBy(desc(invoices |
| 80 | } |
| 81 | |
| 82 | const withRecipients = |
| 83 | invoicesList.map(async |
| 84 | const recipients = |
| 85 | .select({ |
| 86 | assignmentId: projectClientAssignments.id |
| 87 | memberId: invoiceRecipients.memberId |
| 88 | userName: users.name |
| 89 | userEmail: users.email |
| 90 | assignedAt: members.createdAt |
| 91 | userId: members.userId |
| 92 | userImage: users.image |
| 93 | }) |
| 94 | .from(invoiceRecipients) |
| 95 | .innerJoin(members, |
| 96 | .innerJoin(users, |
| 97 | .innerJoin( |
| 98 | projectClientAssignments, |
| 99 | and( |
| 100 | eq(projectClientAssignments.memberId |
| 101 | eq(projectClientAssignments.projectId |
| 102 | ) |
| 103 | ) |
| 104 | .where(eq(invoiceRecipients |
| 105 | |
| 106 | return { |
| 107 | ...invoice, |
| 108 | recipients, |
| 109 | } |
| 110 | }) |
| 111 | ) |
| 112 | |
| 113 | return withRecipients |
| 114 | } |
| 115 | |
| 116 | const getById = async |
| 117 | invoiceId, |
| 118 | organizationId, |
| 119 | projectId, |
| 120 | headers, |
| 121 | }: { |
| 122 | invoiceId: string |
| 123 | projectId: string |
| 124 | organizationId: string |
| 125 | headers: ReadonlyHeaders |
| 126 | }) => { |
| 127 | const [project] |
| 128 | .select() |
| 129 | .from(projects) |
| 130 | .where( |
| 131 | and( |
| 132 | eq(projects.id |
| 133 | eq(projects.organizationId |
| 134 | ) |
| 135 | ) |
| 136 | if (!project) |
| 137 | return null |
| 138 | } |
| 139 | |
| 140 | const activeMember = |
| 141 | let invoice: |
| 142 | if (activeMember?.role |
| 143 | const [row] |
| 144 | .select(getTableColumns(invoices |
| 145 | .from(invoices) |
| 146 | .where( |
| 147 | and(eq(invoices |
| 148 | ) |
| 149 | .innerJoin( |
| 150 | invoiceRecipients, |
| 151 | and( |
| 152 | eq(invoiceRecipients.invoiceId |
| 153 | eq(invoiceRecipients.memberId |
| 154 | ) |
| 155 | ) |
| 156 | .limit(1) |
| 157 | invoice = (row |
| 158 | } else if (activeMember |
| 159 | // Members can view any client-recipient invoice in the project, but |
| 160 | // only member-recipient invoices that are addressed to them. |
| 161 | const candidate = |
| 162 | where: and( |
| 163 | eq(invoices.id |
| 164 | eq(invoices.projectId |
| 165 | ), |
| 166 | })) as unknown |
| 167 | if (candidate?.recipient |
| 168 | const [recipientRow] |
| 169 | .select({ id: |
| 170 | .from(invoiceRecipients) |
| 171 | .where( |
| 172 | and( |
| 173 | eq(invoiceRecipients.invoiceId |
| 174 | eq(invoiceRecipients.memberId |
| 175 | ) |
| 176 | ) |
| 177 | .limit(1) |
| 178 | invoice = recipientRow |
| 179 | } else { |
| 180 | invoice = candidate |
| 181 | } |
| 182 | } else { |
| 183 | invoice = (await |
| 184 | where: and( |
| 185 | eq(invoices.id |
| 186 | eq(invoices.projectId |
| 187 | ), |
| 188 | })) as unknown |
| 189 | } |
| 190 | if (invoice) { |
| 191 | if (invoice.senderLogo |
| 192 | invoice.senderLogoObject = |
| 193 | where: eq(media |
| 194 | }) |
| 195 | } |
| 196 | if (invoice.senderSignature |
| 197 | invoice.senderSignatureObject = |
| 198 | where: eq(media |
| 199 | }) |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return invoice ?? |
| 204 | } |
| 205 | |
| 206 | const getRecipients = async |
| 207 | await db |
| 208 | .select({ |
| 209 | id: invoiceRecipients.id |
| 210 | memberId: invoiceRecipients.memberId |
| 211 | userName: users.name |
| 212 | userEmail: users.email |
| 213 | }) |
| 214 | .from(invoiceRecipients) |
| 215 | .innerJoin(members, |
| 216 | .innerJoin(users, |
| 217 | .where(eq(invoiceRecipients |
| 218 | |
| 219 | const getItems = async |
| 220 | await db |
| 221 | .select() |
| 222 | .from(invoiceItems) |
| 223 | .where(eq(invoiceItems |
| 224 | .orderBy(asc(invoiceItems |
| 225 | |
| 226 | const getLinkedRequirements = async |
| 227 | await db |
| 228 | .select({ |
| 229 | id: invoiceRequirements.id |
| 230 | requirementId: requirements.id |
| 231 | title: requirements.title |
| 232 | slug: requirements.slug |
| 233 | status: requirements.status |
| 234 | }) |
| 235 | .from(invoiceRequirements) |
| 236 | .innerJoin( |
| 237 | requirements, |
| 238 | eq(invoiceRequirements.requirementId |
| 239 | ) |
| 240 | .where(eq(invoiceRequirements |
| 241 | |
| 242 | const listByProjectIds = async |
| 243 | projectIds: string[] |
| 244 | opts: { |
| 245 | ) => { |
| 246 | if (projectIds.length |
| 247 | return [] |
| 248 | } |
| 249 | |
| 250 | const whereClause = |
| 251 | ? and( |
| 252 | inArray(invoices.projectId |
| 253 | inArray(invoices.status |
| 254 | ) |
| 255 | : inArray(invoices. |
| 256 | |
| 257 | return await db |
| 258 | .select() |
| 259 | .from(invoices) |
| 260 | .where(whereClause) |
| 261 | .orderBy(desc(invoices |
| 262 | } |
| 263 | |
| 264 | const getNextSequence = async |
| 265 | const [row] |
| 266 | .select({ value: |
| 267 | .from(invoices) |
| 268 | .where(eq(invoices |
| 269 | return (row?.value |
| 270 | } |
| 271 | |
| 272 | export const invoicesService = |
| 273 | listByProject, |
| 274 | listByProjectIds, |
| 275 | getById, |
| 276 | getRecipients, |
| 277 | getItems, |
| 278 | getLinkedRequirements, |
| 279 | getNextSequence, |
| 280 | } |