Skip to content

Commit

Permalink
Merge pull request #4934 from NMDSdevopsServiceAdm/test
Browse files Browse the repository at this point in the history
Test into live: Workplace Details workflow amendments, Bulk Upload Workplace refactor, Benchmarks view table
  • Loading branch information
Jonopono123 authored May 25, 2022
2 parents 2c98508 + 9248392 commit 1f8c830
Show file tree
Hide file tree
Showing 88 changed files with 4,489 additions and 2,195 deletions.
39 changes: 39 additions & 0 deletions migrations/20220511145442-createBenchmarkTabUsageTable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable(
'BenchmarksViewed',
{
ID: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
EstablishmentID: {
type: Sequelize.DataTypes.INTEGER,
allowNull: false,
references: {
model: {
tableName: 'Establishment',
schema: 'cqc',
},
key: 'EstablishmentID',
},
},
ViewedTime: {
type: Sequelize.DataTypes.DATE,
allowNull: false,
},
},
{ schema: 'cqc' },
);
},

down: (queryInterface) => {
return queryInterface.dropTable({
tableName: 'BenchmarksViewed',
schema: 'cqc',
});
},
};

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions server/models/benchmarksViewed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module.exports = function (sequelize, DataTypes) {
const BenchmarksViewed = sequelize.define(
'benchmarksViewed',
{
ID: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
EstablishmentID: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
},
ViewedTime: {
type: DataTypes.DATE,
allowNull: false,
primaryKey: true,
},
},
{
tableName: 'BenchmarksViewed',
schema: 'cqc',
createdAt: false,
updatedAt: false,
},
);

BenchmarksViewed.associate = (models) => {
BenchmarksViewed.belongsTo(models.establishment, {
foreignKey: 'EstablishmentID',
targetKey: 'id',
as: 'benchmarkEstablishment',
});
};

return BenchmarksViewed;
};
2 changes: 2 additions & 0 deletions server/routes/establishments/benchmarks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const router = express.Router({ mergeParams: true });
const models = require('../../../models');
const clonedeep = require('lodash.clonedeep');
const rankings = require('./rankings');
const usage = require('./usage');
const { getPay, getQualifications, getSickness, getTurnover } = require('./benchmarksService');

const { hasPermission } = require('../../../utils/security/hasPermission');
Expand Down Expand Up @@ -110,6 +111,7 @@ router.use('/', hasPermission('canViewBenchmarks'));
router.route('/').get(viewBenchmarks);

router.use('/rankings', rankings);
router.use('/usage', usage);

module.exports = router;
module.exports.pay = pay;
Expand Down
22 changes: 22 additions & 0 deletions server/routes/establishments/benchmarks/usage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const express = require('express');
const router = express.Router();
const models = require('../../../../models');
const moment = require('moment');

const postBenchmarkTabUsage = async (req, res) => {
const viewedTime = moment();
try {
await models.benchmarksViewed.create({
EstablishmentID: req.establishmentId,
ViewedTime: viewedTime,
});
res.status(200).send();
} catch (err) {
console.error(err);
return res.status(500).json({});
}
};
router.route('/').post(postBenchmarkTabUsage);

module.exports = router;
module.exports.postBenchmarkTabUsage = postBenchmarkTabUsage;
49 changes: 3 additions & 46 deletions server/routes/establishments/bulkUpload/complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const completeNewEstablishment = async (
onloadEstablishments,
primaryEstablishmentId,
primaryEstablishmentUid,
keepAlive = () => {},
) => {
try {
const startTime = new Date();
Expand All @@ -28,11 +27,8 @@ const completeNewEstablishment = async (
if (foundOnloadEstablishment) {
// as this new establishment is created from a parent, it automatically becomes a sub
foundOnloadEstablishment.initialiseSub(primaryEstablishmentId, primaryEstablishmentUid);
keepAlive('foundOnloadEstablishment initialised');
await foundOnloadEstablishment.save(theLoggedInUser, true, transaction, true);
keepAlive('foundOnloadEstablishment saved');
await foundOnloadEstablishment.bulkUploadWdf(theLoggedInUser, transaction);
keepAlive('foundOnloadEstablishment wdf calculated');
}

const endTime = new Date();
Expand All @@ -50,7 +46,6 @@ const completeUpdateEstablishment = async (
transaction,
onloadEstablishments,
myCurrentEstablishments,
keepAlive = () => {},
) => {
try {
const startTime = new Date();
Expand All @@ -72,13 +67,9 @@ const completeUpdateEstablishment = async (
const thisEstablishmentJSON = foundOnloadEstablishment.toJSON(false, false, false, false, true, null, true);
delete thisEstablishmentJSON.localIdentifier;

keepAlive('complete upload');
await foundCurrentEstablishment.load(thisEstablishmentJSON, true, true);
keepAlive('complete upload loaded');
await foundCurrentEstablishment.save(theLoggedInUser, true, transaction, true);
keepAlive('complete upload saved');
await foundCurrentEstablishment.bulkUploadWdf(theLoggedInUser, transaction);
keepAlive('complete upload wdf');

const endTime = new Date();
const numberOfWorkers = foundCurrentEstablishment.workers.length;
Expand Down Expand Up @@ -125,10 +116,6 @@ const completeDeleteEstablishment = async (
};

const completePost = async (req, res) => {
const keepAlive = (stepName = '', stepId = '') => {
console.log(`Bulk Upload /complete keep alive: ${new Date()} ${stepName} ${stepId}`);
};

const theLoggedInUser = req.username;
const primaryEstablishmentId = req.establishment.id;
const primaryEstablishmentUid = req.establishment.uid;
Expand All @@ -140,15 +127,7 @@ const completePost = async (req, res) => {
// validating a bulk upload and completing it.
const completeStartTime = new Date();
// association level is just 1 (we need Establishment's workers for completion, but not the Worker's associated training and qualification)
const myCurrentEstablishments = await restoreExistingEntities(
theLoggedInUser,
primaryEstablishmentId,
isParent,
1,
keepAlive,
);

keepAlive('restore existing entities', primaryEstablishmentId);
const myCurrentEstablishments = await restoreExistingEntities(theLoggedInUser, primaryEstablishmentId, isParent, 1);

const restoredExistingStateTime = new Date();
timerLog(
Expand All @@ -158,14 +137,12 @@ const completePost = async (req, res) => {
);

try {
const onloadEstablishments = await restoreOnloadEntities(theLoggedInUser, primaryEstablishmentId, keepAlive);
const onloadEstablishments = await restoreOnloadEntities(theLoggedInUser, primaryEstablishmentId);
const validationDifferenceReportDownloaded = await downloadContent(
`${primaryEstablishmentId}/validation/difference.report.json`,
null,
null,
).then((data) => {
keepAlive('differences downloaded');

return data;
});
const validationDifferenceReport = JSON.parse(validationDifferenceReportDownloaded.data);
Expand Down Expand Up @@ -198,11 +175,8 @@ const completePost = async (req, res) => {
onloadEstablishments,
primaryEstablishmentId,
primaryEstablishmentUid,
keepAlive,
)
.then((data) => {
keepAlive('complete new establishment');

return data;
})
.then(log),
Expand All @@ -221,11 +195,8 @@ const completePost = async (req, res) => {
t,
onloadEstablishments,
myCurrentEstablishments,
keepAlive,
)
.then((data) => {
keepAlive('completeUpdateEstablishment');

return data;
})
.then(log),
Expand All @@ -238,16 +209,8 @@ const completePost = async (req, res) => {
await validationDifferenceReport.deleted.reduce(
(p, thisDeletedEstablishment) =>
p.then(() =>
completeDeleteEstablishment(
thisDeletedEstablishment,
theLoggedInUser,
t,
myCurrentEstablishments,
keepAlive,
)
completeDeleteEstablishment(thisDeletedEstablishment, theLoggedInUser, t, myCurrentEstablishments)
.then((data) => {
keepAlive('completeDeleteEstablishment');

return data;
})
.then(log),
Expand All @@ -270,19 +233,13 @@ const completePost = async (req, res) => {
// Saves the bulk upload files to lastBulkUpload
await saveLastBulkUpload(primaryEstablishmentId);

keepAlive('saveLastBulkUpload');

// gets here having successfully completed upon the bulk upload
// clean up the S3 objects
await purgeBulkUploadS3Objects(primaryEstablishmentId);

keepAlive('purgeBulkUploadS3Objects');

// confirm success against the primary establishment
await Establishment.bulkUploadSuccess(primaryEstablishmentId);

keepAlive('bulkUploadSuccess');

const completeEndTime = new Date();
timerLog('CHECKPOINT - BU COMPLETE - clean up', completeSaveTime, completeEndTime);
timerLog('CHECKPOINT - BU COMPLETE - overall', completeStartTime, completeEndTime);
Expand Down
6 changes: 3 additions & 3 deletions server/routes/establishments/bulkUpload/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const config = require('../../../config/config');
const models = require('../../../models');

const EstablishmentCsvValidator = require('../../../models/BulkImport/csv/establishments').Establishment;
const WorkplaceCSVValidator = require('../../../models/BulkImport/csv/workplaceCSVValidator').WorkplaceCSVValidator;
const { getWorkerHeadersWithExtraQuals } = require('../bulkUpload/validate/headers/worker');
const { trainingHeaders } = require('./data/trainingHeaders');
const WorkerCSV = require('./download/workerCSV');
Expand All @@ -14,9 +14,9 @@ const s3 = require('./s3');
const NEWLINE = '\r\n';

const establishmentCsv = async (establishments, responseSend) => {
responseSend(EstablishmentCsvValidator.headers());
responseSend(WorkplaceCSVValidator.headers());

establishments.map((establishment) => responseSend(NEWLINE + EstablishmentCsvValidator.toCSV(establishment)));
establishments.map((establishment) => responseSend(NEWLINE + WorkplaceCSVValidator.toCSV(establishment)));
};

const workerCsv = async (establishments, responseSend, downloadType) => {
Expand Down
15 changes: 1 addition & 14 deletions server/routes/establishments/bulkUpload/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,15 @@ const restoreExistingEntities = async (
isParent,
assocationLevel = 1,
onlyMine = false,
keepAlive = () => {},
) => {
try {
const completionBulkUploadStatus = 'COMPLETE';
const thisUser = new User(primaryEstablishmentId);
await thisUser.restore(null, loggedInUsername, false);

keepAlive('begin restore entities'); // keep connection alive

// gets a list of "my establishments", which if a parent, includes all known subsidaries too, and this "parent's" access permissions to those subsidaries
const myEstablishments = await thisUser.myEstablishments(isParent, null);

keepAlive('establishments retrieved'); // keep connection alive

// having got this list of establishments, now need to fully restore each establishment as entities.
// using an object adding entities by a known key to make lookup comparisions easier.
const currentEntities = [];
Expand All @@ -38,8 +33,6 @@ const restoreExistingEntities = async (

restoreEntityPromises.push(
primaryEstablishment.restore(myEstablishments.primary.uid, false, true, assocationLevel).then((data) => {
keepAlive('establishment restored', myEstablishments.primary.uid); // keep connection alive

return data;
}),
);
Expand All @@ -60,8 +53,6 @@ const restoreExistingEntities = async (

restoreEntityPromises.push(
newSub.restore(thisSubsidairy.uid, false, true, assocationLevel).then((data) => {
keepAlive('sub establishment restored', thisSubsidairy.uid); // keep connection alive

return data;
}),
);
Expand All @@ -80,14 +71,12 @@ const restoreExistingEntities = async (

// for the given user, restores all establishment and worker entities only from the DB, associating the workers
// back to the establishment
const restoreOnloadEntities = async (loggedInUsername, primaryEstablishmentId, keepAlive = () => {}) => {
const restoreOnloadEntities = async (loggedInUsername, primaryEstablishmentId) => {
try {
// the result of validation is to make available an S3 object outlining ALL entities ready to be uploaded
const allEntitiesKey = `${primaryEstablishmentId}/intermediary/all.entities.json`;

const onLoadEntitiesJSON = await downloadContent(allEntitiesKey).then((myFile) => {
keepAlive('restoreOnloadEntities');

return myFile;
});

Expand All @@ -114,8 +103,6 @@ const restoreOnloadEntities = async (loggedInUsername, primaryEstablishmentId, k
);
onloadPromises.push(
newOnloadEstablishment.load(thisEntity, true).then((data) => {
keepAlive('newOnloadEstablishment loaded');

return data;
}),
);
Expand Down
2 changes: 1 addition & 1 deletion server/routes/establishments/bulkUpload/errorReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const router = require('express').Router();
const s3 = require('./s3');
const { buStates } = require('./states');
const { getErrorWarningArray } = require('../../../utils/errorWarningArray');
const { EstablishmentFileHeaders } = require('../../../models/BulkImport/csv/establishments');
const { EstablishmentFileHeaders } = require('../../../models/BulkImport/csv/workplaceCSVValidator');
const excelUtils = require('../../../utils/excelUtils');
const { workerHeadersWithCHGUNIQUEWRKID } = require('./data/workerHeaders');
const { trainingHeaders } = require('./data/trainingHeaders');
Expand Down
4 changes: 2 additions & 2 deletions server/routes/establishments/bulkUpload/uploadFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const config = require('../../../config/config');
const S3 = require('./s3');
const { buStates } = require('./states');
const Bucket = S3.Bucket;
const EstablishmentCsvValidator = require('../../../models/BulkImport/csv/establishments').Establishment;
const WorkplaceCSVValidator = require('../../../models/BulkImport/csv/workplaceCSVValidator').WorkplaceCSVValidator;
const { getFileType } = require('./whichFile');
const { validateWorkerHeaders } = require('../bulkUpload/validate/headers/worker');
const { validateTrainingHeaders } = require('../bulkUpload/validate/headers/training');
Expand Down Expand Up @@ -36,7 +36,7 @@ const updateMetaData = async (file, username, establishmentId) => {
} else if (file.type === 'Training') {
passedCheck = validateTrainingHeaders(file.header);
} else {
const validator = new EstablishmentCsvValidator(file.importedData[firstRow], firstLineNumber);
const validator = new WorkplaceCSVValidator(file.importedData[firstRow], firstLineNumber);
passedCheck = validator.preValidate(file.header);
}

Expand Down
Loading

0 comments on commit 1f8c830

Please sign in to comment.