File tree Expand file tree Collapse file tree 1 file changed +78
-1
lines changed
apps/finance/src/app/api/accounts Expand file tree Collapse file tree 1 file changed +78
-1
lines changed Original file line number Diff line number Diff line change 1
1
import { db } from "../../../server/db" ;
2
2
3
- export async function GET ( request : Request ) {
3
+ type RequestBody = {
4
+ studentId : string ;
5
+ } ;
6
+
7
+ export async function GET ( ) {
4
8
const accounts = await db . financeAccount . findMany ( ) ;
5
9
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
+
6
21
return Response . json (
7
22
{ data : accounts } ,
8
23
{
9
24
status : 200 ,
10
25
} ,
11
26
) ;
12
27
}
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
+ }
You can’t perform that action at this time.
0 commit comments