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