Skip to content

Commit

Permalink
Refactor eudr related endpoints to eudr controller
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Feb 29, 2024
1 parent 596d9a6 commit f7d4c48
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 111 deletions.
23 changes: 0 additions & 23 deletions api/src/modules/admin-regions/admin-regions.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,29 +100,6 @@ export class AdminRegionsController {
return this.adminRegionsService.serialize(results);
}

@ApiOperation({
description:
'Find all EUDR admin regions and return them in a tree format. Data in the "children" will recursively extend for the full depth of the tree',
})
@ApiOkTreeResponse({
treeNodeType: AdminRegion,
})
@ApiUnauthorizedResponse()
@ApiForbiddenResponse()
@UseInterceptors(SetScenarioIdsInterceptor)
@Get('/trees/eudr')
async getTreesForEudr(
@Query(ValidationPipe)
adminRegionTreeOptions: GetEUDRAdminRegions,
): Promise<AdminRegion[]> {
const results: AdminRegion[] = await this.adminRegionsService.getTrees({
...adminRegionTreeOptions,
withSourcingLocations: true,
eudr: true,
});
return this.adminRegionsService.serialize(results);
}

@ApiOperation({
description:
'Find all admin regions given a country and return data in a tree format. Data in the "children" will recursively extend for the full depth of the tree',
Expand Down
132 changes: 130 additions & 2 deletions api/src/modules/eudr/eudr.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,138 @@
import { Controller, Get } from '@nestjs/common';
import {
Controller,
Get,
Query,
UseInterceptors,
ValidationPipe,
} from '@nestjs/common';
import { Public } from 'decorators/public.decorator';
import { CartoConnector } from 'modules/eudr/carto/carto.connector';
import {
ApiForbiddenResponse,
ApiOkResponse,
ApiOperation,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';
import { ApiOkTreeResponse } from '../../decorators/api-tree-response.decorator';
import { Supplier } from '../suppliers/supplier.entity';
import { SetScenarioIdsInterceptor } from '../impact/set-scenario-ids.interceptor';
import { GetSupplierEUDR } from '../suppliers/dto/get-supplier-by-type.dto';
import { SuppliersService } from '../suppliers/suppliers.service';
import { MaterialsService } from 'modules/materials/materials.service';
import { GeoRegionsService } from 'modules/geo-regions/geo-regions.service';
import { AdminRegionsService } from 'modules/admin-regions/admin-regions.service';
import { Material } from '../materials/material.entity';
import { GetEUDRMaterials } from '../materials/dto/get-material-tree-with-options.dto';
import { AdminRegion } from '../admin-regions/admin-region.entity';
import { GetEUDRAdminRegions } from '../admin-regions/dto/get-admin-region-tree-with-options.dto';
import { GeoRegion, geoRegionResource } from '../geo-regions/geo-region.entity';
import { JSONAPIQueryParams } from '../../decorators/json-api-parameters.decorator';
import { GetEUDRGeoRegions } from '../geo-regions/dto/get-geo-region.dto';

@Controller('/api/v1/eudr')
export class EudrController {
constructor(private readonly carto: CartoConnector) {}
constructor(
private readonly carto: CartoConnector,
private readonly suppliersService: SuppliersService,
private readonly materialsService: MaterialsService,
private readonly geoRegionsService: GeoRegionsService,
private readonly adminRegionsService: AdminRegionsService,
) {}

@ApiOperation({
description:
'Find all EUDR suppliers and return them in a flat format. Data in the "children" will recursively extend for the full depth of the tree',
})
@ApiOkTreeResponse({
treeNodeType: Supplier,
})
@ApiUnauthorizedResponse()
@ApiForbiddenResponse()
@UseInterceptors(SetScenarioIdsInterceptor)
@Get('/suppliers')
async getSuppliers(
@Query(ValidationPipe) dto: GetSupplierEUDR,
): Promise<Supplier> {
const results: Supplier[] = await this.suppliersService.getSupplierByType({
...dto,
eudr: true,
});
return this.suppliersService.serialize(results);
}

@ApiOperation({
description:
'Find all EUDR materials and return them in a tree format. Data in the "children" will recursively extend for the full depth of the tree',
})
@ApiOkTreeResponse({
treeNodeType: Material,
})
@ApiUnauthorizedResponse()
@ApiForbiddenResponse()
@Get('/eudr')
async getMaterialsTree(
@Query(ValidationPipe) materialTreeOptions: GetEUDRMaterials,
): Promise<Material> {
const results: Material[] = await this.materialsService.getTrees({
...materialTreeOptions,
withSourcingLocations: true,
eudr: true,
});
return this.materialsService.serialize(results);
}

@ApiOperation({
description:
'Find all EUDR admin regions and return them in a tree format. Data in the "children" will recursively extend for the full depth of the tree',
})
@ApiOkTreeResponse({
treeNodeType: AdminRegion,
})
@ApiUnauthorizedResponse()
@ApiForbiddenResponse()
@UseInterceptors(SetScenarioIdsInterceptor)
@Get('/admin-regions')
async getTreesForEudr(
@Query(ValidationPipe)
adminRegionTreeOptions: GetEUDRAdminRegions,
): Promise<AdminRegion[]> {
const results: AdminRegion[] = await this.adminRegionsService.getTrees({
...adminRegionTreeOptions,
withSourcingLocations: true,
eudr: true,
});
return this.adminRegionsService.serialize(results);
}

@ApiOperation({
description: 'Find all EUDR geo regions',
})
@ApiOkResponse({
type: GeoRegion,
isArray: true,
})
@ApiUnauthorizedResponse()
@ApiForbiddenResponse()
@JSONAPIQueryParams({
availableFilters: geoRegionResource.columnsAllowedAsFilter.map(
(columnName: string) => ({
name: columnName,
}),
),
})
@Get('/geo-regions')
async findAllEudr(
@Query(ValidationPipe)
dto: GetEUDRGeoRegions,
): Promise<GeoRegion[]> {
const results: GeoRegion[] =
await this.geoRegionsService.getGeoRegionsFromSourcingLocations({
...dto,
withSourcingLocations: true,
eudr: true,
});
return this.geoRegionsService.serialize(results);
}

@Public()
@Get('test')
Expand Down
5 changes: 4 additions & 1 deletion api/src/modules/eudr/eudr.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import { EudrService } from 'modules/eudr/eudr.service';
import { EudrController } from 'modules/eudr/eudr.controller';
import { CartodbRepository } from 'modules/eudr/carto/cartodb.repository';
import { CartoConnector } from './carto/carto.connector';
import { MaterialsModule } from 'modules/materials/materials.module';
import { SuppliersModule } from 'modules/suppliers/suppliers.module';
import { GeoRegionsModule } from 'modules/geo-regions/geo-regions.module';

@Module({
imports: [HttpModule],
imports: [HttpModule, MaterialsModule, SuppliersModule, GeoRegionsModule],
providers: [EudrService, CartodbRepository, CartoConnector],
controllers: [EudrController],
})
Expand Down
30 changes: 0 additions & 30 deletions api/src/modules/geo-regions/geo-regions.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,36 +73,6 @@ export class GeoRegionsController {
return this.geoRegionsService.serialize(results.data, results.metadata);
}

@ApiOperation({
description: 'Find all EUDR geo regions',
})
@ApiOkResponse({
type: GeoRegion,
isArray: true,
})
@ApiUnauthorizedResponse()
@ApiForbiddenResponse()
@JSONAPIQueryParams({
availableFilters: geoRegionResource.columnsAllowedAsFilter.map(
(columnName: string) => ({
name: columnName,
}),
),
})
@Get('/eudr')
async findAllEudr(
@Query(ValidationPipe)
dto: GetEUDRGeoRegions,
): Promise<GeoRegion[]> {
const results: GeoRegion[] =
await this.geoRegionsService.getGeoRegionsFromSourcingLocations({
...dto,
withSourcingLocations: true,
eudr: true,
});
return this.geoRegionsService.serialize(results);
}

@ApiOperation({ description: 'Find geo region by id' })
@ApiOkResponse({ type: GeoRegion })
@ApiNotFoundResponse({ description: 'Geo region not found' })
Expand Down
21 changes: 0 additions & 21 deletions api/src/modules/materials/materials.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,6 @@ export class MaterialsController {
return this.materialsService.serialize(results);
}

@ApiOperation({
description:
'Find all EUDR materials and return them in a tree format. Data in the "children" will recursively extend for the full depth of the tree',
})
@ApiOkTreeResponse({
treeNodeType: Material,
})
@ApiUnauthorizedResponse()
@ApiForbiddenResponse()
@Get('/trees/eudr')
async getTreesForEudr(
@Query(ValidationPipe) materialTreeOptions: GetEUDRMaterials,
): Promise<Material> {
const results: Material[] = await this.materialsService.getTrees({
...materialTreeOptions,
withSourcingLocations: true,
eudr: true,
});
return this.materialsService.serialize(results);
}

@ApiOperation({ description: 'Find material by id' })
@ApiNotFoundResponse({ description: 'Material not found' })
@ApiOkResponse({ type: Material })
Expand Down
27 changes: 1 addition & 26 deletions api/src/modules/suppliers/suppliers.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ import {
FetchSpecification,
ProcessFetchSpecification,
} from 'nestjs-base-service';
import {
Supplier,
SUPPLIER_TYPES,
supplierResource,
} from 'modules/suppliers/supplier.entity';
import { Supplier, supplierResource } from 'modules/suppliers/supplier.entity';
import { CreateSupplierDto } from 'modules/suppliers/dto/create.supplier.dto';
import { UpdateSupplierDto } from 'modules/suppliers/dto/update.supplier.dto';
import { ApiOkTreeResponse } from 'decorators/api-tree-response.decorator';
Expand Down Expand Up @@ -101,27 +97,6 @@ export class SuppliersController {
return this.suppliersService.serialize(results);
}

@ApiOperation({
description:
'Find all EUDR suppliers and return them in a flat format. Data in the "children" will recursively extend for the full depth of the tree',
})
@ApiOkTreeResponse({
treeNodeType: Supplier,
})
@ApiUnauthorizedResponse()
@ApiForbiddenResponse()
@UseInterceptors(SetScenarioIdsInterceptor)
@Get('/eudr')
async getTreesForEudr(
@Query(ValidationPipe) dto: GetSupplierEUDR,
): Promise<Supplier> {
const results: Supplier[] = await this.suppliersService.getSupplierByType({
...dto,
eudr: true,
});
return this.suppliersService.serialize(results);
}

@ApiOperation({
description: 'Find all suppliers by type',
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { AdminRegionTestManager } from './fixtures';
import { EUDRTestManager } from './fixtures';

describe('Admin Regions EUDR Filters (e2e)', () => {
let testManager: AdminRegionTestManager;
let testManager: EUDRTestManager;

beforeAll(async () => {
testManager = await AdminRegionTestManager.load();
testManager = await EUDRTestManager.load();
});

beforeEach(async () => {
Expand Down
28 changes: 28 additions & 0 deletions api/test/e2e/eudr/eudr-geo-region-filters.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { EUDRTestManager } from './fixtures';

describe('GeoRegions Filters (e2e)', () => {
let testManager: EUDRTestManager;

beforeAll(async () => {
testManager = await EUDRTestManager.load();
});
beforeEach(async () => {
await testManager.refreshState();
});

afterEach(async () => {
await testManager.clearDatabase();
});

afterAll(async () => {
await testManager.close();
});
describe('EUDR Geo Regions Filters', () => {
it('should only get geo-regions that are part of EUDR data', async () => {
await testManager.GivenGeoRegionsOfSourcingLocations();
const { eudrGeoRegions } = await testManager.GivenEUDRGeoRegions();
const response = await testManager.WhenIRequestEUDRGeoRegions();
testManager.ThenIShouldOnlyReceiveCorrespondingGeoRegions(eudrGeoRegions);
});
});
});
Loading

0 comments on commit f7d4c48

Please sign in to comment.