Skip to content

Commit

Permalink
Merge pull request #2232 from NMDSdevopsServiceAdm/test
Browse files Browse the repository at this point in the history
Go from Staging to Live ready for preprod deployment
  • Loading branch information
aaron-russell authored Jun 18, 2020
2 parents 2fd7ca5 + 9456451 commit 1858c09
Show file tree
Hide file tree
Showing 68 changed files with 2,698 additions and 344 deletions.
30 changes: 30 additions & 0 deletions migrations/20200603104056-update_approvals_enum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';
const replaceEnum = require('sequelize-replace-enum-postgres').default;
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.sequelize.query('SET search_path to cqc').then(() => {
return replaceEnum({
queryInterface,
tableName: 'Approvals',
columnName: 'ApprovalType',
newValues: ['BecomeAParent', 'CqcStatusChange'],
enumName: 'enum_Approvals_ApprovalType'
});
}).then(() => {
return queryInterface.sequelize.query('SET search_path to DEFAULT');
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.sequelize.query('SET search_path to cqc').then(() => {
return replaceEnum({
queryInterface,
tableName: 'Approvals',
columnName: 'ApprovalType',
newValues: ['BecomeAParent'],
enumName: 'enum_Approvals_ApprovalType'
});
}).then(() => {
return queryInterface.sequelize.query('SET search_path to DEFAULT');
});
}
}
11 changes: 7 additions & 4 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"rxjs-compat": "^6.5.2",
"sequelize": "^4.44.3",
"sequelize-cli": "^5.5.1",
"sequelize-replace-enum-postgres": "^1.5.0",
"serve-favicon": "^2.5.0",
"slack": "^11.0.1",
"slugify": "^1.3.4",
Expand Down
54 changes: 29 additions & 25 deletions server/models/BulkImport/csv/training.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,31 +323,35 @@ class Training {
}

_validateAccredited () {
const myAccredited = parseInt(this._currentLine.ACCREDITED, 10);
const ALLOWED_VALUES = [0, 1, 999];
if (Number.isNaN(myAccredited) || !ALLOWED_VALUES.includes(myAccredited)) {
this._validationErrors.push({
worker: this._currentLine.UNIQUEWORKERID,
name: this._currentLine.LOCALESTID,
lineNumber: this._lineNumber,
errCode: Training.ACCREDITED_ERROR,
errType: 'ACCREDITED_ERROR',
error: 'ACCREDITED is invalid',
source: this._currentLine.ACCREDITED
});
return false;
} else {
switch (myAccredited) {
case 0:
this._accredited = 'No';
break;
case 1:
this._accredited = 'Yes';
break;
case 999:
this._accredited = 'Don\'t know';
break;
if (this._currentLine.ACCREDITED) {
const myAccredited = parseInt(this._currentLine.ACCREDITED, 10);
const ALLOWED_VALUES = [0, 1, 999];
if (Number.isNaN(myAccredited) || !ALLOWED_VALUES.includes(myAccredited)) {
this._validationErrors.push({
worker: this._currentLine.UNIQUEWORKERID,
name: this._currentLine.LOCALESTID,
lineNumber: this._lineNumber,
errCode: Training.ACCREDITED_ERROR,
errType: 'ACCREDITED_ERROR',
error: 'ACCREDITED is invalid',
source: this._currentLine.ACCREDITED
});
return false;
} else {
switch (myAccredited) {
case 0:
this._accredited = 'No';
break;
case 1:
this._accredited = 'Yes';
break;
case 999:
this._accredited = 'Don\'t know';
break;
}
return true;
}
} else {
return true;
}
}
Expand Down Expand Up @@ -552,7 +556,7 @@ class Training {

return columns.join(',');
}

toCSV(establishmentId, workerId, entity) {
return Training.toCSV(establishmentId, workerId, entity);
}
Expand Down
9 changes: 4 additions & 5 deletions server/models/approvals.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = (sequelize, DataTypes) => {
ApprovalType: {
type: DataTypes.ENUM,
allowNull: false,
values: ['BecomeAParent'],
values: ['BecomeAParent','CqcStatusChange'],
},
Status: {
type: DataTypes.ENUM,
Expand Down Expand Up @@ -66,7 +66,7 @@ module.exports = (sequelize, DataTypes) => {
ApprovalType: approvalType,
Status: 'Pending'
},
attributes: ['ID', 'UUID', 'EstablishmentID', 'UserID', 'createdAt', 'Status'],
attributes: ['ID', 'UUID', 'EstablishmentID', 'UserID', 'createdAt', 'Status', 'Data'],
include: [
{
model: sequelize.models.establishment,
Expand All @@ -90,7 +90,7 @@ module.exports = (sequelize, DataTypes) => {
where: {
ID: id
},
attributes: ['ID', 'UUID', 'EstablishmentID', 'UserID', 'createdAt', 'Status'],
attributes: ['ID', 'UUID', 'EstablishmentID', 'UserID', 'createdAt', 'Status', 'Data'],
include: [
{
model: sequelize.models.establishment,
Expand Down Expand Up @@ -126,15 +126,14 @@ module.exports = (sequelize, DataTypes) => {
],
});
}

Approvals.findbyEstablishmentId = function(establishmentId, approvalType, status) {
return this.findOne({
where: {
EstablishmentID: establishmentId,
ApprovalType: approvalType,
Status: status
},
attributes: ['ID', 'UUID', 'EstablishmentID', 'UserID', 'createdAt', 'Status'],
attributes: ['ID', 'UUID', 'EstablishmentID', 'UserID', 'createdAt', 'Status', 'Data'],
include: [
{
model: sequelize.models.establishment,
Expand Down
2 changes: 2 additions & 0 deletions server/models/cache/singletons/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class ServiceCache {
.filter(x => isCqcRegulated ? true : x.iscqcregistered === false )
.map( x => { return { id: x.id, name: x.name, category: x.category, other: x.other }});
}

return [];
}
}

Expand Down
10 changes: 9 additions & 1 deletion server/models/classes/establishment.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,17 @@ class Establishment extends EntityValidator {
this._ustatus = document.ustatus;
}

// CQC reugulated/location ID
// CQC regulated/location ID
if (hasProp(document, 'isRegulated')) {
this._isRegulated = document.isRegulated;

if(!this.isRegulated) {
this._locationId = null;

if (this.shareWith && this.shareWith.with) {
this.shareWith.with = this.shareWith.with.filter(x => x !== 'CQC');
}
}
}
if (document.locationId) {
// Note - there is more validation to do on location ID - so this really should be a managed property
Expand Down
9 changes: 8 additions & 1 deletion server/models/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ module.exports = function(sequelize, DataTypes) {
createdAt: false,
updatedAt: false
});

Services.findNameByID = function (id) {
return this.findOne({
where: {
id: id
},
attributes: ['name'],
});
};
Services.associate = (models) => {
Services.belongsToMany(models.establishment, {
through: 'establishmentServices',
Expand Down
128 changes: 128 additions & 0 deletions server/routes/admin/cqc-status-change/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
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 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 cqcStatusChanges = await _mapResults(approvalResults);
return res.status(200).json(cqcStatusChanges);
} catch (error) {
console.error(error);
return res.status(400).send();
}
};

const cqcStatusChanges = async (req, res) => {
try {
if (req.body.approve) {
await _approveChange(req, res);
} else {
await _rejectChange(req, res);
}
} catch (error) {
console.error(error);
return res.status(400).send();
}
};

const _mapResults = async (approvalResults) => {
const promises = approvalResults.map(async approval => {
data = approval.Data;
const currentServiceID = data.currentService.id || null;
const requestedServiceID = data.requestedService.id || null;
if (!currentServiceID || !requestedServiceID) throw `Can't find request data with ID ${approval.id}`;
return {
requestId: approval.ID,
requestUUID: approval.UUID,
establishmentId: approval.EstablishmentID,
establishmentUid: approval.Establishment.uid,
userId: approval.UserID,
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'),
data: {
currentService: {
ID: currentServiceID,
name: data.currentService.name,
other: data.currentService.other || null
},
requestedService: {
ID: requestedServiceID,
name: data.requestedService.name,
other: data.requestedService.other || null
}
}
};
});
return await Promise.all(promises);
};

const _approveChange = async (req, res) => {
await _updateApprovalStatus(req.body.approvalId, 'Approved');
const results = await _updateMainService(req, res);
if (results.success) {
return res.status(200).json({ status: '0', message: cqcStatusChangeApprovalConfirmation });
} else {
return res.status(results.errorCode).json({ status: errorCode, message: errorMsg });
}
};

const _rejectChange = async (req, res) => {
await _updateApprovalStatus(req.body.approvalId, 'Rejected');

return res.status(200).json({ status: '0', message: cqcStatusChangeRejectionConfirmation });
};

const _updateApprovalStatus = async (approvalId, status) => {
let singleApproval = await models.Approvals.findbyId(approvalId);
if (singleApproval) {
singleApproval.Status = status;
await singleApproval.save();
} else {
throw `Can't find approval item with id ${approvalId}`;
}
};

const _updateMainService = async (req, res) => {
const approvalId = req.body.approvalId;
const username = req.username;

const singleApproval = await models.Approvals.findbyId(approvalId);
if (singleApproval) {
const data = singleApproval.Data;
const establishmentId = singleApproval.EstablishmentID;

const mainService = {
id: data.requestedService.id,
name: data.requestedService.name,
...(data.requestedService.other && { other: data.requestedService.other })
};

const thisEstablishment = new Establishment.Establishment(username);
await thisEstablishment.restore(establishmentId);

const addIsRegulated = true;

return await mainServiceRouter.changeMainService(res, thisEstablishment, addIsRegulated, mainService, username);
} else {
throw `Can't find Approval with id ${approvalId}`;
}
};

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

module.exports = router;
module.exports.cqcStatusChanges = cqcStatusChanges;
module.exports.getCqcStatusChanges = getCqcStatusChanges;
module.exports.cqcStatusChangeApprovalConfirmation = cqcStatusChangeApprovalConfirmation;
module.exports.cqcStatusChangeRejectionConfirmation = cqcStatusChangeRejectionConfirmation;
2 changes: 2 additions & 0 deletions server/routes/admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const registrations = require('./registrations');
const approval = require('./approval');
const parentApproval = require('./parent-approval');
const unlockAccount = require('./unlock-account');
const cqcStatusChange = require('./cqc-status-change');

// middleware authentication - only role=Admin from here on in
router.use('/', isAdmin);
Expand All @@ -18,6 +19,7 @@ router.use('/recalcWdf', recalcWdf);
router.use('/registrations', registrations);
router.use('/approval', approval);
router.use('/parent-approval', parentApproval);
router.use('/cqc-status-change', cqcStatusChange);
router.use('/unlock-account', unlockAccount);

router.route('/').post(async function (req, res) {
Expand Down
Loading

0 comments on commit 1858c09

Please sign in to comment.