Skip to content

Commit

Permalink
Dates endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Mar 5, 2024
1 parent 84185ee commit 1d0fe38
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SelectQueryBuilder } from 'typeorm';
import { AlertsOutput } from 'modules/eudr-alerts/dto/alerts-output.dto';
import { GetEUDRAlertsDto } from 'modules/eudr-alerts/dto/get-alerts.dto';
import { Query } from '@google-cloud/bigquery';
import { GetEUDRAlertsDto } from 'modules/eudr-alerts/dto/get-alerts.dto';

export class BigQueryAlertsQueryBuilder {
queryBuilder: SelectQueryBuilder<AlertsOutput>;
Expand Down Expand Up @@ -51,6 +51,8 @@ export class BigQueryAlertsQueryBuilder {
this.queryBuilder.limit(this.dto?.limit);

const [query, params] = this.queryBuilder.getQueryAndParameters();
console.log('query', query);
console.log('params', params);

return this.parseToBigQuery(query, params);
}
Expand All @@ -76,7 +78,7 @@ export class BigQueryAlertsQueryBuilder {

addAlertDateRange(): void {
this.queryBuilder.andWhere(
'DATE(alertdate) BETWEEN :startAlertDate AND :endAlertDate',
'DATE(alertdate) BETWEEN DATE(:startAlertDate) AND DATE(:endAlertDate)',
{
startAlertDate: this.dto?.startAlertDate,
endAlertDate: this.dto?.endAlertDate,
Expand Down
22 changes: 17 additions & 5 deletions api/src/modules/eudr-alerts/alerts.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { DataSource, SelectQueryBuilder } from 'typeorm';
import { AlertsOutput } from 'modules/eudr-alerts/dto/alerts-output.dto';
import {
EUDRAlertDates,
GetEUDRAlertDatesDto,
IEUDRAlertsRepository,
} from 'modules/eudr-alerts/eudr.repositoty.interface';
import { GetEUDRAlertsDto } from 'modules/eudr-alerts/dto/get-alerts.dto';
Expand Down Expand Up @@ -52,6 +51,23 @@ export class AlertsRepository implements IEUDRAlertsRepository {
queryBuilder.addSelect('alertconfidence', 'alertConfidence');
queryBuilder.addSelect('year', 'alertYear');
queryBuilder.addSelect('alertcount', 'alertCount');
return this.query(queryBuilder, dto);
}

async getDates(dto: GetEUDRAlertsDto): Promise<EUDRAlertDates[]> {
const queryBuilder: SelectQueryBuilder<AlertsOutput> =
this.dataSource.createQueryBuilder();
queryBuilder.from(this.baseDataset, 'alerts');
queryBuilder.select('alertdate', 'alertDate');
queryBuilder.orderBy('alertdate', 'ASC');
queryBuilder.groupBy('alertdate');
return this.query(queryBuilder, dto);
}

private async query(
queryBuilder: SelectQueryBuilder<any>,
dto?: GetEUDRAlertsDto,
): Promise<any> {
try {
const response: SimpleQueryRowsResponse = await this.bigQueryClient.query(
this.buildQuery(queryBuilder, dto),
Expand All @@ -69,10 +85,6 @@ export class AlertsRepository implements IEUDRAlertsRepository {
}
}

getDates(dto: GetEUDRAlertDatesDto): Promise<EUDRAlertDates[]> {
return [] as any;
}

private buildQuery(
queryBuilder: SelectQueryBuilder<AlertsOutput>,
dto?: GetEUDRAlertsDto,
Expand Down
6 changes: 3 additions & 3 deletions api/src/modules/eudr-alerts/dto/alerts-output.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { GeoJSON } from 'geojson';

export type AlertsOutput = {
alertCount: boolean;
date: Date;
alertDate: {
value: Date | string;
};
year: number;
alertConfidence: 'low' | 'medium' | 'high' | 'very high';
};
Expand Down
9 changes: 8 additions & 1 deletion api/src/modules/eudr-alerts/dto/get-alerts.dto.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,54 @@
import { ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
IsArray,
IsDate,
IsEnum,
IsInt,
IsNumber,
IsOptional,
IsUUID,
} from 'class-validator';

export class GetEUDRAlertsDto {
@ApiPropertyOptional()
@IsOptional()
@IsArray()
@IsUUID('4', { each: true })
supplierIds: string[];

@ApiPropertyOptional()
@IsOptional()
@IsArray()
@IsUUID('4', { each: true })
geoRegionIds: string[];

@ApiPropertyOptional()
@IsOptional()
@IsNumber()
@Type(() => Number)
startYear: number;

@ApiPropertyOptional()
@IsOptional()
@IsNumber()
@Type(() => Number)
endYear: number;

alertConfidence: 'high' | 'medium' | 'low';

@ApiPropertyOptional()
@IsOptional()
@IsDate()
@Type(() => Date)
startAlertDate: Date;

@ApiPropertyOptional()
@IsOptional()
@IsDate()
@Type(() => Date)
endAlertDate: Date;

@ApiPropertyOptional()
@IsOptional()
@IsInt()
limit: number = 1000;
Expand Down
20 changes: 19 additions & 1 deletion api/src/modules/eudr-alerts/eudr.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import {
Controller,
Get,
Query,
Res,
UseInterceptors,
ValidationPipe,
} from '@nestjs/common';
import {
ApiForbiddenResponse,
ApiOkResponse,
ApiOperation,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';
import { Response } from 'express';
Expand All @@ -34,7 +34,9 @@ import { JSONAPIQueryParams } from 'decorators/json-api-parameters.decorator';
import { GetEUDRGeoRegions } from 'modules/geo-regions/dto/get-geo-region.dto';
import { EudrService } from 'modules/eudr-alerts/eudr.service';
import { GetEUDRAlertsDto } from 'modules/eudr-alerts/dto/get-alerts.dto';
import { EUDRAlertDates } from 'modules/eudr-alerts/eudr.repositoty.interface';

@ApiTags('EUDR')
@Controller('/api/v1/eudr')
export class EudrController {
constructor(
Expand Down Expand Up @@ -140,6 +142,22 @@ export class EudrController {
return this.geoRegionsService.serialize(results);
}

@ApiOperation({
description: 'Get EUDR alerts dates',
})
@ApiOkResponse({
type: EUDRAlertDates,
isArray: true,
})
@ApiUnauthorizedResponse()
@ApiForbiddenResponse()
@Get('/dates')
async getAlertDates(
@Query(ValidationPipe) dto: GetEUDRAlertsDto,
): Promise<EUDRAlertDates[]> {
return this.eudrAlertsService.getDates(dto);
}

@Get('/alerts')
async getAlerts(@Query(ValidationPipe) dto: GetEUDRAlertsDto): Promise<any> {
return this.eudrAlertsService.getAlerts(dto);
Expand Down
18 changes: 9 additions & 9 deletions api/src/modules/eudr-alerts/eudr.repositoty.interface.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { GetEUDRAlertsDto } from 'modules/eudr-alerts/dto/get-alerts.dto';
import { AlertsOutput } from 'modules/eudr-alerts/dto/alerts-output.dto';
import { ApiProperty } from '@nestjs/swagger';

export class GetEUDRAlertDatesDto {
startDate: string;
endDate: string;
class DateValue {
@ApiProperty()
value: Date | string;
}

export type EUDRAlertDates = {
alertDate: {
value: Date | string;
};
};
export class EUDRAlertDates {
@ApiProperty()
alertDate: DateValue;
}

export interface IEUDRAlertsRepository {
getAlerts(dto?: GetEUDRAlertsDto): Promise<AlertsOutput[]>;

getDates(dto: GetEUDRAlertDatesDto): Promise<EUDRAlertDates[]>;
getDates(dto: GetEUDRAlertsDto): Promise<EUDRAlertDates[]>;
}
9 changes: 8 additions & 1 deletion api/src/modules/eudr-alerts/eudr.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Inject, Injectable } from '@nestjs/common';
import { GetEUDRAlertsDto } from 'modules/eudr-alerts/dto/get-alerts.dto';
import { AlertsOutput } from 'modules/eudr-alerts/dto/alerts-output.dto';
import { IEUDRAlertsRepository } from 'modules/eudr-alerts/eudr.repositoty.interface';
import {
EUDRAlertDates,
IEUDRAlertsRepository,
} from 'modules/eudr-alerts/eudr.repositoty.interface';

@Injectable()
export class EudrService {
Expand All @@ -13,4 +16,8 @@ export class EudrService {
async getAlerts(dto: GetEUDRAlertsDto): Promise<AlertsOutput[]> {
return this.alertsRepository.getAlerts(dto);
}

async getDates(dto: GetEUDRAlertsDto): Promise<EUDRAlertDates[]> {
return this.alertsRepository.getDates(dto);
}
}
3 changes: 1 addition & 2 deletions api/test/utils/application-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { Type } from '@nestjs/common/interfaces';
import { TestingModule } from '@nestjs/testing/testing-module';
import { isUndefined } from 'lodash';
import { MockAlertRepository, MockEmailService } from './service-mocks';
import { IEUDRAlertsRepositoryToken } from '../../src/modules/eudr-alerts/eudr.module';
import { IEmailServiceToken } from '../../src/modules/notifications/notifications.module';
import { AlertsRepository } from 'modules/eudr-alerts/alerts.repository';

Expand Down Expand Up @@ -48,7 +47,7 @@ export default class ApplicationManager {
})
.overrideProvider(IEmailServiceToken)
.useClass(MockEmailService)
.overrideProvider(AlertsRepository)
.overrideProvider('IEUDRAlertsRepository')
.useClass(MockAlertRepository);

ApplicationManager.testApplication.moduleFixture =
Expand Down
3 changes: 1 addition & 2 deletions api/test/utils/service-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
import { Logger } from '@nestjs/common';
import {
EUDRAlertDates,
GetEUDRAlertDatesDto,
IEUDRAlertsRepository,
} from 'modules/eudr-alerts/eudr.repositoty.interface';

Expand All @@ -28,7 +27,7 @@ export class MockAlertRepository implements IEUDRAlertsRepository {
});
}

getDates(dto: GetEUDRAlertDatesDto): Promise<EUDRAlertDates[]> {
getDates(): Promise<EUDRAlertDates[]> {
return new Promise((resolve) => {
resolve([]);
});
Expand Down

0 comments on commit 1d0fe38

Please sign in to comment.