Skip to content
Snippets Groups Projects
Commit 795f16e6 authored by Jérémie BRISON's avatar Jérémie BRISON
Browse files

feat : remove others demand for same structure

parent 3ad4b8cc
No related branches found
No related tags found
3 merge requests!27Recette,!26Dev,!17feat : remove others demand for same structure
...@@ -326,21 +326,36 @@ export class UsersService { ...@@ -326,21 +326,36 @@ export class UsersService {
structureName: string, structureName: string,
validate: boolean validate: boolean
): Promise<PendingStructureDto[]> { ): Promise<PendingStructureDto[]> {
const users = await this.findOne(userEmail); 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 } })
.exec();
let status = false; let status = false;
if (!users) { if (!user) {
throw new HttpException('User not found', HttpStatus.NOT_FOUND); throw new HttpException('User not found', HttpStatus.NOT_FOUND);
} }
if (users.pendingStructuresLink.includes(structureId)) { if (user.pendingStructuresLink.includes(structureId)) {
users.pendingStructuresLink = users.pendingStructuresLink.filter((item) => item !== structureId); user.pendingStructuresLink = user.pendingStructuresLink.filter((item) => item !== structureId);
// If it's a validation case, push structureId into validated user structures // If it's a validation case, push structureId into validated user structures
if (validate) { if (validate) {
users.structuresLink.push(structureId); user.structuresLink.push(structureId);
// Send validation email // Send validation email
status = true; 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);
// Send a rejection email
this.sendStructureClaimApproval(user.email, structureName, false);
user.save();
});
}
} }
this.sendStructureClaimApproval(userEmail, structureName, status); this.sendStructureClaimApproval(userEmail, structureName, status);
await users.save(); await user.save();
return this.getPendingStructures(); return this.getPendingStructures();
} else { } else {
throw new HttpException( throw new HttpException(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment