@@ -2,7 +2,7 @@ import { json } from '@sveltejs/kit';
22import type { RequestHandler } from './$types' ;
33import { DatabaseService } from '$lib/database' ;
44import { badRequestError , notFoundError , unauthorizedError } from '$lib/utils' ;
5- import type { PatchDescriptionRequest , SubmitVoteRequest } from '$lib/types' ;
5+ import type { Participant , SubmitVoteRequest } from '$lib/types' ;
66
77export const GET : RequestHandler = async ( { params, request, platform } ) => {
88 try {
@@ -99,10 +99,13 @@ export const GET: RequestHandler = async ({ params, request, platform }) => {
9999 reasons : p . reasons
100100 } ) ) ;
101101
102+ const allConfirmed = participants . every ( ( p ) => p . confirmed ) ;
103+
102104 return json ( {
103105 topic : session . topic ,
104106 description : session . description ,
105107 stake : session . stake ,
108+ confirmed : allConfirmed ,
106109 participants : weightedParticipants
107110 } ) ;
108111 } catch ( err ) {
@@ -210,42 +213,56 @@ export const PATCH: RequestHandler = async ({ params, request, platform }) => {
210213 badRequestError ( 'Session key is required' ) ;
211214 }
212215
213- const body : PatchDescriptionRequest = await request . json ( ) ;
214- if ( ! body . participantId || ! ( token || body . description ) ) {
215- badRequestError ( 'Participant ID and description are required' ) ;
216+ const updateParticipant = ( await request . json ( ) ) as Partial < Participant > & {
217+ id : number ;
218+ } ;
219+ if ( ! updateParticipant . id ) {
220+ badRequestError ( 'Participant ID required' ) ;
216221 }
222+
217223 const db = new DatabaseService ( platform . env . DB ) ;
218224 const session = await db . getSession ( sessionKey ) ;
219225 if ( ! session ) {
220226 notFoundError ( ) ;
221227 }
222228
223229 const participants = await db . getParticipants ( session . id ) ;
224- const participant = participants . find ( ( p ) => p . id === body . participantId ) ;
225- if ( ! participant ) {
230+ const existing = participants . find ( ( p ) => p . id === updateParticipant . id ) ;
231+ if ( ! existing ) {
226232 badRequestError ( 'Participant does not exist in this session' ) ;
227233 }
228234
229- if ( ! token && participant . description ) {
230- badRequestError ( 'Description has already been set and cannot be changed' ) ;
235+ const updatedParticipant : Participant = { ...existing } ;
236+
237+ if ( updateParticipant . description !== undefined ) {
238+ if ( ! token && existing . description != existing . description ) {
239+ unauthorizedError ( ) ;
240+ }
241+
242+ updatedParticipant . description = updateParticipant . description ;
231243 }
232244
233- const success = await db . setParticipantDescription (
234- body . participantId ,
235- body . description
236- ) ;
245+ if ( updateParticipant . confirmed !== undefined ) {
246+ if ( ! token && updateParticipant . confirmed == false ) {
247+ unauthorizedError ( ) ;
248+ }
249+
250+ updatedParticipant . confirmed = updateParticipant . confirmed ;
251+ }
252+
253+ const success = await db . updateParticipant ( updatedParticipant ) ;
237254 if ( ! success ) {
238- badRequestError ( 'Failed to update participant description ' ) ;
255+ badRequestError ( 'Failed to update participant' ) ;
239256 }
257+
240258 return json ( { success : true } ) ;
241259 } catch ( err ) {
242260 console . error ( 'Error updating participant description:' , err ) ;
243261 if ( err instanceof Response ) {
244262 throw err ;
245263 }
246-
247264 badRequestError (
248- 'Failed to update participant description: ' + err . body . message
265+ 'Failed to update participant description: ' + ( err as any ) . body ? .message
249266 ) ;
250267 }
251268} ;
0 commit comments