Reimbursements

This commit is contained in:
Sebastien Castiel
2023-12-06 20:21:26 -05:00
parent 6ce2329f5c
commit 57899b0160
9 changed files with 62 additions and 14 deletions

View File

@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Expense" ADD COLUMN "isReimbursement" BOOLEAN NOT NULL DEFAULT false;

View File

@@ -29,14 +29,15 @@ model Participant {
} }
model Expense { model Expense {
id String @id id String @id
group Group @relation(fields: [groupId], references: [id]) group Group @relation(fields: [groupId], references: [id])
title String title String
amount Int amount Int
paidBy Participant @relation(fields: [paidById], references: [id]) paidBy Participant @relation(fields: [paidById], references: [id])
paidById String paidById String
paidFor ExpensePaidFor[] paidFor ExpensePaidFor[]
groupId String groupId String
isReimbursement Boolean @default(false)
} }
model ExpensePaidFor { model ExpensePaidFor {

View File

@@ -22,7 +22,6 @@ export default async function GroupPage({
const expenses = await getGroupExpenses(groupId) const expenses = await getGroupExpenses(groupId)
const balances = getBalances(expenses) const balances = getBalances(expenses)
const reimbursements = getSuggestedReimbursements(balances) const reimbursements = getSuggestedReimbursements(balances)
console.log(reimbursements)
return ( return (
<> <>
@@ -54,6 +53,7 @@ export default async function GroupPage({
reimbursements={reimbursements} reimbursements={reimbursements}
participants={group.participants} participants={group.participants}
currency={group.currency} currency={group.currency}
groupId={groupId}
/> />
</CardContent> </CardContent>
</Card> </Card>

View File

@@ -16,6 +16,7 @@ import {
TableRow, TableRow,
} from '@/components/ui/table' } from '@/components/ui/table'
import { getGroup, getGroupExpenses } from '@/lib/api' import { getGroup, getGroupExpenses } from '@/lib/api'
import { cn } from '@/lib/utils'
import { ChevronRight, Plus } from 'lucide-react' import { ChevronRight, Plus } from 'lucide-react'
import Link from 'next/link' import Link from 'next/link'
import { notFound } from 'next/navigation' import { notFound } from 'next/navigation'
@@ -62,7 +63,10 @@ export default async function GroupExpensesPage({
</TableHeader> </TableHeader>
<TableBody> <TableBody>
{expenses.map((expense) => ( {expenses.map((expense) => (
<TableRow key={expense.id}> <TableRow
key={expense.id}
className={cn(expense.isReimbursement && 'italic')}
>
<TableCell>{expense.title}</TableCell> <TableCell>{expense.title}</TableCell>
<TableCell> <TableCell>
<Badge variant="secondary"> <Badge variant="secondary">

View File

@@ -1,16 +1,20 @@
import { Button } from '@/components/ui/button'
import { Reimbursement } from '@/lib/balances' import { Reimbursement } from '@/lib/balances'
import { Participant } from '@prisma/client' import { Participant } from '@prisma/client'
import Link from 'next/link'
type Props = { type Props = {
reimbursements: Reimbursement[] reimbursements: Reimbursement[]
participants: Participant[] participants: Participant[]
currency: string currency: string
groupId: string
} }
export function ReimbursementList({ export function ReimbursementList({
reimbursements, reimbursements,
participants, participants,
currency, currency,
groupId,
}: Props) { }: Props) {
const getParticipant = (id: string) => participants.find((p) => p.id === id) const getParticipant = (id: string) => participants.find((p) => p.id === id)
return ( return (
@@ -20,6 +24,13 @@ export function ReimbursementList({
<div> <div>
<strong>{getParticipant(reimbursement.from)?.name}</strong> owes{' '} <strong>{getParticipant(reimbursement.from)?.name}</strong> owes{' '}
<strong>{getParticipant(reimbursement.to)?.name}</strong> <strong>{getParticipant(reimbursement.to)?.name}</strong>
<Button variant="link" asChild className="-my-3">
<Link
href={`/groups/${groupId}/expenses/create?reimbursement=yes&from=${reimbursement.from}&to=${reimbursement.to}&amount=${reimbursement.amount}`}
>
Mark as paid
</Link>
</Button>
</div> </div>
<div> <div>
{currency} {reimbursement.amount.toFixed(2)} {currency} {reimbursement.amount.toFixed(2)}

View File

@@ -29,6 +29,7 @@ import {
import { getExpense, getGroup } from '@/lib/api' import { getExpense, getGroup } from '@/lib/api'
import { ExpenseFormValues, expenseFormSchema } from '@/lib/schemas' import { ExpenseFormValues, expenseFormSchema } from '@/lib/schemas'
import { zodResolver } from '@hookform/resolvers/zod' import { zodResolver } from '@hookform/resolvers/zod'
import { useSearchParams } from 'next/navigation'
import { useForm } from 'react-hook-form' import { useForm } from 'react-hook-form'
export type Props = { export type Props = {
@@ -40,6 +41,7 @@ export type Props = {
export function ExpenseForm({ group, expense, onSubmit, onDelete }: Props) { export function ExpenseForm({ group, expense, onSubmit, onDelete }: Props) {
const isCreate = expense === undefined const isCreate = expense === undefined
const searchParams = useSearchParams()
const form = useForm<ExpenseFormValues>({ const form = useForm<ExpenseFormValues>({
resolver: zodResolver(expenseFormSchema), resolver: zodResolver(expenseFormSchema),
defaultValues: expense defaultValues: expense
@@ -48,8 +50,17 @@ export function ExpenseForm({ group, expense, onSubmit, onDelete }: Props) {
amount: expense.amount, amount: expense.amount,
paidBy: expense.paidById, paidBy: expense.paidById,
paidFor: expense.paidFor.map(({ participantId }) => participantId), paidFor: expense.paidFor.map(({ participantId }) => participantId),
isReimbursement: expense.isReimbursement,
} }
: { title: '', amount: 0, paidFor: [] }, : searchParams.get('reimbursement')
? {
title: 'Reimbursement',
amount: Number(searchParams.get('amount')) || 0,
paidBy: searchParams.get('from') ?? undefined,
paidFor: [searchParams.get('to') ?? undefined],
isReimbursement: true,
}
: { title: '', amount: 0, paidFor: [], isReimbursement: false },
}) })
return ( return (
@@ -131,8 +142,25 @@ export function ExpenseForm({ group, expense, onSubmit, onDelete }: Props) {
/> />
</FormControl> </FormControl>
</div> </div>
<FormDescription>Enter the expense amount.</FormDescription>
<FormMessage /> <FormMessage />
<FormField
control={form.control}
name="isReimbursement"
render={({ field }) => (
<FormItem className="flex flex-row gap-2 items-center space-y-0 pt-2">
<FormControl>
<Checkbox
checked={field.value}
onCheckedChange={field.onChange}
/>
</FormControl>
<div>
<FormLabel>This is a reimbursement</FormLabel>
</div>
</FormItem>
)}
/>
</FormItem> </FormItem>
)} )}
/> />
@@ -141,7 +169,7 @@ export function ExpenseForm({ group, expense, onSubmit, onDelete }: Props) {
control={form.control} control={form.control}
name="paidFor" name="paidFor"
render={() => ( render={() => (
<FormItem className="order-4"> <FormItem className="order-5">
<div className="mb-4"> <div className="mb-4">
<FormLabel>Paid for</FormLabel> <FormLabel>Paid for</FormLabel>
<FormDescription> <FormDescription>

View File

@@ -53,6 +53,7 @@ export async function createExpense(
})), })),
}, },
}, },
isReimbursement: expenseFormValues.isReimbursement,
}, },
}) })
} }
@@ -105,6 +106,7 @@ export async function updateExpense(
), ),
), ),
}, },
isReimbursement: expenseFormValues.isReimbursement,
}, },
}) })
} }

View File

@@ -54,7 +54,6 @@ export function getSuggestedReimbursements(
([participantId, { total }]) => ({ participantId, total }), ([participantId, { total }]) => ({ participantId, total }),
) )
balancesArray.sort((b1, b2) => b2.total - b1.total) balancesArray.sort((b1, b2) => b2.total - b1.total)
console.log(balancesArray)
const reimbursements: Reimbursement[] = [] const reimbursements: Reimbursement[] = []
while (balancesArray.length > 1) { while (balancesArray.length > 1) {
const first = balancesArray[0] const first = balancesArray[0]

View File

@@ -49,6 +49,7 @@ export const expenseFormSchema = z.object({
paidFor: z paidFor: z
.array(z.string()) .array(z.string())
.min(1, 'The expense must be paid for at least 1 participant.'), .min(1, 'The expense must be paid for at least 1 participant.'),
isReimbursement: z.boolean(),
}) })
export type ExpenseFormValues = z.infer<typeof expenseFormSchema> export type ExpenseFormValues = z.infer<typeof expenseFormSchema>