-
Notifications
You must be signed in to change notification settings - Fork 4
Feat: ecommerce sales and user's receipts #155
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
Merged
bassgeta
merged 6 commits into
feat/151-client-id-management
from
feat/152-receipts-and-sales
Oct 3, 2025
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e05fd96
feat: add client payment schema and webhook handling of payments
bassgeta e920d26
feat: implement ecommerce sales page
bassgeta 77a7575
feat: scaffold fetching of user receipts
bassgeta 3dd4070
refactor: use the DB ecommerce client id instead of the API one for c…
bassgeta a41867b
feat: implement table of receipts
bassgeta 54d39a3
feat: code review align and generate migration
bassgeta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { DashboardReceipts } from "@/components/dashboard/receipts"; | ||
import { getCurrentSession } from "@/server/auth"; | ||
import { api } from "@/trpc/server"; | ||
import { redirect } from "next/navigation"; | ||
|
||
export default async function ReceiptsPage() { | ||
const { user } = await getCurrentSession(); | ||
|
||
if (!user) { | ||
redirect("/"); | ||
} | ||
|
||
const clientPayments = await api.ecommerce.getAllUserReceipts.query(); | ||
|
||
return <DashboardReceipts initialClientPayments={clientPayments} />; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
"use client"; | ||
|
||
import { Card, CardContent } from "@/components/ui/card"; | ||
import { | ||
Select, | ||
SelectContent, | ||
SelectItem, | ||
SelectTrigger, | ||
SelectValue, | ||
} from "@/components/ui/select"; | ||
import { EmptyState } from "@/components/ui/table/empty-state"; | ||
import { Pagination } from "@/components/ui/table/pagination"; | ||
import { | ||
Table, | ||
TableBody, | ||
TableCell, | ||
TableHeader, | ||
TableRow, | ||
} from "@/components/ui/table/table"; | ||
import { TableHeadCell } from "@/components/ui/table/table-head-cell"; | ||
import type { ClientPaymentWithEcommerceClient } from "@/lib/types"; | ||
import { api } from "@/trpc/react"; | ||
import { format } from "date-fns"; | ||
import { Filter, Receipt } from "lucide-react"; | ||
import { useState } from "react"; | ||
import { ErrorState } from "../ui/table/error-state"; | ||
|
||
interface DashboardReceiptsProps { | ||
initialClientPayments: ClientPaymentWithEcommerceClient[]; | ||
} | ||
|
||
const ReceiptTableColumns = () => ( | ||
<TableRow className="hover:bg-transparent border-none"> | ||
<TableHeadCell>Date</TableHeadCell> | ||
<TableHeadCell>Reference</TableHeadCell> | ||
<TableHeadCell>Amount</TableHeadCell> | ||
<TableHeadCell>Payment Currency</TableHeadCell> | ||
<TableHeadCell>Network</TableHeadCell> | ||
<TableHeadCell>Merchant</TableHeadCell> | ||
</TableRow> | ||
); | ||
|
||
const ReceiptRow = ({ | ||
receipt, | ||
}: { receipt: ClientPaymentWithEcommerceClient }) => { | ||
return ( | ||
<TableRow className="hover:bg-zinc-50/50"> | ||
<TableCell> | ||
{receipt.createdAt | ||
? format(new Date(receipt.createdAt), "do MMM yyyy") | ||
: "N/A"} | ||
</TableCell> | ||
<TableCell> | ||
{receipt.reference || <span className="text-zinc-500">-</span>} | ||
</TableCell> | ||
<TableCell className="font-medium">{receipt.amount}</TableCell> | ||
<TableCell>{receipt.paymentCurrency}</TableCell> | ||
<TableCell>{receipt.network}</TableCell> | ||
<TableCell>{receipt.ecommerceClient.label}</TableCell> | ||
</TableRow> | ||
); | ||
}; | ||
|
||
const ITEMS_PER_PAGE = 10; | ||
|
||
export function DashboardReceipts({ | ||
initialClientPayments, | ||
}: DashboardReceiptsProps) { | ||
const [activeClientId, setActiveClientId] = useState<string | null>(null); | ||
const [currentPage, setCurrentPage] = useState(1); | ||
|
||
const { data, error, refetch, isRefetching } = | ||
api.ecommerce.getAllUserReceipts.useQuery(undefined, { | ||
initialData: initialClientPayments, | ||
refetchOnMount: true, | ||
}); | ||
|
||
if (error) { | ||
return ( | ||
<ErrorState | ||
onRetry={refetch} | ||
isRetrying={isRefetching} | ||
explanation="We couldn't load the receipts data. Please try again." | ||
/> | ||
); | ||
} | ||
|
||
const receipts = data || []; | ||
|
||
const filteredReceipts = activeClientId | ||
? receipts.filter((receipt) => receipt.ecommerceClientId === activeClientId) | ||
: receipts; | ||
|
||
const totalPages = Math.ceil(filteredReceipts.length / ITEMS_PER_PAGE); | ||
const paginatedReceipts = filteredReceipts.slice( | ||
(currentPage - 1) * ITEMS_PER_PAGE, | ||
currentPage * ITEMS_PER_PAGE, | ||
); | ||
|
||
const handleClientFilterChange = (value: string) => { | ||
setActiveClientId(value === "all" ? null : value); | ||
setCurrentPage(1); | ||
}; | ||
|
||
const ecommerceClients = receipts.reduce( | ||
(acc, receipt) => { | ||
if (acc[receipt.ecommerceClient.id]) return acc; | ||
acc[receipt.ecommerceClient.id] = receipt.ecommerceClient; | ||
return acc; | ||
}, | ||
{} as Record<string, ClientPaymentWithEcommerceClient["ecommerceClient"]>, | ||
); | ||
|
||
return ( | ||
<div className="space-y-6"> | ||
<p className="text-sm text-muted-foreground"> | ||
View all your payment receipts from ecommerce transactions | ||
</p> | ||
<div className="flex items-center justify-between"> | ||
<div className="flex items-center gap-4"> | ||
<div className="flex items-center gap-2"> | ||
<Filter className="h-4 w-4 text-zinc-600" /> | ||
<span className="text-sm font-medium text-zinc-700"> | ||
Filter by merchant: | ||
</span> | ||
</div> | ||
<Select | ||
value={activeClientId || "all"} | ||
onValueChange={handleClientFilterChange} | ||
> | ||
<SelectTrigger className="w-[200px]"> | ||
<SelectValue placeholder="All Merchants" /> | ||
</SelectTrigger> | ||
<SelectContent> | ||
<SelectItem value="all">All Merchants</SelectItem> | ||
{Object.entries(ecommerceClients).map(([clientId, client]) => ( | ||
<SelectItem key={clientId} value={clientId}> | ||
{client.label} | ||
</SelectItem> | ||
))} | ||
</SelectContent> | ||
</Select> | ||
</div> | ||
</div> | ||
<Card className="border border-zinc-100"> | ||
<CardContent className="p-0"> | ||
<Table> | ||
<TableHeader> | ||
<ReceiptTableColumns /> | ||
</TableHeader> | ||
<TableBody> | ||
{paginatedReceipts.length === 0 ? ( | ||
<TableRow> | ||
<TableCell colSpan={6} className="p-0"> | ||
<EmptyState | ||
icon={<Receipt className="h-6 w-6 text-zinc-600" />} | ||
title="No receipts" | ||
subtitle={ | ||
activeClientId | ||
? "No receipts found for the selected merchant" | ||
: "You haven't received any payments yet" | ||
} | ||
/> | ||
</TableCell> | ||
</TableRow> | ||
) : ( | ||
paginatedReceipts.map((receipt) => ( | ||
<ReceiptRow key={receipt.id} receipt={receipt} /> | ||
)) | ||
)} | ||
</TableBody> | ||
</Table> | ||
</CardContent> | ||
</Card> | ||
|
||
{totalPages > 1 && ( | ||
<Pagination | ||
page={currentPage} | ||
totalItems={filteredReceipts.length} | ||
itemsPerPage={ITEMS_PER_PAGE} | ||
setPage={setCurrentPage} | ||
/> | ||
)} | ||
</div> | ||
); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make duplicate protection atomic
The “select then insert” guard still lets two concurrent webhook deliveries race past the duplicate check and both insert, so we can double-count the same payment (Request webhooks routinely retry on network hiccups). We need the database to enforce idempotency. Please rely on an atomic insert with
ON CONFLICT DO NOTHING
(or a unique constraint + error handling) instead of the manual pre-check.