Skip to content

Commit

Permalink
fix(#138): making SYNC_PERIOD configureable, adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
witash committed Oct 10, 2024
1 parent 61e26eb commit 928e931
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
5 changes: 3 additions & 2 deletions mediator/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ export const OPENMRS = {
timeout: Number(getEnvironmentVariable('REQUEST_TIMEOUT', '5000'))
};

//export const SYNC_INTERVAL = getEnvironmentVariable('SYNC_INTERVAL', '60000');
// hard code sync interval to 1 minute because it is hard coded in mediator config
export const SYNC_INTERVAL = '60000';
export const SYNC_INTERVAL = '60';
// how far back shoudl the sync look for new resources
export const SYNC_PERIOD = getEnvironmentVariable('SYNC_PERIOD', '3600');

function getEnvironmentVariable(env: string, def: string) {
if (process.env.NODE_ENV === 'test') {
Expand Down
3 changes: 2 additions & 1 deletion mediator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

/*
app.get('*', (_: Request, res: Response) => {
const osUptime = os.uptime();
const processUptime = process.uptime();
res.send({status: 'success', osuptime: osUptime, processuptime: processUptime});
});
});*/

// routes for valid fhir resources
app.use('/patient', patientRoutes);
Expand Down
7 changes: 5 additions & 2 deletions mediator/src/controllers/openmrs.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { logger } from '../../logger';
import { syncPatients, syncEncounters } from '../utils/openmrs_sync'
import { SYNC_PERIOD } from '../../config'

export async function sync() {
try {
const startTime = new Date();
startTime.setHours(startTime.getHours() - 1);
let now = Date.now();
let syncPeriod = parseInt(SYNC_PERIOD, 10);
let startTime = new Date(now - syncPeriod);

await syncPatients(startTime);
await syncEncounters(startTime);
return { status: 200, data: { message: `OpenMRS sync completed successfully`} };
Expand Down
35 changes: 35 additions & 0 deletions mediator/src/routes/tests/openmrs.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import request from 'supertest';
import app from '../../..';
import * as openmrs_sync from '../../utils/openmrs_sync';
import axios from 'axios';

jest.mock('axios');

describe('GET /openmrs/sync', () => {
it('calls syncPatients and syncEncouners', async () => {
jest.spyOn(openmrs_sync, 'syncPatients').mockImplementation(async (startTime) => {
});

jest.spyOn(openmrs_sync, 'syncEncounters').mockImplementation(async (startTime) => {
});

const res = await request(app).get('/openmrs/sync').send();

expect(res.status).toBe(200);

expect(openmrs_sync.syncPatients).toHaveBeenCalled();
expect(openmrs_sync.syncEncounters).toHaveBeenCalled();
});

it('returns 500 if syncPatients throws an error', async () => {
jest.spyOn(openmrs_sync, 'syncPatients').mockImplementation(async (startTime) => {
throw new Error('Sync Failed');
});

const res = await request(app).get('/openmrs/sync').send();

expect(res.status).toBe(500);

expect(openmrs_sync.syncPatients).toHaveBeenCalled();
});
});
7 changes: 5 additions & 2 deletions mediator/src/utils/openmrs_sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ export async function compare(
// get the key for each resource and create a Map
const fhirIds = new Map(comparison.fhirResources.map(resource => [getKey(resource), resource]));

// dont sync resources created with 2 * SYNC_INTERVAL of start time
let syncWindow = (Number(SYNC_INTERVAL) * 1000) * 2

comparison.openMRSResources.forEach((openMRSResource) => {
const key = getKey(openMRSResource);
if (fhirIds.has(key)) {
Expand All @@ -95,7 +98,7 @@ export async function compare(
throw new Error("Invalid date format");
}
const diff = lastUpdated.getTime() - startTime.getTime();
if (diff > (Number(SYNC_INTERVAL) * 2)){
if (diff > syncWindow){
results.incoming.push(openMRSResource);
}
}
Expand All @@ -107,7 +110,7 @@ export async function compare(
throw new Error("Invalid date format");
}
const diff = lastUpdated.getTime() - startTime.getTime();
if (diff > (Number(SYNC_INTERVAL) * 2)){
if (diff > syncWindow){
results.outgoing.push(resource);
}
});
Expand Down

0 comments on commit 928e931

Please sign in to comment.