From 479390a54ad4ce0396286c811f24482f83f5301b Mon Sep 17 00:00:00 2001
From: Niklas Marion <niklas.marion@gmx.de>
Date: Sun, 24 Mar 2024 11:44:29 +0100
Subject: [PATCH] return eventId and competitionId

---
 src/athletes/athlete.dto.ts            | 12 +++++--
 src/athletes/athlete.query.ts          |  6 ++++
 src/athletes/athlete.zod.ts            | 44 ++++++++++++++++++++++++++
 src/athletes/athletes.controller.ts    |  5 +++
 src/athletes/athletes.service.ts       |  7 +++-
 src/athletes/results/result.query.ts   |  2 ++
 src/athletes/results/result.service.ts |  2 ++
 src/athletes/results/result.zod.ts     | 22 +++++++++++++
 8 files changed, 97 insertions(+), 3 deletions(-)

diff --git a/src/athletes/athlete.dto.ts b/src/athletes/athlete.dto.ts
index 784e892..b60b21a 100644
--- a/src/athletes/athlete.dto.ts
+++ b/src/athletes/athlete.dto.ts
@@ -26,14 +26,18 @@ export class Performance {
   wind: number | null;
   @ApiProperty({ nullable: true, type: String })
   competition: string | null;
+  @ApiProperty({ nullable: true, type: Number })
+  competitionId: number | null;
+  @ApiProperty({ nullable: true, type: Number })
+  eventId: number | null;
   @ApiProperty({ nullable: true, type: String })
   category: string | null;
   @ApiProperty({ nullable: true, type: String })
   race: string | null;
   @ApiProperty({ nullable: true, type: Number })
   place: number | null;
-  @ApiProperty({ nullable: true, type: String, isArray: true })
-  records: string[] | null;
+  @ApiProperty({ type: String, isArray: true })
+  records: string[];
 }
 
 export class CurrentWorldRanking {
@@ -64,6 +68,10 @@ export class HonourResult {
   date: Date;
   @ApiProperty()
   competition: string;
+  @ApiProperty({ nullable: true, type: Number })
+  competitionId: number | null;
+  @ApiProperty({ nullable: true, type: Number })
+  eventId: number | null;
 }
 
 export class Honour {
diff --git a/src/athletes/athlete.query.ts b/src/athletes/athlete.query.ts
index f82c6d9..15fc024 100644
--- a/src/athletes/athlete.query.ts
+++ b/src/athletes/athlete.query.ts
@@ -21,6 +21,8 @@ const ATHLETE_QUERY = gql`
           resultScore
           wind
           records
+          eventId
+          competitionId
         }
         activeSeasons
       }
@@ -35,6 +37,8 @@ const ATHLETE_QUERY = gql`
           resultScore
           wind
           records
+          eventId
+          competitionId
         }
       }
       worldRankings {
@@ -56,6 +60,8 @@ const ATHLETE_QUERY = gql`
           venue
           mark
           date
+          eventId
+          competitionId
         }
         categoryName
       }
diff --git a/src/athletes/athlete.zod.ts b/src/athletes/athlete.zod.ts
index f701e2b..719a1ee 100644
--- a/src/athletes/athlete.zod.ts
+++ b/src/athletes/athlete.zod.ts
@@ -41,6 +41,28 @@ const Performance = z.object({
     }, z.number().nullable())
     .nullable(),
   records: z.array(z.string()),
+  eventId: z.preprocess((val) => {
+    if (val === '0') {
+      return null;
+    }
+    try {
+      return Number(val);
+    } catch (error) {
+      console.log(val, error);
+      return null;
+    }
+  }, z.number().nullable()),
+  competitionId: z.preprocess((val) => {
+    if (val === '0') {
+      return null;
+    }
+    try {
+      return Number(val);
+    } catch (error) {
+      console.log(val, error);
+      return null;
+    }
+  }, z.number().nullable()),
 });
 
 const WorldRanking = z.object({
@@ -63,6 +85,28 @@ const Result = z.object({
   place: z.preprocess((val) => {
     return Number(val);
   }, z.number()),
+  competitionId: z.preprocess((val) => {
+    if (val == null) {
+      return null;
+    }
+    try {
+      return Number(val);
+    } catch (error) {
+      console.log(val, error);
+      return null;
+    }
+  }, z.number().nullable()),
+  eventId: z.preprocess((val) => {
+    if (val == null) {
+      return null;
+    }
+    try {
+      return Number(val);
+    } catch (error) {
+      console.log(val, error);
+      return null;
+    }
+  }, z.number().nullable()),
 });
 
 export const Athlete = z.object({
diff --git a/src/athletes/athletes.controller.ts b/src/athletes/athletes.controller.ts
index 4eed35a..0345702 100644
--- a/src/athletes/athletes.controller.ts
+++ b/src/athletes/athletes.controller.ts
@@ -43,6 +43,11 @@ export class AthletesController {
     type: Number,
     required: false,
   })
+  @ApiOkResponse({
+    description: 'Returns the athlete results',
+    type: Performance,
+    isArray: true,
+  })
   async getResultsbyAthleteId(
     @Param('id', ParseIntPipe) id: number,
     @Query('year') year?: number,
diff --git a/src/athletes/athletes.service.ts b/src/athletes/athletes.service.ts
index dde627b..f688751 100644
--- a/src/athletes/athletes.service.ts
+++ b/src/athletes/athletes.service.ts
@@ -99,6 +99,8 @@ export class AthletesService {
               race: null,
               place: null,
               records: result.records,
+              competitionId: result.competitionId,
+              eventId: result.eventId,
             };
           }),
         personalbests: response.getSingleCompetitor.personalBests.results.map(
@@ -123,6 +125,8 @@ export class AthletesService {
               race: null,
               place: null,
               records: result.records,
+              competitionId: result.competitionId,
+              eventId: result.eventId,
             };
           },
         ),
@@ -143,7 +147,8 @@ export class AthletesService {
                 indoor,
                 competition: result.competition,
                 place: result.place,
-                resultScore: 0,
+                competitionId: result.competitionId,
+                eventId: result.eventId,
               };
             }),
           };
diff --git a/src/athletes/results/result.query.ts b/src/athletes/results/result.query.ts
index a28df9a..fcb79b8 100644
--- a/src/athletes/results/result.query.ts
+++ b/src/athletes/results/result.query.ts
@@ -22,6 +22,8 @@ const RESULTS_QUERY = gql`
           race
           place
           category
+          eventId
+          competitionId
         }
       }
     }
diff --git a/src/athletes/results/result.service.ts b/src/athletes/results/result.service.ts
index 6a5b31d..85f8bb6 100644
--- a/src/athletes/results/result.service.ts
+++ b/src/athletes/results/result.service.ts
@@ -63,6 +63,8 @@ export class ResultsService {
               location: parseVenue(result.venue),
               race: result.race,
               records: null,
+              competitionId: result.competitionId,
+              eventId: result.eventId,
             });
           });
         },
diff --git a/src/athletes/results/result.zod.ts b/src/athletes/results/result.zod.ts
index d274d1f..b99a3df 100644
--- a/src/athletes/results/result.zod.ts
+++ b/src/athletes/results/result.zod.ts
@@ -48,6 +48,28 @@ export const ResultsByEvent = z.object({
             }
           }, z.number()),
           category: z.string().nullable().default('F'),
+          competitionId: z.preprocess((val) => {
+            if (val == null) {
+              return null;
+            }
+            try {
+              return Number(val);
+            } catch (error) {
+              console.log(val, error);
+              return null;
+            }
+          }, z.number().nullable()),
+          eventId: z.preprocess((val) => {
+            if (val == null) {
+              return null;
+            }
+            try {
+              return Number(val);
+            } catch (error) {
+              console.log(val, error);
+              return null;
+            }
+          }, z.number().nullable()),
         }),
       ),
     }),