diff --git a/src/structures/dto/create-structure.dto.ts b/src/structures/dto/create-structure.dto.ts index 147b677fa780ca6827161ae71483d8d278e98fc2..ee9cc22565d0a7e4052c8ef3f3f3b961d43e4d75 100644 --- a/src/structures/dto/create-structure.dto.ts +++ b/src/structures/dto/create-structure.dto.ts @@ -1,70 +1,12 @@ import { Type } from 'class-transformer'; -import { ArrayNotEmpty, IsNotEmpty, ValidateNested } from 'class-validator'; -import { Address } from '../schemas/address.schema'; -import { Week } from '../schemas/week.schema'; +import { IsNotEmpty, ValidateNested } from 'class-validator'; +import { structureDto } from './structure.dto'; export class CreateStructureDto { - id: number; - numero: string; - createdAt: string; - updatedAt: string; - - @IsNotEmpty() - structureRepresentation: string; - - @IsNotEmpty() - structureName: string; - - @ArrayNotEmpty() - structureType: string[]; - - @IsNotEmpty() - description: string; - @ValidateNested({ each: true }) - @Type(() => Address) - address: Address; - - @IsNotEmpty() - contactPhone: string; + @Type(() => structureDto) + structure: structureDto; @IsNotEmpty() - contactMail: string; - - website: string; - facebook: string; - twitter: string; - instagram: string; - gender: string; - contactName: string; - contactSurname: string; - fonction: 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[]; + idUser: string; } diff --git a/src/structures/dto/structure.dto.ts b/src/structures/dto/structure.dto.ts new file mode 100644 index 0000000000000000000000000000000000000000..7af244c8643f1d45b2a82b93c465fe1d6d6481fa --- /dev/null +++ b/src/structures/dto/structure.dto.ts @@ -0,0 +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 structureDto { + id: number; + numero: string; + createdAt: string; + updatedAt: string; + + @IsNotEmpty() + structureRepresentation: string; + + @IsNotEmpty() + structureName: string; + + @ArrayNotEmpty() + structureType: string[]; + + @IsNotEmpty() + description: string; + + @ValidateNested({ each: true }) + @Type(() => Address) + address: Address; + + @IsNotEmpty() + contactPhone: string; + + @IsNotEmpty() + contactMail: string; + + website: string; + facebook: string; + twitter: string; + instagram: string; + gender: string; + contactName: string; + contactSurname: string; + fonction: 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/structure.schema.ts b/src/structures/schemas/structure.schema.ts index 3e609fa7d5d2010cabbb09d9e25411cce1505dea..6d7af9fb537f2a266957a596effbe5794b1cc0cf 100644 --- a/src/structures/schemas/structure.schema.ts +++ b/src/structures/schemas/structure.schema.ts @@ -5,7 +5,7 @@ import { Week } from './week.schema'; export type StructureDocument = Structure & Document; -@Schema() +@Schema({ timestamps: { createdAt: 'createdAt', updatedAt: 'updatedAt' } }) export class Structure { @Prop() id: number; @@ -134,5 +134,4 @@ export class Structure { } export const StructureSchema = SchemaFactory.createForClass(Structure); - StructureSchema.index({ '$**': 'text' }); diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts index 8026e174751cf674eea9043758705172c9d41e8f..5178d63765bcc8ace3be2e92f0a75028c70f72b9 100644 --- a/src/structures/structures.controller.ts +++ b/src/structures/structures.controller.ts @@ -1,6 +1,7 @@ import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common'; import { CreateStructureDto } from './dto/create-structure.dto'; import { QueryStructure } from './dto/query-structure.dto'; +import { structureDto } from './dto/structure.dto'; import { Structure } from './schemas/structure.schema'; import { StructuresService } from './structures.service'; @@ -9,8 +10,8 @@ export class StructuresController { constructor(private readonly structureService: StructuresService) {} @Post() - public async create(@Body() createStructureDto: CreateStructureDto) { - await this.structureService.create(createStructureDto); + public async create(@Body() createStructureDto: CreateStructureDto): Promise<Structure> { + return this.structureService.create(createStructureDto.idUser, createStructureDto.structure); } @Post('search') @@ -19,7 +20,7 @@ export class StructuresController { } @Post(':id') - public async update(@Param('id') id: number, @Body() body: CreateStructureDto) { + public async update(@Param('id') id: number, @Body() body: structureDto) { return this.structureService.update(id, body); } diff --git a/src/structures/structures.module.ts b/src/structures/structures.module.ts index 6a82aae92ddcec6e352b3ede79367694322e150f..bae6365d39a951aff5709a1cb0272e2a35502bbf 100644 --- a/src/structures/structures.module.ts +++ b/src/structures/structures.module.ts @@ -1,12 +1,18 @@ import { HttpModule, Module } from '@nestjs/common'; import { MongooseModule } from '@nestjs/mongoose'; import { MailerModule } from '../mailer/mailer.module'; +import { UsersModule } from '../users/users.module'; import { Structure, StructureSchema } from './schemas/structure.schema'; import { StructuresController } from './structures.controller'; import { StructuresService } from './structures.service'; @Module({ - imports: [MongooseModule.forFeature([{ name: Structure.name, schema: StructureSchema }]), HttpModule, MailerModule], + imports: [ + MongooseModule.forFeature([{ name: Structure.name, schema: StructureSchema }]), + HttpModule, + MailerModule, + UsersModule, + ], controllers: [StructuresController], providers: [StructuresService], }) diff --git a/src/structures/structures.service.ts b/src/structures/structures.service.ts index 543058c8c971a9875bf2435c2c8ad44b7446a694..3de8c100217634ee885e754522600167becde9b3 100644 --- a/src/structures/structures.service.ts +++ b/src/structures/structures.service.ts @@ -3,20 +3,30 @@ 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'; +import { structureDto } from './dto/structure.dto'; +import { UsersService } from '../users/users.service'; @Injectable() export class StructuresService { constructor( private readonly httpService: HttpService, + private readonly usersService: UsersService, @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 create(idUser: string, structureDto: structureDto): Promise<Structure> { + const user = await this.usersService.findOne(idUser); + if (!user) { + throw new HttpException('Invalid profile', HttpStatus.NOT_FOUND); + } + let createdStructure = new this.structureModel(structureDto); + createdStructure.id = (await this.getNumberStructures()) + 1; + createdStructure.save(); + user.structuresLink.push(createdStructure.id); + user.save(); + return createdStructure; } public async search(searchString: string, filters?: Array<any>): Promise<Structure[]> { @@ -53,15 +63,22 @@ export class StructuresService { structures.map((structure: Structure) => { // If structre has no address, add it if (!structure.address) { - this.getStructurePosition(structure).then((postition) => { + return 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 new Promise((resolve) => { + this.getStructurePosition(structure).then((postition) => { + this.structureModel + .findOneAndUpdate({ id: postition.id }, { coord: postition.coord }) + .exec() + .then(() => { + resolve(''); + }); + }); }); } }) @@ -69,7 +86,7 @@ export class StructuresService { return this.structureModel.find().exec(); } - public async update(idStructure: number, structure: CreateStructureDto): Promise<Structure> { + public async update(idStructure: number, structure: structureDto): Promise<Structure> { const result = await this.structureModel.update({ id: idStructure }, structure); if (!result) { throw new HttpException('Invalid structure id', HttpStatus.BAD_REQUEST); @@ -122,4 +139,8 @@ export class StructuresService { Logger.log(`Request : ${req}`, 'StructureService - getCoord'); return this.httpService.get(encodeURI(req)); } + + public async getNumberStructures(): Promise<number> { + return await this.structureModel.countDocuments(); + } } diff --git a/src/users/user.interface.ts b/src/users/user.interface.ts index 93aec2489ff596ca7894df6cd4dc8ca2de07fbbd..d235abc515217700995b46e9fa21a851af6ac2f8 100644 --- a/src/users/user.interface.ts +++ b/src/users/user.interface.ts @@ -10,4 +10,5 @@ export interface IUser extends Document { role: number; changeEmailToken: string; newEmail: string; + structuresLink: number[]; } diff --git a/src/users/user.schema.ts b/src/users/user.schema.ts index b429332fe8b17d06eb53488ccf6285ffb7224d19..0c04dba82e74cd4ee231651f48edb821edb01f26 100644 --- a/src/users/user.schema.ts +++ b/src/users/user.schema.ts @@ -25,6 +25,8 @@ export class User { @Prop({ default: null }) newEmail: string; + @Prop({ default: null }) + structuresLink: number[]; } export const UserSchema = SchemaFactory.createForClass(User);