Skip to content

Commit

Permalink
Merge pull request #799 from cbrianbet/feat/ahd-improvements
Browse files Browse the repository at this point in the history
bug fix/ ahd filters
  • Loading branch information
cbrianbet authored Feb 27, 2025
2 parents 1712221 + 2ae8085 commit e4ff993
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,37 @@ export class GetAhdNutritionAssessmentHandler implements IQueryHandler<GetAhdNut
) {}

async execute(query: GetAhdNutritionAssessmentQuery): Promise<any> {
const filters = [];
if (query.county) {
filters.push('County In (:...counties)')
}

if (query.subCounty) {
filters.push('SubCounty In (:...subCounties)')
}

if (query.facility) {
filters.push('FacilityName In (:...facilities)')
}

if (query.partner) {
filters.push('PartnerName In (:...partners)')
}

if (query.agency) {
filters.push('AgencyName In (:...agencies)')
}

if (query.datimAgeGroup) {
filters.push('AgeGroup In (:...ageGroups)')
}

if (query.gender) {
filters.push('Gender In (:...genders)')
}

const whereClause = filters.length ? `WHERE ${filters.join(' AND ')}` : ``;

const ahdAssessment = this.repository
.createQueryBuilder('f')
.select([`TOP 1
Expand All @@ -20,69 +51,41 @@ export class GetAhdNutritionAssessmentHandler implements IQueryHandler<GetAhdNut
SUM(CASE WHEN ConfirmedTreatmentFailure = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN NewPatient = 1 THEN 1 ELSE 0 END) AS TotalCount
FROM REPORTING.dbo.linelist_FactART
WHERE AHD=1 and age <15) AS ChildrenWithAHD,
${whereClause} ${filters.length ? ' AND ': 'WHERE '}
AHD=1 and age <15) AS ChildrenWithAHD,
(SELECT
SUM(CASE WHEN IsRTTLast12MonthsAfter3monthsIIT = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN ConfirmedTreatmentFailure = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN NewPatient = 1 THEN 1 ELSE 0 END) AS TotalCount
FROM REPORTING.dbo.linelist_FactART
WHERE AHD=1 and age <15 and ScreenedForMalnutrition=1) AS ScreenedForMalnutrition,
${whereClause} ${filters.length ? ' AND ': 'WHERE '}
AHD=1 and age <15 and ScreenedForMalnutrition=1) AS ScreenedForMalnutrition,
(SELECT
SUM(CASE WHEN IsRTTLast12MonthsAfter3monthsIIT = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN ConfirmedTreatmentFailure = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN NewPatient = 1 THEN 1 ELSE 0 END) AS TotalCount
FROM REPORTING.dbo.linelist_FactART
WHERE AHD=1 and age <15 and SAM=1) AS NumberWithSAM,
${whereClause} ${filters.length ? ' AND ': 'WHERE '}
AHD=1 and age <15 and SAM=1) AS NumberWithSAM,
(SELECT
SUM(CASE WHEN IsRTTLast12MonthsAfter3monthsIIT = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN ConfirmedTreatmentFailure = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN NewPatient = 1 THEN 1 ELSE 0 END) AS TotalCount
FROM REPORTING.dbo.linelist_FactART
WHERE AHD=1 and age <15 and MAM=1) AS NumberWithMAM
${whereClause} ${filters.length ? ' AND ': 'WHERE '}
AHD=1 and age <15 and MAM=1) AS NumberWithMAM
`])

if (query.county) {
ahdAssessment.andWhere('f.County IN (:...counties)', {
counties: query.county,
});
}

if (query.subCounty) {
ahdAssessment.andWhere('f.Subcounty IN (:...subCounties)', {
subCounties: query.subCounty,
});
}

if (query.facility) {
ahdAssessment.andWhere('f.FacilityName IN (:...facilities)', {
facilities: query.facility,
});
}

if (query.partner) {
ahdAssessment.andWhere('f.PartnerName IN (:...partners)', {
partners: query.partner,
});
}

if (query.agency) {
ahdAssessment.andWhere('f.AgencyName IN (:...agencies)', {
agencies: query.agency,
});
}

if (query.datimAgeGroup) {
ahdAssessment.andWhere('f.AgeGroup IN (:...ageGroups)', {
ageGroups: query.datimAgeGroup,
});
}

if (query.gender) {
ahdAssessment.andWhere('f.Gender IN (:...genders)', {
genders: query.gender,
});
const params = {
facilities: query.facility,
counties: query.county,
subCounties: query.subCounty,
partners: query.partner,
agencies: query.agency,
ageGroups: query.datimAgeGroup,
genders: query.gender
}

return await ahdAssessment.getRawOne();
return await ahdAssessment.setParameters(params).getRawOne();
}
}
122 changes: 67 additions & 55 deletions src/care-treatment/ahd/queries/handlers/get-ahd-screening.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,138 +12,150 @@ export class GetAhdScreeningHandler implements IQueryHandler<GetAhdScreeningQuer
) {}

async execute(query: GetAhdScreeningQuery): Promise<any> {
const filters = [];
if (query.county) {
filters.push('County In (:...counties)')
}

if (query.subCounty) {
filters.push('SubCounty In (:...subCounties)')
}

if (query.facility) {
filters.push('FacilityName In (:...facilities)')
}

if (query.partner) {
filters.push('PartnerName In (:...partners)')
}

if (query.agency) {
filters.push('AgencyName In (:...agencies)')
}

if (query.datimAgeGroup) {
filters.push('AgeGroup In (:...ageGroups)')
}

if (query.gender) {
filters.push('Gender In (:...genders)')
}

const whereClause = filters.length ? `WHERE ${filters.join(' AND ')}` : ``;

const ahdScreening = this.repository
.createQueryBuilder('f')
.select([`TOP 1
(SELECT
SUM(CASE WHEN IsRTTLast12MonthsAfter3monthsIIT = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN ConfirmedTreatmentFailure = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN NewPatient = 1 THEN 1 ELSE 0 END)
FROM REPORTING.dbo.linelist_FactART) AS NewPatient,
FROM REPORTING.dbo.linelist_FactART ${whereClause} ) AS NewPatient,
(SELECT
SUM(CASE WHEN IsRTTLast12MonthsAfter3monthsIIT = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN ConfirmedTreatmentFailure = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN NewPatient = 1 THEN 1 ELSE 0 END)
FROM REPORTING.dbo.linelist_FactART
WHERE (WhoStage IS NOT NULL) OR (LastCD4 IS NOT NULL)) AS AHDScreened,
${whereClause} ${filters.length ? ' AND ': 'WHERE '}
((WhoStage IS NOT NULL) OR (LastCD4 IS NOT NULL)) ) AS AHDScreened,
(SELECT
SUM(CASE WHEN IsRTTLast12MonthsAfter3monthsIIT = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN ConfirmedTreatmentFailure = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN NewPatient = 1 THEN 1 ELSE 0 END)
FROM REPORTING.dbo.linelist_FactART
WHERE AHD = 1) AS AHD,
${whereClause} ${filters.length ? ' AND ': 'WHERE '}
AHD = 1) AS AHD,
(SELECT
SUM(CASE WHEN IsRTTLast12MonthsAfter3monthsIIT = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN ConfirmedTreatmentFailure = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN NewPatient = 1 THEN 1 ELSE 0 END) AS TotalCount
FROM REPORTING.dbo.linelist_FactART
where LastCD4 is not null) AS DoneCD4Test,
${whereClause} ${filters.length ? ' AND ': 'WHERE '}
LastCD4 is not null) AS DoneCD4Test,
(SELECT
SUM(CASE WHEN IsRTTLast12MonthsAfter3monthsIIT = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN ConfirmedTreatmentFailure = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN NewPatient = 1 THEN 1 ELSE 0 END) AS TotalCount
FROM REPORTING.dbo.linelist_FactART
where CONVERT(float, LastCD4) < 200 OR TRY_CAST(LastCD4Percentage AS decimal) < 25) AS less200CD4,
${whereClause} ${filters.length ? ' AND ': 'WHERE '}
(CONVERT(float, LastCD4) < 200 OR TRY_CAST(LastCD4Percentage AS decimal) < 25) ) AS less200CD4,
(SELECT
SUM(CASE WHEN IsRTTLast12MonthsAfter3monthsIIT = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN ConfirmedTreatmentFailure = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN NewPatient = 1 THEN 1 ELSE 0 END) AS TotalCount
FROM REPORTING.dbo.linelist_FactART
where AHD=1 and DoneTBLamTest=1) AS DoneTBLamTest,
${whereClause} ${filters.length ? ' AND ': 'WHERE '}
AHD=1 and DoneTBLamTest=1) AS DoneTBLamTest,
(SELECT
SUM(CASE WHEN IsRTTLast12MonthsAfter3monthsIIT = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN ConfirmedTreatmentFailure = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN NewPatient = 1 THEN 1 ELSE 0 END) AS TotalCount
FROM REPORTING.dbo.linelist_FactART
where AHD=1 and DoneTBLamTest=1 and TBLamPositive=1) AS TBLamPositive,
${whereClause} ${filters.length ? ' AND ': 'WHERE '}
AHD=1 and DoneTBLamTest=1 and TBLamPositive=1) AS TBLamPositive,
(SELECT
SUM(CASE WHEN IsRTTLast12MonthsAfter3monthsIIT = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN ConfirmedTreatmentFailure = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN NewPatient = 1 THEN 1 ELSE 0 END) AS TotalCount
FROM REPORTING.dbo.linelist_FactART
where AHD=1 and DoneTBLamTest=1 and TBLamPositive=1 and TBLamPosonTBRx=1) AS tbInitiated,
${whereClause} ${filters.length ? ' AND ': 'WHERE '}
AHD=1 and DoneTBLamTest=1 and TBLamPositive=1 and TBLamPosonTBRx=1) AS tbInitiated,
(SELECT
SUM(CASE WHEN IsRTTLast12MonthsAfter3monthsIIT = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN ConfirmedTreatmentFailure = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN NewPatient = 1 THEN 1 ELSE 0 END) AS TotalCount
FROM REPORTING.dbo.linelist_FactART
where AHD=1 and DoneCrAgTest=1) AS DoneCrAgTest,
${whereClause} ${filters.length ? ' AND ': 'WHERE '}
AHD=1 and DoneCrAgTest=1) AS DoneCrAgTest,
(SELECT
SUM(CASE WHEN IsRTTLast12MonthsAfter3monthsIIT = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN ConfirmedTreatmentFailure = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN NewPatient = 1 THEN 1 ELSE 0 END) AS TotalCount
FROM REPORTING.dbo.linelist_FactART
where AHD=1 and DoneCrAgTest=1 and CrAgPositive=1) AS CrAgPositive,
${whereClause} ${filters.length ? ' AND ': 'WHERE '}
AHD=1 and DoneCrAgTest=1 and CrAgPositive=1) AS CrAgPositive,
(SELECT
SUM(CASE WHEN IsRTTLast12MonthsAfter3monthsIIT = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN ConfirmedTreatmentFailure = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN NewPatient = 1 THEN 1 ELSE 0 END) AS TotalCount
FROM REPORTING.dbo.linelist_FactART
where AHD=1 and CSFCrAg=1) AS CSFCrAg,
${whereClause} ${filters.length ? ' AND ': 'WHERE '}
AHD=1 and CSFCrAg=1) AS CSFCrAg,
(SELECT
SUM(CASE WHEN IsRTTLast12MonthsAfter3monthsIIT = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN ConfirmedTreatmentFailure = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN NewPatient = 1 THEN 1 ELSE 0 END) AS TotalCount
FROM REPORTING.dbo.linelist_FactART
where AHD=1 and CSFCrAg=1 and CSFCrAgPositive=1) AS CSFCrAgPositive,
${whereClause} ${filters.length ? ' AND ': 'WHERE '}
AHD=1 and CSFCrAg=1 and CSFCrAgPositive=1) AS CSFCrAgPositive,
(SELECT
SUM(CASE WHEN IsRTTLast12MonthsAfter3monthsIIT = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN ConfirmedTreatmentFailure = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN NewPatient = 1 THEN 1 ELSE 0 END) AS TotalCount
FROM REPORTING.dbo.linelist_FactART
where AHD=1 and CSFCrAg=1 and CSFCrAgPositive=1 and InitiatedCMTreatment=1) AS InitiatedCMTreatment,
${whereClause} ${filters.length ? ' AND ': 'WHERE '}
AHD=1 and CSFCrAg=1 and CSFCrAgPositive=1 and InitiatedCMTreatment=1) AS InitiatedCMTreatment,
(SELECT
SUM(CASE WHEN IsRTTLast12MonthsAfter3monthsIIT = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN ConfirmedTreatmentFailure = 1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN NewPatient = 1 THEN 1 ELSE 0 END) AS TotalCount
FROM REPORTING.dbo.linelist_FactART
where AHD=1 and CSFCrAg=1 and CSFCrAgPositive=1 and InitiatedCMTreatment=1 and PreemtiveCMTheraphy=1) AS PreemtiveCMTheraphy
${whereClause} ${filters.length ? ' AND ': 'WHERE '}
AHD=1 and CSFCrAg=1 and CSFCrAgPositive=1 and InitiatedCMTreatment=1 and PreemtiveCMTheraphy=1) AS PreemtiveCMTheraphy
`])

if (query.county) {
ahdScreening.andWhere('f.County IN (:...counties)', {
counties: query.county,
});
}

if (query.subCounty) {
ahdScreening.andWhere('f.Subcounty IN (:...subCounties)', {
subCounties: query.subCounty,
});
}

if (query.facility) {
ahdScreening.andWhere('f.FacilityName IN (:...facilities)', {
facilities: query.facility,
});
}

if (query.partner) {
ahdScreening.andWhere('f.PartnerName IN (:...partners)', {
partners: query.partner,
});
}

if (query.agency) {
ahdScreening.andWhere('f.AgencyName IN (:...agencies)', {
agencies: query.agency,
});
}

if (query.datimAgeGroup) {
ahdScreening.andWhere('f.AgeGroup IN (:...ageGroups)', {
ageGroups: query.datimAgeGroup,
});
}

if (query.gender) {
ahdScreening.andWhere('f.Gender IN (:...genders)', {
genders: query.gender,
});
const params = {
facilities: query.facility,
counties: query.county,
subCounties: query.subCounty,
partners: query.partner,
agencies: query.agency,
ageGroups: query.datimAgeGroup,
genders: query.gender
}

return await ahdScreening.getRawOne();
return await ahdScreening.setParameters(params).getRawOne();
}
}

0 comments on commit e4ff993

Please sign in to comment.