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