Skip to content
Snippets Groups Projects
Select Git revision
  • 56f9bfcc4995e3e1c5caf084367549f3b850e587
  • dev default protected
  • 168-pro-connect
  • master protected
  • 723-populate-personalOffers
  • 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

temp-user.interface.ts

Blame
  • structures.service.ts 4.42 KiB
    import { HttpException, HttpService, Injectable, HttpStatus } from '@nestjs/common';
    import { InjectModel } from '@nestjs/mongoose';
    import { Model } from 'mongoose';
    import { Observable } from 'rxjs';
    import { AxiosResponse } from 'axios';
    import { CreateStructureDto } from './dto/create-structure.dto';
    import { Structure, StructureDocument } from './schemas/structure.schema';
    import { Logger } from '@nestjs/common';
    
    @Injectable()
    export class StructuresService {
      constructor(
        private readonly httpService: HttpService,
        @InjectModel(Structure.name) private structureModel: Model<StructureDocument>
      ) {}
    
      public async create(createStructrureDto: CreateStructureDto): Promise<Structure> {
        const createdStructure = new this.structureModel(createStructrureDto);
        return createdStructure.save();
      }
    
      public async search(searchString: string, filters?: Array<any>): Promise<Structure[]> {
        if (searchString && filters) {
          return this.structureModel
            .find({ $and: [...this.parseFilter(filters), { $text: { $search: searchString } }] })
            .exec();
        } else if (filters) {
          return this.structureModel.find({ $or: this.parseFilter(filters) }).exec();
        } else {
          return this.structureModel.find({ $or: [{ $text: { $search: searchString } }] }).exec();
        }
      }
    
      /**
       * Parse filter value from string to boolean
       * @param filters
       */
      private parseFilter(filters: Array<any>): Array<any> {
        return filters.map((filter) => {
          const key = Object.keys(filter)[0];
          if (filter[key] === 'True') {
            return { [key]: true };
          } else {
            return filter;
          }
        });
      }
    
      public async findAll(): Promise<Structure[]> {
        const structures = await this.structureModel.find().exec();
        // Update structures coord and address before sending them
        await Promise.all(
          structures.map((structure: Structure) => {
            // If structre has no address, add it
            if (!structure.address) {
              this.getStructurePosition(structure).then((postition) => {
                this.structureModel
                  .findOneAndUpdate({ id: structure.id }, { address: postition.address, coord: postition.coord })
                  .exec();
              });
            }
            if (structure.coord.length <= 0) {
              this.getStructurePosition(structure).then((postition) => {
                this.structureModel.findOneAndUpdate({ id: postition.id }, { coord: postition.coord }).exec();
              });
            }
          })
        );
        return this.structureModel.find().exec();
      }
    
      public async update(idStructure: number, structure: CreateStructureDto): Promise<Structure> {
        const result = await this.structureModel.update({ id: idStructure }, structure);
        if (!result) {
          throw new HttpException('Invalid structure id', HttpStatus.BAD_REQUEST);
        }
        return structure;
      }
    
      public findOne(idParam: number): Promise<Structure> {
        return this.structureModel.findOne({ id: idParam }).exec();
      }
      /**
       * Get structures positions and add marker corresponding to those positons on the map
       */
      private getStructurePosition(structure: Structure): Promise<Structure> {
        return new Promise((resolve) => {
          this.getCoord(structure.address.numero, structure.address.street, structure.address.commune).subscribe(
            (res) => {
              const address = res.data.features[0];
              structure.coord = address.geometry.coordinates;
              resolve(structure);
            },
            (err) => {
              Logger.error(`Request error: ${err.config.url}`, 'StructureService');
              Logger.error(err);
            }
          );
        });
      }
    
      /**
       * Count every value occurence of a given key
       * @param key structure key
       * @return [{id: 'key', count: 'value'}]
       */
      public async countByStructureKey(key: string): Promise<any> {
        const uniqueElements = await this.structureModel.distinct(key).exec();
        return await Promise.all(
          uniqueElements.map(async (value) => {
            return {
              id: value,
              count: await this.structureModel.countDocuments({ [key]: { $elemMatch: { $eq: value } } }).exec(),
            };
          })
        );
      }
    
      public getCoord(numero: string, address: string, zipcode: string): Observable<AxiosResponse<any>> {
        const req =
          'https://download.data.grandlyon.com/geocoding/photon/api' + '?q=' + numero + ' ' + address + ' ' + zipcode;
        Logger.log(`Request : ${req}`, 'StructureService - getCoord');
        return this.httpService.get(encodeURI(req));
      }
    }