Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
nimarion committed Nov 4, 2024
1 parent 65a5d64 commit 28990c0
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 122 deletions.
2 changes: 1 addition & 1 deletion src/athletes/athlete.zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const BasicData = z.object({
familyName: LastnameSchema,
birthDate: z.nullable(DateSchema),
countryCode: CountryCodeSchema,
sexNameUrlSlug: z.nullable(z.enum(['women', 'men'])),
sexNameUrlSlug: z.nullable(GenderSchema),
});

const Performance = z.object({
Expand Down
4 changes: 1 addition & 3 deletions src/athletes/athletes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ export class AthletesService {
: 'M'
: null;
const sex = basicData.sexNameUrlSlug
? basicData.sexNameUrlSlug === 'women'
? 'W'
: 'M'
? basicData.sexNameUrlSlug
: worldRankingSex;

function resultToPerformance(
Expand Down
4 changes: 2 additions & 2 deletions src/competitions/competition.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';
import { BaseAthlete, BasePerformance, Sex } from 'src/athletes/athlete.dto';
import { Discipline } from 'src/disciplines/discipline.dto';
import { BaseDiscipline, Discipline } from 'src/disciplines/discipline.dto';
import { Location } from 'src/location.dto';

export class ContactPerson {
Expand Down Expand Up @@ -102,7 +102,7 @@ export class CompetitionResultOptionDay {
day!: number
}

export class CompetitionResultOptionEvent extends Discipline{
export class CompetitionResultOptionEvent extends BaseDiscipline{
@ApiProperty()
id!: number;
@ApiProperty({
Expand Down
89 changes: 88 additions & 1 deletion src/competitions/competition.zod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DateSchema, GenderSchema, VenueSchema } from 'src/zod.schema';
import { DateSchema, DisciplineNameSchema, FullnameSchema, GenderSchema, MarkSchema, PlaceSchema, UrlSlugIdSchema, VenueSchema } from 'src/zod.schema';
import { z } from 'zod';

export const CompetitionOrganiserInfoSchema = z.object({
Expand Down Expand Up @@ -47,3 +47,90 @@ export const CompetitionSchema = z.object({
competitionGroup: z.string().nullable(),
competitionSubgroup: z.string().nullable(),
});

export const CompetitionResultsSchema = z.object({
competition: z.object({
venue: VenueSchema,
name: z.string(),
}),
parameters: z.object({
day: z.number().nullable(),
}),
eventTitles: z.array(
z.object({
rankingCategory: z.string(),
eventTitle: z.string().nullable(),
events: z.array(
z.object({
event: DisciplineNameSchema,
eventId: z.coerce.number(),
gender: GenderSchema,
perResultWind: z.boolean(),
withWind: z.boolean(),
races: z.array(
z.object({
date: z.nullable(DateSchema),
day: z.number().nullable(),
race: z.string(),
raceId: z.number(),
raceNumber: z.number(),
results: z.array(
z.object({
competitor: z.object({
teamMembers: z
.array(
z.object({
id: z.number(),
name: FullnameSchema,
urlSlug: UrlSlugIdSchema,
}),
)
.nullable(),
name: FullnameSchema,
urlSlug: z.nullable(UrlSlugIdSchema),
birthDate: z.nullable(DateSchema),
}),
mark: MarkSchema,
nationality: z.string(),
place: PlaceSchema,
records: z.string().transform((val) => {
if (val === '') return [];
return val
.split(',')
.map((record) => record.trim());
}),
wind: z.coerce
.number()
.nullable()
.catch(() => null),
}),
),
wind: z.coerce
.number()
.nullable()
.catch(() => null),
}),
),
}),
),
}),
),
options: z.object({
days: z.array(
z.object({
date: DateSchema,
day: z.number(),
}),
),
events: z.array(
z.object({
gender: GenderSchema,
id: z.number(),
name: DisciplineNameSchema,
combined: z.boolean().nullable().transform((val) => {
return val === null ? false : val;
}),
}),
),
}),
});
103 changes: 3 additions & 100 deletions src/competitions/competitions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,12 @@ import {
} from './competition.dto';
import {
CompetitionOrganiserInfoSchema,
CompetitionResultsSchema,
CompetitionSchema,
} from './competition.zod';
import {
parsePhoneNumber,
} from 'src/utils';
import mapDisciplineToCode, {
isTechnical,
} from 'src/discipline.utils';
import {
DateSchema,
DisciplineNameSchema,
FullnameSchema,
GenderSchema,
MarkSchema,
PlaceSchema,
UrlSlugIdSchema,
VenueSchema,
} from 'src/zod.schema';
import { Sex } from 'src/athletes/athlete.dto';
import { performanceToFloat } from 'src/performance-conversion';

Expand Down Expand Up @@ -76,7 +64,7 @@ export class CompetitionsService {
return {
email: contact.email,
name: contact.name,
phone: parsePhoneNumber(contact.phoneNumber),
phone: contact.phoneNumber,
role: contact.title,
};
},
Expand Down Expand Up @@ -158,92 +146,7 @@ export class CompetitionsService {
});
const response = z
.object({
getCalendarCompetitionResults: z.object({
competition: z.object({
venue: VenueSchema,
name: z.string(),
}),
parameters: z.object({
day: z.number().nullable(),
}),
eventTitles: z.array(
z.object({
rankingCategory: z.string(),
eventTitle: z.string().nullable(),
events: z.array(
z.object({
event: DisciplineNameSchema,
eventId: z.coerce.number(),
gender: GenderSchema,
perResultWind: z.boolean(),
withWind: z.boolean(),
races: z.array(
z.object({
date: z.nullable(DateSchema),
day: z.number().nullable(),
race: z.string(),
raceId: z.number(),
raceNumber: z.number(),
results: z.array(
z.object({
competitor: z.object({
teamMembers: z
.array(
z.object({
id: z.number(),
name: FullnameSchema,
urlSlug: UrlSlugIdSchema,
}),
)
.nullable(),
name: FullnameSchema,
urlSlug: z.nullable(UrlSlugIdSchema),
birthDate: z.nullable(DateSchema),
}),
mark: MarkSchema,
nationality: z.string(),
place: PlaceSchema,
records: z.string().transform((val) => {
if (val === '') return [];
return val
.split(',')
.map((record) => record.trim());
}),
wind: z.coerce
.number()
.nullable()
.catch(() => null),
}),
),
wind: z.coerce
.number()
.nullable()
.catch(() => null),
}),
),
}),
),
}),
),
options: z.object({
days: z.array(
z.object({
date: DateSchema,
day: z.number(),
}),
),
events: z.array(
z.object({
gender: GenderSchema,
id: z.number(),
name: DisciplineNameSchema,
combined: z.boolean().nullable().transform((val) => {
return val === null ? false : val;
}),
}),
),
}),
}),
getCalendarCompetitionResults:CompetitionResultsSchema,
})
.parse(data);

Expand Down
9 changes: 6 additions & 3 deletions src/disciplines/discipline.dto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { ApiProperty } from '@nestjs/swagger';

export class Discipline {
export class BaseDiscipline {
@ApiProperty()
discipline!: string;
@ApiProperty()
disciplineCode!: string;
@ApiProperty({ required: false })
isTechnical?: boolean;
}

export class Discipline extends BaseDiscipline{
@ApiProperty({ description: "Is also true for disciplines where the 'higher' result is better like Hep/Decathlon and One hour races" })
isTechnical!: boolean;
}
6 changes: 3 additions & 3 deletions src/disciplines/disciplines.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Controller, Get } from '@nestjs/common';
import { ApiOkResponse } from '@nestjs/swagger';
import { Discipline } from './discipline.dto';
import { BaseDiscipline } from './discipline.dto';
import { DisciplinesService } from './disciplines.service';

@Controller('disciplines')
Expand All @@ -9,11 +9,11 @@ export class DisciplinesController {

@Get()
@ApiOkResponse({
type: Discipline,
type: BaseDiscipline,
isArray: true,
description: 'Returns all disciplines, all marked as non-technical!',
})
findAll(): Promise<Discipline[]> {
findAll(): Promise<BaseDiscipline[]> {
return this.disciplinesService.findAll();
}
}
4 changes: 2 additions & 2 deletions src/disciplines/disciplines.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { GraphQLClient } from 'graphql-request';
import { z } from 'zod';
import { Discipline } from './discipline.dto';
import { BaseDiscipline } from './discipline.dto';

import { GraphqlService } from 'src/graphql/graphql.service';
import { DISCIPLINES_QUERY } from 'src/disciplines/disciplines.query';
Expand All @@ -14,7 +14,7 @@ export class DisciplinesService {
this.graphQLClient = this.graphqlService.getClient();
}

async findAll(): Promise<Discipline[]> {
async findAll(): Promise<BaseDiscipline[]> {
const data = await this.graphQLClient.request(DISCIPLINES_QUERY);
const reponse = z
.object({
Expand Down
9 changes: 2 additions & 7 deletions src/performance-conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@ export function performanceToFloat({
if(performance === ""){
return null;
}
// Combined Events
if(!isNaN(Number(performance)) && Number(performance) % 1 === 0) {
// Combined Events or One Hour Race
if(technical && !isNaN(Number(performance)) && Number(performance) % 1 === 0) {
return Number(performance);
}
// One Hour Race
if (performance == parseInt(performance).toString()) {
return null;
}

performance = performance.trim().replace(',', '.');

if (technical) {
Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function extractName(name: string) {
lastname: formatLastname(match[2].trim()),
};
}
console.error('Could not extract name from', name);
return null;
}

Expand Down
2 changes: 2 additions & 0 deletions src/zod.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
extractName,
formatLastname,
formatSex,
parsePhoneNumber,
parseVenue,
} from './utils';
import { findCountryByCode, mapCountryToCode } from './country.utils';
Expand All @@ -28,6 +29,7 @@ export const CountryCodeSchema = z.string().transform((val) => {
return val;
})
export const VenueSchema = z.string().transform(parseVenue);
export const PhoneSchema = z.string().transform(parsePhoneNumber);

// spain/alvaro-martin-14410246
export const UrlSlugIdSchema = z.string().transform((val) => {
Expand Down

0 comments on commit 28990c0

Please sign in to comment.