diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts index d8c077ce4b8174d58c72ee4fcc4e0f5aa3899989..dde0166a7d25b2c8a654b68cecb487a870bea569 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 94a6dca7760be4679d2376414728abdc21033e80..b30dbd828577bb018cf359c7d5c183b2ceebefb9 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 8acbeb4f25429981843ff49870278f61ee63ff4a..ecdb286411dae454f08e01be33bcff6dd0a78c55 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) {