diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts index 6fc689024f17f42a152a3697b92f1f8808b4567b..ee574e2c203cd7a446c63a55870f8071c72a9862 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 748493ee0caea09ca488b3bab7448ef30dc97b90..b98796cdf193be380e04de4826c6454f6a4477e7 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 312a312561258df14bbb3930506f8070f94feba1..683a069b82d162e1116318e825af966b4b89ca28 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 deadfe5a0ca1b1bb0a616a4fe5fed566e8ca3d2b..a540db0d6ff77fe751d942e9b4980951d29e0ab2 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 28a7bac340fc9596a66066ec49bcf5107deeaba2..f683ebdca2a3a881a199c5b85964daf50557db6f 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();