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