diff --git a/src/athlete_representatives/athlete_representatives.service.ts b/src/athlete_representatives/athlete_representatives.service.ts index 27f7534..03271ab 100644 --- a/src/athlete_representatives/athlete_representatives.service.ts +++ b/src/athlete_representatives/athlete_representatives.service.ts @@ -16,6 +16,27 @@ export class AthleteRepresentativesService { this.graphQLClient = new GraphQLClient(process.env.STELLATE_ENDPOINT); } + sanitizeEmail(email) { + // Define regex for valid characters in the local part of the email + const localPartRegex = /[^a-zA-Z0-9._%+-]/g; + // Define regex for valid characters in the domain part of the email + const domainPartRegex = /[^a-zA-Z0-9.-]/g; + + // Split the email into local and domain parts + let [localPart, domainPart] = email.split('@'); + + // Sanitize the local part + localPart = localPart.replace(localPartRegex, ''); + + // Sanitize the domain part if it exists + if (domainPart) { + domainPart = domainPart.replace(domainPartRegex, ''); + } + + // Reassemble the sanitized email + return domainPart ? `${localPart}@${domainPart}` : localPart; + } + async getAthleteRepresentative( id: number, ): Promise { @@ -40,7 +61,9 @@ export class AthleteRepresentativesService { country: response.getAthleteRepresentativeProfile.countryCode, email: response.getAthleteRepresentativeProfile.email.length > 0 - ? response.getAthleteRepresentativeProfile.email[0] + ? this.sanitizeEmail( + response.getAthleteRepresentativeProfile.email[0], + ) : null, firstname: response.getAthleteRepresentativeProfile.firstName, lastname: formatLastname( diff --git a/src/athletes/athlete.dto.ts b/src/athletes/athlete.dto.ts index b60b21a..67850a1 100644 --- a/src/athletes/athlete.dto.ts +++ b/src/athletes/athlete.dto.ts @@ -104,4 +104,6 @@ export class Athlete { honours: Honour[]; @ApiProperty({ type: Number, isArray: true }) activeSeasons: number[]; + @ApiProperty({ nullable: true, type: Number }) + athleteRepresentativeId: number | null; } diff --git a/src/athletes/athlete.query.ts b/src/athletes/athlete.query.ts index 8abc0e2..29729e2 100644 --- a/src/athletes/athlete.query.ts +++ b/src/athletes/athlete.query.ts @@ -11,13 +11,7 @@ const ATHLETE_QUERY = gql` sexNameUrlSlug } athleteRepresentative { - name - email - mobile - telephone - email - countryCode - countryName + _id } seasonsBests { results { diff --git a/src/athletes/athlete.zod.ts b/src/athletes/athlete.zod.ts index 99fcb50..824fcc6 100644 --- a/src/athletes/athlete.zod.ts +++ b/src/athletes/athlete.zod.ts @@ -127,4 +127,5 @@ export const Athlete = z.object({ categoryName: z.string(), }), ), + athleteRepresentative: z.nullable(z.object({ _id: z.number() })), }); diff --git a/src/athletes/athletes.service.ts b/src/athletes/athletes.service.ts index f80a2bc..4997a70 100644 --- a/src/athletes/athletes.service.ts +++ b/src/athletes/athletes.service.ts @@ -59,6 +59,8 @@ export class AthletesService { ? 'FEMALE' : 'MALE' : worldRankingSex, + athleteRepresentativeId: + response.getSingleCompetitor.athleteRepresentative?._id ?? null, activeSeasons: response.getSingleCompetitor.seasonsBests.activeSeasons, currentWorldRankings: response.getSingleCompetitor.worldRankings.current.map((ranking) => {