11import axios from "./axiosConfig" ;
2+ import { z } from "zod" ;
3+
4+ const scoreSchema = z . object ( {
5+ id : z . string ( ) ,
6+ team_id : z . string ( ) ,
7+ design : z . number ( ) ,
8+ implementation : z . number ( ) ,
9+ presentation : z . number ( ) ,
10+ innovation : z . number ( ) ,
11+ teamwork : z . number ( ) ,
12+ comment : z . string ( ) ,
13+ round : z . number ( ) ,
14+ } ) ;
15+ const scoresResponseSchema = z . object ( {
16+ status : z . string ( ) ,
17+ message : z . string ( ) ,
18+ data : z . object ( {
19+ message : z . string ( ) ,
20+ scores : z . array ( scoreSchema ) ,
21+ } ) ,
22+ } ) ;
223
3- interface ScoreResponse {
4- id : string ;
5- team_id : string ;
6- design : number ;
7- implementation : number ;
8- presentation : number ;
9- innovation : number ;
10- teamwork : number ;
11- comment : string ;
12- round : number ;
13- }
1424
15- interface CreateScoreRequest {
16- team_id : string ;
17- design : number ;
18- implementation : number ;
19- presentation : number ;
20- innovation : number ;
21- teamwork : number ;
22- comment : string ;
23- round : number ;
25+ interface ScoreResponse extends z . infer < typeof scoreSchema > { }
26+ interface CreateScoreRequest extends Omit < z . infer < typeof scoreSchema > , 'id' > {
27+ team_id : string ;
2428}
2529
2630export const fetchScores = async ( teamId : string ) => {
27- try {
28- const response = await axios . get < {
29- status : string ;
31+ try {
32+ const response = await axios . get < {
33+ status : string ;
34+ message : string ;
35+ data : {
3036 message : string ;
31- data : {
32- message : string ;
33- scores : ScoreResponse [ ] ;
34- } ;
35- } > ( `panel/getscore/${ teamId } ` , {
36- withCredentials : true ,
37- } ) ;
38-
39- // The scores are nested inside response.data.data.scores
40- if ( ! response . data . data ?. scores ) {
41- throw new Error ( 'No scores data received' ) ;
42- }
43-
44- return response . data . data . scores ;
45- } catch ( err ) {
46- console . error ( 'Error fetching scores:' , err ) ;
47- throw err ;
48- }
37+ scores : ScoreResponse [ ] ;
38+ } ;
39+ } > ( `panel/getscore/${ teamId } ` , {
40+ withCredentials : true ,
41+ } ) ;
42+ const parsedResponse = scoresResponseSchema . parse ( response . data ) ;
43+ return parsedResponse . data . scores ;
44+ } catch ( err ) {
45+ console . error ( err )
46+ throw err ;
47+ }
4948} ;
5049
50+
5151export const createScore = async ( {
52- team_id,
53- design,
54- implementation,
55- presentation,
56- innovation,
57- teamwork,
58- comment,
59- round,
52+ team_id,
53+ design,
54+ implementation,
55+ presentation,
56+ innovation,
57+ teamwork,
58+ comment,
59+ round,
6060} : CreateScoreRequest ) => {
6161 try {
62- const response = await axios . post (
62+ const response = await axios . post (
6363 `panel/createscore` ,
6464 {
6565 design,
@@ -71,32 +71,32 @@ export const createScore = async ({
7171 team_id,
7272 round,
7373 } ,
74- {
75- withCredentials : true ,
76- }
74+ {
75+ withCredentials : true ,
76+ }
7777 ) ;
78- return response . data ;
78+ return response . data ;
7979 } catch ( err ) {
80- console . log ( err ) ;
80+ console . error ( err ) ;
8181 throw err ;
8282 }
8383} ;
8484
8585export const deleteScore = async ( scoreId : string ) => {
8686 try {
87- const response = await axios . delete ( `panel/deletescore/${ scoreId } ` , {
88- withCredentials : true ,
89- } ) ;
87+ const response = await axios . delete ( `panel/deletescore/${ scoreId } ` , {
88+ withCredentials : true ,
89+ } ) ;
9090 return response . data ;
9191 } catch ( err ) {
92- console . log ( err ) ;
92+ console . error ( err ) ;
9393 throw err ;
9494 }
9595} ;
9696
9797export const updateScore = async ( {
98- scoreId,
99- design,
98+ scoreId,
99+ design,
100100 implementation,
101101 presentation,
102102 innovation,
@@ -116,20 +116,21 @@ export const updateScore = async ({
116116 try {
117117 const response = await axios . put ( `panel/updatescore/${ scoreId } ` ,
118118 {
119- design,
119+ design,
120120 implementation,
121121 presentation,
122122 innovation,
123- teamwork,
124- comment,
125- round
126- } ,
127- {
128- withCredentials : true
129- } ) ;
130- return response . data ;
123+ teamwork,
124+ comment,
125+ round
126+ } ,
127+ {
128+ withCredentials : true ,
129+ }
130+ ) ;
131+ return response . data ;
131132 } catch ( err ) {
132- console . log ( err ) ;
133+ console . error ( err ) ;
133134 throw err ;
134135 }
135136} ;
0 commit comments