diff --git a/package-lock.json b/package-lock.json index e7b34caac450c58b2119446195eaa18a0318399f..9cbd9ccb691588704b567e79dd12534926bcd2d6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3538,6 +3538,11 @@ "integrity": "sha512-uc2Vix1frTfnuzxxu1Hp4ktSvM3QaI4oXl4ZUqL1wjTu/BGki9TrCWoqLTg/drR1KwAEarXuRFCG2Svr1GxPFw==", "dev": true }, + "class-transformer": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/class-transformer/-/class-transformer-0.3.1.tgz", + "integrity": "sha512-cKFwohpJbuMovS8xVLmn8N2AUbAuc8pVo4zEfsUVo8qgECOogns1WVk/FkOZoxhOPTyTYFckuoH+13FO+MQ8GA==" + }, "class-utils": { "version": "0.3.6", "resolved": "https://registry.npmjs.org/class-utils/-/class-utils-0.3.6.tgz", diff --git a/package.json b/package.json index e31fb5242390f4bc84e6f9313ae223c447e19a24..35186b8b3707f6b468d07db0d8bcabf1f294e6b1 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "@nestjs/swagger": "^4.7.5", "@types/bcrypt": "^3.0.0", "bcrypt": "^5.0.0", + "class-transformer": "^0.3.1", "class-validator": "^0.12.2", "dotenv": "^8.2.0", "ejs": "^3.1.5", diff --git a/src/main.ts b/src/main.ts index 65ac8383570553afd58e1b089359a71bd922ceb5..ed8e487ca1cfc569b967338942d322c5c1f7f31c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,10 +1,11 @@ +import { ValidationPipe } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); - + app.useGlobalPipes(new ValidationPipe()); const options = new DocumentBuilder().setTitle('RAM').setDescription('RAM API description').setVersion('1.0').build(); const document = SwaggerModule.createDocument(app, options); SwaggerModule.setup('api', app, document); diff --git a/src/structures/dto/create-structure.dto.ts b/src/structures/dto/create-structure.dto.ts index 857697914c2aad758535f706b2e2f0811fb3e2bd..147b677fa780ca6827161ae71483d8d278e98fc2 100644 --- a/src/structures/dto/create-structure.dto.ts +++ b/src/structures/dto/create-structure.dto.ts @@ -1,55 +1,70 @@ +import { Type } from 'class-transformer'; +import { ArrayNotEmpty, IsNotEmpty, ValidateNested } from 'class-validator'; +import { Address } from '../schemas/address.schema'; import { Week } from '../schemas/week.schema'; export class CreateStructureDto { id: number; - numero: number; - dateDeCreation: string; - derniereModification: string; - nomDeLusager: string; - votreStructureEstElle: string; - nomDeVotreStructure: string; + numero: string; + createdAt: string; + updatedAt: string; + + @IsNotEmpty() + structureRepresentation: string; + + @IsNotEmpty() + structureName: string; + + @ArrayNotEmpty() + structureType: string[]; + + @IsNotEmpty() description: string; - activitesMaintenuesDansLeCadreDuConfinement: string; - n: string; - voie: string; - telephone: string; - courriel: string; - siteWeb: string; + + @ValidateNested({ each: true }) + @Type(() => Address) + address: Address; + + @IsNotEmpty() + contactPhone: string; + + @IsNotEmpty() + contactMail: string; + + website: string; facebook: string; twitter: string; instagram: string; - civilite: string; - nom: string; - prenom: string; + gender: string; + contactName: string; + contactSurname: string; fonction: string; - accessibilitePersonnesAMobiliteReduitePmr: string; - modalitesDacces: string[]; - labelsEtQualifications: string[]; - publicsAcceptes: string[]; - fermeturesExceptionnelles: string; - jaccompagneLesUsagersDansLeursDemarchesEnLigne: boolean; - accompagnementDesDemarches: string[]; - autresAccompagnements: string; - lesCompetencesDeBase: string[]; - accesAuxDroits: string[]; - insertionSocialeEtProfessionnelle: string[]; - aideALaParentalite: string[]; - cultureEtSecuriteNumerique: string[]; - wifiEnAccesLibre: boolean; - nbComputers: boolean; - nombre: string; - tablettes: boolean; - bornesNumeriques: boolean; - imprimantes: boolean; - precisionsSiNecessaire: string; - statutJuridique: string; - appartenezVousAUnReseauDeMediation: string; - precisezLequel: string; - idDeLitemStructureDansDirectus: string; - statutDeLitemStructureDansDirectus: string; - idDeLitemOffreDansDirectus: string; - statut: string; - typeDeStructure: string[]; - commune: string; + lockdownActivity: string; + pmrAccess: boolean; + publicsAccompaniment: string[]; + proceduresAccompaniment: string[]; + @ArrayNotEmpty() + accessModality: string[]; + + documentsMeeting: string; + labelsQualifications: string[]; + + @ArrayNotEmpty() + publics: string[]; + + nbComputers: number; + nbPrinters: number; + nbTablets: number; + nbNumericTerminal: number; + exceptionalClosures: string; + equipmentsAndServices: string[]; hours: Week; + equipmentsDetails: string; + equipmentsAccessType: string[]; + baseSkills: string[]; + accessRight: string[]; + parentingHelp: string[]; + socialAndProfessional: string[]; + digitalCultureSecurity: string[]; + coord: number[]; } diff --git a/src/structures/schemas/address.schema.ts b/src/structures/schemas/address.schema.ts new file mode 100644 index 0000000000000000000000000000000000000000..8fa4e35c5c469dcfd2a36d88b78faf7d1575a93f --- /dev/null +++ b/src/structures/schemas/address.schema.ts @@ -0,0 +1,16 @@ +import { SchemaFactory } from '@nestjs/mongoose'; +import { IsNotEmpty } from 'class-validator'; + +export type AddressDocument = Address & Document; + +export class Address { + numero: string; + + @IsNotEmpty() + street: string; + + @IsNotEmpty() + commune: string; +} + +export const AddressSchema = SchemaFactory.createForClass(Address); diff --git a/src/structures/schemas/structure.schema.ts b/src/structures/schemas/structure.schema.ts index 711c63714c5c39442ed74f8bf29d333788155755..3e609fa7d5d2010cabbb09d9e25411cce1505dea 100644 --- a/src/structures/schemas/structure.schema.ts +++ b/src/structures/schemas/structure.schema.ts @@ -1,5 +1,6 @@ import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; import { Document } from 'mongoose'; +import { Address } from './address.schema'; import { Week } from './week.schema'; export type StructureDocument = Structure & Document; @@ -13,43 +14,36 @@ export class Structure { numero: string; @Prop() - dateDeCreation: string; + createdAt: string; @Prop() - derniereModification: string; + updatedAt: string; @Prop() - nomDeLusager: string; + structureRepresentation: string; @Prop() - votreStructureEstElle: string; + structureName: string; @Prop() - nomDeVotreStructure: string; + structureType: string[]; @Prop() description: string; @Prop() - activitesMaintenuesDansLeCadreDuConfinement: string; + lockdownActivity: string; @Prop() - n: string; + address: Address; @Prop() - voie: string; + contactPhone: string; @Prop() - address: string; - - @Prop() - telephone: string; - - @Prop() - courriel: string; - + contactMail: string; @Prop() - siteWeb: string; + website: string; @Prop() facebook: string; @@ -61,91 +55,76 @@ export class Structure { instagram: string; @Prop() - civilite: string; + gender: string; @Prop() - nom: string; + contactName: string; @Prop() - prenom: string; + contactSurname: string; @Prop() fonction: string; @Prop() - accessibilitePersonnesAMobiliteReduitePmr: string; + pmrAccess: boolean; @Prop() - modalitesDacces: string[]; + accessModality: string[]; @Prop() - labelsEtQualifications: string[]; + documentsMeeting: string; @Prop() - publicsAcceptes: string[]; + labelsQualifications: string[]; @Prop() - fermeturesExceptionnelles: string; + publics: string[]; @Prop() - jaccompagneLesUsagersDansLeursDemarchesEnLigne: boolean; + exceptionalClosures: string; @Prop() - accompagnementDesDemarches: string[]; + publicsAccompaniment: string[]; @Prop() - autresAccompagnements: string; + proceduresAccompaniment: string[]; @Prop() - lesCompetencesDeBase: string[]; + baseSkills: string[]; @Prop() - accesAuxDroits: string[]; + accessRight: string[]; @Prop() - insertionSocialeEtProfessionnelle: string[]; + socialAndProfessional: string[]; @Prop() - aideALaParentalite: string[]; + parentingHelp: string[]; @Prop() - cultureEtSecuriteNumerique: string[]; + digitalCultureSecurity: string[]; @Prop() - equipementsEtServicesProposes: string[]; + equipmentsDetails: string; @Prop() - nbComputers: number; - - @Prop() - precisionsSiNecessaire: string; + equipmentsAccessType: string[]; @Prop() - statutJuridique: string; + equipmentsAndServices: string[]; @Prop() - appartenezVousAUnReseauDeMediation: string; - - @Prop() - precisezLequel: string; - - @Prop() - idDeLitemStructureDansDirectus: string; - - @Prop() - statutDeLitemStructureDansDirectus: string; - - @Prop() - idDeLitemOffreDansDirectus: string; + nbComputers: number; @Prop() - statut: string; + nbPrinters: number; @Prop() - typeDeStructure: string[]; + nbTablets: number; @Prop() - commune: string; + nbNumericTerminal: number; @Prop() hours: Week; diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts index 5ffa5394b89d881fc908c202851acbf9aab3ba7a..cf57ddf3ad02cd7bc1786b4abdb87e97b5131487 100644 --- a/src/structures/structures.controller.ts +++ b/src/structures/structures.controller.ts @@ -1,7 +1,7 @@ import { Body, Controller, Get, Post, Query } from '@nestjs/common'; import { CreateStructureDto } from './dto/create-structure.dto'; import { QueryStructure } from './dto/query-structure.dto'; -import { Structure } from './schemas/structure.schema'; +import { Structure, StructureDocument } from './schemas/structure.schema'; import { StructuresService } from './structures.service'; @Controller('structures') @@ -18,6 +18,11 @@ export class StructuresController { return this.structureService.search(query.query, body ? body.filters : null); } + @Post(':id') + public async update(@Param('id') id: number, @Body() body: CreateStructureDto) { + return this.structureService.update(id, body); + } + @Get() public async findAll(): Promise<Structure[]> { return this.structureService.findAll(); @@ -26,18 +31,26 @@ export class StructuresController { @Get('count') public async countCategories(): Promise<Array<{ id: string; count: number }>> { const data = await Promise.all([ - this.structureService.countByStructureKey('accesAuxDroits'), - this.structureService.countByStructureKey('aideALaParentalite'), - this.structureService.countByStructureKey('cultureEtSecuriteNumerique'), - this.structureService.countByStructureKey('insertionSocialeEtProfessionnelle'), - this.structureService.countByStructureKey('accompagnementDesDemarches'), - this.structureService.countByStructureKey('labelsEtQualifications'), - this.structureService.countByStructureKey('publicsAcceptes'), - this.structureService.countByStructureKey('modalitesDacces'), - this.structureService.countByStructureKey('lesCompetencesDeBase'), - this.structureService.countByStructureKey('equipementsEtServicesProposes'), + 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]); } + + @Get(':id') + public async find(@Param('id') id: number) { + return this.structureService.findOne(id); + } } diff --git a/src/structures/structures.service.ts b/src/structures/structures.service.ts index 3e473a5e65cf26b369fded508fc8328bfae73f7a..543058c8c971a9875bf2435c2c8ad44b7446a694 100644 --- a/src/structures/structures.service.ts +++ b/src/structures/structures.service.ts @@ -1,4 +1,4 @@ -import { HttpService, Injectable } from '@nestjs/common'; +import { HttpException, HttpService, Injectable, HttpStatus } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { Observable } from 'rxjs'; @@ -69,15 +69,25 @@ export class StructuresService { 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.n, structure.voie, structure.commune).subscribe( + this.getCoord(structure.address.numero, structure.address.street, structure.address.commune).subscribe( (res) => { const address = res.data.features[0]; - structure.address = structure.voie + ' - ' + address.properties.postcode + ' ' + address.properties.city; structure.coord = address.geometry.coordinates; resolve(structure); },