From 92d121083bd6a6b9da971c799f8cfc8e04f51c7a Mon Sep 17 00:00:00 2001 From: Haralan Dobrev Date: Tue, 22 Jun 2021 23:40:10 +0300 Subject: [PATCH] Expose only public town codes, not internal IDs This ensures we are consistent with other identifiers where we use common, public identifiers where possible like we do for election regions or sections. --- src/sections/api/town-exists.constraint.ts | 10 +++++----- src/sections/api/town.dto.ts | 2 +- src/sections/entities/sections.repository.ts | 4 ++-- src/sections/entities/towns.repository.ts | 8 ++++---- src/violations/api/violations-filters.dto.ts | 2 +- src/violations/entities/violations.repository.ts | 2 +- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/sections/api/town-exists.constraint.ts b/src/sections/api/town-exists.constraint.ts index 1a486b9a..726a0ce5 100644 --- a/src/sections/api/town-exists.constraint.ts +++ b/src/sections/api/town-exists.constraint.ts @@ -16,20 +16,20 @@ export class IsTownExistsConstraint implements ValidatorConstraintInterface { @Inject(TownsRepository) private readonly repo: TownsRepository, ) {} - async validate(townId?: number): Promise { - if (!townId) { + async validate(townCode?: number): Promise { + if (!townCode) { return true; } - if (typeof townId !== 'number' || townId <= 0) { + if (typeof townCode !== 'number' || townCode <= 0) { return false; } - return !!(await this.repo.findOne(townId)); + return !!(await this.repo.findOneByCode(townCode)); } defaultMessage?(validationArguments?: ValidationArguments): string { - return `Town with ID "${validationArguments.value}" does not exist!`; + return `Town with code "${validationArguments.value}" does not exist!`; } } diff --git a/src/sections/api/town.dto.ts b/src/sections/api/town.dto.ts index 495b4990..15ca77d1 100644 --- a/src/sections/api/town.dto.ts +++ b/src/sections/api/town.dto.ts @@ -11,7 +11,7 @@ import { IsTownExists } from './town-exists.constraint'; @Exclude() export class TownDto { @ApiProperty() - @Expose({ groups: ['read', 'create'] }) + @Expose({ name: 'code', groups: ['read', 'create'] }) @IsTownExists({ groups: ['create'] }) @IsNumber({}, { groups: ['create'] }) @Min(1, { groups: ['create'] }) diff --git a/src/sections/entities/sections.repository.ts b/src/sections/entities/sections.repository.ts index 7c0d1033..853e0331 100644 --- a/src/sections/entities/sections.repository.ts +++ b/src/sections/entities/sections.repository.ts @@ -39,7 +39,7 @@ export class SectionsRepository { } findByTownAndCityRegion( - townId: number, + townCode: number, cityRegionCode?: string, ): Promise { return this.repo.find({ @@ -50,7 +50,7 @@ export class SectionsRepository { }, }, where: (qb: SelectQueryBuilder
): void => { - qb.where({ town: townId }); + qb.andWhere('town.code = :townCode', { townCode }); if (cityRegionCode) { qb.innerJoinAndSelect( diff --git a/src/sections/entities/towns.repository.ts b/src/sections/entities/towns.repository.ts index 1e398b16..78f71d49 100644 --- a/src/sections/entities/towns.repository.ts +++ b/src/sections/entities/towns.repository.ts @@ -10,12 +10,12 @@ export class TownsRepository { private repo: Repository, ) {} - findOne(id: number): Promise { - return this.repo.findOne(id); + findOneByCode(code: number): Promise { + return this.repo.findOne({ code }); } - findOneOrFail(id: number): Promise { - return this.repo.findOneOrFail(id); + findOneByCodeOrFail(code: number): Promise { + return this.repo.findOneOrFail({ code }); } filter( diff --git a/src/violations/api/violations-filters.dto.ts b/src/violations/api/violations-filters.dto.ts index 85e0d08e..b100ff03 100644 --- a/src/violations/api/violations-filters.dto.ts +++ b/src/violations/api/violations-filters.dto.ts @@ -16,7 +16,7 @@ export class ViolationsFilters extends PageDTO { author: string; @Optional() - town: string; + town: number; @Optional() organization: number; diff --git a/src/violations/entities/violations.repository.ts b/src/violations/entities/violations.repository.ts index e162beff..4c35b696 100644 --- a/src/violations/entities/violations.repository.ts +++ b/src/violations/entities/violations.repository.ts @@ -57,7 +57,7 @@ export class ViolationsRepository { } if (filters.town) { - qb.andWhere('town.id = :town', { town: filters.town }); + qb.andWhere('town.code = :town', { town: filters.town }); } if (filters.author || filters.organization) {