Skip to content

Commit

Permalink
Merge feature branch in
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanc19 committed Aug 27, 2024
2 parents 1fb9202 + 3e32baf commit 4bbdc1c
Show file tree
Hide file tree
Showing 28 changed files with 589 additions and 282 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ install:
npm install --prefix backend

run:
(cd backend && npm run dev-start) & \
(cd backend && npm run new-start) & \
(cd frontend && npm run build:watch)

test-fe:
Expand Down
3 changes: 1 addition & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"name": "ng-sfc-v2",
"version": "0.0.1",
"scripts": {
"dev-start": "npm run api:server",
"new-start": "npm run db:migrate:pipeline && npm run api:server",
"new-start": "npm run api:server",
"start": "npm run db:migrate:pipeline && node --max-old-space-size=4096 server.js",
"build": "node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --no-progress",
"build:clean": "rimraf dist",
Expand Down
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

0 comments on commit 4bbdc1c

Please sign in to comment.