Skip to content

Commit

Permalink
refactor: use leadTime enum instead of entity
Browse files Browse the repository at this point in the history
  • Loading branch information
jannisvisser committed Jan 24, 2025
1 parent 0c0ecd6 commit 71806ef
Show file tree
Hide file tree
Showing 18 changed files with 74 additions and 111 deletions.
16 changes: 2 additions & 14 deletions interfaces/IBF-dashboard/src/app/services/api.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map, tap } from 'rxjs/operators';
import { tap } from 'rxjs/operators';
import { DEBUG_LOG } from 'src/app/config';
import { Country, DisasterType } from 'src/app/models/country.model';
import { CountryTriggers } from 'src/app/models/country-triggers.model';
Expand Down Expand Up @@ -154,19 +154,7 @@ export class ApiService {
if (minimalInfo) {
params = params.append('minimalInfo', String(minimalInfo));
}
return this.get(path, false, params).pipe(
map((countries) => {
return countries.map((country) => {
country.countryDisasterSettings?.map((disaster) => {
disaster.activeLeadTimes = disaster.activeLeadTimes.map(
(leadTime) => leadTime.leadTimeName,
);
return disaster;
});
return country;
});
}),
);
return this.get(path, false, params);
}

getTyphoonTrack(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class RemoveLeadTimeEntity1737713818319 implements MigrationInterface {
name = 'RemoveLeadTimeEntity1737713818319';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "IBF-app"."admin-area-dynamic-data" DROP CONSTRAINT "FK_cb6c2fc0f20bcf164afc3e0301b"`,
);
await queryRunner.query(
`ALTER TABLE "IBF-app"."lines-data-dynamic-status" DROP CONSTRAINT "FK_2456d6fa601344982bacf47f6b9"`,
);
await queryRunner.query(
`ALTER TABLE "IBF-app"."dynamic-point-data" DROP CONSTRAINT "FK_289a1f52e25e270d9a28bd9d35a"`,
);
await queryRunner.query(
`ALTER TABLE "IBF-app"."typhoon-track" DROP CONSTRAINT "FK_7911edcf74dd1da5905dbc7e44e"`,
);
await queryRunner.query(
`ALTER TABLE "IBF-app"."country-disaster-settings" ADD "activeLeadTimes" json`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "IBF-app"."country-disaster-settings" DROP COLUMN "activeLeadTimes"`,
);
await queryRunner.query(
`ALTER TABLE "IBF-app"."typhoon-track" ADD CONSTRAINT "FK_7911edcf74dd1da5905dbc7e44e" FOREIGN KEY ("leadTime") REFERENCES "IBF-app"."lead-time"("leadTimeName") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "IBF-app"."dynamic-point-data" ADD CONSTRAINT "FK_289a1f52e25e270d9a28bd9d35a" FOREIGN KEY ("leadTime") REFERENCES "IBF-app"."lead-time"("leadTimeName") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "IBF-app"."lines-data-dynamic-status" ADD CONSTRAINT "FK_2456d6fa601344982bacf47f6b9" FOREIGN KEY ("leadTime") REFERENCES "IBF-app"."lead-time"("leadTimeName") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "IBF-app"."admin-area-dynamic-data" ADD CONSTRAINT "FK_cb6c2fc0f20bcf164afc3e0301b" FOREIGN KEY ("leadTime") REFERENCES "IBF-app"."lead-time"("leadTimeName") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {

import { CountryEntity } from '../country/country.entity';
import { DisasterEntity } from '../disaster/disaster.entity';
import { LeadTimeEntity } from '../lead-time/lead-time.entity';
import { DynamicIndicator } from './enum/dynamic-data-unit';
import { LeadTime } from './enum/lead-time.enum';

@Entity('admin-area-dynamic-data')
export class AdminAreaDynamicDataEntity {
Expand Down Expand Up @@ -53,7 +53,6 @@ export class AdminAreaDynamicDataEntity {
@Column({ nullable: true, type: 'real' })
public value: number;

@ManyToOne((): typeof LeadTimeEntity => LeadTimeEntity)
@JoinColumn({ name: 'leadTime', referencedColumnName: 'leadTimeName' })
public leadTime: string;
@Column({ nullable: true })
public leadTime: LeadTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class UploadAdminAreaDynamicDataDto {
@ApiProperty({ example: LeadTime.month0 })
@IsNotEmpty()
@IsString()
@IsEnum(LeadTime)
public leadTime: LeadTime;

@ApiProperty({ example: DynamicIndicator.populationAffected })
Expand Down
12 changes: 3 additions & 9 deletions services/API-service/src/api/country/country-disaster.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import {
Column,
Entity,
JoinColumn,
JoinTable,
ManyToMany,
ManyToOne,
PrimaryGeneratedColumn,
} from 'typeorm';

import { LeadTime } from '../admin-area-dynamic-data/enum/lead-time.enum';
import { DisasterType } from '../disaster/disaster-type.enum';
import { DisasterEntity } from '../disaster/disaster.entity';
import { LeadTimeEntity } from '../lead-time/lead-time.entity';
import { AdminLevel } from './admin-level.enum';
import { CountryEntity } from './country.entity';

Expand Down Expand Up @@ -48,12 +46,8 @@ export class CountryDisasterSettingsEntity {
public defaultAdminLevel: AdminLevel;

@ApiProperty()
@ManyToMany(
(): typeof LeadTimeEntity => LeadTimeEntity,
(leadTime): CountryDisasterSettingsEntity[] => leadTime.countries,
)
@JoinTable()
public activeLeadTimes: LeadTimeEntity[];
@Column('json', { nullable: true })
public activeLeadTimes: LeadTime[];

@ApiProperty({ example: [3, 10] })
@Column('json', { nullable: true })
Expand Down
2 changes: 0 additions & 2 deletions services/API-service/src/api/country/country.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { DisasterEntity } from '../disaster/disaster.entity';
import { LeadTimeEntity } from '../lead-time/lead-time.entity';
import { NotificationInfoEntity } from '../notification/notifcation-info.entity';
import { UserModule } from '../user/user.module';
import { CountryDisasterSettingsEntity } from './country-disaster.entity';
Expand All @@ -19,7 +18,6 @@ import { CountryService } from './country.service';
CountryEntity,
DisasterEntity,
CountryDisasterSettingsEntity,
LeadTimeEntity,
NotificationInfoEntity,
]),
],
Expand Down
19 changes: 1 addition & 18 deletions services/API-service/src/api/country/country.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { In, Repository } from 'typeorm';
import countries from '../../scripts/json/countries.json';
import notificationInfos from '../../scripts/json/notification-info.json';
import { DisasterEntity } from '../disaster/disaster.entity';
import { LeadTimeEntity } from '../lead-time/lead-time.entity';
import { NotificationInfoEntity } from '../notification/notifcation-info.entity';
import { CountryDisasterSettingsEntity } from './country-disaster.entity';
import { CountryEntity } from './country.entity';
Expand All @@ -19,7 +18,6 @@ describe('CountryService', () => {
let countryRepository: Repository<CountryEntity>;
let disasterRepository: Repository<DisasterEntity>;
let countryDisasterSettingsRepository: Repository<CountryDisasterSettingsEntity>;
let leadTimeRepository: Repository<LeadTimeEntity>;
let notificationInfoRepository: Repository<NotificationInfoEntity>;

const relations = [
Expand All @@ -45,10 +43,6 @@ describe('CountryService', () => {
provide: getRepositoryToken(CountryDisasterSettingsEntity),
useClass: Repository,
},
{
provide: getRepositoryToken(LeadTimeEntity),
useClass: Repository,
},
{
provide: getRepositoryToken(NotificationInfoEntity),
useClass: Repository,
Expand All @@ -66,9 +60,6 @@ describe('CountryService', () => {
countryDisasterSettingsRepository = module.get<
Repository<CountryDisasterSettingsEntity>
>(getRepositoryToken(CountryDisasterSettingsEntity));
leadTimeRepository = module.get<Repository<LeadTimeEntity>>(
getRepositoryToken(LeadTimeEntity),
);
notificationInfoRepository = module.get<Repository<NotificationInfoEntity>>(
getRepositoryToken(NotificationInfoEntity),
);
Expand Down Expand Up @@ -114,9 +105,6 @@ describe('CountryService', () => {
jest
.spyOn(countryRepository, 'save')
.mockResolvedValue(new CountryEntity());
jest
.spyOn(leadTimeRepository, 'find')
.mockResolvedValue([new LeadTimeEntity()]);
jest
.spyOn(countryDisasterSettingsRepository, 'save')
.mockResolvedValue(new CountryDisasterSettingsEntity());
Expand All @@ -140,12 +128,7 @@ describe('CountryService', () => {
});
expect(countryRepository.save).toHaveBeenCalled();

for (const countryDisasterSetting of country.countryDisasterSettings as CountryDisasterSettingsDto[]) {
expect(leadTimeRepository.find).toHaveBeenCalledWith({
where: countryDisasterSetting.activeLeadTimes.map(
(leadTimeName) => ({ leadTimeName }),
),
});
for (const _countryDisasterSetting of country.countryDisasterSettings as CountryDisasterSettingsDto[]) {
expect(countryDisasterSettingsRepository.save).toHaveBeenCalled();
expect(
countryDisasterSettingsRepository.findOne,
Expand Down
13 changes: 2 additions & 11 deletions services/API-service/src/api/country/country.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { InjectRepository } from '@nestjs/typeorm';

import { In, Repository } from 'typeorm';

import { LeadTime } from '../admin-area-dynamic-data/enum/lead-time.enum';
import { DisasterType } from '../disaster/disaster-type.enum';
import { DisasterEntity } from '../disaster/disaster.entity';
import { LeadTimeEntity } from '../lead-time/lead-time.entity';
import { NotificationInfoEntity } from '../notification/notifcation-info.entity';
import { AdminLevel } from './admin-level.enum';
import { CountryDisasterSettingsEntity } from './country-disaster.entity';
Expand All @@ -25,14 +25,11 @@ export class CountryService {
private readonly disasterRepository: Repository<DisasterEntity>;
@InjectRepository(CountryDisasterSettingsEntity)
private readonly countryDisasterSettingsRepository: Repository<CountryDisasterSettingsEntity>;
@InjectRepository(LeadTimeEntity)
private readonly leadTimeRepository: Repository<LeadTimeEntity>;
@InjectRepository(NotificationInfoEntity)
private readonly notificationInfoRepository: Repository<NotificationInfoEntity>;

private readonly relations: string[] = [
'countryDisasterSettings',
'countryDisasterSettings.activeLeadTimes',
'disasterTypes',
'notificationInfo',
];
Expand Down Expand Up @@ -192,13 +189,7 @@ export class CountryService {
? JSON.parse(JSON.stringify(disaster.monthlyForecastInfo))
: null;
countryDisasterSettingsEntity.activeLeadTimes =
await this.leadTimeRepository.find({
where: disaster.activeLeadTimes.map(
(countryLeadTime: string): object => {
return { leadTimeName: countryLeadTime };
},
),
});
disaster.activeLeadTimes as LeadTime[];
countryDisasterSettingsEntity.isEventBased = disaster.isEventBased;

const saveResult = await this.countryDisasterSettingsRepository.save(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { ApiProperty } from '@nestjs/swagger';

import { IsBoolean, IsNotEmpty, IsOptional, IsString } from 'class-validator';
import {
IsBoolean,
IsEnum,
IsNotEmpty,
IsOptional,
IsString,
} from 'class-validator';

import { LeadTime } from '../../admin-area-dynamic-data/enum/lead-time.enum';

export class TriggerPerLeadTimeDto {
@ApiProperty({ example: '7-day' })
@IsNotEmpty()
@IsString()
@IsEnum(LeadTime)
public leadTime: LeadTime;

@ApiProperty({ default: false })
Expand Down
21 changes: 0 additions & 21 deletions services/API-service/src/api/lead-time/lead-time.entity.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class UploadLinesExposureStatusDto {
@ApiProperty({ example: LeadTime.hour1 })
@IsNotEmpty()
@IsString()
@IsEnum(LeadTime)
public leadTime: LeadTime;

@ApiProperty({ example: new Date() })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from 'typeorm';

import { LeadTime } from '../admin-area-dynamic-data/enum/lead-time.enum';
import { LeadTimeEntity } from '../lead-time/lead-time.entity';
import { LinesDataEntity } from './lines-data.entity';

@Entity('lines-data-dynamic-status')
Expand All @@ -31,10 +30,9 @@ export class LinesDataDynamicStatusEntity {
public timestamp: Date;

@ApiProperty({ example: LeadTime.hour1 })
@ManyToOne((): typeof LeadTimeEntity => LeadTimeEntity)
@JoinColumn({ name: 'leadTime', referencedColumnName: 'leadTimeName' })
@Column({ nullable: true }) // ##TODO: check!
@Index()
public leadTime: string;
public leadTime: LeadTime;

@ApiProperty({ example: true })
@Column()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class UploadAssetExposureStatusDto {
@ApiProperty({ example: LeadTime.hour1 })
@IsNotEmpty()
@IsString()
@IsEnum(LeadTime)
public leadTime: LeadTime;

@ApiProperty({ example: new Date() })
Expand All @@ -48,6 +49,7 @@ export class UploadDynamicPointDataDto {
@ApiProperty({ example: LeadTime.hour1 })
@IsOptional()
@IsString()
@IsEnum(LeadTime)
public leadTime: LeadTime;

@ApiProperty({ example: new Date() })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import {
Column,
Entity,
Index,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn,
} from 'typeorm';

import { LeadTimeEntity } from '../lead-time/lead-time.entity';
import { LeadTime } from '../admin-area-dynamic-data/enum/lead-time.enum';
import { PointDataEntity } from './point-data.entity';

@Entity('dynamic-point-data')
Expand All @@ -21,9 +20,8 @@ export class DynamicPointDataEntity {
)
public point: PointDataEntity;

@ManyToOne((): typeof LeadTimeEntity => LeadTimeEntity)
@JoinColumn({ name: 'leadTime', referencedColumnName: 'leadTimeName' })
public leadTime: string;
@Column({ nullable: true }) // ##TODO: check!
public leadTime: LeadTime;

@Column({ type: 'timestamp' })
@Index()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
IsArray,
IsEnum,
IsNotEmpty,
IsOptional,
IsString,
Expand All @@ -21,6 +22,7 @@ export class UploadTyphoonTrackDto {
@ApiProperty({ example: LeadTime.hour72 })
@IsNotEmpty()
@IsString()
@IsEnum(LeadTime)
public leadTime: LeadTime;

@ApiProperty({ example: 'Typhoon name' })
Expand Down
Loading

0 comments on commit 71806ef

Please sign in to comment.