From 638d5cacde266f708e1517d277e59c56ebcada65 Mon Sep 17 00:00:00 2001
From: Jeremie BRISON <ext.sopra.jbrison@grandlyon.com>
Date: Wed, 23 Dec 2020 11:38:21 +0100
Subject: [PATCH] feat(createStructure) : link structure to a profil

---
 src/structures/dto/create-structure.dto.ts | 68 ++-------------------
 src/structures/dto/structure.dto.ts        | 70 ++++++++++++++++++++++
 src/structures/structures.controller.ts    |  5 +-
 src/structures/structures.module.ts        |  8 ++-
 src/structures/structures.service.ts       | 16 +++--
 src/users/user.interface.ts                |  1 +
 src/users/user.schema.ts                   |  2 +
 7 files changed, 100 insertions(+), 70 deletions(-)
 create mode 100644 src/structures/dto/structure.dto.ts

diff --git a/src/structures/dto/create-structure.dto.ts b/src/structures/dto/create-structure.dto.ts
index 147b677fa..ee9cc2256 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 000000000..7af244c86
--- /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/structures.controller.ts b/src/structures/structures.controller.ts
index be360f7ad..5178d6376 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';
 
@@ -10,7 +11,7 @@ export class StructuresController {
 
   @Post()
   public async create(@Body() createStructureDto: CreateStructureDto): Promise<Structure> {
-    return this.structureService.create(createStructureDto);
+    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 6a82aae92..bae6365d3 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 3eee743ef..3de8c1002 100644
--- a/src/structures/structures.service.ts
+++ b/src/structures/structures.service.ts
@@ -3,21 +3,29 @@ 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> {
-    let createdStructure = new this.structureModel(createStructrureDto);
+  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;
   }
 
@@ -78,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);
diff --git a/src/users/user.interface.ts b/src/users/user.interface.ts
index 93aec2489..d235abc51 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 b429332fe..0c04dba82 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);
-- 
GitLab