Skip to content
Snippets Groups Projects
Commit 362e81d2 authored by Hugo SUBTIL's avatar Hugo SUBTIL
Browse files

Merge branch 'feat/removeOtherDemands-claimStructure' into 'dev'

feat : remove others demand for same structure

See merge request web-et-numerique/pamn_plateforme-des-acteurs-de-la-mediation-numerique/pamn_server!17
parents b91fccba 795f16e6
Branches
Tags
3 merge requests!27Recette,!26Dev,!17feat : remove others demand for same structure
......@@ -326,21 +326,36 @@ export class UsersService {
structureName: string,
validate: boolean
): 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;
if (!users) {
if (!user) {
throw new HttpException('User not found', HttpStatus.NOT_FOUND);
}
if (users.pendingStructuresLink.includes(structureId)) {
users.pendingStructuresLink = users.pendingStructuresLink.filter((item) => item !== structureId);
if (user.pendingStructuresLink.includes(structureId)) {
user.pendingStructuresLink = user.pendingStructuresLink.filter((item) => item !== structureId);
// If it's a validation case, push structureId into validated user structures
if (validate) {
users.structuresLink.push(structureId);
user.structuresLink.push(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);
// Send a rejection email
this.sendStructureClaimApproval(user.email, structureName, false);
user.save();
});
}
}
this.sendStructureClaimApproval(userEmail, structureName, status);
await users.save();
await user.save();
return this.getPendingStructures();
} else {
throw new HttpException(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment