Skip to content

Commit

Permalink
Merge pull request #4414 from NMDSdevopsServiceAdm/test
Browse files Browse the repository at this point in the history
Test > Live: registration bug fixes, admin inactive emails, CQC main service change, parent requests main page
  • Loading branch information
joannafawl authored Dec 20, 2021
2 parents 0227c07 + bb513a8 commit 8051f16
Show file tree
Hide file tree
Showing 53 changed files with 2,515 additions and 323 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const table = {
tableName: 'Approvals',
schema: 'cqc',
};
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.sequelize.transaction(async (transaction) => {
await Promise.all([
queryInterface.addColumn(
table,
'Reviewer',
{
type: Sequelize.DataTypes.TEXT,
allowNull: true,
},
{ transaction },
),
queryInterface.addColumn(
table,
'InReview',
{
type: Sequelize.DataTypes.BOOLEAN,
defaultValue: false,
},
{ transaction },
),
]);
});
},

down: async (queryInterface) => {
await queryInterface.seqeulize.transaction(async (transaction) => {
await Promise.all([
queryInterface.removeColumn(table, 'Reviewer', { transaction }),
queryInterface.removeColumn(table, 'InReview', { transaction }),
]);
});
},
};
15 changes: 15 additions & 0 deletions migrations/20211214141318-alterApprovalsStatusEnum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.sequelize.query(
`ALTER TYPE cqc."enum_Approvals_Status" ADD VALUE IF NOT EXISTS 'In progress'`,
);
},

down: (queryInterface, Sequelize) => {
const query = `DELETE FROM pg_enum WHERE enumlabel = cqc."enum_Approvals_Status"
AND enumtypid = (SELECT oid FROM pg_type WHERE typname = cqc."enum_Approvals_Status)`;
return queryInterface.sequelize.query(query);
},
};
9 changes: 9 additions & 0 deletions migrations/20211214152944-archivingRejectedWorkplaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = {
up: async (queryInterface) => {
return queryInterface.sequelize.query(
'UPDATE cqc."Establishment" SET "Archived" = true WHERE "Status" = \'REJECTED\';',
);
},
};
47 changes: 41 additions & 6 deletions server/models/approvals.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
'use strict';

const { Op } = require('sequelize');

module.exports = (sequelize, DataTypes) => {
const Approvals = sequelize.define(
'Approvals',
Expand Down Expand Up @@ -41,6 +44,14 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.JSON,
allowNull: true,
},
Reviewer: {
type: DataTypes.TEXT,
allowNull: true,
},
InReview: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
createdAt: DataTypes.DATE,
updatedAt: DataTypes.DATE,
},
Expand All @@ -64,11 +75,11 @@ module.exports = (sequelize, DataTypes) => {
});
};

Approvals.findAllPending = function (approvalType) {
Approvals.findAllPendingAndInProgress = function (approvalType) {
return this.findAll({
where: {
ApprovalType: approvalType,
Status: 'Pending',
Status: { [Op.or]: ['Pending', 'In progress'] },
},
attributes: ['ID', 'UUID', 'EstablishmentID', 'UserID', 'createdAt', 'Status', 'Data'],
include: [
Expand Down Expand Up @@ -129,23 +140,47 @@ module.exports = (sequelize, DataTypes) => {
});
};
Approvals.findbyEstablishmentId = function (establishmentId, approvalType, status) {
const params = status ? status : { [Op.or]: ['Pending', 'In progress'] };

return this.findOne({
where: {
EstablishmentID: establishmentId,
ApprovalType: approvalType,
Status: status,
Status: params,
},
attributes: ['ID', 'UUID', 'EstablishmentID', 'UserID', 'createdAt', 'Status', 'Data'],
attributes: [
'ID',
'UUID',
'EstablishmentID',
'UserID',
'createdAt',
'Status',
'Data',
'Reviewer',
'InReview',
'createdAt',
],
include: [
{
model: sequelize.models.establishment,
as: 'Establishment',
attributes: ['nmdsId', 'NameValue'],
attributes: [
'id',
'uid',
'nmdsId',
'NameValue',
'address1',
'address2',
'address3',
'postcode',
'town',
'county',
],
},
{
model: sequelize.models.user,
as: 'User',
attributes: ['FullNameValue'],
attributes: ['FullNameValue', 'RegistrationID'],
},
],
});
Expand Down
10 changes: 10 additions & 0 deletions server/models/classes/establishment.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Establishment extends EntityValidator {
this._lastBulkUploaded = null;
this._eightWeeksFromFirstLogin = null;
this._showSharingPermissionsBanner = null;
this._expiresSoonAlertDate = null;

// interim reasons for leaving - https://trello.com/c/vNHbfdms
this._reasonsForLeaving = null;
Expand Down Expand Up @@ -319,6 +320,10 @@ class Establishment extends EntityValidator {
return this._status;
}

get expiresSoonAlertDate() {
return this._expiresSoonAlertDate;
}

get key() {
return (
this._properties.get('LocalIdentifier') && this._properties.get('LocalIdentifier').property
Expand Down Expand Up @@ -514,6 +519,10 @@ class Establishment extends EntityValidator {
if ('showSharingPermissionsBanner' in document) {
this._showSharingPermissionsBanner = document.showSharingPermissionsBanner;
}

if (document.expiresSoonAlertDate) {
this._expiresSoonAlertDate = document.expiresSoonAlertDate;
}
}

// allow for deep restoration of entities (associations - namely Worker here)
Expand Down Expand Up @@ -763,6 +772,7 @@ class Establishment extends EntityValidator {
source: bulkUploaded ? 'Bulk' : 'Online',
attributes: ['id', 'created', 'updated'],
ustatus: this._ustatus,
expiresSoonAlertDate: '90',
};

// need to create the Establishment record and the Establishment Audit event
Expand Down
10 changes: 7 additions & 3 deletions server/models/establishment.js
Original file line number Diff line number Diff line change
Expand Up @@ -862,10 +862,11 @@ module.exports = function (sequelize, DataTypes) {
});
};

Establishment.findByUid = async function (uid) {
Establishment.findByUid = async function (uid, isRegistration = false) {
return await this.findOne({
where: {
uid,
archived: isRegistration ? { [Op.or]: [true, false] } : false,
},
});
};
Expand Down Expand Up @@ -1068,7 +1069,7 @@ module.exports = function (sequelize, DataTypes) {
return await this.scope(scopes).count();
};

Establishment.getEstablishmentWithPrimaryUser = async function (uid) {
Establishment.getEstablishmentWithPrimaryUser = async function (uid, isRegistration = false) {
return await this.findOne({
attributes: [
'NameValue',
Expand All @@ -1094,6 +1095,7 @@ module.exports = function (sequelize, DataTypes) {
],
where: {
uid,
archived: isRegistration ? { [Op.or]: [true, false] } : false,
},
include: [
{
Expand Down Expand Up @@ -1128,7 +1130,9 @@ module.exports = function (sequelize, DataTypes) {
};

Establishment.getEstablishmentRegistrationsByStatus = async function (isRejection) {
const params = isRejection ? { ustatus: 'REJECTED' } : { ustatus: { [Op.or]: ['PENDING', 'IN PROGRESS'] } };
const params = isRejection
? { ustatus: 'REJECTED', archived: true }
: { ustatus: { [Op.or]: ['PENDING', 'IN PROGRESS'] } };

return await this.findAll({
attributes: [
Expand Down
2 changes: 2 additions & 0 deletions server/routes/admin/approval/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ const _rejectNewUser = async (user, workplace, req, res) => {
reviewer: null,
updated: new Date(),
updatedBy: adminUser.FullNameValue,
archived: true,
});

if (deletedUser && rejectedWorkplace) {
Expand Down Expand Up @@ -157,6 +158,7 @@ const _rejectNewWorkplace = async (workplace, req, res) => {
reviewer: null,
updated: new Date(),
updatedBy: adminUser.FullNameValue,
archived: true,
});

if (rejectedWorkplace) {
Expand Down
32 changes: 29 additions & 3 deletions server/routes/admin/cqc-status-change/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
const express = require('express');
const router = express.Router();
const models = require('../../../models');
const moment = require('moment-timezone');
const config = require('../../../config/config');
const mainServiceRouter = require('../../establishments/mainService');
const Establishment = require('../../../models/classes/establishment');
const { convertIndividualCqcStatusChange } = require('../../../utils/cqcStatusChangeUtils');

const cqcStatusChangeApprovalConfirmation = 'CQC status change approved';
const cqcStatusChangeRejectionConfirmation = 'CQC status change rejected';

const getCqcStatusChanges = async (req, res) => {
try {
let approvalResults = await models.Approvals.findAllPending('CqcStatusChange');
let approvalResults = await models.Approvals.findAllPendingAndInProgress('CqcStatusChange');
let cqcStatusChanges = await _mapResults(approvalResults);
return res.status(200).json(cqcStatusChanges);
} catch (error) {
Expand All @@ -33,6 +32,27 @@ const cqcStatusChanges = async (req, res) => {
}
};

const getIndividualCqcStatusChange = async (req, res) => {
try {
const { establishmentUid } = req.params;
const establishment = await models.establishment.findByUid(establishmentUid);

if (!establishment) {
return res.status(400).json({
message: 'Establishment could not be found',
});
}

const individualCqcStatusChange = await models.Approvals.findbyEstablishmentId(establishment.id, 'CqcStatusChange');

const convertedIndividualCqcStatusChange = convertIndividualCqcStatusChange(individualCqcStatusChange);
res.status(200).send(convertedIndividualCqcStatusChange);
} catch (error) {
console.error(error);
res.status(500).json({ message: 'There was an error retrieving the cqc status change' });
}
};

const _mapResults = async (approvalResults) => {
let data;
const promises = approvalResults.map(async (approval) => {
Expand Down Expand Up @@ -88,6 +108,8 @@ const _updateApprovalStatus = async (approvalId, status) => {
let singleApproval = await models.Approvals.findbyId(approvalId);
if (singleApproval) {
singleApproval.Status = status;
singleApproval.InReview = false;
singleApproval.Reviewer = null;
await singleApproval.save();
} else {
throw `Can't find approval item with id ${approvalId}`;
Expand Down Expand Up @@ -122,9 +144,13 @@ const _updateMainService = async (req, res) => {

router.route('/').post(cqcStatusChanges);
router.route('/').get(getCqcStatusChanges);
router.route('/:establishmentUid').get(getIndividualCqcStatusChange);

router.use('/updateStatus', require('./updateStatus.js'));

module.exports = router;
module.exports.cqcStatusChanges = cqcStatusChanges;
module.exports.getCqcStatusChanges = getCqcStatusChanges;
module.exports.getIndividualCqcStatusChange = getIndividualCqcStatusChange;
module.exports.cqcStatusChangeApprovalConfirmation = cqcStatusChangeApprovalConfirmation;
module.exports.cqcStatusChangeRejectionConfirmation = cqcStatusChangeRejectionConfirmation;
39 changes: 39 additions & 0 deletions server/routes/admin/cqc-status-change/updateStatus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const models = require('../../../models');

const updateStatus = async (req, res) => {
try {
const { uid, status, reviewer, inReview } = req.body;
const establishment = await models.establishment.findByUid(uid);

if (!establishment) {
return res.status(400).send({ error: 'Workplace could not be found' });
}

const approval = await models.Approvals.findbyEstablishmentId(establishment.id, 'CqcStatusChange');

if (!approval) {
return res.status(400).send({ error: 'CQC status change request could not be found' });
}

if (approval.InReview && reviewer && approval.Reviewer !== reviewer) {
return res.status(400).send({ error: 'This CQC status change request is already being reviewed' });
}

approval.Status = status;
approval.Reviewer = reviewer;
approval.InReview = inReview;
await approval.save();

res.status(200).send();
} catch (error) {
console.log(error);
res.status(500).send();
}
};

const router = require('express').Router();

router.route('/').post(updateStatus);

module.exports = router;
module.exports.updateStatus = updateStatus;
7 changes: 3 additions & 4 deletions server/routes/admin/parent-approval/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const express = require('express');
const router = express.Router();
const models = require('../../../models');
const moment = require('moment-timezone');
const config = require('../../../config/config');
const notifications = require('../../../data/notifications');
const uuid = require('uuid');

Expand All @@ -11,7 +9,7 @@ const parentRejectionConfirmation = 'Parent request rejected';

const getParentRequests = async (req, res) => {
try {
let approvalResults = await models.Approvals.findAllPending('BecomeAParent');
let approvalResults = await models.Approvals.findAllPendingAndInProgress('BecomeAParent');
let parentRequests = await _mapResults(approvalResults);
return res.status(200).json(parentRequests);
} catch (error) {
Expand Down Expand Up @@ -44,7 +42,8 @@ const _mapResults = async (approvalResults) => {
workplaceId: approval.Establishment.nmdsId,
userName: approval.User.FullNameValue,
orgName: approval.Establishment.NameValue,
requested: moment.utc(approval.createdAt).tz(config.get('timezone')).format('D/M/YYYY h:mma'),
requested: approval.createdAt,
status: approval.Status,
};
});
};
Expand Down
2 changes: 1 addition & 1 deletion server/routes/admin/registrations/getRegistrationNotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const models = require('../../../models');
const getRegistrationNotes = async (req, res) => {
try {
const { establishmentUid } = req.params;
const establishment = await models.establishment.findByUid(establishmentUid);
const establishment = await models.establishment.findByUid(establishmentUid, true);

if (!establishment) {
return res.status(400).json({
Expand Down
Loading

0 comments on commit 8051f16

Please sign in to comment.