Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add export to CSV support #292

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions messages/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
"create": "Create expense",
"createFirst": "Create the first one",
"noExpenses": "Your group doesn’t contain any expense yet.",
"export": "Export",
"exportJson": "Export to JSON",
"exportCsv": "Export to CSV",
"searchPlaceholder": "Search for an expense…",
"ActiveUserModal": {
"title": "Who are you?",
Expand Down
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dependencies": {
"@formatjs/intl-localematcher": "^0.5.4",
"@hookform/resolvers": "^3.3.2",
"@json2csv/plainjs": "^7.0.6",
"@prisma/client": "^5.6.0",
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-collapsible": "^1.0.3",
Expand Down
142 changes: 142 additions & 0 deletions src/app/groups/[groupId]/expenses/export/csv/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { Parser } from '@json2csv/plainjs'
import { PrismaClient } from '@prisma/client'
import contentDisposition from 'content-disposition'
import { NextResponse } from 'next/server'

const splitModeLabel = {
EVENLY: 'Evenly',
BY_SHARES: 'Unevenly – By shares',
BY_PERCENTAGE: 'Unevenly – By percentage',
BY_AMOUNT: 'Unevenly – By amount',
}

function formatDate(isoDateString: Date): string {
const date = new Date(isoDateString)
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0') // Months are zero-based
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}` // YYYY-MM-DD format
}

const prisma = new PrismaClient()

export async function GET(
req: Request,
{ params: { groupId } }: { params: { groupId: string } },
) {
const group = await prisma.group.findUnique({
where: { id: groupId },
select: {
id: true,
name: true,
currency: true,
expenses: {
select: {
expenseDate: true,
title: true,
category: { select: { 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 })
}

/*

CSV Structure:

--------------------------------------------------------------
| Date | Description | Category | Currency | Cost
--------------------------------------------------------------
| Is Reimbursement | Split mode | UserA | UserB
--------------------------------------------------------------

Columns:
- Date: The date of the expense.
- Description: A brief description of the expense.
- Category: The category of the expense (e.g., Food, Travel, etc.).
- Currency: The currency in which the expense is recorded.
- Cost: The amount spent.
- Is Reimbursement: Whether the expense is a reimbursement or not.
- Split mode: The method used to split the expense (e.g., Evenly, By shares, By percentage, By amount).
- UserA, UserB: User-specific data or balances (e.g., amount owed or contributed by each user).

Example Row:
------------------------------------------------------------------------------------------
| 2025-01-06 | Dinner with team | Food | ₹ | 5000 | No | Evenly | John | Jane
------------------------------------------------------------------------------------------

*/

const fields = [
{ label: 'Date', value: 'date' },
{ label: 'Description', value: 'title' },
{ label: 'Category', value: 'categoryName' },
{ label: 'Currency', value: 'currency' },
{ label: 'Cost', value: 'amount' },
{ label: 'Is Reimbursement', value: 'isReimbursement' },
{ label: 'Split mode', value: 'splitMode' },
...group.participants.map((participant) => ({
label: participant.name,
value: participant.name,
})),
]

const expenses = group.expenses.map((expense) => ({
date: formatDate(expense.expenseDate),
title: expense.title,
categoryName: expense.category?.name || '',
currency: group.currency,
amount: (expense.amount / 100).toFixed(2),
isReimbursement: expense.isReimbursement ? 'Yes' : 'No',
splitMode: splitModeLabel[expense.splitMode],
...Object.fromEntries(
group.participants.map((participant) => {
const { totalShares, participantShare } = expense.paidFor.reduce(
(acc, { participantId, shares }) => {
acc.totalShares += shares
if (participantId === participant.id) {
acc.participantShare = shares
}
return acc
},
{ totalShares: 0, participantShare: 0 },
)

const isPaidByParticipant = expense.paidById === participant.id
const participantAmountShare = +(
((expense.amount / totalShares) * participantShare) /
100
).toFixed(2)

return [
participant.name,
participantAmountShare * (isPaidByParticipant ? 1 : -1),
]
}),
),
}))

const json2csvParser = new Parser({ fields })
const csv = json2csvParser.parse(expenses)

const date = new Date().toISOString().split('T')[0]
const filename = `Spliit Export - ${group.name} - ${date}.csv`

// \uFEFF character is added at the beginning of the CSV content to ensure that it is interpreted as UTF-8 with BOM (Byte Order Mark), which helps some applications correctly interpret the encoding.
return new NextResponse(`\uFEFF${csv}`, {
headers: {
'Content-Type': 'text/csv; charset=utf-8',
'Content-Disposition': contentDisposition(filename),
},
})
}
14 changes: 3 additions & 11 deletions src/app/groups/[groupId]/expenses/page.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { ActiveUserModal } from '@/app/groups/[groupId]/expenses/active-user-modal'
import { CreateFromReceiptButton } from '@/app/groups/[groupId]/expenses/create-from-receipt-button'
import { ExpenseList } from '@/app/groups/[groupId]/expenses/expense-list'
import ExportButton from '@/app/groups/[groupId]/export-button'
import { Button } from '@/components/ui/button'
import {
Card,
Expand All @@ -11,7 +12,7 @@ import {
CardHeader,
CardTitle,
} from '@/components/ui/card'
import { Download, Plus } from 'lucide-react'
import { Plus } from 'lucide-react'
import { Metadata } from 'next'
import { useTranslations } from 'next-intl'
import Link from 'next/link'
Expand Down Expand Up @@ -40,16 +41,7 @@ export default function GroupExpensesPageClient({
<CardDescription>{t('description')}</CardDescription>
</CardHeader>
<CardHeader className="p-4 sm:p-6 flex flex-row space-y-0 gap-2">
<Button variant="secondary" size="icon" asChild>
<Link
prefetch={false}
href={`/groups/${groupId}/expenses/export/json`}
target="_blank"
title={t('exportJson')}
>
<Download className="w-4 h-4" />
</Link>
</Button>
<ExportButton groupId={groupId} />
{enableReceiptExtract && <CreateFromReceiptButton />}
<Button asChild size="icon">
<Link
Expand Down
52 changes: 52 additions & 0 deletions src/app/groups/[groupId]/export-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use client'

import { Button } from '@/components/ui/button'
import {
Popover,
PopoverContent,
PopoverTrigger,
} from '@/components/ui/popover'
import { Download, FileDown, FileJson } from 'lucide-react'
import { useTranslations } from 'next-intl'
import Link from 'next/link'

export default function ExportButton({ groupId }: { groupId: string }) {
const t = useTranslations('Expenses')
return (
<Popover>
<PopoverTrigger asChild>
<Button title={t('export')} variant="secondary" size="icon">
<Download className="w-4 h-4" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-48 flex flex-col gap-3">
<Button variant="ghost" asChild>
<Link
prefetch={false}
href={`/groups/${groupId}/expenses/export/json`}
target="_blank"
title={t('exportJson')}
>
<div className="flex items-center gap-2">
<FileJson className="w-4 h-4" />
<p>{t('exportJson')}</p>
</div>
</Link>
</Button>
<Button variant="ghost" asChild>
<Link
prefetch={false}
href={`/groups/${groupId}/expenses/export/csv`}
target="_blank"
title={t('exportCsv')}
>
<div className="flex items-center gap-2">
<FileDown className="w-4 h-4" />
<p>{t('exportCsv')}</p>
</div>
</Link>
</Button>
</PopoverContent>
</Popover>
)
}
Loading