Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
nimarion committed Oct 29, 2024
1 parent 28b7850 commit 055e05f
Show file tree
Hide file tree
Showing 18 changed files with 910 additions and 766 deletions.
59 changes: 35 additions & 24 deletions src/athlete_representatives/athlete_representative.zod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { formatIncompletePhoneNumber, isValidPhoneNumber, parseIncompletePhoneNumber, parsePhoneNumberWithError } from 'libphonenumber-js/max';
import {
formatIncompletePhoneNumber,
isValidPhoneNumber,
parseIncompletePhoneNumber,
parsePhoneNumberWithError,
} from 'libphonenumber-js/max';
import { LastnameSchema } from 'src/zod.schema';
import { z } from 'zod';

Expand Down Expand Up @@ -28,29 +33,35 @@ export const AthleteRepresentative = z.object({
countryCode: z.string().nullable(),
firstName: z.string(),
lastName: LastnameSchema,
email: z.array(z.string().nullable()).optional().default([]).transform((val) => {
if(val.length === 0) return null;
return sanitizeEmail(
val[0],
) as string;
}),
mobile: z.array(z.string().nullable()).optional().default([]).transform((val) => {
if(val.length === 0) return null;
if(!isValidPhoneNumber(val[0], 'US')) {
if(isValidPhoneNumber(parseIncompletePhoneNumber(val[0]), 'US')) {
val[0] = formatIncompletePhoneNumber(val[0]);
} else {
console.error("invalid phone number", val[0]);
email: z
.array(z.string().nullable())
.optional()
.default([])
.transform((val) => {
if (val.length === 0) return null;
return sanitizeEmail(val[0]) as string;
}),
mobile: z
.array(z.string().nullable())
.optional()
.default([])
.transform((val) => {
if (val.length === 0) return null;
if (!isValidPhoneNumber(val[0], 'US')) {
if (isValidPhoneNumber(parseIncompletePhoneNumber(val[0]), 'US')) {
val[0] = formatIncompletePhoneNumber(val[0]);
} else {
console.error('invalid phone number', val[0]);
return null;
}
}
try {
const phoneNumber = parsePhoneNumberWithError(val[0], 'US');
if (!phoneNumber) return null;
return phoneNumber.formatInternational();
} catch (error) {
console.error('error parsing phone number', val[0]);
return null;
}
}
try {
const phoneNumber = parsePhoneNumberWithError(val[0], 'US');
if(!phoneNumber) return null;
return phoneNumber.formatInternational();
} catch (error) {
console.error("error parsing phone number", val[0]);
return null;
}
}),
}),
});
23 changes: 13 additions & 10 deletions src/athletes/athlete.zod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { CompetitionNameSchema, DateSchema, LastnameSchema, MarkSchema, PlaceSchema, StringNumberSchema } from 'src/zod.schema';
import {
CompetitionNameSchema,
DateSchema,
LastnameSchema,
MarkSchema,
PlaceSchema,
StringNumberSchema,
} from 'src/zod.schema';
import { z } from 'zod';

export const BasicData = z.object({
Expand Down Expand Up @@ -61,11 +68,11 @@ export const Athlete = z.object({
categoryName: z.string(),
}),
),
athleteRepresentative: z.nullable(z.object({ _id: z.number() })).transform(
(val) => {
athleteRepresentative: z
.nullable(z.object({ _id: z.number() }))
.transform((val) => {
return val?._id ?? null;
},
),
}),
});

export const AthleteSearchSchema = z.object({
Expand All @@ -74,11 +81,7 @@ export const AthleteSearchSchema = z.object({
givenName: z.string(),
birthDate: DateSchema,
gender: z.string().transform((val) => {
return val === 'Men'
? 'MALE'
: val === 'Women'
? 'FEMALE'
: null
return val === 'Men' ? 'MALE' : val === 'Women' ? 'FEMALE' : null;
}),
country: z.string(),
});
22 changes: 10 additions & 12 deletions src/athletes/athletes.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ParseIntPipe,
NotFoundException,
Query,
BadRequestException,
} from '@nestjs/common';
import { AthletesService } from './athletes.service';
import { ApiOkResponse, ApiNotFoundResponse, ApiQuery } from '@nestjs/swagger';
Expand All @@ -26,13 +27,9 @@ export class AthletesController {
@Query('name') name: string,
): Promise<AthleteSearchResult[]> {
if (!name) {
throw new NotFoundException();
throw new BadRequestException('Query parameter "name" is required');
}
const athlete = await this.athletesService.searchAthlete(name);
if (!athlete) {
throw new NotFoundException();
}
return athlete;
return await this.athletesService.searchAthlete(name);
}

@Get(':id')
Expand All @@ -45,13 +42,14 @@ export class AthletesController {
@Param('id', ParseIntPipe) id: number,
): Promise<Athlete> {
if (id < 1 || id > 2147483647) {
throw new NotFoundException();
}
const athlete = await this.athletesService.getAthlete(id);
if (!athlete) {
throw new NotFoundException();
throw new BadRequestException('Invalid value for athlete id');
}
return athlete;
return this.athletesService.getAthlete(id).then((athlete) => {
if (!athlete) {
throw new NotFoundException();
}
return athlete;
});
}

@Get(':id/results')
Expand Down
Loading

0 comments on commit 055e05f

Please sign in to comment.