| 1 | import { and, asc, desc, eq, inArray, isNull, or } from 'drizzle-orm' |
| 2 | import type { ReadonlyHeaders |
| 3 | import { getCachedActiveOrgMember } |
| 4 | import { db } |
| 5 | import { |
| 6 | expenseCategories, |
| 7 | expenseRecipients, |
| 8 | expenses, |
| 9 | invoiceExpenses, |
| 10 | media, |
| 11 | members, |
| 12 | users, |
| 13 | } from '@/server/db/schema' |
| 14 | import { authService } |
| 15 | |
| 16 | const listByProject = async |
| 17 | const activeMember = |
| 18 | |
| 19 | const baseQuery = |
| 20 | .select({ |
| 21 | id: expenses.id |
| 22 | projectId: expenses.projectId |
| 23 | memberId: expenses.memberId |
| 24 | categoryId: expenses.categoryId |
| 25 | milestoneId: expenses.milestoneId |
| 26 | description: expenses.description |
| 27 | title: expenses.title |
| 28 | amountCents: expenses.amountCents |
| 29 | currency: expenses.currency |
| 30 | date: expenses.date |
| 31 | billable: expenses.billable |
| 32 | recurring: expenses.recurring |
| 33 | status: expenses.status |
| 34 | rejectReason: expenses.rejectReason |
| 35 | receiptMediaId: expenses.receiptMediaId |
| 36 | receiptContentType: media.contentType |
| 37 | createdAt: expenses.createdAt |
| 38 | updatedAt: expenses.updatedAt |
| 39 | memberName: users.name |
| 40 | memberEmail: users.email |
| 41 | categoryName: expenseCategories.name |
| 42 | categoryColor: expenseCategories.color |
| 43 | }) |
| 44 | .from(expenses) |
| 45 | .leftJoin(members, |
| 46 | .leftJoin(users, |
| 47 | .leftJoin(expenseCategories, |
| 48 | .leftJoin(media, |
| 49 | |
| 50 | if (activeMember?.role |
| 51 | return baseQuery |
| 52 | .innerJoin( |
| 53 | expenseRecipients, |
| 54 | and( |
| 55 | eq(expenses.id |
| 56 | eq(expenseRecipients.clientMemberId |
| 57 | ) |
| 58 | ) |
| 59 | .where( |
| 60 | and( |
| 61 | eq(expenses.projectId |
| 62 | inArray(expenses.status |
| 63 | 'client_accepted', |
| 64 | 'client_rejected', |
| 65 | 'submitted_to_client', |
| 66 | ]) |
| 67 | ) |
| 68 | ) |
| 69 | .orderBy(desc(expenses |
| 70 | } |
| 71 | if (activeMember?.role |
| 72 | return baseQuery |
| 73 | .where( |
| 74 | and( |
| 75 | eq(expenses.projectId |
| 76 | eq(expenses.memberId |
| 77 | ) |
| 78 | ) |
| 79 | .orderBy(desc(expenses |
| 80 | } |
| 81 | return baseQuery |
| 82 | .where(eq(expenses |
| 83 | .orderBy(desc(expenses |
| 84 | } |
| 85 | |
| 86 | const listCategoriesByOrg = async |
| 87 | await db |
| 88 | .select() |
| 89 | .from(expenseCategories) |
| 90 | .where(eq(expenseCategories |
| 91 | .orderBy(asc(expenseCategories |
| 92 | |
| 93 | const listUnpaidExpensesByProject = async |
| 94 | organizationId: string, |
| 95 | projectId: string, |
| 96 | currentUserId: string |
| 97 | ) => { |
| 98 | const auth = |
| 99 | organizationId, |
| 100 | projectId, |
| 101 | currentUserId |
| 102 | ) |
| 103 | if (!auth.success |
| 104 | return [] |
| 105 | } |
| 106 | const rows = |
| 107 | .selectDistinctOn([expenses.id |
| 108 | .from(expenses) |
| 109 | .leftJoin(invoiceExpenses, |
| 110 | .where( |
| 111 | and( |
| 112 | eq(expenses.projectId |
| 113 | eq(expenses.status |
| 114 | or(isNull(invoiceExpenses |
| 115 | ) |
| 116 | ) |
| 117 | |
| 118 | return rows.map |
| 119 | } |
| 120 | |
| 121 | const listExpensesByInvoiceId = async |
| 122 | await db |
| 123 | .select({ expense: |
| 124 | .from(invoiceExpenses) |
| 125 | .innerJoin(expenses, |
| 126 | .where(eq(invoiceExpenses |
| 127 | .then((rows) |
| 128 | |
| 129 | const listByProjectIds = async |
| 130 | if (projectIds.length |
| 131 | return [] |
| 132 | } |
| 133 | |
| 134 | return await db |
| 135 | .select() |
| 136 | .from(expenses) |
| 137 | .where(inArray(expenses |
| 138 | .orderBy(desc(expenses |
| 139 | } |
| 140 | |
| 141 | const getRecipientsByExpenseIds = async |
| 142 | if (expenseIds.length |
| 143 | return [] |
| 144 | } |
| 145 | return await db |
| 146 | .select({ |
| 147 | id: expenseRecipients.id |
| 148 | expenseId: expenseRecipients.expenseId |
| 149 | clientMemberId: expenseRecipients.clientMemberId |
| 150 | status: expenseRecipients.status |
| 151 | rejectReason: expenseRecipients.rejectReason |
| 152 | respondedAt: expenseRecipients.respondedAt |
| 153 | clientName: users.name |
| 154 | clientEmail: users.email |
| 155 | }) |
| 156 | .from(expenseRecipients) |
| 157 | .innerJoin(members, |
| 158 | .innerJoin(users, |
| 159 | .where(inArray(expenseRecipients |
| 160 | } |
| 161 | |
| 162 | export const expensesServices = |
| 163 | getRecipientsByExpenseIds, |
| 164 | listByProject, |
| 165 | listByProjectIds, |
| 166 | listCategoriesByOrg, |
| 167 | listExpensesByInvoiceId, |
| 168 | listUnpaidExpensesByProject, |
| 169 | } |