Skip to content
Snippets Groups Projects
Select Git revision
  • 2bef58b7b8e01679202ae9849fc002d32b0ea688
  • dev default protected
  • 168-pro-connect
  • master protected
  • nestjs11
  • renovate/ghcr.io-browserless-chromium-2.x
  • renovate/major-nest-monorepo
  • renovate/luxon-3.x
  • renovate/gouvfr-anct-timetable-to-osm-opening-hours-2.x
  • renovate/major-typescript-eslint-monorepo
  • renovate/npm-11.x
  • renovate/mysql-9.x
  • renovate/mongo-express-1.x
  • renovate/major-jest-monorepo
  • renovate/tsconfig-paths-4.x
  • renovate/jest-junit-16.x
  • renovate/express-5.x
  • renovate/elastic-elasticsearch-8.x
  • renovate/ghost-5.x
  • renovate/elasticsearch-7.x
  • renovate/devdependencies-(non-major)
  • v4.1.1
  • v4.1.0
  • v4.0.3
  • v4.0.1
  • v4.0.0
  • v3.4.3
  • v3.4.2
  • v3.4.1
  • v3.4.0
  • v3.3.1
  • v3.3.0
  • v3.2.0
  • v3.1.0
  • v3.0.1
  • v3.0.0
  • v2.5.0
  • v2.4.2
  • v2.4.1
  • v2.4.0
  • v2.3.2
41 results

structures.controller.ts

Blame
  • structures.controller.ts 10.89 KiB
    import {
      Body,
      Controller,
      Delete,
      Get,
      HttpException,
      HttpService,
      HttpStatus,
      Param,
      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 { CategoriesAccompagnementService } from '../categories/services/categories-accompagnement.service';
    import { CategoriesFormationsService } from '../categories/services/categories-formations.service';
    import { CategoriesOthersService } from '../categories/services/categories-others.service';
    import { CreateTempUserDto } from '../temp-user/dto/create-temp-user.dto';
    import { TempUserService } from '../temp-user/temp-user.service';
    import { Roles } from '../users/decorators/roles.decorator';
    import { IsStructureOwnerGuard } from '../users/guards/isStructureOwner.guard';
    import { User } from '../users/schemas/user.schema';
    import { UsersService } from '../users/services/users.service';
    import { CreateStructureDto } from './dto/create-structure.dto';
    import { QueryStructure } from './dto/query-structure.dto';
    import { structureDto } from './dto/structure.dto';
    import { Structure, StructureDocument } from './schemas/structure.schema';
    import { StructuresService } from './services/structures.service';
    import { RolesGuard } from '../users/guards/roles.guard';
    import { depRegex } from './common/regex';
    
    @Controller('structures')
    export class StructuresController {
      constructor(
        private readonly httpService: HttpService,
        private readonly structureService: StructuresService,
        private readonly userService: UsersService,
        private readonly tempUserService: TempUserService,
        private readonly categoriesFormationsService: CategoriesFormationsService,
        private readonly categoriesOthersService: CategoriesOthersService,
        private readonly categoriesAccompagnementService: CategoriesAccompagnementService
      ) {}
    
      /**
       * Return points of given town exist.
       * @param zipcode
       * @returns Array of points
       */
      @Get('coordinates/:zipcode')
      @ApiParam({ name: 'zipcode', type: String, required: true })
      public async getCoordinates(@Param('zipcode') city: string): Promise<any> {
        return this.httpService
          .get(encodeURI(`https://download.data.grandlyon.com/geocoding/photon/api?q=${city}`))
          .toPromise()
          .then(async (res) => res.data.features)
          .then((data) =>
            data.filter(
              (cityPoint) =>
                cityPoint.properties.city?.toLowerCase().includes(city.toLowerCase()) &&
                cityPoint.properties.postcode.match(depRegex)
            )
          )
          .then((data) => data.map((filteredCityPoint) => filteredCityPoint.geometry.coordinates));
      }
    
      @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.searchForStructures(query.query, body ? body.filters : null);
      }
    
      @Post('resetSearchIndex')
      @UseGuards(JwtAuthGuard, RolesGuard)
      @Roles('admin')
      public async resetES(): Promise<StructureDocument[]> {
        return this.structureService.initiateStructureIndex();
      }
    
      @Put('updateAfterOwnerVerify/:id')
      public async updateAfterOwnerVerify(@Param('id') id: string): Promise<Structure> {
        return this.structureService.updateAccountVerified(id);
      }
    
      @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();
      }
    
      @Get('formated')
      public async findAllFormated(): Promise<Structure[]> {
        const formationCategories = await this.categoriesFormationsService.findAll();
        const accompagnementCategories = await this.categoriesAccompagnementService.findAll();
        const otherCategories = await this.categoriesOthersService.findAll();
        return this.structureService.findAllFormated(formationCategories, accompagnementCategories, otherCategories);
      }
    
      @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[]> {
        const structure = await this.structureService.findOne(idStructure);
        return this.userService.updateStructureLinkedClaim(user.email, idStructure, structure);
      }
    
      @Post('count')
      public async countCategories(
        @Body()
        selectedFilter: { id: string; text: string }[]
      ): Promise<Array<{ id: string; count: number }>> {
        const data = await Promise.all([
          this.structureService.countByStructureKey('proceduresAccompaniment', selectedFilter),
    
          this.structureService.countByStructureKey('accessRight', selectedFilter),
          this.structureService.countByStructureKey('baseSkills', selectedFilter),
          this.structureService.countByStructureKey('parentingHelp', selectedFilter),
          this.structureService.countByStructureKey('digitalCultureSecurity', selectedFilter),
          this.structureService.countByStructureKey('socialAndProfessional', selectedFilter),
    
          this.structureService.countByStructureKey('publicsAccompaniment', selectedFilter),
          this.structureService.countByStructureKey('labelsQualifications', selectedFilter),
          this.structureService.countByStructureKey('publics', selectedFilter),
          this.structureService.countByStructureKey('accessModality', selectedFilter),
          this.structureService.countByStructureKey('equipmentsAndServices', selectedFilter),
        ]);
        // Return a concat of all arrays
        return data.reduce((a, b) => [...a, ...b]);
      }
    
      @Post('address')
      public async searchAddress(@Body() data: { searchQuery: string }) {
        return this.structureService.searchAddress(data);
      }
    
      @Get(':id')
      public async find(@Param('id') id: string) {
        const result = await this.structureService.findOne(id);
        if (!result || result.deletedAt) {
          throw new HttpException('Structure does not exist', HttpStatus.NOT_FOUND);
        } else {
          return result;
        }
      }
    
      @Post(':id/withOwners')
      public async findWithOwners(@Param('id') id: string, @Body() data: { emailUser: string }) {
        return this.structureService.findWithOwners(id, data.emailUser);
      }
    
      @Delete(':id')
      @UseGuards(JwtAuthGuard, IsStructureOwnerGuard)
      @Roles('admin')
      @ApiParam({ name: 'id', type: String, required: true })
      public async delete(@Param('id') id: string) {
        return this.structureService.deleteOne(id);
      }
    
      @Post(':id/addOwner')
      @UseGuards(JwtAuthGuard, IsStructureOwnerGuard)
      @Roles('admin')
      @ApiParam({ name: 'id', type: String, required: true })
      public async addOwner(@Param('id') id: string, @Body() user: CreateTempUserDto): Promise<any> {
        // Get structure name
        const structure = await this.structureService.findOne(id);
        if (!structure) {
          throw new HttpException('Invalid Structure', HttpStatus.NOT_FOUND);
        }
        user.pendingStructuresLink = [Types.ObjectId(id)];
        // If user already exist, use created account
        if (await this.userService.verifyUserExist(user.email)) {
          return this.userService.updateStructureLinked(user.email, id);
        }
        // If temp user exist, update it
        if (await this.tempUserService.findOne(user.email)) {
          return this.tempUserService.updateStructureLinked(user);
        }
        // If not, create
        return this.tempUserService.create(user, structure.structureName);
      }
    
      @Post(':id/join')
      @ApiParam({ name: 'id', type: String, required: true })
      public async join(@Param('id') id: string, @Body() user: User): Promise<void> {
        // Get structure name
        const structure = await this.structureService.findOne(id);
        if (!structure) {
          throw new HttpException('Invalid Structure', HttpStatus.NOT_FOUND);
        }
        // Get user and add pending structure
        const userFromDb = await this.userService.findOne(user.email);
        if (!userFromDb) {
          throw new HttpException('Invalid User', HttpStatus.NOT_FOUND);
        }
        // If user has not already request it, send owner's validation email
        if (!userFromDb.pendingStructuresLink.includes(Types.ObjectId(id))) {
          userFromDb.pendingStructuresLink.push(Types.ObjectId(id));
          userFromDb.save();
          // Send structure owner's an email
          this.structureService.sendStructureJoinRequest(userFromDb, structure);
        }
      }
    
      @Post(':id/join/:userId/:status')
      @UseGuards(JwtAuthGuard, IsStructureOwnerGuard)
      @ApiParam({ name: 'id', type: String, required: true })
      @ApiParam({ name: 'userId', type: String, required: true })
      @ApiParam({ name: 'status', type: String, required: true })
      public async joinValidation(
        @Param('id') id: string,
        @Param('status') status: string,
        @Param('userId') userId: string
      ): Promise<any> {
        // Get structure name
        const structure = await this.structureService.findOne(id);
        if (!structure) {
          throw new HttpException('Invalid Structure', HttpStatus.NOT_FOUND);
        }
    
        // Get user and add pending structure
        const userFromDb = await this.userService.findById(userId);
        if (!userFromDb) {
          throw new HttpException('Invalid User', HttpStatus.NOT_FOUND);
        }
    
        if (!userFromDb.pendingStructuresLink.includes(Types.ObjectId(id))) {
          throw new HttpException('User not linked to structure', HttpStatus.NOT_FOUND);
        }
    
        if (status === 'true') {
          // Accept
          await this.userService.updateStructureLinked(userFromDb.email, id);
          await this.userService.removeFromPendingStructureLinked(userFromDb.email, id);
        } else {
          // Refuse
          this.userService.removeFromPendingStructureLinked(userFromDb.email, id);
        }
      }
    
      @Delete(':id/owner/:userId')
      @UseGuards(JwtAuthGuard, IsStructureOwnerGuard)
      @Roles('admin')
      @ApiParam({ name: 'id', type: String, required: true })
      @ApiParam({ name: 'userId', type: String, required: true })
      public async removeOwner(@Param('id') id: string, @Param('userId') userId: string): Promise<void> {
        // Get structure
        const structure = await this.structureService.findOne(id);
        if (!structure) {
          throw new HttpException('Invalid Structure', HttpStatus.NOT_FOUND);
        }
        // Get user
        const userFromDb = await this.userService.findById(userId);
        if (!userFromDb || !userFromDb.structuresLink.includes(Types.ObjectId(id))) {
          throw new HttpException('Invalid User', HttpStatus.NOT_FOUND);
        }
        this.userService.removeFromStructureLinked(userFromDb.email, id);
      }
    
      @Post('reportStructureError')
      public async reportStructureError(@Body() data: { structureId: string; content: string }): Promise<void> {
        return this.structureService.reportStructureError(data.structureId, data.content);
      }
    }