aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastien Castiel <sebastien@castiel.me>2024-01-15 11:44:49 -0500
committerSebastien Castiel <sebastien@castiel.me>2024-01-15 11:44:49 -0500
commit28902ad0eace265b207fea7f3328eeea8e018c5f (patch)
tree96ad4cd6a43685a76a18fc976c6c632df536e09f
parent8abdcb7d6f961bec867ab7e5b8ad3735ce9f9d34 (diff)
Export group expenses as JSON (Closes #42)
-rw-r--r--src/app/groups/[groupId]/expenses/export/json/route.ts39
-rw-r--r--src/app/groups/[groupId]/expenses/page.tsx11
2 files changed, 47 insertions, 3 deletions
diff --git a/src/app/groups/[groupId]/expenses/export/json/route.ts b/src/app/groups/[groupId]/expenses/export/json/route.ts
new file mode 100644
index 0000000..fe5be65
--- /dev/null
+++ b/src/app/groups/[groupId]/expenses/export/json/route.ts
@@ -0,0 +1,39 @@
+import { getPrisma } from '@/lib/prisma'
+import { NextResponse } from 'next/server'
+
+export async function GET(
+ req: Request,
+ { params: { groupId } }: { params: { groupId: string } },
+) {
+ console.log({ groupId })
+ const prisma = await getPrisma()
+ const group = await prisma.group.findUnique({
+ where: { id: groupId },
+ select: {
+ id: true,
+ name: true,
+ currency: true,
+ expenses: {
+ select: {
+ expenseDate: true,
+ title: true,
+ category: { select: { grouping: true, name: true } },
+ amount: true,
+ paidById: true,
+ paidFor: { select: { participantId: true, shares: true } },
+ isReimbursement: true,
+ splitMode: true,
+ },
+ },
+ participants: { select: { id: true, name: true } },
+ },
+ })
+ if (!group)
+ return NextResponse.json({ error: 'Invalid group ID' }, { status: 404 })
+ return NextResponse.json(group, {
+ headers: {
+ 'content-type': 'application/json',
+ 'content-disposition': `attachment; filename="${group.name}.json"`,
+ },
+ })
+}
diff --git a/src/app/groups/[groupId]/expenses/page.tsx b/src/app/groups/[groupId]/expenses/page.tsx
index c9b5a8a..bd4f600 100644
--- a/src/app/groups/[groupId]/expenses/page.tsx
+++ b/src/app/groups/[groupId]/expenses/page.tsx
@@ -10,7 +10,7 @@ import {
} from '@/components/ui/card'
import { Skeleton } from '@/components/ui/skeleton'
import { getGroup, getGroupExpenses } from '@/lib/api'
-import { Plus } from 'lucide-react'
+import { Download, DownloadCloud, Plus } from 'lucide-react'
import { Metadata } from 'next'
import Link from 'next/link'
import { notFound } from 'next/navigation'
@@ -38,10 +38,15 @@ export default async function GroupExpensesPage({
Here are the expenses that you created for your group.
</CardDescription>
</CardHeader>
- <CardHeader className="p-4 sm:p-6">
+ <CardHeader className="p-4 sm:p-6 flex flex-row space-y-0 gap-2">
+ <Button variant="secondary" size="icon" asChild>
+ <Link href={`/groups/${groupId}/expenses/export/json`} target="_blank">
+ <Download className="w-4 h-4" />
+ </Link>
+ </Button>
<Button asChild size="icon">
<Link href={`/groups/${groupId}/expenses/create`}>
- <Plus />
+ <Plus className="w-4 h-4" />
</Link>
</Button>
</CardHeader>