Skip to content

Commit

Permalink
Merge pull request #746 from palladiumkenya/feat/new-reporting-layer
Browse files Browse the repository at this point in the history
Fixed hts partner bug
  • Loading branch information
cbrianbet authored Jun 12, 2024
2 parents 47991db + 051cfff commit 5feea09
Show file tree
Hide file tree
Showing 19 changed files with 6,977 additions and 12,122 deletions.
14,378 changes: 5,300 additions & 9,078 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@nestjs/config": "^3.2.0",
"@nestjs/core": "10.3.3",
"@nestjs/cqrs": "^10.2.7",
"@nestjs/platform-express": "^10.3.3",
"@nestjs/platform-express": "^10.3.9",
"@nestjs/typeorm": "^10.0.2",
"@types/lodash": "^4.14.149",
"axios": "^1.0.0",
Expand All @@ -54,7 +54,7 @@
"uuid": "^8.1.0"
},
"devDependencies": {
"@nestjs/cli": "^7.0.0",
"@nestjs/cli": "^10.3.2",
"@nestjs/schematics": "^10.1.1",
"@nestjs/testing": "^10.3.3",
"@types/express": "^4.17.3",
Expand All @@ -67,11 +67,11 @@
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-import": "^2.20.1",
"jest": "^25.1.0",
"jest": "^29.7.0",
"prettier": "^1.19.1",
"sqlite3": "^5.0.0",
"supertest": "^4.0.2",
"ts-jest": "25.2.1",
"ts-jest": "29.1.2",
"ts-loader": "^6.2.1",
"ts-node": "^10.7.0",
"tsconfig-paths": "^3.9.0",
Expand Down
29 changes: 29 additions & 0 deletions src/common/common.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { GetFacilityLevelByOwnershipPartnerQuery } from './queries/impl/get-faci
import { GetFacilityLevelByOwnershipCountyQuery } from './queries/impl/get-facility-level-by-ownership-county.query';
import { GetFacilityByInfrastructureQuery } from './queries/impl/get-facility-by-infrastructure.query';
import { GetFacilityLinelistQuery } from './queries/impl/get-facility-linelist.query';
import { GetFacilityTxcurrQuery } from './queries/impl/get-facility-txcurr.query';

@Controller('common')
export class CommonController {
Expand Down Expand Up @@ -367,6 +368,34 @@ export class CommonController {
return this.queryBus.execute(query);
}


@Get('facilityTxcurr')
async getFacilityTxCurr(
@Query('county') county,
@Query('subCounty') subCounty,
@Query('facility') facility,
@Query('partner') partner,
@Query('agency') agency,
): Promise<any> {
const query = new GetFacilityTxcurrQuery();
if(county) {
query.county = county;
}
if(subCounty) {
query.subCounty = subCounty;
}
if(facility) {
query.facility = facility;
}
if(partner) {
query.partner = partner;
}
if(agency) {
query.agency = agency;
}
return this.queryBus.execute(query);
}

@Get('sites')
async getSites(): Promise<any> {
return this.queryBus.execute(new GetSitesQuery());
Expand Down
5 changes: 4 additions & 1 deletion src/common/common.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ import { GetFacilityLevelByOwnershipPartnerHandler } from './queries/handlers/ge
import { GetFacilityLevelByOwnershipCountyHandler } from './queries/handlers/get-facility-level-by-ownership-county.handler';
import { GetFacilityByInfrastructureHandler } from './queries/handlers/get-facility-by-infrastructure.handler';
import { GetFacilityLinelistHandler } from './queries/handlers/get-facility-linelist.handler';
import { GetFacilityTxcurrHandler } from './queries/handlers/get-facility-txcurr.handler';
import { CommonController } from './common.controller';
import { AllEmrSites } from '../care-treatment/common/entities/all-emr-sites.model';
import { LinelistFACTART } from '../care-treatment/common/entities/linelist-fact-art.model';

@Module({
imports: [
CqrsModule,
ConfigurationModule,
TypeOrmModule.forFeature([AllEmrSites], 'mssql')
TypeOrmModule.forFeature([AllEmrSites, LinelistFACTART], 'mssql')
],
providers: [
GetCountiesHandler,
Expand All @@ -36,6 +38,7 @@ import { AllEmrSites } from '../care-treatment/common/entities/all-emr-sites.mod
GetFacilityLevelByOwnershipCountyHandler,
GetFacilityByInfrastructureHandler,
GetFacilityLinelistHandler,
GetFacilityTxcurrHandler,
],
controllers: [CommonController],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export class GetFacilityStatusByPartnerHandler implements IQueryHandler<GetFacil
});
}


return await facilitiesStatus
.groupBy('q.EMR_Status, PartnerName')
.orderBy('PartnerName')
Expand Down
38 changes: 38 additions & 0 deletions src/common/queries/handlers/get-facility-txcurr.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { GetFacilityTxcurrQuery } from '../impl/get-facility-txcurr.query';
import { LinelistFACTART } from '../../../care-treatment/common/entities/linelist-fact-art.model';

@QueryHandler(GetFacilityTxcurrQuery)
export class GetFacilityTxcurrHandler implements IQueryHandler<GetFacilityTxcurrQuery> {
constructor(
@InjectRepository(LinelistFACTART, 'mssql')
private readonly repository: Repository<LinelistFACTART>,
) {}

async execute(query: GetFacilityTxcurrQuery): Promise<any> {
const facilities = this.repository
.createQueryBuilder('q')
.select('count (*) TxCurr, KEPH_Level')
.leftJoin('all_EMRSites', 'e', 'e.MFLCode = q.SiteCode')
.where(`ARTOutcomeDescription='Active'`);

if (query.county) {
facilities.andWhere('q.County IN (:...county)', {
county: [query.county],
});
}

if (query.subCounty) {
facilities.andWhere('q.SubCounty IN (:...subCounty)', {
subCounty: [query.subCounty],
});
}


return await facilities
.groupBy('KEPH_Level')
.getRawMany();
}
}
7 changes: 7 additions & 0 deletions src/common/queries/impl/get-facility-txcurr.query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class GetFacilityTxcurrQuery {
county?: string[];
subCounty?: string[];
facility?: string[];
partner?: string[];
agency?: string[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export class GetUptakeByAgeSexHandler
uptakeByAgeSexSql = `${uptakeByAgeSexSql} and PartnerName IN ('${query.partner
.toString()
.replace(/,/g, "','")}')`;
params.push(query.partner);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export class GetUptakeByCountyHandler
uptakeByCountySql = `${uptakeByCountySql} and PartnerName IN ('${query.partner
.toString()
.replace(/,/g, "','")}')`;
params.push(query.partner);
}

// if(query.month) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class GetUptakeByEntrypointHandler
uptakeByEntryPointSql = `${uptakeByEntryPointSql} and PartnerName IN ('${query.partner
.toString()
.replace(/,/g, "','")}')`;
params.push(query.partner);
}

// if(query.month) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class GetUptakeByMonthsSinceLastTestHandler
uptakeByPopulationTypeSql = `${uptakeByPopulationTypeSql} and PartnerName IN ('${query.partner
.toString()
.replace(/,/g, "','")}')`;
params.push(query.partner);
}

// if(query.month) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class GetUptakeByPartnerHandler
uptakeByPartnerSql = `${uptakeByPartnerSql} and PartnerName IN ('${query.partner
.toString()
.replace(/,/g, "','")}')`;
params.push(query.partner);
}

if (query.fromDate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export class GetUptakeByPopulationTypeHandler implements IQueryHandler<GetUptake
uptakeByPopulationTypeSql = `${uptakeByPopulationTypeSql} and PartnerName IN ('${query.partner
.toString()
.replace(/,/g, "','")}')`;
params.push(query.partner);
}

if(query.month) {
Expand Down
59 changes: 28 additions & 31 deletions src/hts/uptake/queries/handlers/get-uptake-by-sex.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,44 @@ import { IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import { GetUptakeBySexQuery } from '../impl/get-uptake-by-sex.query';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { FactHTSClientTests } from './../../../linkage/entities/fact-hts-client-tests.model';
import { AggregateHTSUptake } from '../../entities/aggregate-hts-uptake.model';

@QueryHandler(GetUptakeBySexQuery)
export class GetUptakeBySexHandler
implements IQueryHandler<GetUptakeBySexQuery> {
constructor(
@InjectRepository(FactHTSClientTests, 'mssql')
private readonly repository: Repository<FactHTSClientTests>,
@InjectRepository(AggregateHTSUptake, 'mssql')
private readonly repository: Repository<AggregateHTSUptake>,
) {}

async execute(query: GetUptakeBySexQuery): Promise<any> {
const params = [];
let uptakeBySexSql = `SELECT
CASE WHEN Gender = 'M' THEN 'Male' WHEN Gender = 'F' THEN 'Female' ELSE Gender END gender,

let uptakeBySexSql = this.repository
.createQueryBuilder('q')
.select(
`Gender gender,
SUM(Tested) tested,
SUM(Positive) positive,
((CAST(SUM(CASE WHEN positive IS NULL THEN 0 ELSE positive END) AS FLOAT)/CAST(SUM(Tested) AS FLOAT))*100) AS positivity
FROM
NDWH.dbo.FactHTSClientTests AS link
LEFT JOIN NDWH.dbo.DimPatient AS pat ON link.PatientKey = pat.PatientKey
LEFT JOIN NDWH.dbo.DimAgeGroup AS age ON link.AgeGroupKey = age.AgeGroupKey
LEFT JOIN NDWH.dbo.DimPartner AS part ON link.PartnerKey = part.PartnerKey
LEFT JOIN NDWH.dbo.DimFacility AS fac ON link.FacilityKey = fac.FacilityKey
LEFT JOIN NDWH.dbo.DimAgency AS agency ON link.AgencyKey = agency.AgencyKey
WHERE Tested > 0 and TestType IN ('Initial', 'Initial Test')`;
((CAST(SUM(CASE WHEN positive IS NULL THEN 0 ELSE positive END) AS FLOAT)/CAST(SUM(Tested) AS FLOAT))*100) AS positivity`
)
.where(`Tested > 0`)

if (query.county) {
uptakeBySexSql = `${uptakeBySexSql} and County IN ('${query.county
.toString()
.replace(/,/g, "','")}')`
uptakeBySexSql.andWhere('County IN (:...counties)', { counties: query.county });
}

if (query.subCounty) {
uptakeBySexSql = `${uptakeBySexSql} and SubCounty IN ('${query.subCounty
.toString()
.replace(/,/g, "','")}')`
uptakeBySexSql.andWhere('SubCounty IN (:...subCounties)', { subCounties: query.subCounty });
}

if (query.facility) {
uptakeBySexSql = `${uptakeBySexSql} and FacilityName IN ('${query.facility
.toString()
.replace(/,/g, "','")}')`
uptakeBySexSql.andWhere('FacilityName IN (:...facilities)', { facilities: query.facility });
}


if (query.partner) {
uptakeBySexSql = `${uptakeBySexSql} and PartnerName IN ('${query.partner
.toString()
.replace(/,/g, "','")}')`;
params.push(query.partner);
uptakeBySexSql.andWhere('PartnerName IN (:...partners)', { partners: query.partner });
}

// if(query.month) {
Expand All @@ -64,14 +53,22 @@ export class GetUptakeBySexHandler
// }

if (query.fromDate) {
uptakeBySexSql = `${uptakeBySexSql} and DateTestedKey >= ${query.fromDate}01`;
let year = `${query.fromDate}`.substring(0, 4); // Extract year
let month = `${query.fromDate}`.substring(4, 6); // Extract month
let formattedDate = `${year}-${month}-01`;
uptakeBySexSql.andWhere('AsOfDate >= :fromDate', { fromDate: formattedDate });
}

if (query.toDate) {
uptakeBySexSql = `${uptakeBySexSql} and DateTestedKey <= EOMONTH('${query.toDate}01')`;
let toDate = `${query.toDate}`;
let year = toDate.substring(0, 4);
let month = toDate.substring(4, 6);
let formattedDate = `${year}-${month}-01`;
uptakeBySexSql.andWhere('AsOfDate <= EOMONTH(:toDate)', { toDate: formattedDate });
}

uptakeBySexSql = `${uptakeBySexSql} GROUP BY CASE WHEN Gender = 'M' THEN 'Male' WHEN Gender = 'F' THEN 'Female' ELSE Gender END`;
return await this.repository.query(uptakeBySexSql, params);
return await uptakeBySexSql
.groupBy('Gender')
.getRawMany();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class GetUptakeByTbScreenedHandler
uptakeByTBScreenedSql = `${uptakeByTBScreenedSql} and PartnerName IN ('${query.partner
.toString()
.replace(/,/g, "','")}')`;
params.push(query.partner);
}

// if(query.month) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export class GetUptakeByTBScreeningHandler
uptakeByClientTestedAsSql = `${uptakeByClientTestedAsSql} and PartnerName IN ('${query.partner
.toString()
.replace(/,/g, "','")}')`;
params.push(query.partner);
}

if (query.facility) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class GetUptakeByTestedasHandler
uptakeByClientTestedAsSql = `${uptakeByClientTestedAsSql} and PartnerName IN ('${query.partner
.toString()
.replace(/,/g, "','")}')`;
params.push(query.partner);
}

// if(query.month) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class GetUptakeByTestingStrategyHandler
uptakeByPopulationTypeSql = `${uptakeByPopulationTypeSql} and PartnerName IN ('${query.partner
.toString()
.replace(/,/g, "','")}')`;
params.push(query.partner);
}

// if(query.month) {
Expand Down
Loading

0 comments on commit 5feea09

Please sign in to comment.