@@ -9,148 +9,109 @@ const scoreSchema = z.object({
99 presentation : z . number ( ) ,
1010 innovation : z . number ( ) ,
1111 teamwork : z . number ( ) ,
12- comment : z . string ( ) ,
13- round : z . number ( ) ,
12+ comment : z . string ( ) ,
13+ round : z . number ( ) ,
1414} ) ;
15+
1516const scoresResponseSchema = z . object ( {
16- status : z . string ( ) ,
1717 message : z . string ( ) ,
18- data : z . object ( {
19- message : z . string ( ) ,
20- scores : z . array ( scoreSchema ) ,
21- } ) ,
18+ data : z . object ( {
19+ scores : z . array ( scoreSchema )
20+ } ) . optional ( )
21+ } ) ;
22+
23+ const createUpdateResponseSchema = z . object ( {
24+ message : z . string ( ) ,
25+ data : z . object ( {
26+ message : z . string ( )
27+ } ) . optional ( )
2228} ) ;
2329
24- const deleteSchema = z . object ( {
30+ const deleteResponseSchema = z . object ( {
2531 status : z . string ( ) ,
26- data : z . record ( z . unknown ( ) ) ,
32+ message : z . string ( )
2733} ) ;
2834
35+ type ScoreResponse = z . infer < typeof scoreSchema > ;
36+
37+ interface CreateScoreRequest extends Omit < z . infer < typeof scoreSchema > , 'id' > {
38+ team_id : string ;
39+ }
2940
30- type ScoreResponse = z . infer < typeof scoreSchema >
31- interface CreateScoreRequest extends Omit < z . infer < typeof scoreSchema > , 'id' > {
32- team_id : string ;
41+ interface UpdateScoreRequest extends Partial < Omit < CreateScoreRequest , 'team_id' > > {
42+ scoreId : string ;
43+ team_id ? : string ;
3344}
3445
35- export const fetchScores = async ( teamId : string ) => {
46+ export const fetchScores = async ( teamId : string ) : Promise < ScoreResponse [ ] > => {
3647 try {
3748 const response = await axios . get < {
3849 status : string ;
3950 message : string ;
40- data : {
41- message : string ;
42- scores : ScoreResponse [ ] ;
51+ data ? : {
52+ message ? : string ;
53+ scores ? : ScoreResponse [ ] ;
4354 } ;
4455 } > ( `panel/getscore/${ teamId } ` , {
4556 withCredentials : true ,
4657 } ) ;
47- const parsedResponse = scoresResponseSchema . parse ( response . data ) ;
48- return parsedResponse . data . scores ;
49- } catch ( err ) {
50- console . error ( err )
51- throw err ;
58+ const parsedResponse = scoresResponseSchema . parse ( response . data ) ;
59+ return parsedResponse . data ?. scores ?? [ ] ;
60+ } catch ( err : any ) {
61+ if ( err . response ?. status === 404 ) {
62+ return [ ] ;
63+ }
64+ throw new Error ( err . response ?. data ?. message || 'Failed to fetch scores' ) ;
5265 }
5366} ;
54- const createResponseSchema = z . object ( {
55-
56- message : z . string ( ) ,
5767
58- } ) ;
59-
60- export const createScore = async ( {
61- team_id,
62- design,
63- implementation,
64- presentation,
65- innovation,
66- teamwork,
67- comment,
68- round,
69- } : CreateScoreRequest ) => {
68+ export const createScore = async ( data : CreateScoreRequest ) => {
7069 try {
71- const response = await axios . post (
72- `panel/createscore` ,
70+ const response = await axios . post (
71+ 'panel/createscore' ,
72+ data ,
7373 {
74- design,
75- implementation,
76- presentation,
77- innovation,
78- teamwork,
79- comment,
80- team_id,
81- round,
82- } ,
83- {
84- withCredentials : true ,
85- }
74+ withCredentials : true ,
75+ }
8676 ) ;
87- const parsedResponse = createResponseSchema . parse ( response . data ) ;
77+ const parsedResponse = createUpdateResponseSchema . parse ( response . data ) ;
8878 return parsedResponse ;
89- } catch ( err ) {
90- console . error ( err ) ;
91- throw err ;
79+ } catch ( error : any ) {
80+ if ( error . response ?. data ?. message ) {
81+ throw new Error ( error . response . data . message ) ;
82+ }
83+ throw new Error ( 'Failed to create score' ) ;
9284 }
9385} ;
9486
9587export const deleteScore = async ( scoreId : string ) => {
9688 try {
97- const response = await axios . delete ( `panel/deletescore/${ scoreId } ` , {
98- withCredentials : true ,
99- } ) ;
100-
101- const parsedResponse = deleteSchema . parse ( response . data ) ;
102- return parsedResponse ;
103- } catch ( err ) {
104- console . error ( err ) ;
105- throw err ;
89+ const response = await axios . delete ( `panel/deletescore/${ scoreId } ` , {
90+ withCredentials : true ,
91+ } ) ;
92+
93+ const parsedResponse = deleteResponseSchema . parse ( response . data ) ;
94+ return parsedResponse ;
95+ } catch ( err : any ) {
96+ throw new Error ( err . response ?. data ?. message || 'Failed to delete score' ) ;
10697 }
10798} ;
10899
109- const updateResponseSchema = z . object ( {
110-
111- message : z . string ( ) ,
112-
113- } ) ;
114-
115- export const updateScore = async ( {
116- scoreId,
117- design,
118- implementation,
119- presentation,
120- innovation,
121- teamwork,
122- comment,
123- round
124- } : {
125- scoreId :string ,
126- design :number ,
127- implementation :number ,
128- presentation :number ,
129- innovation :number ,
130- teamwork :number ,
131- comment :string ,
132- round :number ,
133- } ) => {
100+ export const updateScore = async ( data : UpdateScoreRequest ) => {
134101 try {
135- const response = await axios . put ( `panel/updatescore/${ scoreId } ` ,
136- {
137- design,
138- implementation,
139- presentation,
140- innovation,
141- teamwork,
142- comment,
143- round
144- } ,
102+ const response = await axios . put (
103+ `panel/updatescore/${ data . scoreId } ` ,
104+ data ,
145105 {
146106 withCredentials : true ,
147107 }
148108 ) ;
149-
150- const parsedResponse = updateResponseSchema . parse ( response . data ) ;
151- return parsedResponse ;
152- } catch ( err ) {
153- console . error ( err ) ;
154- throw err ;
109+ const parsedResponse = createUpdateResponseSchema . parse ( response . data ) ;
110+ return parsedResponse ;
111+ } catch ( error : any ) {
112+ if ( error . response ?. data ?. message ) {
113+ throw new Error ( error . response . data . message ) ;
114+ }
115+ throw new Error ( 'Failed to update score' ) ;
155116 }
156117} ;
0 commit comments