Skip to content

Commit

Permalink
proposal-api (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
HrikB authored Mar 24, 2023
1 parent a502160 commit 9a19331
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions client/pages/api/proposals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { NextApiRequest, NextApiResponse } from "next";
import prisma from "lib/prisma";

interface Proposal {
id: number;
proposalId: string;
createdAt: string;
description: string;
transactions: any;
}

interface Data {
count: number;
proposals?: Proposal[];
}

const handler = async (req: NextApiRequest, res: NextApiResponse<Data>) => {
const rawProposals = await prisma.proposal.findMany({
orderBy: { id: "desc" },
include: { transactions: true },
});

if (req.query.onlyCount)
return res.status(200).json({ count: rawProposals.length });

const proposals = rawProposals.map((p) => ({
id: p.id,
proposalId: p.proposalId,
createdAt: p.createdAt.toString(),
description: p.description,
transactions: JSON.parse(JSON.stringify(p.transactions)), // transactions.createdAt [object Date] cannot be serialized as JSON without JSON.parse/stringify
}));

return res.status(200).json({ count: proposals.length, proposals });
};

export default handler;

0 comments on commit 9a19331

Please sign in to comment.