Skip to content

Commit

Permalink
Merge pull request #747 from cbrianbet/feat/new-reporting-layer
Browse files Browse the repository at this point in the history
added county apis to his deployment
  • Loading branch information
cbrianbet authored Jun 12, 2024
2 parents 051cfff + 45cc6bb commit 5b2c812
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 66 deletions.
58 changes: 58 additions & 0 deletions src/common/common.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { GetFacilityLevelByOwnershipCountyQuery } from './queries/impl/get-facil
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';
import { GetFacilityStatusByCountyQuery } from './queries/impl/get-facility-status-by-county.query';
import { GetFacilityByInfrastructureCountyQuery } from './queries/impl/get-facility-by-infrastructure-county.query';

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


@Get('facilityByInfrastructureCounty')
async getFacilityByInfrastructureCounty(
@Query('county') county,
@Query('subCounty') subCounty,
@Query('facility') facility,
@Query('partner') partner,
@Query('agency') agency,
): Promise<any> {
const query = new GetFacilityByInfrastructureCountyQuery();
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('facilityStatusByCounty')
async getFacilityStatusByCounty(
@Query('county') county,
@Query('subCounty') subCounty,
@Query('facility') facility,
@Query('partner') partner,
@Query('agency') agency,
): Promise<any> {
const query = new GetFacilityStatusByCountyQuery();
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
4 changes: 4 additions & 0 deletions src/common/common.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { GetFacilityLevelByOwnershipCountyHandler } from './queries/handlers/get
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 { GetFacilityStatusByCountyHandler } from './queries/handlers/get-facility-status-by-county.handler';
import { GetFacilityByInfrastructureCountyHandler } from './queries/handlers/get-facility-by-infrastructure-county.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';
Expand All @@ -39,6 +41,8 @@ import { LinelistFACTART } from '../care-treatment/common/entities/linelist-fact
GetFacilityByInfrastructureHandler,
GetFacilityLinelistHandler,
GetFacilityTxcurrHandler,
GetFacilityStatusByCountyHandler,
GetFacilityByInfrastructureCountyHandler
],
controllers: [CommonController],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { AllEmrSites } from '../../../care-treatment/common/entities/all-emr-sites.model';
import { GetFacilityByInfrastructureCountyQuery } from '../impl/get-facility-by-infrastructure-county.query';

@QueryHandler(GetFacilityByInfrastructureCountyQuery)
export class GetFacilityByInfrastructureCountyHandler implements IQueryHandler<GetFacilityByInfrastructureCountyQuery> {
constructor(
@InjectRepository(AllEmrSites, 'mssql')
private readonly repository: Repository<AllEmrSites>,
) {}

async execute(query: GetFacilityByInfrastructureCountyQuery): Promise<any> {
const facilitiesInfrastructure = this.repository
.createQueryBuilder('q')
.select('COUNT(1) facilities, InfrastructureType, County')
.where(`InfrastructureType is not null AND EMR_Status = 'Active'`);

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

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


return await facilitiesInfrastructure
.groupBy('q.InfrastructureType, County')
.orderBy('County')
.distinct(true)
.getRawMany();
}
}
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 { AllEmrSites } from '../../../care-treatment/common/entities/all-emr-sites.model';
import { GetFacilityStatusByCountyQuery } from '../impl/get-facility-status-by-county.query';

@QueryHandler(GetFacilityStatusByCountyQuery)
export class GetFacilityStatusByCountyHandler implements IQueryHandler<GetFacilityStatusByCountyQuery> {
constructor(
@InjectRepository(AllEmrSites, 'mssql')
private readonly repository: Repository<AllEmrSites>,
) {}

async execute(query: GetFacilityStatusByCountyQuery): Promise<any> {
const facilitiesStatus = this.repository
.createQueryBuilder('q')
.select('COUNT(1) facilities, EMR_Status, County')
.where('EMR_Status is not null');

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

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

return await facilitiesStatus
.groupBy('q.EMR_Status, County')
.orderBy('County')
.distinct(true)
.getRawMany();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class GetFacilityByInfrastructureCountyQuery {
county?: string[];
subCounty?: string[];
facility?: string[];
partner?: string[];
agency?: string[];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class GetFacilityStatusByCountyQuery {
county?: string[];
subCounty?: string[];
facility?: string[];
partner?: string[];
agency?: string[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,60 +13,47 @@ export class GetUptakeByAgeSexPositivityHandler
) {}

async execute(query: GetUptakeByAgeSexPositivityQuery): Promise<any> {
const params = [];
let uptakeByAgeSexSql = `SELECT
DATIMAgeGroup AgeGroup,
((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')`;

let uptakeByAgeSex = this.repository
.createQueryBuilder(`q`)
.select(`AgeGroup, ((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) {
uptakeByAgeSexSql = `${uptakeByAgeSexSql} and County IN ('${query.county
.toString()
.replace(/,/g, "','")}')`
uptakeByAgeSex.andWhere('County IN (:...counties)', { counties: query.county });
}

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

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

if (query.partner) {
uptakeByAgeSexSql = `${uptakeByAgeSexSql} and PartnerName IN ('${query.partner
.toString()
.replace(/,/g, "','")}')`
}

if (query.month) {
uptakeByAgeSexSql = `${uptakeByAgeSexSql} and month(DateTestedKey) = ${query.month}`;
if (query.partner) {
uptakeByAgeSex.andWhere('PartnerName IN (:...partners)', { partners: query.partner });
}

if (query.year) {
uptakeByAgeSexSql = `${uptakeByAgeSexSql} and year(DateTestedKey) = ${query.year}`;
}

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

if (query.toDate) {
uptakeByAgeSexSql = `${uptakeByAgeSexSql} 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`;
uptakeByAgeSex.andWhere('AsOfDate <= EOMONTH(:toDate)', { toDate: formattedDate });
}

uptakeByAgeSexSql = `${uptakeByAgeSexSql} GROUP BY DATIMAgeGroup`;
return await this.repository.query(uptakeByAgeSexSql, params);
return await uptakeByAgeSex
.groupBy(`AgeGroup`)
.getRawMany();
}
}
55 changes: 24 additions & 31 deletions src/hts/uptake/queries/handlers/get-uptake-by-age-sex.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,58 @@ import { GetUptakeByAgeSexQuery } from '../impl/get-uptake-by-age-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(GetUptakeByAgeSexQuery)
export class GetUptakeByAgeSexHandler
implements IQueryHandler<GetUptakeByAgeSexQuery> {
constructor(
@InjectRepository(FactHTSClientTests, 'mssql')
private readonly repository: Repository<FactHTSClientTests>,
@InjectRepository(AggregateHTSUptake, 'mssql')
private readonly repository: Repository<AggregateHTSUptake>,
) {}

async execute(query: GetUptakeByAgeSexQuery): Promise<any> {
const params = [];
let uptakeByAgeSexSql = `SELECT
DATIMAgeGroup AgeGroup,
CASE WHEN Gender = 'M' THEN 'Male' WHEN Gender = 'F' THEN 'Female' ELSE Gender END Gender,
SUM(Tested) Tested
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 is not null and TestType IN ('Initial', 'Initial Test')`;
let uptakeByAgeSex = this.repository
.createQueryBuilder(`q`)
.select(`AgeGroup, Gender, SUM(Tested) Tested`)
.where(`Tested is not null`);

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

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

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


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



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

if (query.toDate) {
uptakeByAgeSexSql = `${uptakeByAgeSexSql} 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`;
uptakeByAgeSex.andWhere('AsOfDate <= EOMONTH(:toDate)', { toDate: formattedDate });
}

uptakeByAgeSexSql = `${uptakeByAgeSexSql} GROUP BY DATIMAgeGroup, CASE WHEN Gender = 'M' THEN 'Male' WHEN Gender = 'F' THEN 'Female' ELSE Gender END`;
return await this.repository.query(uptakeByAgeSexSql, params);
return await uptakeByAgeSex
.groupBy(`AgeGroup, Gender`)
.getRawMany();
}
}

0 comments on commit 5b2c812

Please sign in to comment.