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