From 35fa953761b6800426155d27595efaa26df0f47f Mon Sep 17 00:00:00 2001 From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com> Date: Thu, 4 Feb 2021 16:57:20 +0100 Subject: [PATCH] fix: user model structureid from string to objectid --- src/structures/services/structures.service.ts | 4 +-- src/structures/structures.controller.ts | 3 +- src/users/interfaces/user.interface.ts | 4 +-- src/users/schemas/user.schema.ts | 4 +-- src/users/users.service.ts | 33 +++++++++++-------- 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts index 6fc689024..ee574e2c2 100644 --- a/src/structures/services/structures.service.ts +++ b/src/structures/services/structures.service.ts @@ -136,7 +136,7 @@ export class StructuresService { } public async isClaimed(structureId: string, user: User): Promise<boolean> { - const isStructureClaimed = await this.userService.isStructureClaimed(structureId.toString()); + const isStructureClaimed = await this.userService.isStructureClaimed(structureId); const isUserAlreadyClaimed = await this.userService.isUserAlreadyClaimedStructure(structureId, user.email); if (isStructureClaimed || isUserAlreadyClaimed) { return true; @@ -300,7 +300,7 @@ export class StructuresService { public async updateAfterOwnerVerify(emailUser: string): Promise<Structure> { const user = await this.userService.findOne(emailUser); - const structureLinked = await this.findOne(user.structuresLink[0]); + const structureLinked = await this.findOne(user.structuresLink[0].toHexString()); const structure = new this.structureModel(structureLinked); if (!structure) { throw new HttpException('Invalid structure', HttpStatus.NOT_FOUND); diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts index 748493ee0..b98796cdf 100644 --- a/src/structures/structures.controller.ts +++ b/src/structures/structures.controller.ts @@ -1,5 +1,6 @@ import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put, Query, UseGuards } from '@nestjs/common'; import { ApiParam } from '@nestjs/swagger'; +import { Types } from 'mongoose'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { Roles } from '../users/decorators/roles.decorator'; import { IsStructureOwnerGuard } from '../users/guards/isStructureOwner.guard'; @@ -49,7 +50,7 @@ export class StructuresController { } @Post(':id/claim') - public async claim(@Param('id') idStructure: string, @Body() user: User): Promise<string[]> { + public async claim(@Param('id') idStructure: string, @Body() user: User): Promise<Types.ObjectId[]> { return this.userService.updateStructureLinked(user.email, idStructure); } diff --git a/src/users/interfaces/user.interface.ts b/src/users/interfaces/user.interface.ts index 312a31256..683a069b8 100644 --- a/src/users/interfaces/user.interface.ts +++ b/src/users/interfaces/user.interface.ts @@ -13,7 +13,7 @@ export interface IUser extends Document { role: number; changeEmailToken: string; newEmail: string; - structuresLink: string[]; - pendingStructuresLink: string[]; + structuresLink: Types.ObjectId[]; + pendingStructuresLink: Types.ObjectId[]; structureOutdatedMailSent: Types.ObjectId[]; } diff --git a/src/users/schemas/user.schema.ts b/src/users/schemas/user.schema.ts index deadfe5a0..a540db0d6 100644 --- a/src/users/schemas/user.schema.ts +++ b/src/users/schemas/user.schema.ts @@ -37,10 +37,10 @@ export class User { newEmail: string; @Prop({ default: null }) - structuresLink: string[]; + structuresLink: Types.ObjectId[]; @Prop({ default: null }) - pendingStructuresLink: string[]; + pendingStructuresLink: Types.ObjectId[]; @Prop({ default: null }) structureOutdatedMailSent: Types.ObjectId[]; diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 28a7bac34..f683ebdca 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -302,25 +302,28 @@ export class UsersService { return this.userModel.find({ role: 1 }).exec(); } - public async isStructureClaimed(structureId: string): Promise<User> { - return this.userModel.findOne({ structuresLink: structureId }).exec(); + public async isStructureClaimed(structureId: string): Promise<IUser> { + return this.userModel.findOne({ structuresLink: Types.ObjectId(structureId) }).exec(); } public async isUserAlreadyClaimedStructure(structureId: string, userEmail: string): Promise<boolean> { const user = await this.findOne(userEmail, true); if (user) { - return user.pendingStructuresLink.includes(structureId); + return user.pendingStructuresLink.includes(Types.ObjectId(structureId)); } return false; } - public async updateStructureLinked(userEmail: string, idStructure: string): Promise<string[]> { + public async updateStructureLinked(userEmail: string, idStructure: string): Promise<Types.ObjectId[]> { const user = await this.findOne(userEmail, true); if (user) { - user.pendingStructuresLink.push(idStructure); - user.save(); - this.sendAdminStructureValidationMail(); - return user.pendingStructuresLink; + if (!user.pendingStructuresLink.includes(Types.ObjectId(idStructure))) { + user.pendingStructuresLink.push(Types.ObjectId(idStructure)); + user.save(); + this.sendAdminStructureValidationMail(); + return user.pendingStructuresLink; + } + throw new HttpException('User already claimed this structure', HttpStatus.NOT_FOUND); } throw new HttpException('Invalid user', HttpStatus.NOT_FOUND); } @@ -355,25 +358,29 @@ export class UsersService { const user = await this.findOne(userEmail); // Get other users who have made the demand on the same structure const otherUsers = await this.userModel - .find({ pendingStructuresLink: structureId, email: { $ne: userEmail } }) + .find({ pendingStructuresLink: Types.ObjectId(structureId), email: { $ne: userEmail } }) .exec(); let status = false; if (!user) { throw new HttpException('User not found', HttpStatus.NOT_FOUND); } - if (user.pendingStructuresLink.includes(structureId)) { - user.pendingStructuresLink = user.pendingStructuresLink.filter((item) => item !== structureId); + if (user.pendingStructuresLink.includes(Types.ObjectId(structureId))) { + user.pendingStructuresLink = user.pendingStructuresLink.filter((item) => { + return !Types.ObjectId(structureId).equals(item); + }); // If it's a validation case, push structureId into validated user structures if (validate) { - user.structuresLink.push(structureId); + user.structuresLink.push(Types.ObjectId(structureId)); // Send validation email status = true; // For other users who have made the demand on the same structure if (otherUsers) { otherUsers.forEach((user) => { // Remove the structure id from their demand - user.pendingStructuresLink = user.pendingStructuresLink.filter((item) => item !== structureId); + user.pendingStructuresLink = user.pendingStructuresLink.filter((item) => { + return !Types.ObjectId(structureId).equals(item); + }); // Send a rejection email this.sendStructureClaimApproval(user.email, structureName, false); user.save(); -- GitLab