Skip to content
Snippets Groups Projects
structures.controller.ts 2.82 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jérémie BRISON's avatar
    Jérémie BRISON committed
    import { Body, Controller, Get, Param, ParseIntPipe, Post, Put, Query } from '@nestjs/common';
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    import { User } from '../users/schemas/user.schema';
    
    Jérémie BRISON's avatar
    Jérémie BRISON committed
    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';
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    import { Structure } from './schemas/structure.schema';
    
    import { StructuresService } from './services/structures.service';
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    
    @Controller('structures')
    
    export class StructuresController {
    
    Jérémie BRISON's avatar
    Jérémie BRISON committed
      constructor(private readonly structureService: StructuresService, private readonly userService: UsersService) {}
    
      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);
    
    Jérémie BRISON's avatar
    Jérémie BRISON committed
      @Put(':id')
    
      //TODO: protect, only structure owner can edit it
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
      public async update(@Param('id') id: number, @Body() body: structureDto): Promise<Structure> {
    
        return this.structureService.update(id, body);
      }
    
    
      @Get()
      public async findAll(): Promise<Structure[]> {
        return this.structureService.findAll();
    
    Jérémie BRISON's avatar
    Jérémie BRISON committed
      @Get(':id/isClaimed')
      public async isClaimed(@Param('id') id: number): Promise<boolean> {
        return this.structureService.isClaimed(id);
      }
    
      @Post(':id/claim')
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
      public async claim(@Param('id', new ParseIntPipe()) idStructure: number, @Body() user: User): Promise<number[]> {
    
    Jérémie BRISON's avatar
    Jérémie BRISON committed
        return this.userService.updateStructureLinked(user.email, idStructure);
      }
    
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
      @Get('count')
    
      public async countCategories(): Promise<Array<{ id: string; count: number }>> {
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
        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'),
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
        ]);
        // Return a concat of all arrays
        return data.reduce((a, b) => [...a, ...b]);
      }
    
    
      @Get(':id')
      public async find(@Param('id') id: number) {
        return this.structureService.findOne(id);
      }