From b8f63f0bad6f1617739c7ceabe4303cf09c382cb Mon Sep 17 00:00:00 2001 From: Niklas Marion Date: Fri, 22 Dec 2023 20:24:22 +0100 Subject: [PATCH] static map disciplineCode if missing --- .github/workflows/monitoring.yml | 11 ++++++++++- src/athletes/athlete.zod.ts | 6 +++--- src/athletes/athletes.service.ts | 4 +++- src/athletes/results/result.zod.ts | 2 +- src/discipline.utils.ts | 9 +++++++++ src/disciplines.json | 1 + tsconfig.json | 3 ++- updateDisciplines.sh | 3 +++ 8 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 src/discipline.utils.ts create mode 100644 src/disciplines.json create mode 100644 updateDisciplines.sh diff --git a/.github/workflows/monitoring.yml b/.github/workflows/monitoring.yml index 4d4daa9..cdbcb21 100644 --- a/.github/workflows/monitoring.yml +++ b/.github/workflows/monitoring.yml @@ -13,4 +13,13 @@ jobs: - name: Run monitoring script uses: actions/checkout@v4 - run: | - bash monitoring.sh \ No newline at end of file + bash monitoring.sh + - name: Update Disciplines + run: | + bash updateDisciplines.sh + git config user.name Github Action + git config user.email actions@github.com + git add src/disciplines.json + timestamp=$(date -u) + git commit -m "update disciplines: ${timestamp}" || exit 0 + git push \ No newline at end of file diff --git a/src/athletes/athlete.zod.ts b/src/athletes/athlete.zod.ts index 231a46c..1c435d3 100644 --- a/src/athletes/athlete.zod.ts +++ b/src/athletes/athlete.zod.ts @@ -29,7 +29,7 @@ const Performance = z.object({ return date; }), discipline: z.string(), - disciplineCode: z.string(), + disciplineCode: z.string().nullable(), mark: z.string(), venue: z.string(), indoor: z.boolean(), @@ -59,10 +59,10 @@ const Result = z.object({ return date; }), discipline: z.string(), - disciplineCode: z.string(), + disciplineCode: z.string().nullable(), mark: z.string(), venue: z.string(), - indoor: z.boolean(), + indoor: z.boolean().nullable().default(false), competition: z.string(), place: z.preprocess((val) => { return Number(val); diff --git a/src/athletes/athletes.service.ts b/src/athletes/athletes.service.ts index d05d397..0b5c17d 100644 --- a/src/athletes/athletes.service.ts +++ b/src/athletes/athletes.service.ts @@ -6,6 +6,7 @@ import { Athlete } from './athlete.dto'; import ATHLETE_QUERY from './athlete.query'; import { Athlete as AthleteSchema } from './athlete.zod'; import parseVenue from './venue.utils'; +import mapDisciplineToCode from 'src/discipline.utils'; @Injectable() export class AthletesService { @@ -78,7 +79,8 @@ export class AthletesService { return { date: result.date, discipline: result.discipline, - disciplineCode: result.disciplineCode, + disciplineCode: + result.disciplineCode || mapDisciplineToCode(result.discipline), mark: result.mark.replace(/[^0-9:.]/g, ''), venue: result.venue, location, diff --git a/src/athletes/results/result.zod.ts b/src/athletes/results/result.zod.ts index 90d7053..f24a31b 100644 --- a/src/athletes/results/result.zod.ts +++ b/src/athletes/results/result.zod.ts @@ -3,7 +3,7 @@ import { z } from 'zod'; export const ResultsByEvent = z.object({ resultsByEvent: z.array( z.object({ - disciplineCode: z.string(), + disciplineCode: z.string().nullable(), discipline: z.string(), indoor: z.boolean(), results: z.array( diff --git a/src/discipline.utils.ts b/src/discipline.utils.ts new file mode 100644 index 0000000..e7f1d07 --- /dev/null +++ b/src/discipline.utils.ts @@ -0,0 +1,9 @@ +import * as disciplines from './disciplines.json'; + +export default function mapDisciplineToCode(discipline: string): string { + const disciplineItem = disciplines.find((d) => d.discipline === discipline); + if (disciplineItem) { + return disciplineItem.disciplineCode; + } + throw new Error(`Discipline ${discipline} not found`); +} diff --git a/src/disciplines.json b/src/disciplines.json new file mode 100644 index 0000000..1dfbd80 --- /dev/null +++ b/src/disciplines.json @@ -0,0 +1 @@ +[{"discipline":"50 Metres","disciplineCode":"50"},{"discipline":"55 Metres","disciplineCode":"55"},{"discipline":"60 Metres","disciplineCode":"60"},{"discipline":"100 Yards","disciplineCode":"100y"},{"discipline":"100 Metres","disciplineCode":"100"},{"discipline":"150 Metres","disciplineCode":"150"},{"discipline":"200 Metres","disciplineCode":"200"},{"discipline":"200 Metres Short Track","disciplineCode":"200sh"},{"discipline":"300 Metres","disciplineCode":"300"},{"discipline":"300 Metres Short Track","disciplineCode":"300sh"},{"discipline":"400 Metres","disciplineCode":"400"},{"discipline":"400 Metres Short Track","disciplineCode":"400sh"},{"discipline":"500 Metres","disciplineCode":"500"},{"discipline":"500 Metres Short Track","disciplineCode":"500sh"},{"discipline":"600 Metres","disciplineCode":"600"},{"discipline":"600 Metres Short Track","disciplineCode":"600sh"},{"discipline":"800 Metres","disciplineCode":"800"},{"discipline":"800 Metres Short Track","disciplineCode":"800sh"},{"discipline":"1000 Metres","disciplineCode":"1000"},{"discipline":"1000 Metres Short Track","disciplineCode":"1000sh"},{"discipline":"1500 Metres","disciplineCode":"1500"},{"discipline":"1500 Metres Short Track","disciplineCode":"1500sh"},{"discipline":"Mile","disciplineCode":"MILE"},{"discipline":"Mile Short Track","disciplineCode":"MILEsh"},{"discipline":"2000 Metres","disciplineCode":"2000"},{"discipline":"2000 Metres Short Track","disciplineCode":"2000sh"},{"discipline":"3000 Metres","disciplineCode":"3000"},{"discipline":"3000 Metres Short Track","disciplineCode":"3000sh"},{"discipline":"2 Miles","disciplineCode":"2MLS"},{"discipline":"2 Miles Short Track","disciplineCode":"2MLSsh"},{"discipline":"5000 Metres","disciplineCode":"5000"},{"discipline":"5000 Metres Short Track","disciplineCode":"5000sh"},{"discipline":"5 Kilometres Road","disciplineCode":"5RR"},{"discipline":"10,000 Metres","disciplineCode":"10K"},{"discipline":"10,000 Metres Short Track","disciplineCode":"10Ksh"},{"discipline":"10 Kilometres Road","disciplineCode":"10RR"},{"discipline":"12 Kilometres Road","disciplineCode":"12RR"},{"discipline":"15,000 Metres","disciplineCode":"15K"},{"discipline":"15 Kilometres Road","disciplineCode":"15RR"},{"discipline":"20,000 Metres","disciplineCode":"20K"},{"discipline":"20 Kilometres Road","disciplineCode":"20RR"},{"discipline":"One Hour","disciplineCode":"HOUR"},{"discipline":"Half Marathon","disciplineCode":"HMAR"},{"discipline":"25,000 Metres","disciplineCode":"25K"},{"discipline":"25 Kilometres Road","disciplineCode":"25RR"},{"discipline":"30,000 Metres","disciplineCode":"30K"},{"discipline":"30 Kilometres Road","disciplineCode":"30RR"},{"discipline":"50 Kilometres Road","disciplineCode":"50RR"},{"discipline":"Marathon","disciplineCode":"MAR"},{"discipline":"100 Kilometres Road","disciplineCode":"100K"},{"discipline":"2000 Metres Steeplechase","disciplineCode":"2KSC"},{"discipline":"2000 Metres Steeplechase Short Track","disciplineCode":"2KSCsh"},{"discipline":"2000 Metres Steeplechase (0.84)","disciplineCode":"2KSC84"},{"discipline":"3000 Metres Steeplechase","disciplineCode":"3KSC"},{"discipline":"3000 Metres Steeplechase Short Track","disciplineCode":"3KSCsh"},{"discipline":"50 Metres Hurdles","disciplineCode":"50H"},{"discipline":"50 Metres Hurdles (91.4cm)","disciplineCode":"50HB"},{"discipline":"50 Metres Hurdles (76.2cm)","disciplineCode":"50HG"},{"discipline":"55 Metres Hurdles","disciplineCode":"55H"},{"discipline":"60 Metres Hurdles","disciplineCode":"60H"},{"discipline":"60 Metres Hurdles (99.0cm)","disciplineCode":"60HJ"},{"discipline":"60 Metres Hurdles (91.4cm)","disciplineCode":"60HY"},{"discipline":"60 Metres Hurdles (76.2cm)","disciplineCode":"60HYG"},{"discipline":"80 Metres Hurdles","disciplineCode":"80H"},{"discipline":"100 Metres Hurdles","disciplineCode":"100H"},{"discipline":"100 Metres Hurdles (76.2cm)","disciplineCode":"100HY"},{"discipline":"110 Metres Hurdles (99.0cm)","disciplineCode":"110HJ"},{"discipline":"110 Metres Hurdles (91.4cm)","disciplineCode":"110HY"},{"discipline":"110 Metres Hurdles","disciplineCode":"110H"},{"discipline":"200 Metres Hurdles","disciplineCode":"200H"},{"discipline":"300 Metres Hurdles","disciplineCode":"300H"},{"discipline":"300 Metres Hurdles (84.0cm)","disciplineCode":"300HY"},{"discipline":"400m hurdles (84.0cm)","disciplineCode":"400HY"},{"discipline":"400 Metres Hurdles","disciplineCode":"400H"},{"discipline":"400 Metres Hurdles Short Track","disciplineCode":"400Hsh"},{"discipline":"High Jump","disciplineCode":"HJ"},{"discipline":"Pole Vault","disciplineCode":"PV"},{"discipline":"Long Jump","disciplineCode":"LJ"},{"discipline":"Triple Jump","disciplineCode":"TJ"},{"discipline":"Shot Put","disciplineCode":"SP"},{"discipline":"Shot Put (6kg)","disciplineCode":"SPJ"},{"discipline":"Shot Put (5kg)","disciplineCode":"SPY"},{"discipline":"Shot Put (3kg)","disciplineCode":"SPYG"},{"discipline":"Shot Put (4kg)","disciplineCode":"SPYG4"},{"discipline":"Discus Throw","disciplineCode":"DT"},{"discipline":"Discus Throw (1.750kg)","disciplineCode":"DTJ"},{"discipline":"Discus Throw (1.500kg)","disciplineCode":"DTY"},{"discipline":"Hammer Throw","disciplineCode":"HT"},{"discipline":"Hammer Throw (6kg)","disciplineCode":"HTJ"},{"discipline":"Hammer Throw (5kg)","disciplineCode":"HTY"},{"discipline":"Hammer Throw (3kg)","disciplineCode":"HTYG"},{"discipline":"Javelin Throw","disciplineCode":"JT"},{"discipline":"Javelin Throw (700g)","disciplineCode":"JTY"},{"discipline":"Javelin Throw (500g)","disciplineCode":"JTYG"},{"discipline":"Pentathlon","disciplineCode":"PEN"},{"discipline":"Pentathlon Short Track","disciplineCode":"PENsh"},{"discipline":"Pentathlon Girls Short Track","disciplineCode":"PENGsh"},{"discipline":"Heptathlon U20 Short Track","disciplineCode":"HEPJsh"},{"discipline":"Heptathlon Short Track","disciplineCode":"HEPsh"},{"discipline":"Heptathlon","disciplineCode":"HEP"},{"discipline":"Heptathlon-100mH 76.2cm","disciplineCode":"HEPY"},{"discipline":"Heptathlon U18","disciplineCode":"HEPG"},{"discipline":"Heptathlon U18 Short Track","disciplineCode":"HEPBsh"},{"discipline":"Octathlon U16","disciplineCode":"OCT"},{"discipline":"Octathlon Boys","disciplineCode":"OCTY"},{"discipline":"Decathlon","disciplineCode":"DEC"},{"discipline":"Decathlon (62-84)","disciplineCode":"DEC6284"},{"discipline":"Decathlon U20","disciplineCode":"DECJ"},{"discipline":"Decathlon (1.06) U20","disciplineCode":"DECJO"},{"discipline":"Decathlon Boys","disciplineCode":"DECY"},{"discipline":"Mile Race Walk","disciplineCode":"MILEW"},{"discipline":"Mile Race Walk Short Track","disciplineCode":"MILEWsh"},{"discipline":"3000 Metres Race Walk","disciplineCode":"3KW"},{"discipline":"3000 Metres Race Walk Short Track","disciplineCode":"3KWsh"},{"discipline":"5000 Metres Race Walk","disciplineCode":"5KW"},{"discipline":"5000 Metres Race Walk Short Track","disciplineCode":"5KWsh"},{"discipline":"5 Kilometres Race Walk","disciplineCode":"5KR"},{"discipline":"10,000 Metres Race Walk","disciplineCode":"10KW"},{"discipline":"10,000 Metres Race Walk Short Track","disciplineCode":"10KWsh"},{"discipline":"10 Kilometres Race Walk","disciplineCode":"10KR"},{"discipline":"15 Kilometers Race Walk","disciplineCode":"15KR"},{"discipline":"20,000 Metres Race Walk","disciplineCode":"20KW"},{"discipline":"20 Kilometres Race Walk","disciplineCode":"20KR"},{"discipline":"2 Hours Race Walk","disciplineCode":"2HW"},{"discipline":"30,000 Metres Race Walk","disciplineCode":"30KW"},{"discipline":"30 Kilometres Race Walk","disciplineCode":"30KR"},{"discipline":"35 Kilometres Race Walk","disciplineCode":"35KR"},{"discipline":"50,000 Metres Race Walk","disciplineCode":"50KW"},{"discipline":"50 Kilometres Race Walk","disciplineCode":"50KR"},{"discipline":"4x100 Metres Relay","disciplineCode":"4X1"},{"discipline":"4x200 Metres Relay","disciplineCode":"4X2"},{"discipline":"4x200 Metres Relay Short Track","disciplineCode":"4X2sh"},{"discipline":"4x400 Metres Relay","disciplineCode":"4X4"},{"discipline":"4x400 Metres Relay Short Track","disciplineCode":"4X4sh"},{"discipline":"4x800 Metres Relay","disciplineCode":"4X8"},{"discipline":"4x800 Metres Relay Short Track","disciplineCode":"4X8sh"},{"discipline":"4xMile Relay","disciplineCode":"4XMILE"},{"discipline":"4xMile Relay Short Track","disciplineCode":"4XMILsh"},{"discipline":"4x1500 Metres Relay","disciplineCode":"4X15"},{"discipline":"Sprint Medley Relay","disciplineCode":"MEAD"},{"discipline":"8x100 Metres Relay","disciplineCode":"8X1"},{"discipline":"Road Relay","disciplineCode":"EKID"},{"discipline":"Distance Medley Relay","disciplineCode":"DISMEAD"},{"discipline":"Distance Medley Relay Short Track","disciplineCode":"DISMEsh"},{"discipline":"Cross Country Relay","disciplineCode":"MIXREL"},{"discipline":"2x2x400m Relay","disciplineCode":"2X2X4"},{"discipline":"Shuttle Hurdles","disciplineCode":"SHUTHUR"},{"discipline":"3x100 Metres Relay","disciplineCode":"3X1"},{"discipline":"Hour Race Walk","disciplineCode":"1HW"},{"discipline":"600 Metres Road","disciplineCode":"600R"},{"discipline":"Mile Road","disciplineCode":"1MR"},{"discipline":"5 Miles Road","disciplineCode":"5MR"},{"discipline":"10 Miles Road","disciplineCode":"10MR"},{"discipline":"8 Kilometres Road","disciplineCode":"8RR"},{"discipline":"Cross Country Senior Race","disciplineCode":"XSE"},{"discipline":"Cross Country U20 Race","disciplineCode":"XJ"},{"discipline":"Cross Country U23 Race","disciplineCode":"XU23"},{"discipline":"Cross Country Short Race","disciplineCode":"XC"},{"discipline":"Cross Country Long Race","disciplineCode":"XS"},{"discipline":"Cross Country 4000m","disciplineCode":"XC4KM"},{"discipline":"24 Hours","disciplineCode":"24H"},{"discipline":"Marathon Race Walk Mixed Relay","disciplineCode":"MARMXT"},{"discipline":"100m Amputee","disciplineCode":"100A"},{"discipline":"200m Amputee","disciplineCode":"200A"},{"discipline":"100m Blind","disciplineCode":"100B"},{"discipline":"200m Blind","disciplineCode":"200B"},{"discipline":"400m Blind","disciplineCode":"400B"},{"discipline":"400m Cerebral Palsy","disciplineCode":"400CP"},{"discipline":"800m Masters","disciplineCode":"800MA"},{"discipline":"800m Wheelchair","disciplineCode":"800W"},{"discipline":"1500m Wheelchair","disciplineCode":"1500W"},{"discipline":"100m Wheelchair","disciplineCode":"100W"},{"discipline":"800 Metres T54","disciplineCode":"800T54"},{"discipline":"Javelin Throw Wheelchair","disciplineCode":"JTW"},{"discipline":"400 Metres T53","disciplineCode":"400T53"},{"discipline":"400m Masters","disciplineCode":"400MA"},{"discipline":"200m Visually Impaired","disciplineCode":"200VI"},{"discipline":"200m Wheelchair","disciplineCode":"200W"},{"discipline":"Road Race","disciplineCode":"RR"}] \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index adb614c..7a64609 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,6 +16,7 @@ "noImplicitAny": false, "strictBindCallApply": false, "forceConsistentCasingInFileNames": false, - "noFallthroughCasesInSwitch": false + "noFallthroughCasesInSwitch": false, + "resolveJsonModule": true } } diff --git a/updateDisciplines.sh b/updateDisciplines.sh new file mode 100644 index 0000000..f09e7cd --- /dev/null +++ b/updateDisciplines.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +curl -o src/disciplines.json --silent --show-error --fail https://worldathletics.pfingstsportfest.de/disciplines \ No newline at end of file