Skip to content

Commit

Permalink
Merge pull request #14 from koskedk/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
koskedk authored Jun 13, 2020
2 parents e500598 + 0945a91 commit 7f44f49
Show file tree
Hide file tree
Showing 23 changed files with 509 additions and 7,948 deletions.
10 changes: 5 additions & 5 deletions packages/server/development.env
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
STATS_PORT = 4720

STATS_RABBITMQ_URI = amqp://guest:[email protected].3:5672/spot
STATS_RABBITMQ_URI = amqp://guest:[email protected].4:5672/spot
STATS_RABBITMQ_EXCHANGE = stats.exchange
STATS_RABBITMQ_EXCHANGE_TYPE = direct
STATS_RABBITMQ_ROUTES = manifest.route|stats.route|metric.route
STATS_RABBITMQ_QUEUES = manifest.queue|stats.queue|metric.queue
STATS_RABBITMQ_ROUTES = manifest.route|stats.route|metric.route|syncstats.route
STATS_RABBITMQ_QUEUES = manifest.queue|stats.queue|metric.queue|syncstats.queue

GLOBE_RABBITMQ_URI = amqp://guest:[email protected].3:5672/spot
GLOBE_RABBITMQ_URI = amqp://guest:[email protected].4:5672/spot
GLOBE_RABBITMQ_EXCHANGE = globe.exchange
GLOBE_RABBITMQ_EXCHANGE_TYPE = direct
GLOBE_RABBITMQ_ROUTES = practice.route
GLOBE_RABBITMQ_QUEUES = practice.queue

STATS_MONGODB_URI = mongodb://192.168.100.3/dwapiStats
STATS_MONGODB_URI = mongodb://192.168.100.4/dwapiStats
STATS_KEY = koskedk.com+5-key.pem
STATS_CERT = koskedk.com+5.pem
2 changes: 1 addition & 1 deletion packages/server/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Controller, Get, Logger, Res } from '@nestjs/common';
export class AppController {
@Get()
getAppName(): string {
return 'dwapi Stats v11JUN200143';
return 'dwapi Stats v14JUN200113';
}

@Get('version')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { Inject, Logger } from '@nestjs/common';
import { IManifestRepository } from '../../../../domain/transfers/manifest-repository.interface';
import { RequestStatsCommand } from '../request-stats.command';
import { MessagingService } from '../../../../infrastructure/messging/messaging.service';
import { ConfigService } from '../../../../config/config.service';

@CommandHandler(RequestStatsCommand)
export class RequestStatsHandler
implements ICommandHandler<RequestStatsCommand> {
constructor(
@Inject('IManifestRepository')
private readonly manifestRepository: IManifestRepository,
private readonly client: MessagingService,
private readonly config: ConfigService,
) {}

async execute(command: RequestStatsCommand): Promise<any> {
let manifests = [];
manifests = await this.manifestRepository.getCurrentMissing();
if (command.facilityCodes && command.facilityCodes.length > 0) {
manifests = manifests.filter((x) =>
command.facilityCodes.includes(x.code),
);
}
for (const manifest of manifests) {
try {
const result = await this.client.publish(
JSON.stringify({
label: RequestStatsCommand.name,
body: {
id: manifest.mId,
code: manifest.code,
docket: manifest.docket,
},
}),
this.config.QueueStatsExchange,
this.config.getRoute('syncstats.route'),
);
} catch (e) {
Logger.error(`PUBLISH`, e);
}
}
return manifests;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Test, TestingModule } from '@nestjs/testing';
import { MongooseModule } from '@nestjs/mongoose';
import { CommandBus } from '@nestjs/cqrs';
import { Logger } from '@nestjs/common';
import { TestDbHelper } from '../../../../../test/test-db.helper';
import {
getDockets,
getFacilities,
getMasterFacs,
getUpdateStatsCommands,
} from '../../../../../test/test.data';
import {
Docket,
Facility,
IFacilityRepository,
MasterFacility,
} from '../../../../domain';
import { TransfersModule } from '../../transfers.module';
import { CourtsInfrastructureModule } from '../../../../infrastructure/courts';
import { UpdateStatsCommand } from '../update-stats.command';
import { UpdateStatsHandler } from './update-stats.handler';
import { plainToClass } from 'class-transformer';
import { LogManifestCommand } from '../log-manifest.command';
import { SyncStatsCommand } from '../sync-stats.command';
import { SyncStatsHandler } from './sync-stats.handler';

describe('Sync Stats Command Tests', () => {
let module: TestingModule;
let commandBus: CommandBus;

let dockets: Docket[];
let masterFacilities: MasterFacility[];
let facilities: Facility[];
let syncStatsCommands: SyncStatsCommand[];

const dbHelper = new TestDbHelper();
let facilityRepository: IFacilityRepository;

beforeAll(async () => {
module = await Test.createTestingModule({
imports: [
MongooseModule.forRoot(dbHelper.url, dbHelper.options),
TransfersModule,
CourtsInfrastructureModule,
],
}).compile();
dockets = await getDockets();
masterFacilities = await getMasterFacs();
facilities = await getFacilities();
syncStatsCommands = await getUpdateStatsCommands();
await dbHelper.initConnection();
await dbHelper.seedDb(
'facilities',
facilities.filter((x) => x.code === 12618),
);
const handler = module.get<SyncStatsHandler>(SyncStatsHandler);
facilityRepository = module.get<IFacilityRepository>('IFacilityRepository');
commandBus = module.get<CommandBus>(CommandBus);
commandBus.bind(handler, SyncStatsCommand.name);
});

afterAll(async () => {
await dbHelper.clearDb();
await dbHelper.closeConnection();
});

it('should Sync Facility Stats', async () => {
const existingFacility = facilities.find((x) => x.code === 12618);
const syncStats = syncStatsCommands.filter((x) => x.facilityCode === 12618);
for (const command of syncStats) {
const result = await commandBus.execute(command);
expect(result).not.toBeNull();

const findresult = await facilityRepository.get(existingFacility._id);
const facility = plainToClass(Facility, findresult);
command.stats.forEach((stat) => {
expect(
facility.getStats(command.docket.name, stat.name).recieved,
).toEqual(stat.recieved);
});
}
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { CommandHandler, EventPublisher, ICommandHandler } from '@nestjs/cqrs';
import { LogManifestCommand } from '../log-manifest.command';
import { Inject, Logger } from '@nestjs/common';
import {
Facility,
IDocketRepository,
IFacilityRepository,
IMasterFacilityRepository,
} from '../../../../domain';
import { UpdateStatsCommand } from '../update-stats.command';
import { plainToClass } from 'class-transformer';
import { IManifestRepository } from '../../../../domain/transfers/manifest-repository.interface';
import { SyncStatsCommand } from '../sync-stats.command';

@CommandHandler(SyncStatsCommand)
export class SyncStatsHandler implements ICommandHandler<SyncStatsCommand> {
constructor(
@Inject('IDocketRepository')
private readonly docketRepository: IDocketRepository,
@Inject('IFacilityRepository')
private readonly facilityRepository: IFacilityRepository,
@Inject('IManifestRepository')
private readonly manifestRepository: IManifestRepository,
private readonly publisher: EventPublisher,
) {}

async execute(command: SyncStatsCommand): Promise<any> {
let facility = await this.facilityRepository.findByCode(
command.facilityCode,
);
if (facility) {
facility = plainToClass(Facility, facility);
facility.updateSummary(command.docket, command.stats, command.updated);

const updatedFacility = await this.facilityRepository.update(facility);
this.publisher.mergeObjectContext(facility).commit();
Logger.log(`Updated Stats ${facility.name}`);

const manifest = await this.manifestRepository.getCurrentDocket(
facility._id,
command.docket.name,
);
if (manifest) {
const recieved = facility.getPatientSummary(command.docket.name);
if (recieved) {
manifest.recievedCount = recieved;
await this.manifestRepository.update(manifest);
}
}
return updatedFacility;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class UpdateStatsHandler implements ICommandHandler<UpdateStatsCommand> {
const recieved = facility.getPatientSummary(command.docket.name);
if (recieved) {
manifest.recievedCount = recieved;
manifest.recievedDate = new Date();
manifest.recievedDate = command.updated;
await this.manifestRepository.update(manifest);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class RequestStatsCommand {
constructor(public facilityCodes?: number[]) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class SyncStatsCommand {
constructor(
public facilityCode: number,
public docket: any,
public stats: any,
public updated: Date,
public manifestId?: string,
) {}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Controller, Get, Param, Query } from '@nestjs/common';
import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common';
import { CommandBus, QueryBus } from '@nestjs/cqrs';
import { GetStatsPagedQuery } from '../queries/get-stats-paged.query';
import { GetStatsCountQuery } from '../queries/get-stats-count.query';
import { GetStatsQuery } from '../queries/get-stats.query';
import { RequestSyncDto } from 'src/domain/transfers/dtos/request-sync.dto';
import { RequestStatsCommand } from '../commands/request-stats.command';
import { GetMisssingStatsQuery } from '../queries/get-misssing-stats.query';

@Controller('manifests')
export class ManifestsController {
Expand Down Expand Up @@ -40,4 +43,22 @@ export class ManifestsController {
async getStatsCount(): Promise<number> {
return this.queryBus.execute(new GetStatsCountQuery());
}
@Get('missing')
async getForSync(): Promise<any> {
const query = new GetMisssingStatsQuery();
return this.queryBus.execute(query);
}

@Post('sync')
async requestSync(@Body() requestSync: RequestSyncDto) {
if (requestSync.codes && requestSync.codes.length) {
return this.commandBus.execute(
new RequestStatsCommand(requestSync.codes),
);
} else {
return this.commandBus.execute(
new RequestStatsCommand(requestSync.codes),
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import {
export class FacilityEnrolledHandler
implements IEventHandler<FacilityEnrolledEvent> {
handle(event: FacilityEnrolledEvent) {
Logger.debug(`=== FacilityEnrolled ===:${event._id}`);
Logger.debug(`=== FacilityEnrolled ===`);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class GetMisssingStatsQuery {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Test, TestingModule } from '@nestjs/testing';
import { MongooseModule } from '@nestjs/mongoose';
import { QueryBus } from '@nestjs/cqrs';
import { Logger } from '@nestjs/common';
import { TestDbHelper } from '../../../../../test/test-db.helper';
import {
getTestFacilities,
getTestStatsData,
} from '../../../../../test/test.data';
import { TransfersModule } from '../../transfers.module';
import { CourtsInfrastructureModule } from '../../../../infrastructure/courts';
import { GetMisssingStatsQuery } from '../get-misssing-stats.query';
import { GetMisssingStatsHandler } from './get-missing-stats.handler';

describe('Get Manifests Stats', () => {
let module: TestingModule;
const { dockets, masterfacilities } = getTestStatsData();
const { facilities, manifests } = getTestFacilities();
const dbHelper = new TestDbHelper();
const liveData = facilities[0];
liveData.summaries = [];
let queryBus: QueryBus;

beforeAll(async () => {
module = await Test.createTestingModule({
imports: [
MongooseModule.forRoot(dbHelper.url, dbHelper.options),
TransfersModule,
CourtsInfrastructureModule,
],
}).compile();

await dbHelper.initConnection();
await dbHelper.seedDb('dockets', dockets);
await dbHelper.seedDb('masterfacilities', masterfacilities);
liveData.code = masterfacilities[0].code;
await dbHelper.seedDb('facilities', [liveData]);
await dbHelper.seedDb('manifests', manifests);

const handler = module.get<GetMisssingStatsHandler>(
GetMisssingStatsHandler,
);

queryBus = module.get<QueryBus>(QueryBus);
queryBus.bind(handler, GetMisssingStatsQuery.name);
});

afterAll(async () => {
await dbHelper.clearDb();
await dbHelper.closeConnection();
});

it('should get Manifest Missing Stats', async () => {
const query = new GetMisssingStatsQuery();
const result = await queryBus.execute<GetMisssingStatsQuery, any>(query);
expect(result.length).toBeGreaterThan(0);
result.forEach((c) => Logger.debug(`${c}`));
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import { Inject } from '@nestjs/common';
import { IManifestRepository } from '../../../../domain/transfers/manifest-repository.interface';
import { GetStatsQuery } from '../get-stats.query';
import { GetMisssingStatsQuery } from '../get-misssing-stats.query';

@QueryHandler(GetMisssingStatsQuery)
export class GetMisssingStatsHandler
implements IQueryHandler<GetMisssingStatsQuery, any> {
constructor(
@Inject('IManifestRepository')
private readonly repository: IManifestRepository,
) {}
async execute(query: GetMisssingStatsQuery): Promise<any> {
return await this.repository.getCurrentMissing();
}
}
8 changes: 8 additions & 0 deletions packages/server/src/application/transfers/transfers.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import { ManifestLoggedHandler } from './events/handlers/manifest-logged.handler
import { GetStatsCountHandler } from './queries/handlers/get-stats-count.handler';
import { GetStatsHandler } from './queries/handlers/get-stats.handler';
import { MetricsInfrastructureModule } from '../../infrastructure/metrices/metrics-infrastructure.module';
import { GetMisssingStatsHandler } from './queries/handlers/get-missing-stats.handler';
import { RequestStatsHandler } from './commands/handlers/request-stats.handler';
import { MessagingModule } from '../../infrastructure/messging/messaging.module';
import { ConfigModule } from '../../config/config.module';

const CommandHandlers = [
LogManifestHandler,
Expand All @@ -27,11 +31,15 @@ const QueryHandlers = [
GetStatsPagedHandler,
GetSummaryHandler,
GetStatsCountHandler,
GetMisssingStatsHandler,
RequestStatsHandler,
];

@Module({
imports: [
CqrsModule,
MessagingModule,
ConfigModule,
MetricsInfrastructureModule,
TransfersInfrastructureModule,
RegistriesInfrastructureModule,
Expand Down
Loading

0 comments on commit 7f44f49

Please sign in to comment.