@@ -28,7 +28,7 @@ export const GET: RequestHandler = async ({ params, request, platform }) => {
2828
2929 const participantShares : Record <
3030 string ,
31- Array < number >
31+ Array < { share : number ; reason : string } >
3232 > = await db . getParticipantShares ( sessionId ) ;
3333
3434 const participantNames = Object . keys ( participantShares ) ;
@@ -50,9 +50,10 @@ export const GET: RequestHandler = async ({ params, request, platform }) => {
5050 }
5151
5252 const participants = Object . entries ( participantShares ) . map (
53- ( [ name , shares ] ) => ( {
53+ ( [ name , vote ] ) => ( {
5454 name,
55- share : shares . reduce ( ( sum , value ) => sum + value , 0 ) / participantLength
55+ share : vote . reduce ( ( sum , v ) => sum + v . share , 0 ) / participantLength ,
56+ reasons : vote . map ( ( v ) => v . reason )
5657 } )
5758 ) ;
5859
@@ -92,12 +93,30 @@ export const POST: RequestHandler = async ({ params, request, platform }) => {
9293 notFoundError ( ) ;
9394 }
9495
96+ const allReasonsExist = vote . participants . every (
97+ ( p ) => p . reason && p . reason . trim ( ) . length > 0
98+ ) ;
99+ if ( ! allReasonsExist ) {
100+ badRequestError (
101+ 'One or more participants in the vote do not have a share reason'
102+ ) ;
103+ }
104+
95105 const participants = await db . getParticipants ( session . id ) ;
96106
97107 if ( vote . participants . length !== participants . length - 1 ) {
98108 badRequestError ( 'Invalid number of share participants.' ) ;
99109 }
100110
111+ const voterExists = participants . some ( ( p ) => p . id === voterId ) ;
112+ if ( ! voterExists ) {
113+ badRequestError ( 'Voter is not a participant in this session' ) ;
114+ }
115+
116+ if ( vote . participants . some ( ( s ) => s . id === voterId ) ) {
117+ badRequestError ( 'You cannot vote for yourself' ) ;
118+ }
119+
101120 const totalShares = vote . participants . reduce ( ( sum , s ) => sum + s . share , 0 ) ;
102121 if ( totalShares != session . stake ) {
103122 badRequestError ( 'Total shares must equal session stake' ) ;
@@ -110,15 +129,6 @@ export const POST: RequestHandler = async ({ params, request, platform }) => {
110129 badRequestError ( 'You have already voted in this session' ) ;
111130 }
112131
113- const voterExists = participants . some ( ( p ) => p . id === voterId ) ;
114- if ( ! voterExists ) {
115- badRequestError ( 'Voter is not a participant in this session' ) ;
116- }
117-
118- if ( vote . participants . some ( ( s ) => s . id === voterId ) ) {
119- badRequestError ( 'You cannot vote for yourself' ) ;
120- }
121-
122132 const allShareParticipantsExist = vote . participants . every ( ( s ) =>
123133 participants . some ( ( p ) => p . id === s . id )
124134 ) ;
0 commit comments