From 074663fcdf9a7837b3ce622656df4b0ecbb19adb Mon Sep 17 00:00:00 2001 From: Jeremie BRISON <ext.sopra.jbrison@grandlyon.com> Date: Tue, 19 Jan 2021 15:49:26 +0100 Subject: [PATCH] fix (structureClaim) : fix the claim of a structure multiple times with a user --- src/structures/services/structures.service.ts | 6 ++++-- src/structures/structures.controller.ts | 6 +++--- src/users/users.service.ts | 8 ++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts index d8c077ce4..dde0166a7 100644 --- a/src/structures/services/structures.service.ts +++ b/src/structures/services/structures.service.ts @@ -7,6 +7,7 @@ import { Structure, StructureDocument } from '../schemas/structure.schema'; import { Logger } from '@nestjs/common'; import { structureDto } from '../dto/structure.dto'; import { UsersService } from '../../users/users.service'; +import { User } from '../../users/schemas/user.schema'; @Injectable() export class StructuresService { @@ -117,9 +118,10 @@ export class StructuresService { }); } - public async isClaimed(structureId: string): Promise<boolean> { + public async isClaimed(structureId: string, user: User): Promise<boolean> { const isStructureClaimed = await this.userService.isStructureClaimed(structureId.toString()); - if (isStructureClaimed) { + const isUserAlreadyClaimed = await this.userService.isUserAlreadyClaimedStructure(structureId, user.email); + if (isStructureClaimed || isUserAlreadyClaimed) { return true; } return false; diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts index 94a6dca77..b30dbd828 100644 --- a/src/structures/structures.controller.ts +++ b/src/structures/structures.controller.ts @@ -36,9 +36,9 @@ export class StructuresController { return this.structureService.findAll(); } - @Get(':id/isClaimed') - public async isClaimed(@Param('id') id: string): Promise<boolean> { - return this.structureService.isClaimed(id); + @Post(':id/isClaimed') + public async isClaimed(@Param('id') id: string, @Body() user?: User): Promise<boolean> { + return this.structureService.isClaimed(id, user); } @Post(':id/claim') diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 8acbeb4f2..ecdb28641 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -288,6 +288,14 @@ export class UsersService { return this.userModel.findOne({ structuresLink: 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 false; + } + public async updateStructureLinked(userEmail: string, idStructure: string): Promise<string[]> { const user = await this.findOne(userEmail, true); if (user) { -- GitLab