Skip to content

Commit

Permalink
Add initial get eudr georegions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Feb 28, 2024
1 parent 60c0243 commit 500f1a9
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 12 deletions.
2 changes: 1 addition & 1 deletion api/src/modules/admin-regions/admin-regions.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class AdminRegionsController {
async getTreesForEudr(
@Query(ValidationPipe)
adminRegionTreeOptions: GetEUDRAdminRegions,
): Promise<AdminRegion> {
): Promise<AdminRegion[]> {
const results: AdminRegion[] = await this.adminRegionsService.getTrees({
...adminRegionTreeOptions,
withSourcingLocations: true,
Expand Down
5 changes: 5 additions & 0 deletions api/src/modules/geo-regions/dto/get-geo-region.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { CommonEUDRFiltersDTO } from 'utils/base.query-builder';

export class GetEUDRGeoRegions extends CommonEUDRFiltersDTO {
withSourcingLocations!: boolean;
}
2 changes: 2 additions & 0 deletions api/src/modules/geo-regions/geo-region.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export class GeoRegion extends BaseEntity {
@ApiPropertyOptional()
theGeom?: JSON;

// TODO: It might be interesting to add a trigger to calculate the value in case it's not provided. We are considering that EUDR will alwaus provide the value
// but not the regular ingestion
@Column({ type: 'decimal', nullable: true })
totalArea?: number;

Expand Down
18 changes: 18 additions & 0 deletions api/src/modules/geo-regions/geo-region.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import {
import { GeoRegion } from 'modules/geo-regions/geo-region.entity';
import { LocationGeoRegionDto } from 'modules/geo-regions/dto/location.geo-region.dto';
import { Injectable } from '@nestjs/common';
import { GetAdminRegionTreeWithOptionsDto } from '../admin-regions/dto/get-admin-region-tree-with-options.dto';
import { AdminRegion } from '../admin-regions/admin-region.entity';
import { SourcingLocation } from '../sourcing-locations/sourcing-location.entity';
import { BaseQueryBuilder } from '../../utils/base.query-builder';
import { GetEUDRGeoRegions } from './dto/get-geo-region.dto';

@Injectable()
export class GeoRegionRepository extends Repository<GeoRegion> {
Expand Down Expand Up @@ -128,4 +133,17 @@ export class GeoRegionRepository extends Repository<GeoRegion> {
],
);
}

async getGeoRegionsFromSourcingLocations(
getGeoRegionsDto: GetEUDRGeoRegions,
): Promise<GeoRegion[]> {
const initialQueryBuilder: SelectQueryBuilder<GeoRegion> =
this.createQueryBuilder('gr')
.innerJoin(SourcingLocation, 'sl', 'sl.geoRegionId = gr.id')
.distinct(true);
const queryBuilder: SelectQueryBuilder<GeoRegion> =
BaseQueryBuilder.addFilters(initialQueryBuilder, getGeoRegionsDto);

return queryBuilder.getMany();
}
}
28 changes: 28 additions & 0 deletions api/src/modules/geo-regions/geo-regions.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Param,
Patch,
Post,
Query,
UsePipes,
ValidationPipe,
} from '@nestjs/common';
Expand Down Expand Up @@ -35,6 +36,7 @@ import {
import { CreateGeoRegionDto } from 'modules/geo-regions/dto/create.geo-region.dto';
import { UpdateGeoRegionDto } from 'modules/geo-regions/dto/update.geo-region.dto';
import { PaginationMeta } from 'utils/app-base.service';
import { GetEUDRGeoRegions } from './dto/get-geo-region.dto';

@Controller(`/api/v1/geo-regions`)
@ApiTags(geoRegionResource.className)
Expand Down Expand Up @@ -71,6 +73,32 @@ 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);
return this.geoRegionsService.serialize(results);
}

@ApiOperation({ description: 'Find geo region by id' })
@ApiOkResponse({ type: GeoRegion })
@ApiNotFoundResponse({ description: 'Geo region not found' })
Expand Down
10 changes: 9 additions & 1 deletion api/src/modules/geo-regions/geo-regions.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ import { GeoRegionsController } from 'modules/geo-regions/geo-regions.controller
import { GeoRegionsService } from 'modules/geo-regions/geo-regions.service';
import { GeoRegion } from 'modules/geo-regions/geo-region.entity';
import { GeoRegionRepository } from 'modules/geo-regions/geo-region.repository';
import { AdminRegionsModule } from 'modules/admin-regions/admin-regions.module';
import { MaterialsModule } from 'modules/materials/materials.module';
import { SuppliersModule } from 'modules/suppliers/suppliers.module';

@Module({
imports: [TypeOrmModule.forFeature([GeoRegion])],
imports: [
TypeOrmModule.forFeature([GeoRegion]),
AdminRegionsModule,
MaterialsModule,
SuppliersModule,
],
controllers: [GeoRegionsController],
providers: [GeoRegionsService, GeoRegionRepository],
exports: [GeoRegionsService, GeoRegionRepository],
Expand Down
30 changes: 29 additions & 1 deletion api/src/modules/geo-regions/geo-regions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import { GeoRegionRepository } from 'modules/geo-regions/geo-region.repository';
import { CreateGeoRegionDto } from 'modules/geo-regions/dto/create.geo-region.dto';
import { UpdateGeoRegionDto } from 'modules/geo-regions/dto/update.geo-region.dto';
import { LocationGeoRegionDto } from 'modules/geo-regions/dto/location.geo-region.dto';
import { GetSupplierByType } from '../suppliers/dto/get-supplier-by-type.dto';
import { Supplier } from '../suppliers/supplier.entity';
import { AdminRegionsService } from 'modules/admin-regions/admin-regions.service';
import { MaterialsService } from 'modules/materials/materials.service';
import { SupplierRepository } from 'modules/suppliers/supplier.repository';
import { GetEUDRGeoRegions } from './dto/get-geo-region.dto';

@Injectable()
export class GeoRegionsService extends AppBaseService<
Expand All @@ -20,7 +26,11 @@ export class GeoRegionsService extends AppBaseService<
UpdateGeoRegionDto,
AppInfoDTO
> {
constructor(protected readonly geoRegionRepository: GeoRegionRepository) {
constructor(
protected readonly geoRegionRepository: GeoRegionRepository,
private readonly adminRegionService: AdminRegionsService,
private readonly materialsService: MaterialsService,
) {
super(
geoRegionRepository,
geoRegionResource.name.singular,
Expand Down Expand Up @@ -87,4 +97,22 @@ export class GeoRegionsService extends AppBaseService<
async deleteGeoRegionsCreatedByUser(): Promise<void> {
await this.geoRegionRepository.delete({ isCreatedByUser: true });
}

async getGeoRegionsFromSourcingLocations(
options: GetEUDRGeoRegions,
): Promise<GeoRegion[]> {
if (options.originIds) {
options.originIds =
await this.adminRegionService.getAdminRegionDescendants(
options.originIds,
);
}
if (options.materialIds) {
options.materialIds = await this.materialsService.getMaterialsDescendants(
options.materialIds,
);
}

return this.geoRegionRepository.getGeoRegionsFromSourcingLocations(options);
}
}
8 changes: 0 additions & 8 deletions api/src/modules/suppliers/suppliers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,10 @@ export class SuppliersService extends AppBaseService<
await this.supplierRepository.delete({});
}

async getSuppliersByIds(ids: string[]): Promise<Supplier[]> {
return this.supplierRepository.findByIds(ids);
}

async findTreesWithOptions(depth?: number): Promise<Supplier[]> {
return this.supplierRepository.findTrees({ depth });
}

async findAllUnpaginated(): Promise<Supplier[]> {
return this.supplierRepository.find({});
}

/**
*
* @description Get a tree of Suppliers that are associated with sourcing locations
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/base/modules/aws/eks/thumbprint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash

THUMBPRINT=$(echo | openssl s_client -servername oidc.eks.${1}.amazonaws.com -showcerts -connect oidc.eks.${1}.amazonaws.com:443 2>&- | tail -r | sed -n '/-----END CERTIFICATE-----/,/-----BEGIN CERTIFICATE-----/p; /-----BEGIN CERTIFICATE-----/q' | tail -r | openssl x509 -fingerprint -noout | sed 's/://g' | awk -F= '{print tolower($2)}')
THUMBPRINT=$(echo | openssl s_client -servername oidc.eks.${1}.amazonaws.com -showcerts -connect oidc.eks.${1}.amazonaws.com:443 2>&- | tac | sed -n '/-----END CERTIFICATE-----/,/-----BEGIN CERTIFICATE-----/p; /-----BEGIN CERTIFICATE-----/q' | tac | openssl x509 -fingerprint -noout | sed 's/://g' | awk -F= '{print tolower($2)}')
THUMBPRINT_JSON="{\"thumbprint\": \"${THUMBPRINT}\"}"
echo $THUMBPRINT_JSON

0 comments on commit 500f1a9

Please sign in to comment.