Skip to content

Commit

Permalink
fix: ORV2-2038 Delete Active user corrections (#1291)
Browse files Browse the repository at this point in the history
  • Loading branch information
krishnan-aot authored Mar 27, 2024
1 parent 5af9131 commit 660bff7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 47 deletions.
22 changes: 11 additions & 11 deletions vehicles/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 45 additions & 36 deletions vehicles/src/modules/company-user-management/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,15 +709,15 @@ export class UsersService {
* at least one company administrator remains. It performs checks before deletion, updates
* user statuses, and returns details on successful and failed deletions.
*
* @param {string[]} userGUIDs The GUIDs of the users slated for deletion.
* @param {string[]} userGUIDsFromRequest The GUIDs of the users slated for deletion.
* @param {number} companyId The ID of the company the users belong to.
* @param {IUserJWT} currentUser JWT token details of the current user for auditing.
* @returns {Promise<DeleteDto>} An object containing arrays of successfully deleted user GUIDs
* and those that failed to delete.
*/
@LogAsyncMethodExecution()
async removeAll(
userGUIDs: string[],
userGUIDsFromRequest: string[],
companyId: number,
currentUser: IUserJWT,
): Promise<DeleteDto> {
Expand All @@ -733,59 +733,68 @@ export class UsersService {
},
});

// Mapping users to check against COMPANY_ADMINISTRATOR group before operations
const companyAdminUserGuids = usersBeforeDelete.map(
(companyUser) =>
companyUser.userAuthGroup ===
ClientUserAuthGroup.COMPANY_ADMINISTRATOR &&
companyUser?.user?.userGUID,
);

// Filter admin GUIDs to exclude those in the deletion list, ensuring at least one remains
const filteredCompanyAdminUserGuids = companyAdminUserGuids.filter(
(guid) => !userGUIDs.includes(guid),
);

// Check if operation results in zero company admins, throwing an error if true
if (!filteredCompanyAdminUserGuids?.length) {
// Collect the company admin userGUIDs.
const remainingCompanyAdmins = usersBeforeDelete
.filter(
({ userAuthGroup, user }) =>
userAuthGroup === ClientUserAuthGroup.COMPANY_ADMINISTRATOR &&
Boolean(user?.userGUID),
)
.map(({ user: { userGUID } }) => userGUID)
.filter((guid) => !userGUIDsFromRequest.includes(guid));

// Throw a bad request if there are no remaining admins.
if (!remainingCompanyAdmins.length) {
throw new BadRequestException(
'This operation is not allowed as a company should have atlease one CVAdmin at any given moment.',
);
}
// Extract only the GUIDs of the users to be deleted and modify them.
const userToBeDeleted = usersBeforeDelete.map((companyUser) => {
return (
userGUIDs.includes(companyUser.user.userGUID) &&
({
// Modify user status and other housekeeping fields for deletion.
const usersToBeDeleted = usersBeforeDelete
.filter(({ user: { userGUID } }) =>
userGUIDsFromRequest.includes(userGUID),
)
.map((companyUser) => {
return {
...companyUser,
statusCode: UserStatus.DELETED,
updatedDateTime: new Date(),
updatedUser: currentUser.userName,
updatedUserDirectory: currentUser.orbcUserDirectory,
updatedUserGuid: currentUser.userGUID,
} as CompanyUser)
);
});
} as CompanyUser;
});

// Identify which GUIDs were not found (failure to delete)
const failure = userGUIDs?.filter(
const failure = userGUIDsFromRequest?.filter(
(id) =>
!userToBeDeleted.some(
(companyUser) => companyUser.user.userGUID === id,
),
!usersToBeDeleted.some(({ user: { userGUID } }) => userGUID === id),
);

// Execute the deletion of users by their GUIDs within the specified company
await this.companyUserRepository.save(userToBeDeleted);
for (const userToDelete of usersToBeDeleted) {
const {
user: { userGUID },
} = userToDelete;
try {
// Execute the deletion of users by their GUIDs within the specified company
const { statusCode } =
await this.companyUserRepository.save(userToDelete);
if (statusCode !== UserStatus.DELETED) {
failure.push(userGUID);
}
} catch (error) {
this.logger.error(error);
failure.push(userGUID);
}
}

// Determine successful deletions by filtering out failures
const success = userGUIDs?.filter((id) => !failure?.includes(id));
const success = userGUIDsFromRequest?.filter((id) => !failure.includes(id));

// Prepare the response DTO with lists of successful and failed deletions
const deleteDto: DeleteDto = {
success: success,
failure: failure,
return {
success,
failure,
};
return deleteDto;
}
}

0 comments on commit 660bff7

Please sign in to comment.