Skip to content

Commit

Permalink
Feat: route search numero by polygon (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
fufeck authored Oct 29, 2024
1 parent 863c0d3 commit 73c8d76
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
25 changes: 25 additions & 0 deletions apps/api/src/modules/base_locale/base_locale.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ import { RecoverBaseLocaleDTO } from './dto/recover_base_locale.dto';
import { AllDeletedInBalDTO } from './dto/all_deleted_in_bal.dto';
import { BatchNumeroResponseDTO } from '../numeros/dto/batch_numero_response.dto';
import { isSuperAdmin } from '@/lib/utils/is-admin.utils';
import { SearchNumeroDTO } from '../numeros/dto/search_numero.dto';
import { Numero } from '@/shared/entities/numero.entity';

@ApiTags('bases-locales')
@Controller('bases-locales')
Expand Down Expand Up @@ -463,6 +465,29 @@ export class BaseLocaleController {
res.status(HttpStatus.OK).json(allDeleted);
}

@Put(':baseLocaleId/numeros')
@ApiOperation({
summary: 'Search numero',
operationId: 'searchNumeros',
})
@ApiParam({ name: 'baseLocaleId', required: true, type: String })
@ApiBody({ type: SearchNumeroDTO, required: true })
@ApiResponse({ status: HttpStatus.OK, type: Numero, isArray: true })
@ApiBearerAuth('admin-token')
@UseGuards(AdminGuard)
async searchNumeros(
@Req() req: CustomRequest,
@Body() searchNumeroDTO: SearchNumeroDTO,
@Res() res: Response,
) {
const result: Numero[] =
await this.numeroService.findManyWherePositionInPolygon(
req.baseLocale.id,
searchNumeroDTO.polygon,
);
res.status(HttpStatus.OK).json(result);
}

@Put(':baseLocaleId/numeros/uncertify-all')
@ApiOperation({
summary: 'Uncertify all numeros in Bal',
Expand Down
11 changes: 11 additions & 0 deletions apps/api/src/modules/numeros/dto/search_numero.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';

export class SearchNumeroDTO {
@ApiProperty({
type: [Number],
required: true,
nullable: false,
isArray: true,
})
polygon: number[][];
}
18 changes: 18 additions & 0 deletions apps/api/src/modules/numeros/numero.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,24 @@ export class NumeroService {
return query.getMany();
}

async findManyWherePositionInPolygon(
balId: string,
polygon: number[][],
): Promise<Numero[]> {
const linestring: string = polygon
.map((arr) => `${arr[0]} ${arr[1]}`)
.join(',');
// Requète postgis qui permet de récupèré les numeros dans un polygon simple
const query = this.numerosRepository
.createQueryBuilder('numeros')
.leftJoinAndSelect('numeros.positions', 'positions')
.where('numeros.balId = :balId', { balId })
.andWhere(
`ST_Contains(ST_Polygon('LINESTRING(${linestring})'::geometry, 4326), positions.point)`,
);
return query.getMany();
}

public async count(where: FindOptionsWhere<Numero>): Promise<number> {
return this.numerosRepository.count({ where });
}
Expand Down

0 comments on commit 73c8d76

Please sign in to comment.