Skip to content

Commit 16630c1

Browse files
committed
✨ feat (finance): Created POST endpoint to create a new finance account
* Also added error checks if data exists or is invalid
1 parent 0436f02 commit 16630c1

File tree

1 file changed

+78
-1
lines changed
  • apps/finance/src/app/api/accounts

1 file changed

+78
-1
lines changed
Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,89 @@
11
import { db } from "../../../server/db";
22

3-
export async function GET(request: Request) {
3+
type RequestBody = {
4+
studentId: string;
5+
};
6+
7+
export async function GET() {
48
const accounts = await db.financeAccount.findMany();
59

10+
if (accounts.length === 0) {
11+
return Response.json(
12+
{
13+
message: "No finance accounts exist",
14+
},
15+
{
16+
status: 400,
17+
},
18+
);
19+
}
20+
621
return Response.json(
722
{ data: accounts },
823
{
924
status: 200,
1025
},
1126
);
1227
}
28+
29+
export async function POST(request: Request) {
30+
// Receive studentCardId and lectureId from request body
31+
const { studentId } = (await request.json()) as RequestBody;
32+
33+
if (request.body === undefined) {
34+
return Response.json(
35+
{ error: "Missing request body" },
36+
{
37+
status: 400,
38+
},
39+
);
40+
}
41+
42+
// If request body is missing parameters, throw error
43+
if (!studentId) {
44+
return Response.json(
45+
{ error: "Missing parameters in request body" },
46+
{
47+
status: 400,
48+
},
49+
);
50+
}
51+
52+
const existingAccount = await db.financeAccount.findFirst({
53+
where: {
54+
studentId: studentId,
55+
},
56+
});
57+
58+
if (existingAccount) {
59+
{
60+
return Response.json(
61+
{
62+
message: "Finance account already exists",
63+
},
64+
{ status: 400 },
65+
);
66+
}
67+
}
68+
69+
const newAccount = await db.financeAccount.create({
70+
data: {
71+
studentId: studentId,
72+
hasOutstandingBalance: false,
73+
},
74+
select: {
75+
id: true,
76+
studentId: true,
77+
hasOutstandingBalance: true,
78+
},
79+
});
80+
81+
return Response.json(
82+
{
83+
data: newAccount,
84+
},
85+
{
86+
status: 200,
87+
},
88+
);
89+
}

0 commit comments

Comments
 (0)