Skip to content
21 changes: 21 additions & 0 deletions src/controllers/userProfileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
const yearMonthDayDateValidator = require('../utilities/yearMonthDayDateValidator');
const cacheClosure = require('../utilities/nodeCache');
const followUp = require('../models/followUp');
const task = require('../models/task');
const team = require('../models/team');
const HGNFormResponses = require('../models/hgnFormResponse');
const userService = require('../services/userService');
const { hasPermission, canRequestorUpdateUser } = require('../utilities/permissions');
Expand Down Expand Up @@ -1427,6 +1429,25 @@
await UserProfile.deleteOne({ _id: userId });
// delete followUp for deleted user
await followUp.findOneAndDelete({ userId });
// Validate and convert userId to ObjectId for proper matching in MongoDB
let userIdObject;
try {
userIdObject = new mongoose.Types.ObjectId(userId);
} catch (idError) {
// If conversion fails, userId might already be an ObjectId or invalid
logger.logInfo(`Invalid userId format for ObjectId conversion: ${userId}`);
userIdObject = userId;
}

Check warning on line 1440 in src/controllers/userProfileController.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Handle this exception or don't catch it at all.

See more on https://sonarcloud.io/project/issues?id=OneCommunityGlobal_HGNRest&issues=AZ1RZQ0qHkM_Q1xd4d4g&open=AZ1RZQ0qHkM_Q1xd4d4g&pullRequest=2149
// delete user from task-resources
await task.updateMany(
{ 'resources.userID': userIdObject },
{ $pull: { resources: { userID: userIdObject } } },
);
// delete user from teams-members
await team.updateMany(
{ 'members.userId': userIdObject },
{ $pull: { members: { userId: userIdObject } } },
);
res.status(200).send({ message: 'Executed Successfully' });
auditIfProtectedAccountUpdated({
requestorId: req.body.requestor.requestorId,
Expand Down
Loading