import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put, Query, UseGuards } from '@nestjs/common'; import { ApiParam } from '@nestjs/swagger'; import { Types } from 'mongoose'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { Roles } from '../users/decorators/roles.decorator'; import { IsStructureOwnerGuard } from '../users/guards/isStructureOwner.guard'; import { RolesGuard } from '../users/guards/roles.guard'; import { User } from '../users/schemas/user.schema'; import { UsersService } from '../users/users.service'; import { CreateStructureDto } from './dto/create-structure.dto'; import { QueryStructure } from './dto/query-structure.dto'; import { structureDto } from './dto/structure.dto'; import { Structure } from './schemas/structure.schema'; import { StructuresService } from './services/structures.service'; @Controller('structures') export class StructuresController { constructor(private readonly structureService: StructuresService, private readonly userService: UsersService) {} @Post() public async create(@Body() createStructureDto: CreateStructureDto): Promise<Structure> { return this.structureService.create(createStructureDto.idUser, createStructureDto.structure); } @Post('search') public async search(@Query() query: QueryStructure, @Body() body): Promise<Structure[]> { return this.structureService.search(query.query, body ? body.filters : null); } @Put('updateAfterOwnerVerify/:id') public async updateAfterOwnerVerify(@Param('id') id: string, @Body() body: structureDto): Promise<Structure> { return this.structureService.update(id, body); } @Put(':id') @UseGuards(JwtAuthGuard, IsStructureOwnerGuard) @Roles('admin') public async update(@Param('id') id: string, @Body() body: structureDto): Promise<Structure> { return this.structureService.update(id, body); } @Get() public async findAll(): Promise<Structure[]> { return this.structureService.findAll(); } @Post(':id/isClaimed') public async isClaimed(@Param('id') id: string, @Body() user?: User): Promise<boolean> { return this.structureService.isClaimed(id, user); } @Post(':id/claim') public async claim(@Param('id') idStructure: string, @Body() user: User): Promise<Types.ObjectId[]> { return this.userService.updateStructureLinked(user.email, idStructure); } @Get('count') public async countCategories(): Promise<Array<{ id: string; count: number }>> { const data = await Promise.all([ this.structureService.countByStructureKey('proceduresAccompaniment'), this.structureService.countByStructureKey('accessRight'), this.structureService.countByStructureKey('baseSkills'), this.structureService.countByStructureKey('parentingHelp'), this.structureService.countByStructureKey('digitalCultureSecurity'), this.structureService.countByStructureKey('socialAndProfessional'), this.structureService.countByStructureKey('publicsAccompaniment'), this.structureService.countByStructureKey('labelsQualifications'), this.structureService.countByStructureKey('publics'), this.structureService.countByStructureKey('accessModality'), this.structureService.countByStructureKey('equipmentsAndServices'), ]); // Return a concat of all arrays return data.reduce((a, b) => [...a, ...b]); } @Post('address') public async searchAddress(@Body() data: { searchQuery: string }) { return await this.structureService.searchAddress(data); } @Get(':id') public async find(@Param('id') id: string) { return this.structureService.findOne(id); } @Delete(':id') @UseGuards(JwtAuthGuard, RolesGuard) @Roles('admin') @ApiParam({ name: 'id', type: String, required: true }) public async delete(@Param('id') id: string) { return this.structureService.deleteOne(id); } }