diff --git a/package-lock.json b/package-lock.json index fbfc4cf8..bf3d80e6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29,6 +29,7 @@ "fs": "^0.0.1-security", "helmet": "^3.22.0", "joi": "^17.12.2", + "jsonwebtoken": "^9.0.2", "lodash": "^4.17.21", "moment": "^2.27.0", "mssql": "^10.0.2", diff --git a/src/common/common.controller.ts b/src/common/common.controller.ts index d8c1df21..c2122afc 100644 --- a/src/common/common.controller.ts +++ b/src/common/common.controller.ts @@ -7,6 +7,12 @@ import { GetPartnersQuery } from './queries/impl/get-partners.query'; import { GetAgenciesQuery } from './queries/impl/get-agencies.query'; import { GetProjectsQuery } from './queries/impl/get-projects.query'; import { GetSitesQuery } from './queries/impl/get-sites.query'; +import { GetFacilityStatusQuery } from './queries/impl/get-facility-status.query'; +import { GetFacilityStatusByPartnerQuery } from './queries/impl/get-facility-status-by-partner.query'; +import { GetFacilityLevelByOwnershipPartnerQuery } from './queries/impl/get-facility-level-by-ownership-partner.query'; +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'; @Controller('common') export class CommonController { @@ -198,6 +204,169 @@ export class CommonController { return this.queryBus.execute(query); } + @Get('facilityStatus') + async getFacilityStatus( + @Query('county') county, + @Query('subCounty') subCounty, + @Query('facility') facility, + @Query('partner') partner, + @Query('agency') agency, + ): Promise { + const query = new GetFacilityStatusQuery(); + 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('facilityStatusByPartner') + async getFacilityStatusByPartner( + @Query('county') county, + @Query('subCounty') subCounty, + @Query('facility') facility, + @Query('partner') partner, + @Query('agency') agency, + ): Promise { + const query = new GetFacilityStatusByPartnerQuery(); + 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('facilityLevelByOwnershipPartner') + async getFacilityLevelByOwnershipPartner( + @Query('county') county, + @Query('subCounty') subCounty, + @Query('facility') facility, + @Query('partner') partner, + @Query('agency') agency, + ): Promise { + const query = new GetFacilityLevelByOwnershipPartnerQuery(); + 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('facilityLevelByOwnershipCounty') + async getFacilityLevelByOwnershipCounty( + @Query('county') county, + @Query('subCounty') subCounty, + @Query('facility') facility, + @Query('partner') partner, + @Query('agency') agency, + ): Promise { + const query = new GetFacilityLevelByOwnershipCountyQuery(); + 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('facilityByInfrastructure') + async getFacilityByInfrastructure( + @Query('county') county, + @Query('subCounty') subCounty, + @Query('facility') facility, + @Query('partner') partner, + @Query('agency') agency, + ): Promise { + const query = new GetFacilityByInfrastructureQuery(); + 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('facilityLinelist') + async getFacilityLinelist( + @Query('county') county, + @Query('subCounty') subCounty, + @Query('facility') facility, + @Query('partner') partner, + @Query('agency') agency, + ): Promise { + const query = new GetFacilityLinelistQuery(); + 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 { return this.queryBus.execute(new GetSitesQuery()); diff --git a/src/common/common.module.ts b/src/common/common.module.ts index 527992c4..e129887f 100644 --- a/src/common/common.module.ts +++ b/src/common/common.module.ts @@ -8,6 +8,12 @@ import { GetFacilitiesHandler } from './queries/handlers/get-facilities.handler' import { GetPartnersHandler } from './queries/handlers/get-partners.handler'; import { GetAgenciesHandler } from './queries/handlers/get-agencies.handler'; import { GetSitesHandler } from './queries/handlers/get-sites.handler'; +import { GetFacilityStatusHandler } from './queries/handlers/get-facility-status.handler'; +import { GetFacilityStatusByPartnerHandler } from './queries/handlers/get-facility-status-by-partner.handler'; +import { GetFacilityLevelByOwnershipPartnerHandler } from './queries/handlers/get-facility-level-by-ownership-partner.handler'; +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 { CommonController } from './common.controller'; import { AllEmrSites } from '../care-treatment/common/entities/all-emr-sites.model'; @@ -24,6 +30,12 @@ import { AllEmrSites } from '../care-treatment/common/entities/all-emr-sites.mod GetPartnersHandler, GetAgenciesHandler, GetSitesHandler, + GetFacilityStatusHandler, + GetFacilityStatusByPartnerHandler, + GetFacilityLevelByOwnershipPartnerHandler, + GetFacilityLevelByOwnershipCountyHandler, + GetFacilityByInfrastructureHandler, + GetFacilityLinelistHandler, ], controllers: [CommonController], }) diff --git a/src/common/queries/handlers/get-facility-by-infrastructure.handler.ts b/src/common/queries/handlers/get-facility-by-infrastructure.handler.ts new file mode 100644 index 00000000..aa66b828 --- /dev/null +++ b/src/common/queries/handlers/get-facility-by-infrastructure.handler.ts @@ -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 { GetFacilityByInfrastructureQuery } from '../impl/get-facility-by-infrastructure.query'; + +@QueryHandler(GetFacilityByInfrastructureQuery) +export class GetFacilityByInfrastructureHandler implements IQueryHandler { + constructor( + @InjectRepository(AllEmrSites, 'mssql') + private readonly repository: Repository, + ) {} + + async execute(query: GetFacilityByInfrastructureQuery): Promise { + const projects = this.repository + .createQueryBuilder('q') + .select('COUNT(1) facilities, InfrastructureType, PartnerName') + .where('InfrastructureType is not null'); + + if (query.county) { + projects.andWhere('q.County IN (:...county)', { + county: [query.county], + }); + } + + if (query.subCounty) { + projects.andWhere('q.SubCounty IN (:...subCounty)', { + subCounty: [query.subCounty], + }); + } + + + return await projects + .groupBy('q.InfrastructureType, PartnerName') + .orderBy('PartnerName') + .distinct(true) + .getRawMany(); + } +} diff --git a/src/common/queries/handlers/get-facility-level-by-ownership-county.handler.ts b/src/common/queries/handlers/get-facility-level-by-ownership-county.handler.ts new file mode 100644 index 00000000..5dddf2cd --- /dev/null +++ b/src/common/queries/handlers/get-facility-level-by-ownership-county.handler.ts @@ -0,0 +1,40 @@ +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 { GetFacilityLevelByOwnershipCountyQuery } from '../impl/get-facility-level-by-ownership-county.query'; + +@QueryHandler(GetFacilityLevelByOwnershipCountyQuery) +export class GetFacilityLevelByOwnershipCountyHandler implements IQueryHandler { + constructor( + @InjectRepository(AllEmrSites, 'mssql') + private readonly repository: Repository, + ) {} + + async execute(query: GetFacilityLevelByOwnershipCountyQuery): Promise { + const projects = this.repository + .createQueryBuilder('q') + .select('COUNT(1) facilities, County, Keph_level') + .where('Keph_level is not null'); + + if (query.county) { + projects.andWhere('q.County IN (:...county)', { + county: [query.county], + }); + } + + if (query.subCounty) { + projects.andWhere('q.SubCounty IN (:...subCounty)', { + subCounty: [query.subCounty], + }); + } + + + return await projects + .groupBy('q.County, Keph_level') + .orderBy('Keph_level') + .orderBy('County') + .distinct(true) + .getRawMany(); + } +} diff --git a/src/common/queries/handlers/get-facility-level-by-ownership-partner.handler.ts b/src/common/queries/handlers/get-facility-level-by-ownership-partner.handler.ts new file mode 100644 index 00000000..1bfd681e --- /dev/null +++ b/src/common/queries/handlers/get-facility-level-by-ownership-partner.handler.ts @@ -0,0 +1,40 @@ +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 { GetFacilityLevelByOwnershipPartnerQuery } from '../impl/get-facility-level-by-ownership-partner.query'; + +@QueryHandler(GetFacilityLevelByOwnershipPartnerQuery) +export class GetFacilityLevelByOwnershipPartnerHandler implements IQueryHandler { + constructor( + @InjectRepository(AllEmrSites, 'mssql') + private readonly repository: Repository, + ) {} + + async execute(query: GetFacilityLevelByOwnershipPartnerQuery): Promise { + const projects = this.repository + .createQueryBuilder('q') + .select('COUNT(1) facilities, PartnerName, Keph_level') + .where('Keph_level is not null'); + + if (query.county) { + projects.andWhere('q.County IN (:...county)', { + county: [query.county], + }); + } + + if (query.subCounty) { + projects.andWhere('q.SubCounty IN (:...subCounty)', { + subCounty: [query.subCounty], + }); + } + + + return await projects + .groupBy('q.PartnerName, Keph_level') + .orderBy('Keph_level') + .orderBy('PartnerName') + .distinct(true) + .getRawMany(); + } +} diff --git a/src/common/queries/handlers/get-facility-linelist.handler.ts b/src/common/queries/handlers/get-facility-linelist.handler.ts new file mode 100644 index 00000000..8f356ab3 --- /dev/null +++ b/src/common/queries/handlers/get-facility-linelist.handler.ts @@ -0,0 +1,41 @@ +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 { GetFacilityLinelistQuery } from '../impl/get-facility-linelist.query'; + +@QueryHandler(GetFacilityLinelistQuery) +export class GetFacilityLinelistHandler implements IQueryHandler { + constructor( + @InjectRepository(AllEmrSites, 'mssql') + private readonly repository: Repository, + ) {} + + async execute(query: GetFacilityLinelistQuery): Promise { + const projects = this.repository + .createQueryBuilder('q') + .select( + `MFLCode, FacilityName, SubCounty, County, + [PartnerName], [AgencyName],[EMR_Status],[EMR] + ,[owner],[InfrastructureType], [KEPH_Level]` + ); + + if (query.county) { + projects.andWhere('q.County IN (:...county)', { + county: [query.county], + }); + } + + if (query.subCounty) { + projects.andWhere('q.SubCounty IN (:...subCounty)', { + subCounty: [query.subCounty], + }); + } + + + return await projects + .orderBy('MFLCode') + .distinct(true) + .getRawMany(); + } +} diff --git a/src/common/queries/handlers/get-facility-status-by-partner.handler.ts b/src/common/queries/handlers/get-facility-status-by-partner.handler.ts new file mode 100644 index 00000000..51a282c9 --- /dev/null +++ b/src/common/queries/handlers/get-facility-status-by-partner.handler.ts @@ -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 { GetFacilityStatusByPartnerQuery } from '../impl/get-facility-status-by-partner.query'; + +@QueryHandler(GetFacilityStatusByPartnerQuery) +export class GetFacilityStatusByPartnerHandler implements IQueryHandler { + constructor( + @InjectRepository(AllEmrSites, 'mssql') + private readonly repository: Repository, + ) {} + + async execute(query: GetFacilityStatusByPartnerQuery): Promise { + const projects = this.repository + .createQueryBuilder('q') + .select('COUNT(1) facilities, EMR_Status, PartnerName') + .where('EMR_Status is not null'); + + if (query.county) { + projects.andWhere('q.County IN (:...county)', { + county: [query.county], + }); + } + + if (query.subCounty) { + projects.andWhere('q.SubCounty IN (:...subCounty)', { + subCounty: [query.subCounty], + }); + } + + + return await projects + .groupBy('q.EMR_Status, PartnerName') + .orderBy('PartnerName') + .distinct(true) + .getRawMany(); + } +} diff --git a/src/common/queries/handlers/get-facility-status.handler.ts b/src/common/queries/handlers/get-facility-status.handler.ts new file mode 100644 index 00000000..5b3a320d --- /dev/null +++ b/src/common/queries/handlers/get-facility-status.handler.ts @@ -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 { GetFacilityStatusQuery } from '../impl/get-facility-status.query'; + +@QueryHandler(GetFacilityStatusQuery) +export class GetFacilityStatusHandler implements IQueryHandler { + constructor( + @InjectRepository(AllEmrSites, 'mssql') + private readonly repository: Repository, + ) {} + + async execute(query: GetFacilityStatusQuery): Promise { + const projects = this.repository + .createQueryBuilder('q') + .select('COUNT(1) facilities, EMR_Status') + .where('EMR_Status is not null'); + + if (query.county) { + projects.andWhere('q.County IN (:...county)', { + county: [query.county], + }); + } + + if (query.subCounty) { + projects.andWhere('q.SubCounty IN (:...subCounty)', { + subCounty: [query.subCounty], + }); + } + + + return await projects + .groupBy('q.EMR_Status') + .distinct(true) + .getRawMany(); + } +} diff --git a/src/common/queries/impl/get-facility-by-infrastructure.query.ts b/src/common/queries/impl/get-facility-by-infrastructure.query.ts new file mode 100644 index 00000000..f3a427bb --- /dev/null +++ b/src/common/queries/impl/get-facility-by-infrastructure.query.ts @@ -0,0 +1,7 @@ +export class GetFacilityByInfrastructureQuery { + county?: string[]; + subCounty?: string[]; + facility?: string[]; + partner?: string[]; + agency?: string[]; +} \ No newline at end of file diff --git a/src/common/queries/impl/get-facility-level-by-ownership-county.query.ts b/src/common/queries/impl/get-facility-level-by-ownership-county.query.ts new file mode 100644 index 00000000..481a9098 --- /dev/null +++ b/src/common/queries/impl/get-facility-level-by-ownership-county.query.ts @@ -0,0 +1,7 @@ +export class GetFacilityLevelByOwnershipCountyQuery { + county?: string[]; + subCounty?: string[]; + facility?: string[]; + partner?: string[]; + agency?: string[]; +} \ No newline at end of file diff --git a/src/common/queries/impl/get-facility-level-by-ownership-partner.query.ts b/src/common/queries/impl/get-facility-level-by-ownership-partner.query.ts new file mode 100644 index 00000000..bd2ee834 --- /dev/null +++ b/src/common/queries/impl/get-facility-level-by-ownership-partner.query.ts @@ -0,0 +1,7 @@ +export class GetFacilityLevelByOwnershipPartnerQuery { + county?: string[]; + subCounty?: string[]; + facility?: string[]; + partner?: string[]; + agency?: string[]; +} \ No newline at end of file diff --git a/src/common/queries/impl/get-facility-linelist.query.ts b/src/common/queries/impl/get-facility-linelist.query.ts new file mode 100644 index 00000000..133b3624 --- /dev/null +++ b/src/common/queries/impl/get-facility-linelist.query.ts @@ -0,0 +1,7 @@ +export class GetFacilityLinelistQuery { + county?: string[]; + subCounty?: string[]; + facility?: string[]; + partner?: string[]; + agency?: string[]; +} \ No newline at end of file diff --git a/src/common/queries/impl/get-facility-status-by-partner.query.ts b/src/common/queries/impl/get-facility-status-by-partner.query.ts new file mode 100644 index 00000000..126e9445 --- /dev/null +++ b/src/common/queries/impl/get-facility-status-by-partner.query.ts @@ -0,0 +1,7 @@ +export class GetFacilityStatusByPartnerQuery { + county?: string[]; + subCounty?: string[]; + facility?: string[]; + partner?: string[]; + agency?: string[]; +} \ No newline at end of file diff --git a/src/common/queries/impl/get-facility-status.query.ts b/src/common/queries/impl/get-facility-status.query.ts new file mode 100644 index 00000000..3a1066ef --- /dev/null +++ b/src/common/queries/impl/get-facility-status.query.ts @@ -0,0 +1,7 @@ +export class GetFacilityStatusQuery { + county?: string[]; + subCounty?: string[]; + facility?: string[]; + partner?: string[]; + agency?: string[]; +} \ No newline at end of file