Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [ASL-4604] Add role changes - copy code to new named person dir #1689

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pages/role/content/named-roles.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//TODO: Remove this file once named person mvp flag is removed

const dictionary = require('@ukhomeoffice/asl-dictionary');

const roles = [
Expand Down
13 changes: 13 additions & 0 deletions pages/role/named-person-mvp/apply/content/confirm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { merge } = require('lodash');
const baseContent = require('../../../profile/content');

module.exports = merge({}, baseContent, {
reviewRoleApplication: 'Add role',
applyingFor: 'You are assigning:',
onBehalfOf: 'To:',
rcvsNumber: 'RCVS number:',
explanation: 'Why is this person suitable for this role?',
buttons: {
submit: 'Submit'
}
});
30 changes: 30 additions & 0 deletions pages/role/named-person-mvp/apply/content/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { merge } = require('lodash');
const baseContent = require('../../../../profile/content');

module.exports = merge({}, baseContent, {
title: 'Add role',
openTasks: {
title: 'Role assignments in progress',
description: {
single: 'There is already a request to add the following role:',
multiple: 'There are already requests to add the following roles:'
},
link: 'View task'
},
fields: {
type: {
label: 'Which role do you want to add?'
},
rcvsNumber: {
label: 'RCVS membership number'
}
},
buttons: {
submit: 'Continue'
},
errors: {
type: {
required: 'Please select a role'
}
}
});
124 changes: 124 additions & 0 deletions pages/role/named-person-mvp/apply/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
const { get, omit, merge } = require('lodash');
const { page } = require('@asl/service/ui');
const { form } = require('../../../common/routers');
const { clearSessionIfNotFromTask, populateNamedPeople } = require('../../../common/middleware');
const getSchema = require('./schema');
const confirm = require('../../routers/confirm');
const success = require('../../routers/success');
const { profileReplaced, PELH_OR_NPRC_ROLES } = require('../../helper');

const sendData = (req, params = {}) => {
const { type, rcvsNumber, comment } = req.session.form[req.model.id].values;

const replaceProfile = profileReplaced(req.establishment, type);
const opts = {
method: 'POST',
json: merge({
data: { type, rcvsNumber, profileId: req.profileId, replaceProfile, replaceRoles: PELH_OR_NPRC_ROLES },
meta: { comment }
}, params)
};

return req.api(`/establishment/${req.establishmentId}/role`, opts);
};

module.exports = settings => {
const app = page({
root: __dirname,
...settings,
paths: ['/confirm', '/success']
});

app.use((req, res, next) => {
req.model = {
id: 'new-role'
};
next();
});

app.get('/', clearSessionIfNotFromTask());

app.use('/', (req, res, next) => {
const query = {
model: 'establishment',
modelId: req.establishmentId,
onlyOpen: true,
action: 'replace'
};
req.api('/tasks/related', { query })
.then((response) => {
return response.json.data || [];
})
.then(data => {
res.locals.static.pelhOrNprcTasks = data
.filter(task => PELH_OR_NPRC_ROLES.includes(task.data.data.type))
.map(task => ({
id: task.id,
type: task.data.data.type
}));
next();
})
.catch(next);
});

app.use('/', form({
configure: (req, res, next) => {
const rolesHeld = req.profile.roles
.filter(role => role.establishmentId === req.establishmentId)
.map(role => role.type);

const addRoleTasks = req.profile.openTasks
.filter(task => task.data.model === 'role' && (task.data.action === 'create'))
.map(task => ({
id: task.id,
type: task.data.data.type
}));

const pelOrNprcTasks = res.locals.static.pelhOrNprcTasks;
const rolesRequested = addRoleTasks.map(task => task.type).concat(pelOrNprcTasks.length > 0 ? PELH_OR_NPRC_ROLES : []);

req.form.schema = {
...getSchema(rolesHeld.concat(rolesRequested), req.establishment),
rcvsNumber: {}
};

res.locals.static.addRoleTasks = addRoleTasks.concat(pelOrNprcTasks);
req.model.openTasks = []; // hide the open tasks warning on role forms as it is not applicable

next();
},
getValues: (req, res, next) => {
req.form.values.rcvsNumber = req.form.values.rcvsNumber || req.profile.rcvsNumber;
next();
},
locals: (req, res, next) => {
res.locals.static.schema = omit(req.form.schema, 'rcvsNumber');
res.locals.static.ownProfile = req.user.profile.id === req.profileId;
res.locals.pageTitle = `${res.locals.static.content.title} - ${req.establishment.name}`;
next();
}
}));

app.post('/', (req, res, next) => {
return res.redirect(req.buildRoute('role.create', { suffix: 'confirm' }));
});

app.use('/confirm', populateNamedPeople, confirm({
action: 'create',
sendData
}));

app.post('/confirm', populateNamedPeople, (req, res, next) => {
sendData(req)
.then(response => {
req.session.success = { taskId: get(response, 'json.data.id') };
delete req.session.form[req.model.id];
return res.redirect(req.buildRoute('role.create', { suffix: 'success' }));
})
.catch(next);
});

app.use('/success', success());

return app;
};
39 changes: 39 additions & 0 deletions pages/role/named-person-mvp/apply/schema/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const namedRoles = require('../../helpers/named-roles');
const hintText = require('../../../content/hint-text');

const excludedRoles = {
corporate: ['pelh'],
'non-profit': ['nprc']
};

module.exports = (roles, establishment) => {
const excludeRoles = (establishment.corporateStatus && excludedRoles[establishment.corporateStatus]) || [];
roles = Object.keys(namedRoles)
.filter(r => !roles.includes(r))
.filter(r => !excludeRoles.includes(r));

const options = roles.map(role => {
return {
value: role,
label: namedRoles[role],
hint: hintText[role],
reveal: role === 'nvs'
? { rcvsNumber: { inputType: 'inputText' } }
: null
};
});

return {
type: {
inputType: 'radioGroup',
validate: [
'required',
{
definedValues: roles
}
],
options,
nullValue: []
}
};
};
4 changes: 4 additions & 0 deletions pages/role/named-person-mvp/apply/views/confirm.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import React from 'react';
import Confirm from '../../../views/confirm';

export default props => <Confirm action="apply" {...props} />;
65 changes: 65 additions & 0 deletions pages/role/named-person-mvp/apply/views/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React, { Fragment } from 'react';
import { connect } from 'react-redux';
import { Link, Snippet, FormLayout, Fieldset, Inset, Header } from '@ukhomeoffice/asl-components';
import OpenTasks from '../../../component/open-tasks';

const connectComponent = key => {
const mapStateToProps = ({ model, static: { schema } }) => {
schema = schema.type.options.find(role => role.value === key).reveal;

return {
model,
schema
};
};

return connect(mapStateToProps)(Fieldset);
};

const formatters = {
type: {
mapOptions: option => {
const ConnectedComponent = connectComponent(option.value);
return {
...option,
reveal: option.reveal ? <Inset><ConnectedComponent /></Inset> : null
};
}
}
};

const Page = ({ addRoleTasks, schema, profile }) => {
if (schema.type.options.length === 0) {
return (
<Fragment>
<Header
title={<Snippet>title</Snippet>}
subtitle={`${profile.firstName} ${profile.lastName}`}
/>
<OpenTasks roleTasks={addRoleTasks} />
<p>
<Link page="profile.read" label={<Snippet>buttons.cancel</Snippet>} className="govuk-button" />
</p>
</Fragment>
);
}

const CancelLink = () => {
return <Link page="profile.read" label={<Snippet>buttons.cancel</Snippet>} />;
};

return (
<Fragment>
<FormLayout formatters={formatters} cancelLink={<CancelLink />}>
<Header
title={<Snippet>title</Snippet>}
subtitle={`${profile.firstName} ${profile.lastName}`}
/>
<OpenTasks roleTasks={addRoleTasks} />
</FormLayout>
</Fragment>
);
};

const mapStateToProps = ({ static: { addRoleTasks, schema, profile } }) => ({ addRoleTasks, schema, profile });
export default connect(mapStateToProps)(Page);
3 changes: 3 additions & 0 deletions pages/role/named-person-mvp/apply/views/success.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Success from '../../../../success/views';

export default Success;
2 changes: 1 addition & 1 deletion pages/role/named-person-mvp/before-you-apply/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = settings => {
app.post('/', (req, res, next) => {
const { type } = req.form.values;
if (type) {
return res.redirect(req.buildRoute('role.create'));
return res.redirect(req.buildRoute('role.namedPersonMvp.create'));
} else {
return res.redirect(req.buildRoute('training.dashboard'));
}
Expand Down
18 changes: 18 additions & 0 deletions pages/role/named-person-mvp/helpers/named-roles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const dictionary = require('@ukhomeoffice/asl-dictionary');

const roles = [
'nacwo',
'nio',
'ntco',
'nvs',
'sqp',
'nprc',
'holc'
];

module.exports = roles.reduce((map, role) => {
return {
...map,
[role]: `${dictionary[role.toUpperCase()]} (${role.toUpperCase()})`
};
}, {});
6 changes: 6 additions & 0 deletions pages/role/named-person-mvp/routes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const beforeYouApply = require('./before-you-apply');
const create = require('./apply');

module.exports = {
beforeYouApply: {
path: '/before-you-apply',
router: beforeYouApply,
breadcrumb: false
},
create: {
path: '/create',
router: create,
breadcrumb: false
}
};