diff --git a/src/structures/dto/create-structure.dto.ts b/src/structures/dto/create-structure.dto.ts index ee9cc22565d0a7e4052c8ef3f3f3b961d43e4d75..a36c5c60eba6da83917dba0ab06d13012e398136 100644 --- a/src/structures/dto/create-structure.dto.ts +++ b/src/structures/dto/create-structure.dto.ts @@ -1,11 +1,11 @@ import { Type } from 'class-transformer'; import { IsNotEmpty, ValidateNested } from 'class-validator'; -import { structureDto } from './structure.dto'; +import { StructureDto } from './structure.dto'; export class CreateStructureDto { @ValidateNested({ each: true }) - @Type(() => structureDto) - structure: structureDto; + @Type(() => StructureDto) + structure: StructureDto; @IsNotEmpty() idUser: string; diff --git a/src/structures/dto/structure.dto.ts b/src/structures/dto/structure.dto.ts index 3a201678c40cb2ba34cc5d201f5e0e8a0179d167..d3fec8041d456424cb8d0ab1c17547b1a28e3794 100644 --- a/src/structures/dto/structure.dto.ts +++ b/src/structures/dto/structure.dto.ts @@ -4,7 +4,7 @@ import { Address } from '../schemas/address.schema'; import { Week } from '../../shared/schemas/week.schema'; import { PersonalOfferDocument } from '../../personal-offers/schemas/personal-offer.schema'; -export class structureDto { +export class StructureDto { numero: string; createdAt: string; updatedAt: string; @@ -69,7 +69,7 @@ export class structureDto { socialAndProfessional: string[]; digitalCultureSecurity: string[]; coord: number[]; - accountVerified: boolean; + accountVerified = true; // Value is forced to true because there is no verification needed now. Cannot be removed because of old data prod dataShareConsentDate: Date; @IsArray() diff --git a/src/structures/services/structure.service.spec.ts b/src/structures/services/structure.service.spec.ts index f20271c7f727f400d8e008408d9b66b6c1fdf30d..f66440c0efc47eeb1a8066d76d93f4385399a0e8 100644 --- a/src/structures/services/structure.service.spec.ts +++ b/src/structures/services/structure.service.spec.ts @@ -9,7 +9,7 @@ import { ConfigurationService } from '../../configuration/configuration.service' import { MailerModule } from '../../mailer/mailer.module'; import { SearchModule } from '../../search/search.module'; import { UsersService } from '../../users/services/users.service'; -import { structureDto } from '../dto/structure.dto'; +import { StructureDto } from '../dto/structure.dto'; import { Structure, StructureDocument } from '../schemas/structure.schema'; import { StructuresSearchService } from './structures-search.service'; import { StructuresService } from './structures.service'; @@ -351,7 +351,7 @@ describe('StructuresService', () => { }); it('should create structure', () => { - const structure = new structureDto(); + const structure = new StructureDto(); let res = service.create(null, structure); expect(res).toBeTruthy(); res = service.create('tsfsf6296', structure); diff --git a/src/structures/services/structures-search.service.ts b/src/structures/services/structures-search.service.ts index c483471db55779db9bfdbf0d1acd62d57cc719e3..cdd3575d031b51c4b4f62ec2f96d19b19ce3de9d 100644 --- a/src/structures/services/structures-search.service.ts +++ b/src/structures/services/structures-search.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@nestjs/common'; import { ElasticsearchService } from '@nestjs/elasticsearch'; -import { structureDto } from '../dto/structure.dto'; +import { StructureDto } from '../dto/structure.dto'; import { StructureDocument } from '../schemas/structure.schema'; import { StructureSearchBody } from '../interfaces/structure-search-body.interface'; import { StructureSearchResult } from '../interfaces/structure-search-response.interface'; @@ -137,7 +137,7 @@ export class StructuresSearchService { return sortedHits.map((item) => item._source); } - public async update(structure: structureDto, id: string): Promise<any> { + public async update(structure: StructureDto, id: string): Promise<any> { return this.elasticsearchService.update({ index: this.index, id: id, diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts index 0cd74d9631d4435967b0f4ca78bf5b4871eb93b9..21d458e6d8e393d89f853ea047605e32284691cc 100644 --- a/src/structures/services/structures.service.ts +++ b/src/structures/services/structures.service.ts @@ -5,7 +5,7 @@ import { Observable } from 'rxjs'; import { AxiosResponse } from 'axios'; import { Structure, StructureDocument } from '../schemas/structure.schema'; import * as ejs from 'ejs'; -import { structureDto } from '../dto/structure.dto'; +import { StructureDto } from '../dto/structure.dto'; import { UsersService } from '../../users/services/users.service'; import { User } from '../../users/schemas/user.schema'; import { MailerService } from '../../mailer/mailer.service'; @@ -37,13 +37,13 @@ export class StructuresService { ) {} async initiateStructureIndex(): Promise<StructureDocument[]> { - Logger.log('Reset structures indexes'); + Logger.log('Reset structures indexes', StructuresService.name); await this.structuresSearchService.dropIndex(); await this.structuresSearchService.createStructureIndex(); return this.populateES(); } - public fillFilters(filters: Array<any>): Promise<{}[]>[] { + public fillFilters(filters: Array<any>): Promise<any[]>[] { return filters?.map(async (elem) => { const key = Object.keys(elem)[0]; const modules = (await this.categoriesFormationsService.findOneComplete(elem[key])).modules; @@ -110,11 +110,15 @@ export class StructuresService { } } - public async create(idUser: string, structure: structureDto): Promise<Structure> { + public async create(idUser: string, structure: StructureDto): Promise<Structure> { const user = await this.userService.findOne(idUser); if (!user) { throw new HttpException('Invalid profile', HttpStatus.NOT_FOUND); } + // Handle equipments + structure = this.addEquipmentsCategories(structure); + // Value is forced to true because there is no verification needed now. Cannot be removed because of old data prod + structure.accountVerified = true; const createdStructure = new this.structureModel(structure); createdStructure._id = Types.ObjectId(); await createdStructure.save(); @@ -168,6 +172,28 @@ export class StructuresService { } } + public addEquipmentsCategories(structure: Structure): Structure { + Logger.debug(`addEquipmentsCategories of ${structure.structureName}`, StructuresService.name); + if (structure.nbComputers > 0) { + structure.equipmentsAndServices.push('ordinateurs'); + } + if (structure.nbPrinters > 0) { + structure.equipmentsAndServices.push('imprimantes'); + } + if (structure.nbNumericTerminal > 0) { + structure.equipmentsAndServices.push('bornesNumeriques'); + } + if (structure.nbScanners > 0) { + structure.equipmentsAndServices.push('scanners'); + } + if (structure.nbTablets > 0) { + structure.equipmentsAndServices.push('tablettes'); + } + // Remove duplicate if exists + structure.equipmentsAndServices = [...new Set(structure.equipmentsAndServices)]; + return structure; + } + /** * Parse filter value from string to boolean * @param filters @@ -184,6 +210,7 @@ export class StructuresService { } public async findAll(): Promise<StructureDocument[]> { + Logger.debug('findAll', StructuresService.name); const structures = await this.structureModel.find({ deletedAt: { $exists: false } }).exec(); // Update structures coord and address before sending them await Promise.all( @@ -193,7 +220,6 @@ export class StructuresService { return this.getStructurePosition(structure).then((postition: StructureDocument) => { this.structureModel .findByIdAndUpdate(Types.ObjectId(structure._id), { address: postition.address, coord: postition.coord }) - .populate('personalOffers') .exec(); }); } @@ -244,7 +270,10 @@ export class StructuresService { .populate('personalOffers') .exec(); // Update structures coord and address before sending them - Logger.debug('Find all formated structures, only returning structures who consented to data sharing'); + Logger.debug( + 'Find all formated structures, only returning structures who consented to data sharing', + StructuresService.name + ); await Promise.all( structures.map((structure: StructureDocument) => { // If structure has no address, add it @@ -341,11 +370,13 @@ export class StructuresService { }); } - public async update(idStructure: string, structure: structureDto): Promise<Structure> { + public async update(idStructure: string, structure: StructureDto): Promise<Structure> { const oldStructure = await this.findOne(idStructure); if (!_.isEqual(oldStructure.address, structure.address)) { await this.getStructurePosition(structure).then(); } + // Parse equipments + structure = this.addEquipmentsCategories(structure); const result = await this.structureModel.findByIdAndUpdate(Types.ObjectId(idStructure), structure).exec(); if (!result) { throw new HttpException('Invalid structure id', HttpStatus.BAD_REQUEST); @@ -388,7 +419,7 @@ export class StructuresService { } else { Logger.error( `No coord found for: ${structure.address.numero} ${structure.address.street} ${structure.address.commune}`, - 'StructureService' + StructuresService.name ); structure.coord = []; } @@ -402,7 +433,7 @@ export class StructuresService { } else { Logger.error( `No coord found for: ${structure.address.numero} ${structure.address.street} ${structure.address.commune}`, - 'StructureService' + StructuresService.name ); structure.coord = []; } @@ -410,14 +441,14 @@ export class StructuresService { } else { Logger.error( `No structure found for: ${structure.address.numero} ${structure.address.street} ${structure.address.commune}`, - 'StructureService' + StructuresService.name ); } } }); }, (err) => { - Logger.error(`Request error: ${err.config.url}`, 'StructureService'); + Logger.error(`Request error: ${err.config.url}`, StructuresService.name); Logger.error(err); reject(err); } @@ -440,14 +471,14 @@ export class StructuresService { */ public async searchAddress(data: { searchQuery: string; - }): Promise<{ features: { geometry: {}; type: string; properties: {} }[] }> { + }): Promise<{ features: { geometry: any; type: string; properties: any }[] }> { const reqBan = `https://download.data.grandlyon.com/geocoding/photon/api?q=${data.searchQuery}&lang=fr&limit=500&osm_tag=:!construction&osm_tag=:!bus_stop`; const reqBal = `https://download.data.grandlyon.com/geocoding/photon-bal/api?q=${data.searchQuery}&lang=fr&limit=500&osm_tag=:!construction&osm_tag=:!bus_stop`; - const requestGroup = (url): Promise<{ features: { geometry: {}; type: string; properties: {} }[] }> => - new Promise((resolve, reject) => { - Logger.debug(`Search request: ${encodeURI(url)}`, 'StructureService'); + const requestGroup = (url): Promise<{ features: { geometry: any; type: string; properties: any }[] }> => + new Promise((resolve) => { + Logger.debug(`Search request: ${encodeURI(url)}`, StructuresService.name); return this.httpService .request({ url: encodeURI(url), @@ -456,7 +487,7 @@ export class StructuresService { }) .subscribe( (reply) => { - Logger.debug(`Search request response length : ${reply.data.features.length}`, 'StructureService'); + Logger.debug(`Search request response length : ${reply.data.features.length}`, StructuresService.name); reply.data.features = reply.data.features .filter((doc) => doc.properties.postcode && doc.properties.postcode.match(depRegex)) .sort((a, b) => { @@ -465,7 +496,7 @@ export class StructuresService { return resolve(reply.data); }, (err) => { - Logger.error(`Search - Request error: ${err.config.url}`, 'StructureService'); + Logger.error(`Search - Request error: ${err.config.url}`, StructuresService.name); Logger.error(err); } ); @@ -531,7 +562,7 @@ export class StructuresService { public getCoord(numero: string, address: string, zipcode: string, scope: string): Observable<AxiosResponse<any>> { const req = `https://download.data.grandlyon.com/geocoding/${scope}/api?q=` + numero + ' ' + address + ' ' + zipcode; - Logger.debug('Print getCoord' + req); + Logger.debug('Print getCoord' + req, StructuresService.name); return this.httpService.get(encodeURI(req)); } @@ -749,7 +780,7 @@ export class StructuresService { phone: user.phone, structures: await Promise.all( user.structuresLink.map(async (id) => { - return await this.findOne(id.toHexString()); + return this.findOne(id.toHexString()); }) ), }; diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts index 8285902e3b4ae9fb7d7d74b5a4bf484e34c0f89a..51fd2b1160a0782fb70f745f4e63a701f3cd4d09 100644 --- a/src/structures/structures.controller.ts +++ b/src/structures/structures.controller.ts @@ -6,6 +6,7 @@ import { HttpException, HttpService, HttpStatus, + Logger, Param, Post, Put, @@ -26,7 +27,7 @@ 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 { 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'; @@ -68,6 +69,7 @@ export class StructuresController { @Post() public async create(@Body() createStructureDto: CreateStructureDto): Promise<Structure> { + Logger.debug(`create | ${createStructureDto.structure.structureName}`, StructuresController.name); return this.structureService.create(createStructureDto.idUser, createStructureDto.structure); } @@ -90,18 +92,20 @@ export class StructuresController { @Put('updateAfterOwnerVerify/:id') public async updateAfterOwnerVerify(@Param('id') id: string): Promise<Structure> { + Logger.debug(`updateAfterOwnerVerify | structure ${id}`, StructuresController.name); return this.structureService.updateAccountVerified(id); } @Put(':id') @UseGuards(JwtAuthGuard, IsStructureOwnerGuard) @Roles('admin') - public async update(@Param('id') id: string, @Body() body: structureDto): Promise<Structure> { + public async update(@Param('id') id: string, @Body() body: StructureDto): Promise<Structure> { return this.structureService.update(id, body); } @Get() public async findAll(): Promise<Structure[]> { + Logger.debug(`findAll`, StructuresController.name); return this.structureService.findAll(); } diff --git a/src/users/services/jobs.service.ts b/src/users/services/jobs.service.ts index f44f48fdb2b5c49c75cac6d5a3b671fd715aabeb..43e77b4c6fc19ef73e7da020b31308a05aa2c6aa 100644 --- a/src/users/services/jobs.service.ts +++ b/src/users/services/jobs.service.ts @@ -24,8 +24,8 @@ export class JobsService { return this.jobModel.findOne({ name }); } - public async create(employer: CreateJobDto, validated = false, hasPersonalOffer = false): Promise<Job> { - this.logger.debug(`createEmployer: ${employer.name}`); - return this.jobModel.create({ name: employer.name, validated, hasPersonalOffer }); + public async create(job: CreateJobDto, validated = false, hasPersonalOffer = true): Promise<Job> { + this.logger.debug(`createJob: ${job.name}`); + return this.jobModel.create({ name: job.name, validated, hasPersonalOffer }); } }