Skip to content

Commit

Permalink
feat(annotation-server): validate internal dtos
Browse files Browse the repository at this point in the history
Co-authored-by: Matti Schmidt <contact@schmidtchen.me>
  • Loading branch information
jannis-baum and DevSchmidtchen committed Jul 18, 2022
1 parent f11f9c1 commit 3a15341
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
7 changes: 7 additions & 0 deletions annotation-server/src/common/dtos/instantiable.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class InstantiableDto<T> {
constructor(dto: T) {
Object.entries(dto).forEach(([key, value]) => {
this[key] = value;
});
}
}
4 changes: 3 additions & 1 deletion annotation-server/src/guidelines/dtos/cpic-guideline.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { IsNotEmpty, IsNumber, IsString, IsUrl } from 'class-validator';

export class CpicGuidelineDto {
import { InstantiableDto } from '../../common/dtos/instantiable.dto';

export class CpicGuidelineDto extends InstantiableDto<CpicGuidelineDto> {
@IsNumber()
@IsNotEmpty()
id: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { IsNotEmpty, IsNumber, IsObject, IsString } from 'class-validator';

export class CpicRecommendationDto {
import { InstantiableDto } from '../../common/dtos/instantiable.dto';

export class CpicRecommendationDto extends InstantiableDto<CpicRecommendationDto> {
@IsString()
@IsNotEmpty()
drugid: string;
Expand Down
13 changes: 11 additions & 2 deletions annotation-server/src/guidelines/guidelines.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { HttpService } from '@nestjs/axios';
import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { InjectRepository } from '@nestjs/typeorm';
import { validateOrReject } from 'class-validator';
import { lastValueFrom } from 'rxjs';
import { FindOptionsOrder, FindOptionsOrderValue, Repository } from 'typeorm';

Expand Down Expand Up @@ -167,7 +168,13 @@ export class GuidelinesService {
);
const recommendationDtos: CpicRecommendationDto[] = (
await lastValueFrom(response)
).data;
).data.map(
(response: CpicRecommendationDto) =>
new CpicRecommendationDto(response),
);
await Promise.all(
recommendationDtos.map((dto) => validateOrReject(dto)),
);

const guidelinesByMeds: Map<string, Guideline[]> = new Map();
const guidelineErrors: Set<GuidelineError> = new Set();
Expand Down Expand Up @@ -234,7 +241,9 @@ export class GuidelinesService {
);
const guidelineDtos: CpicGuidelineDto[] = (
await lastValueFrom(response)
).data;
).data.map((data: CpicGuidelineDto) => new CpicGuidelineDto(data));
await Promise.all(guidelineDtos.map((dto) => validateOrReject(dto)));

const guidelineDtoById: Map<number, CpicGuidelineDto> = new Map();
guidelineDtos.forEach((guidelineDto) =>
guidelineDtoById.set(guidelineDto.id, guidelineDto),
Expand Down
8 changes: 5 additions & 3 deletions annotation-server/src/phenotypes/dtos/diplotype.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { IsNotEmpty, IsString } from 'class-validator';
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';

export class DiplotypeDto {
import { InstantiableDto } from '../../common/dtos/instantiable.dto';

export class DiplotypeDto extends InstantiableDto<DiplotypeDto> {
@IsString()
@IsNotEmpty()
genesymbol: string;
Expand All @@ -10,6 +12,6 @@ export class DiplotypeDto {
generesult: string;

@IsString()
@IsNotEmpty()
@IsOptional()
consultationtext: string;
}
7 changes: 5 additions & 2 deletions annotation-server/src/phenotypes/phenotypes.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpService } from '@nestjs/axios';
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { validateOrReject } from 'class-validator';
import { lastValueFrom } from 'rxjs';
import { FindOneOptions, Repository } from 'typeorm';

Expand Down Expand Up @@ -40,8 +41,10 @@ export class PhenotypesService {
},
);

const diplotypeDtos: DiplotypeDto[] = (await lastValueFrom(response))
.data;
const diplotypeDtos: DiplotypeDto[] = (
await lastValueFrom(response)
).data.map((data: DiplotypeDto) => new DiplotypeDto(data));
await Promise.all(diplotypeDtos.map((dto) => validateOrReject(dto)));

this.savePhenotypes(diplotypeDtos);

Expand Down

0 comments on commit 3a15341

Please sign in to comment.