Skip to content

Commit

Permalink
Use common invite service removal code
Browse files Browse the repository at this point in the history
  • Loading branch information
insacuri committed Dec 11, 2024
1 parent 2d67261 commit 13075ac
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
15 changes: 3 additions & 12 deletions src/app/users/postConfirmInvitationDeactivate.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-disable no-restricted-syntax */
const logger = require('../../infrastructure/logger');
const {
getUserDetails, getUserDetailsById, updateUserDetails, waitForIndexToUpdate,
getUserDetails, getUserDetailsById, updateUserDetails, waitForIndexToUpdate, removeAllServicesForInvitedUser,
} = require('./utils');
const { deactivateInvite } = require('../../infrastructure/directories');
const { getServicesByInvitationId, removeServiceFromInvitation } = require('../../infrastructure/access');
const { sendResult } = require('../../infrastructure/utils');

const updateUserIndex = async (uid, correlationId) => {
Expand All @@ -19,8 +18,7 @@ const updateUserIndex = async (uid, correlationId) => {

const postConfirmDeactivate = async (req, res) => {
const user = await getUserDetails(req);
const correlationId = req.id;


if (req.body['select-reason'] && req.body['select-reason'] !== 'Select a reason' && req.body.reason.trim() === '') {
req.body.reason = req.body['select-reason'];
} else if (req.body['select-reason'] && req.body['select-reason'] !== 'Select a reason' && req.body.reason.length > 0) {
Expand All @@ -40,14 +38,7 @@ const postConfirmDeactivate = async (req, res) => {
await deactivateInvite(user.id, req.body.reason, req.id);
await updateUserIndex(user.id, req.id);
if (req.body['remove-services-from-invite']) {
logger.info(`Attemping to remove services from invite with id: ${req.params.uid}`, { correlationId });
// No need to get the invitation to double check as getUserDetails does that already, if we're here then the invite definitely exists.
// getUserDetails parrots back the 'inv-<uuid>' as its id instead of giving us the true one without the 'inv-' prefix.
const invitationServiceRecords = await getServicesByInvitationId(user.id.substr(4)) || [];
for (const serviceRecord of invitationServiceRecords) {
logger.info(`Deleting invitation service record for invitationId: ${serviceRecord.invitationId}, serviceId: ${serviceRecord.serviceId} and organisationId: ${serviceRecord.organisationIdId}`, { correlationId });
removeServiceFromInvitation(serviceRecord.invitationId, serviceRecord.serviceId, serviceRecord.organisationId, req.id);
}
await removeAllServicesForInvitedUser(user.id, req);
}

logger.audit(`${req.user.email} (id: ${req.user.sub}) deactivated user invitation ${user.email} (id: ${user.id})`, {
Expand Down
2 changes: 1 addition & 1 deletion src/app/users/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ const removeAllServicesForInvitedUser = async (userId, req) => {
const invitationServiceRecords = await getServicesByInvitationId(userId.substr(4)) || [];
for (const serviceRecord of invitationServiceRecords) {
logger.info(`Deleting invitation service record for invitationId: ${serviceRecord.invitationId}, serviceId: ${serviceRecord.serviceId} and organisationId: ${serviceRecord.organisationIdId}`, { correlationId });
removeServiceFromInvitation(serviceRecord.invitationId, serviceRecord.serviceId, serviceRecord.organisationId, req.id);
removeServiceFromInvitation(serviceRecord.invitationId, serviceRecord.serviceId, serviceRecord.organisationId, correlationId);
}
}

Expand Down

0 comments on commit 13075ac

Please sign in to comment.