cascade on superuser delete
This commit is contained in:
1 parent
1cc9f4e4d9
commit
40f4294cae
3 files changed
+25
-7
No files matched your search
@@ -423,13 +423,6 @@ class AuthController {
|
|||||||
await this.jobQueue.deleteJob(monitor);
|
await this.jobQueue.deleteJob(monitor);
|
||||||
})
|
})
|
||||||
));
|
));
|
||||||
|
|
||||||
// 3. Delete team
|
|
||||||
await this.db.deleteTeam(user.teamId);
|
|
||||||
// 4. Delete all other team members
|
|
||||||
await this.db.deleteAllOtherUsers();
|
|
||||||
// 5. Delete each monitor
|
|
||||||
await this.db.deleteMonitorsByUserId(user._id);
|
|
||||||
}
|
}
|
||||||
// 6. Delete the user by id
|
// 6. Delete the user by id
|
||||||
await this.db.deleteUser(user._id);
|
await this.db.deleteUser(user._id);
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
import bcrypt from "bcryptjs";
|
import bcrypt from "bcryptjs";
|
||||||
import logger from "../../utils/logger.js";
|
import logger from "../../utils/logger.js";
|
||||||
|
import Monitor from "./Monitor.js";
|
||||||
|
import Team from "./Team.js";
|
||||||
|
import Notification from "./Notification.js";
|
||||||
|
|
||||||
const UserSchema = mongoose.Schema(
|
const UserSchema = mongoose.Schema(
|
||||||
{
|
{
|
||||||
@@ -74,6 +77,24 @@ UserSchema.pre("findOneAndUpdate", function (next) {
|
|||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
UserSchema.pre("findOneAndDelete", async function (next) {
|
||||||
|
try {
|
||||||
|
const userToDelete = await this.model.findOne(this.getFilter());
|
||||||
|
if (userToDelete.role.includes("superadmin")) {
|
||||||
|
await Team.deleteOne({ _id: userToDelete.teamId });
|
||||||
|
await Monitor.deleteMany({ userId: userToDelete._id });
|
||||||
|
await this.model.deleteMany({
|
||||||
|
teamId: userToDelete.teamId,
|
||||||
|
_id: { $ne: userToDelete._id },
|
||||||
|
});
|
||||||
|
await Notification.deleteMany({ teamId: userToDelete.teamId });
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
UserSchema.methods.comparePassword = async function (submittedPassword) {
|
UserSchema.methods.comparePassword = async function (submittedPassword) {
|
||||||
const res = await bcrypt.compare(submittedPassword, this.password);
|
const res = await bcrypt.compare(submittedPassword, this.password);
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
@@ -444,6 +444,10 @@ class StringService {
|
|||||||
get verifyOwnerUnauthorized() {
|
get verifyOwnerUnauthorized() {
|
||||||
return this.translationService.getTranslation("verifyOwnerUnauthorized");
|
return this.translationService.getTranslation("verifyOwnerUnauthorized");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get dbUserNotFound() {
|
||||||
|
return this.translationService.getTranslation("dbUserNotFound");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default StringService;
|
export default StringService;
|
||||||
Reference in new issue
Block a user