@@ -157,32 +157,71 @@ export class VoteService {
157157 } ;
158158 } else if ( poll . mode === "point" ) {
159159 // Calculate point-based voting results
160+ console . log ( '=== POINTS-BASED VOTING DEBUG ===' ) ;
161+ console . log ( 'Poll ID:' , pollId ) ;
162+ console . log ( 'Poll options:' , poll . options ) ;
163+ console . log ( 'Total votes to process:' , votes . length ) ;
164+
160165 const optionPoints : Record < string , number > = { } ;
161166 poll . options . forEach ( ( option , index ) => {
162167 optionPoints [ option ] = 0 ;
168+ console . log ( `Initialized option ${ index } : "${ option } " with 0 points` ) ;
163169 } ) ;
164170
165- votes . forEach ( ( vote ) => {
166- if ( vote . data . mode === "point" && Array . isArray ( vote . data . data ) ) {
167- // Handle the stored format: [{ option: "points", points: { "0": 100 } }]
168- vote . data . data . forEach ( ( item : any ) => {
169- if ( item . option === "points" && item . points && typeof item . points === 'object' ) {
170- Object . entries ( item . points ) . forEach ( ( [ optionIndex , points ] ) => {
171- const index = parseInt ( optionIndex ) ;
172- const option = poll . options [ index ] ;
173- if ( option && typeof points === 'number' ) {
174- optionPoints [ option ] += points ;
175- }
176- } ) ;
177- }
178- } ) ;
171+ votes . forEach ( ( vote , voteIndex ) => {
172+ console . log ( `\n--- Processing Vote ${ voteIndex + 1 } ---` ) ;
173+ console . log ( 'Vote ID:' , vote . id ) ;
174+ console . log ( 'Vote data:' , JSON . stringify ( vote . data , null , 2 ) ) ;
175+ console . log ( 'Vote data type:' , typeof vote . data ) ;
176+ console . log ( 'Vote data.mode:' , ( vote . data as any ) . mode ) ;
177+ console . log ( 'Vote data.data:' , ( vote . data as any ) . data ) ;
178+ console . log ( 'Vote data.data type:' , typeof ( vote . data as any ) . data ) ;
179+ console . log ( 'Is vote.data.data array?' , Array . isArray ( ( vote . data as any ) . data ) ) ;
180+
181+ if ( vote . data . mode === "point" ) {
182+ console . log ( '✅ This is a point vote, processing...' ) ;
183+
184+ // Handle the actual stored format: {"0": 10, "1": 10, "2": 50, "3": 20, "5": 10}
185+ if ( typeof vote . data . data === 'object' && ! Array . isArray ( vote . data . data ) ) {
186+ console . log ( '✅ Processing direct object format vote data' ) ;
187+ console . log ( 'Points object entries:' , Object . entries ( vote . data . data ) ) ;
188+
189+ Object . entries ( vote . data . data ) . forEach ( ( [ optionIndex , points ] ) => {
190+ const index = parseInt ( optionIndex ) ;
191+ const option = poll . options [ index ] ;
192+ console . log ( `Processing optionIndex: "${ optionIndex } " -> parsed index: ${ index } ` ) ;
193+ console . log ( `Option at index ${ index } :` , option ) ;
194+ console . log ( `Points value:` , points , `(type: ${ typeof points } )` ) ;
195+
196+ if ( option && typeof points === 'number' ) {
197+ const oldPoints = optionPoints [ option ] ;
198+ optionPoints [ option ] += points ;
199+ console . log ( `✅ Option "${ option } " gets ${ points } points: ${ oldPoints } + ${ points } = ${ optionPoints [ option ] } ` ) ;
200+ } else {
201+ console . log ( `❌ Skipping invalid option "${ optionIndex } " or points ${ points } ` ) ;
202+ console . log ( ` - option exists: ${ ! ! option } ` ) ;
203+ console . log ( ` - points is number: ${ typeof points === 'number' } ` ) ;
204+ }
205+ } ) ;
206+ } else {
207+ console . log ( '❌ Unexpected data format for point vote' ) ;
208+ console . log ( ' - Expected: object and not array' ) ;
209+ console . log ( ' - Got: type =' , typeof vote . data . data , ', isArray =' , Array . isArray ( vote . data . data ) ) ;
210+ }
211+ } else {
212+ console . log ( '❌ This is NOT a point vote, skipping' ) ;
179213 }
180214 } ) ;
181215
182216 const totalVotes = votes . length ;
217+ console . log ( '\n=== FINAL CALCULATION ===' ) ;
218+ console . log ( 'Total votes:' , totalVotes ) ;
219+ console . log ( 'Final optionPoints object:' , optionPoints ) ;
220+
183221 const results = poll . options . map ( ( option , index ) => {
184222 const points = optionPoints [ option ] || 0 ;
185223 const averagePoints = totalVotes > 0 ? points / totalVotes : 0 ;
224+ console . log ( `Option "${ option } ": ${ points } total points, ${ averagePoints } average points` ) ;
186225 return {
187226 option,
188227 totalPoints : points ,
@@ -193,6 +232,8 @@ export class VoteService {
193232
194233 // Sort by total points (highest first)
195234 results . sort ( ( a , b ) => b . totalPoints - a . totalPoints ) ;
235+ console . log ( '\n=== FINAL RESULTS ===' ) ;
236+ console . log ( 'Sorted results:' , JSON . stringify ( results , null , 2 ) ) ;
196237
197238 return {
198239 pollId,
0 commit comments