Skip to content

Commit

Permalink
Merge pull request #391 from DFE-Digital/feature/NSA-5730_deactivatio…
Browse files Browse the repository at this point in the history
…n_reason_mandatory

NSA-5730: Deactivation reason form is now mandatory
  • Loading branch information
rashthedude authored Nov 1, 2022
2 parents 511af39 + 3cf3723 commit 92bcac5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
17 changes: 15 additions & 2 deletions src/app/users/postConfirmDeactivate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const logger = require('./../../infrastructure/logger');
const { getUserDetails, getUserDetailsById, updateUserDetails, waitForIndexToUpdate } = require('./utils');
const { deactivate } = require('./../../infrastructure/directories');
const { sendResult } = require('./../../infrastructure/utils');


const updateUserIndex = async (uid, correlationId) => {
const user = await getUserDetailsById(uid, correlationId);
Expand All @@ -17,8 +19,18 @@ const updateUserIndex = async (uid, correlationId) => {
const postConfirmDeactivate = async (req, res) => {
const user = await getUserDetails(req);

await deactivate(user.id, req.id);
await updateUserIndex(user.id, req.id);
if (req.body.reason.match(/^\s*$/) !== null) {
sendResult(req, res, 'users/views/confirmDeactivate', {
csrfToken: req.csrfToken(),
backLink: 'services',
reason: '',
validationMessages: {
reason: 'Please give a reason for deactivation'
},
});
} else {
await deactivate(user.id, req.id);
await updateUserIndex(user.id, req.id);

logger.audit(`${req.user.email} (id: ${req.user.sub}) deactivated user ${user.email} (id: ${user.id})`, {
type: 'support',
Expand All @@ -37,6 +49,7 @@ const postConfirmDeactivate = async (req, res) => {
});

return res.redirect('services');
}
};

module.exports = postConfirmDeactivate;
52 changes: 32 additions & 20 deletions src/app/users/postConfirmInvitationDeactivate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const logger = require('./../../infrastructure/logger');
const { getUserDetails, getUserDetailsById, updateUserDetails, waitForIndexToUpdate } = require('./utils');
const { deactivateInvite } = require('./../../infrastructure/directories');
const { sendResult } = require('./../../infrastructure/utils');

const updateUserIndex = async (uid, correlationId) => {
const user = await getUserDetailsById(uid, correlationId);
Expand All @@ -15,26 +16,37 @@ const updateUserIndex = async (uid, correlationId) => {
const postConfirmDeactivate = async (req, res) => {
const user = await getUserDetails(req);

await deactivateInvite(user.id, req.body.reason, req.id);
await updateUserIndex(user.id, req.id);

logger.audit(`${req.user.email} (id: ${req.user.sub}) deactivated user invitation ${user.email} (id: ${user.id})`, {
type: 'support',
subType: 'user-edit',
userId: req.user.sub,
userEmail: req.user.email,
editedUser: user.id,
editedFields: [
{
name: 'status',
oldValue: user.status.id,
newValue: -2,
}
],
reason: req.body.reason,
});

return res.redirect('services');
if (req.body.reason.match(/^\s*$/) !== null) {
sendResult(req, res, 'users/views/confirmDeactivate', {
csrfToken: req.csrfToken(),
backLink: 'services',
reason: '',
validationMessages: {
reason: 'Please give a reason for deactivation'
},
});
} else {
await deactivateInvite(user.id, req.body.reason, req.id);
await updateUserIndex(user.id, req.id);

logger.audit(`${req.user.email} (id: ${req.user.sub}) deactivated user invitation ${user.email} (id: ${user.id})`, {
type: 'support',
subType: 'user-edit',
userId: req.user.sub,
userEmail: req.user.email,
editedUser: user.id,
editedFields: [
{
name: 'status',
oldValue: user.status.id,
newValue: -2,
}
],
reason: req.body.reason,
});

return res.redirect('services');
}
};

module.exports = postConfirmDeactivate;
2 changes: 1 addition & 1 deletion src/app/users/views/confirmDeactivate.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<fieldset>
<legend><p>Please confirm you would like to deactivate the user.</p></legend>
<div class="form-group <%= (locals.validationMessages.reason !== undefined) ? 'form-group-error' : '' %>">
<label class="form-label" for="reason">Reason for deactivation of user (optional)</span> </label>
<label class="form-label" for="reason">Reason for deactivation of user (mandatory)</span> </label>
<% if (locals.validationMessages.reason !== undefined) { %>
<p class="error-message" id="validation-reason"><%= locals.validationMessages.reason %></p>
<% } %>
Expand Down

0 comments on commit 92bcac5

Please sign in to comment.