Skip to content

Commit

Permalink
Merge pull request #6410 from NMDSdevopsServiceAdm/fix/make-bu-check-…
Browse files Browse the repository at this point in the history
…for-training-certs-more-efficient

Increase efficiency of check for training certificates on Bulk Upload page
  • Loading branch information
duncanc19 authored Nov 15, 2024
2 parents a5ba36a + d6a5383 commit 2ccc66f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 158 deletions.
47 changes: 16 additions & 31 deletions backend/server/models/establishment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2395,38 +2395,23 @@ module.exports = function (sequelize, DataTypes) {
return await this.findAll(nhsBsaApiQuery({ parentId }));
};

Establishment.getTrainingCertificatesForWorkplaceAndAnySubs = async function (workplaceId) {
return await this.findAll({
where: {
[Op.or]: [
{
id: workplaceId,
},
{
parentId: workplaceId,
dataOwner: 'Parent',
},
],
},
include: {
model: sequelize.models.worker,
attributes: ['id'],
as: 'workers',
where: {
archived: false,
},
include: {
model: sequelize.models.workerTraining,
as: 'workerTraining',
attributes: ['id'],
include: {
model: sequelize.models.trainingCertificates,
as: 'trainingCertificates',
attributes: ['id'],
},
},
Establishment.workplaceOrSubHasAtLeastOneTrainingCertificate = async function (workplaceId) {
const [result] = await sequelize.query(
`
SELECT 1 FROM cqc."Establishment" establishment
INNER JOIN cqc."Worker" worker ON establishment."EstablishmentID" = worker."EstablishmentFK"
INNER JOIN cqc."TrainingCertificates" trainingCertificate ON worker."ID" = trainingCertificate."WorkerFK"
WHERE establishment."Archived" = false AND worker."Archived" = false
AND (establishment."EstablishmentID" = :workplaceId OR establishment."ParentID" = :workplaceId)
LIMIT 1;
`,
{
replacements: { workplaceId },
type: sequelize.QueryTypes.SELECT,
},
});
);

return !!result;
};

return Establishment;
Expand Down
11 changes: 2 additions & 9 deletions backend/server/routes/establishments/hasTrainingCertificates.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@ const models = require('../../models');

const workplaceOrSubHasTrainingCertificates = async (req, res) => {
try {
const workplacesWithTrainingCertificates = await models.establishment.getTrainingCertificatesForWorkplaceAndAnySubs(
const hasTrainingCertificates = await models.establishment.workplaceOrSubHasAtLeastOneTrainingCertificate(
req.establishmentId,
);

const hasTrainingCertificates = workplacesWithTrainingCertificates.some((workplace) =>
workplace.workers.some((worker) =>
worker.workerTraining.some(
(training) => training.trainingCertificates && training.trainingCertificates.length > 0,
),
),
);

return res.status(200).json({ hasTrainingCertificates });
} catch (error) {
console.error(error);
return res.status(500).send('Failed to complete check for training certificates');
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,8 @@ describe('server/routes/establishments/hasTrainingCertificates', () => {
});

describe('workplaceOrSubHasTrainingCertificates', () => {
it('should return 200 status and false when single workplace has no workers with training certificates', async () => {
sinon.stub(models.establishment, 'getTrainingCertificatesForWorkplaceAndAnySubs').returns([
{
id: 123,
workers: [
{
workerTraining: [
{
trainingCertificates: [],
},
],
},
],
},
]);
it('should return 200 status and hasTrainingCertificates as false when false returned from DB query', async () => {
sinon.stub(models.establishment, 'workplaceOrSubHasAtLeastOneTrainingCertificate').returns(false);

await workplaceOrSubHasTrainingCertificates(req, res);
const response = res._getJSONData();
Expand All @@ -49,108 +36,8 @@ describe('server/routes/establishments/hasTrainingCertificates', () => {
expect(response.hasTrainingCertificates).to.equal(false);
});

it('should return 200 status and true when single workplace has workers with training certificates', async () => {
sinon.stub(models.establishment, 'getTrainingCertificatesForWorkplaceAndAnySubs').returns([
{
id: 123,
workers: [
{
workerTraining: [
{
trainingCertificates: [],
},
{
trainingCertificates: [{ id: 123 }],
},
],
},
],
},
]);

await workplaceOrSubHasTrainingCertificates(req, res);
const response = res._getJSONData();

expect(res.statusCode).to.deep.equal(200);
expect(response.hasTrainingCertificates).to.equal(true);
});

it('should return 200 status and false when several workplaces which do not have workers with training certificates', async () => {
sinon.stub(models.establishment, 'getTrainingCertificatesForWorkplaceAndAnySubs').returns([
{
id: 123,
workers: [
{
workerTraining: [
{
trainingCertificates: [],
},
],
},
],
},
{
id: 456,
workers: [],
},
{
id: 789,
workers: [
{
workerTraining: [
{
trainingCertificates: [],
},
{
trainingCertificates: [],
},
],
},
],
},
]);

await workplaceOrSubHasTrainingCertificates(req, res);
const response = res._getJSONData();

expect(res.statusCode).to.deep.equal(200);
expect(response.hasTrainingCertificates).to.equal(false);
});

it('should return 200 status and true when several workplaces where one has a worker with a training certificate', async () => {
sinon.stub(models.establishment, 'getTrainingCertificatesForWorkplaceAndAnySubs').returns([
{
id: 123,
workers: [
{
workerTraining: [
{
trainingCertificates: [],
},
],
},
],
},
{
id: 456,
workers: [],
},
{
id: 789,
workers: [
{
workerTraining: [
{
trainingCertificates: [],
},
{
trainingCertificates: [{ id: 12 }],
},
],
},
],
},
]);
it('should return 200 status and hasTrainingCertificates as true when true returned from DB query', async () => {
sinon.stub(models.establishment, 'workplaceOrSubHasAtLeastOneTrainingCertificate').returns(true);

await workplaceOrSubHasTrainingCertificates(req, res);
const response = res._getJSONData();
Expand All @@ -160,7 +47,7 @@ describe('server/routes/establishments/hasTrainingCertificates', () => {
});

it('should return 500 status and default error message when DB call throws error', async () => {
sinon.stub(models.establishment, 'getTrainingCertificatesForWorkplaceAndAnySubs').throws();
sinon.stub(models.establishment, 'workplaceOrSubHasAtLeastOneTrainingCertificate').throws();

await workplaceOrSubHasTrainingCertificates(req, res);

Expand Down

0 comments on commit 2ccc66f

Please sign in to comment.