diff --git a/backend/server/models/establishment.js b/backend/server/models/establishment.js index 6c4b321585..60233523e4 100644 --- a/backend/server/models/establishment.js +++ b/backend/server/models/establishment.js @@ -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; diff --git a/backend/server/routes/establishments/hasTrainingCertificates.js b/backend/server/routes/establishments/hasTrainingCertificates.js index ca27b05af8..a181817f5c 100644 --- a/backend/server/routes/establishments/hasTrainingCertificates.js +++ b/backend/server/routes/establishments/hasTrainingCertificates.js @@ -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'); } }; diff --git a/backend/server/test/unit/routes/establishments/hasTrainingCertificates.spec.js b/backend/server/test/unit/routes/establishments/hasTrainingCertificates.spec.js index ffddf51a62..2e70e7198b 100644 --- a/backend/server/test/unit/routes/establishments/hasTrainingCertificates.spec.js +++ b/backend/server/test/unit/routes/establishments/hasTrainingCertificates.spec.js @@ -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(); @@ -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(); @@ -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);