@@ -33,6 +33,33 @@ export class WinningsRepository {
3333
3434 constructor ( private readonly prisma : PrismaService ) { }
3535
36+ /**
37+ * Extracts the optional hours-worked value from winning attributes.
38+ * @param attributes Winning attributes payload retrieved from the database.
39+ * @returns Parsed hours-worked value, or `undefined` when absent/invalid.
40+ */
41+ private getHoursWorked (
42+ attributes : Prisma . JsonValue | null ,
43+ ) : number | undefined {
44+ if (
45+ ! attributes ||
46+ typeof attributes !== 'object' ||
47+ Array . isArray ( attributes )
48+ ) {
49+ return undefined ;
50+ }
51+
52+ const value = ( attributes as Record < string , unknown > ) . hoursWorked ;
53+ if ( value === undefined || value === null || value === '' ) {
54+ return undefined ;
55+ }
56+
57+ const parsedValue = Number ( value ) ;
58+ return Number . isFinite ( parsedValue ) && parsedValue > 0
59+ ? parsedValue
60+ : undefined ;
61+ }
62+
3663 private generateFilterDate ( date ?: DateFilterType ) {
3764 let filterDate : object | undefined ;
3865 const currentDay = new Date ( new Date ( ) . setHours ( 0 , 0 , 0 , 0 ) ) ;
@@ -252,38 +279,43 @@ export class WinningsRepository {
252279 const totalItems = includeCount ? count : winnings . length ;
253280
254281 result . data = {
255- winnings : winnings . map ( ( item ) => ( {
256- id : item . winning_id ,
257- type : item . type ,
258- winnerId : item . winner_id ,
259- origin : item . origin ?. origin_name ,
260- category : ( item . category ?? '' ) as WinningsCategory ,
261- title : item . title as string ,
262- description : item . description as string ,
263- externalId : item . external_id as string ,
264- attributes : ( item . attributes ?? { } ) as object ,
265- details : item . payment ?. map ( ( paymentItem ) => ( {
266- id : paymentItem . payment_id ,
267- netAmount : Number ( paymentItem . net_amount ) ,
268- grossAmount : Number ( paymentItem . gross_amount ) ,
269- totalAmount : Number ( paymentItem . total_amount ) ,
270- installmentNumber : paymentItem . installment_number as number ,
271- datePaid : ( paymentItem . date_paid ?? undefined ) as Date ,
272- status : paymentItem . payment_status as PaymentStatus ,
273- currency : paymentItem . currency as string ,
274- releaseDate : paymentItem . release_date as Date ,
275- category : item . category as string ,
276- billingAccount : paymentItem . billing_account ,
277- } ) ) ,
278- createdAt : item . created_at as Date ,
279- updatedAt : ( item . payment ?. [ 0 ] . date_paid ??
280- item . payment ?. [ 0 ] . updated_at ??
281- undefined ) as Date ,
282- releaseDate : item . payment ?. [ 0 ] ?. release_date as Date ,
283- paymentStatus : usersPayoutStatusMap [
284- item . winner_id
285- ] as WinningDto [ 'paymentStatus' ] ,
286- } ) ) ,
282+ winnings : winnings . map ( ( item ) => {
283+ const attributes = ( item . attributes ?? { } ) as object ;
284+
285+ return {
286+ id : item . winning_id ,
287+ type : item . type ,
288+ winnerId : item . winner_id ,
289+ origin : item . origin ?. origin_name ,
290+ category : ( item . category ?? '' ) as WinningsCategory ,
291+ title : item . title as string ,
292+ description : item . description as string ,
293+ externalId : item . external_id as string ,
294+ attributes,
295+ hoursWorked : this . getHoursWorked ( item . attributes ) ,
296+ details : item . payment ?. map ( ( paymentItem ) => ( {
297+ id : paymentItem . payment_id ,
298+ netAmount : Number ( paymentItem . net_amount ) ,
299+ grossAmount : Number ( paymentItem . gross_amount ) ,
300+ totalAmount : Number ( paymentItem . total_amount ) ,
301+ installmentNumber : paymentItem . installment_number as number ,
302+ datePaid : ( paymentItem . date_paid ?? undefined ) as Date ,
303+ status : paymentItem . payment_status as PaymentStatus ,
304+ currency : paymentItem . currency as string ,
305+ releaseDate : paymentItem . release_date as Date ,
306+ category : item . category as string ,
307+ billingAccount : paymentItem . billing_account ,
308+ } ) ) ,
309+ createdAt : item . created_at as Date ,
310+ updatedAt : ( item . payment ?. [ 0 ] . date_paid ??
311+ item . payment ?. [ 0 ] . updated_at ??
312+ undefined ) as Date ,
313+ releaseDate : item . payment ?. [ 0 ] ?. release_date as Date ,
314+ paymentStatus : usersPayoutStatusMap [
315+ item . winner_id
316+ ] as WinningDto [ 'paymentStatus' ] ,
317+ } ;
318+ } ) ,
287319 pagination : {
288320 totalItems,
289321 totalPages : Math . ceil ( totalItems / limit ) ,
@@ -344,36 +376,41 @@ export class WinningsRepository {
344376 ? await this . getUsersPayoutStatusForWinnings ( winnings )
345377 : ( { } as { [ key : string ] : payment_status } ) ;
346378
347- result . data = winnings . map ( ( item ) => ( {
348- id : item . winning_id ,
349- type : item . type ,
350- winnerId : item . winner_id ,
351- origin : item . origin ?. origin_name ,
352- category : ( item . category ?? '' ) as WinningsCategory ,
353- title : item . title as string ,
354- description : item . description as string ,
355- externalId : item . external_id as string ,
356- attributes : ( item . attributes ?? { } ) as object ,
357- details : item . payment ?. map ( ( paymentItem ) => ( {
358- id : paymentItem . payment_id ,
359- netAmount : Number ( paymentItem . net_amount ) ,
360- grossAmount : Number ( paymentItem . gross_amount ) ,
361- totalAmount : Number ( paymentItem . total_amount ) ,
362- installmentNumber : paymentItem . installment_number as number ,
363- datePaid : ( paymentItem . date_paid ?? undefined ) as Date ,
364- status : paymentItem . payment_status as PaymentStatus ,
365- currency : paymentItem . currency as string ,
366- releaseDate : paymentItem . release_date as Date ,
367- category : item . category as string ,
368- billingAccount : paymentItem . billing_account ,
369- } ) ) ,
370- createdAt : item . created_at as Date ,
371- updatedAt : ( item . payment ?. [ 0 ] . date_paid ??
372- item . payment ?. [ 0 ] . updated_at ??
373- undefined ) as Date ,
374- releaseDate : item . payment ?. [ 0 ] ?. release_date as Date ,
375- paymentStatus : usersPayoutStatusMap [ item . winner_id ] ,
376- } ) ) ;
379+ result . data = winnings . map ( ( item ) => {
380+ const attributes = ( item . attributes ?? { } ) as object ;
381+
382+ return {
383+ id : item . winning_id ,
384+ type : item . type ,
385+ winnerId : item . winner_id ,
386+ origin : item . origin ?. origin_name ,
387+ category : ( item . category ?? '' ) as WinningsCategory ,
388+ title : item . title as string ,
389+ description : item . description as string ,
390+ externalId : item . external_id as string ,
391+ attributes,
392+ hoursWorked : this . getHoursWorked ( item . attributes ) ,
393+ details : item . payment ?. map ( ( paymentItem ) => ( {
394+ id : paymentItem . payment_id ,
395+ netAmount : Number ( paymentItem . net_amount ) ,
396+ grossAmount : Number ( paymentItem . gross_amount ) ,
397+ totalAmount : Number ( paymentItem . total_amount ) ,
398+ installmentNumber : paymentItem . installment_number as number ,
399+ datePaid : ( paymentItem . date_paid ?? undefined ) as Date ,
400+ status : paymentItem . payment_status as PaymentStatus ,
401+ currency : paymentItem . currency as string ,
402+ releaseDate : paymentItem . release_date as Date ,
403+ category : item . category as string ,
404+ billingAccount : paymentItem . billing_account ,
405+ } ) ) ,
406+ createdAt : item . created_at as Date ,
407+ updatedAt : ( item . payment ?. [ 0 ] . date_paid ??
408+ item . payment ?. [ 0 ] . updated_at ??
409+ undefined ) as Date ,
410+ releaseDate : item . payment ?. [ 0 ] ?. release_date as Date ,
411+ paymentStatus : usersPayoutStatusMap [ item . winner_id ] ,
412+ } ;
413+ } ) ;
377414 } catch ( error ) {
378415 this . logger . error ( 'Getting winnings by external ID failed' , error ) ;
379416 const message = 'Getting winnings by external ID failed. ' + error ;
0 commit comments