Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/remove wdf report call in nhs api #6292

Merged
merged 5 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 20 additions & 36 deletions backend/server/models/establishment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2354,25 +2354,9 @@ module.exports = function (sequelize, DataTypes) {
});
};

const nhsBsaAttributes = [
'id',
'nmdsId',
'NameValue',
'address1',
'locationId',
'town',
'postcode',
'isParent',
'dataOwner',
'NumberOfStaffValue',
'parentId',
];

Establishment.getNhsBsaApiDataByWorkplaceId = async function (where) {
return await this.findOne({
nhsBsaAttributes,
const nhsBsaApiQuery = (where) => {
return {
as: 'establishment',

where: {
archived: false,
...where,
Expand All @@ -2384,29 +2368,29 @@ module.exports = function (sequelize, DataTypes) {
attributes: ['name', 'category'],
required: true,
},
{
model: sequelize.models.worker,
as: 'workers',
attributes: ['WdfEligible'],
where: {
archived: false,
},
required: false,
},
],
});
};
};

Establishment.getNhsBsaApiDataForSubs = async function (establishmentId) {
return await this.findAll({
nhsBsaAttributes,
as: 'establishment',
Establishment.getNhsBsaApiDataByWorkplaceId = async function (workplaceId) {
return await this.findOne(nhsBsaApiQuery({ nmdsId: workplaceId }));
};

where: {
archived: false,
parentId: establishmentId,
},
Establishment.getNhsBsaApiDataForParent = async function (workplaceId) {
return await this.findOne(nhsBsaApiQuery({ id: workplaceId }));
};

include: [
{
model: sequelize.models.services,
as: 'mainService',
attributes: ['name', 'category'],
required: true,
},
],
});
Establishment.getNhsBsaApiDataForSubs = async function (parentId) {
return await this.findAll(nhsBsaApiQuery({ parentId }));
};

return Establishment;
Expand Down
72 changes: 27 additions & 45 deletions backend/server/routes/nhsBsaApi/workplaceData.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,30 @@ const WdfCalculator = require('../../models/classes/wdfCalculator').WdfCalculato
const nhsBsaApi = async (req, res) => {
const workplaceId = req.params.workplaceId;

const where = {
nmdsId: workplaceId,
};

try {
const workplaceDetail = await models.establishment.getNhsBsaApiDataByWorkplaceId(where);
if (!workplaceDetail) return res.status(404).json({ error: 'Can not find this Id.' });
const workplace = await models.establishment.getNhsBsaApiDataByWorkplaceId(workplaceId);
if (!workplace) return res.status(404).json({ error: 'Cannot find this Id.' });

const isParent = workplaceDetail.isParent;
const establishmentId = workplaceDetail.id;
const parentId = workplaceDetail.parentId;
const isParent = workplace.isParent;
const establishmentId = workplace.id;
const parentId = workplace.parentId;

let workplaceData = null;

if (isParent) {
workplaceData = {
isParent: workplaceDetail.isParent,
workplaceDetails: await workplaceObject(workplaceDetail),
isParent,
workplaceDetails: await workplaceObject(workplace),
subsidiaries: await subsidiariesList(establishmentId),
};
} else if (parentId) {
workplaceData = {
workplaceDetails: await workplaceObject(workplaceDetail),
workplaceDetails: await workplaceObject(workplace),
parentWorkplace: await parentWorkplace(parentId),
};
} else {
workplaceData = {
workplaceDetails: await workplaceObject(workplaceDetail),
workplaceDetails: await workplaceObject(workplace),
};
}

Expand All @@ -51,8 +47,6 @@ const nhsBsaApi = async (req, res) => {
};

const workplaceObject = async (workplace) => {
const wdfEligible = await wdfData(workplace.id, WdfCalculator.effectiveDate);

return {
workplaceId: workplace.nmdsId,
workplaceName: workplace.NameValue,
Expand All @@ -66,12 +60,20 @@ const workplaceObject = async (workplace) => {
numberOfWorkplaceStaff: workplace.NumberOfStaffValue,
serviceName: workplace.mainService.name,
serviceCategory: workplace.mainService.category,
...wdfEligible,
eligibilityPercentage: calculatePercentageOfWorkersEligible(workplace.workers),
eligibilityDate: workplace.overallWdfEligibility,
isEligible: workplaceIsEligible(workplace),
};
};

const subsidiariesList = async (establishmentId) => {
const subs = await models.establishment.getNhsBsaApiDataForSubs(establishmentId);
const workplaceIsEligible = (workplace) => {
return workplace.overallWdfEligibility && workplace.overallWdfEligibility.getTime() > WdfCalculator.effectiveDate
? true
: false;
};

const subsidiariesList = async (parentId) => {
const subs = await models.establishment.getNhsBsaApiDataForSubs(parentId);

const subsidiaries = await Promise.all(
subs.map(async (workplace) => {
Expand All @@ -82,41 +84,21 @@ const subsidiariesList = async (establishmentId) => {
};

const parentWorkplace = async (parentId) => {
const where = {
id: parentId,
};
const parentWorkplace = await models.establishment.getNhsBsaApiDataByWorkplaceId(where);
const parentWorkplace = await models.establishment.getNhsBsaApiDataForParent(parentId);

return await workplaceObject(parentWorkplace);
};

const wdfData = async (workplaceId, effectiveFrom) => {
const reportData = await models.sequelize.query(
`SELECT * FROM cqc.wdfsummaryreport(:givenEffectiveDate) WHERE "EstablishmentID" = '${workplaceId}'`,
{
replacements: {
givenEffectiveDate: effectiveFrom,
},
type: models.sequelize.QueryTypes.SELECT,
},
);
const calculatePercentageOfWorkersEligible = (workers) => {
const numberOfWorkers = workers?.length;

const wdfMeeting = reportData.find((workplace) => workplace.EstablishmentID === workplaceId);
if (wdfMeeting) {
const percentageEligibleWorkers =
wdfMeeting.WorkerCount > 0 ? Math.floor((wdfMeeting.WorkerCompletedCount / wdfMeeting.WorkerCount) * 100) : 0;

return {
eligibilityPercentage: percentageEligibleWorkers,
eligibilityDate: wdfMeeting.OverallWdfEligibility,
isEligible:
wdfMeeting.OverallWdfEligibility && wdfMeeting.OverallWdfEligibility.getTime() > effectiveFrom ? true : false,
};
}
if (!numberOfWorkers) return 0;
const numberOfEligibleWorkers = workers.filter((worker) => worker.get('WdfEligible')).length;

return Math.floor((numberOfEligibleWorkers / numberOfWorkers) * 100);
};

router.route('/:workplaceId').get(authLimiter, authorization.isAuthorised, nhsBsaApi);
module.exports = router;
module.exports.nhsBsaApi = nhsBsaApi;
module.exports.subsidiariesList = subsidiariesList;
module.exports.wdfData = wdfData;
Loading
Loading