-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
8 changed files
with
127 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import * as dotenv from 'dotenv'; | |
dotenv.config(); | ||
|
||
export const PORT = process.env.PORT || 6000; | ||
const REQUEST_TIMEOUT = Number(getEnvironmentVariable('REQUEST_TIMEOUT', '5000')); | ||
|
||
export const OPENHIM = { | ||
username: getEnvironmentVariable('OPENHIM_USERNAME', '[email protected]'), | ||
|
@@ -11,27 +12,30 @@ export const OPENHIM = { | |
}; | ||
|
||
export const FHIR = { | ||
url: getEnvironmentVariable('FHIR_URL', 'http://openhim-core:5001/fhir'), | ||
url: getEnvironmentVariable('FHIR_URL', 'https://openhim-core:5001/fhir'), | ||
username: getEnvironmentVariable('FHIR_USERNAME', 'interop-client'), | ||
password: getEnvironmentVariable('FHIR_PASSWORD', 'interop-password'), | ||
timeout: Number(getEnvironmentVariable('REQUEST_TIMEOUT', '5000')) | ||
timeout: REQUEST_TIMEOUT | ||
}; | ||
|
||
export const CHT = { | ||
url: getEnvironmentVariable('CHT_URL', 'https://nginx'), | ||
username: getEnvironmentVariable('CHT_USERNAME', 'admin'), | ||
password: getEnvironmentVariable('CHT_PASSWORD', 'password'), | ||
timeout: Number(getEnvironmentVariable('REQUEST_TIMEOUT', '5000')) | ||
timeout: REQUEST_TIMEOUT | ||
}; | ||
|
||
export const OPENMRS = { | ||
url: getEnvironmentVariable('OPENMRS_CHANNEL_URL', 'http://openhim-core:5001/openmrs'), | ||
url: getEnvironmentVariable('OPENMRS_CHANNEL_URL', 'https://openhim-core:5001/openmrs'), | ||
username: getEnvironmentVariable('OPENMRS_CHANNEL_USERNAME', 'interop-client'), | ||
password: getEnvironmentVariable('OPENMRS_CHANNEL_PASSWORD', 'interop-password'), | ||
timeout: Number(getEnvironmentVariable('REQUEST_TIMEOUT', '5000')) | ||
timeout: REQUEST_TIMEOUT | ||
}; | ||
|
||
export const SYNC_INTERVAL = getEnvironmentVariable('SYNC_INTERVAL', '60000'); | ||
// how often in seconds the sync should run. hardcoded to 1 minute | ||
export const SYNC_INTERVAL = '60'; | ||
// how far back should the sync look for new resources. Defaults to one hour | ||
export const SYNC_PERIOD = getEnvironmentVariable('SYNC_PERIOD', '3600'); | ||
|
||
function getEnvironmentVariable(env: string, def: string) { | ||
if (process.env.NODE_ENV === 'test') { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { logger } from '../../logger'; | ||
import { syncPatients, syncEncounters } from '../utils/openmrs_sync' | ||
import { SYNC_PERIOD } from '../../config' | ||
|
||
export async function sync() { | ||
try { | ||
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`} }; | ||
} catch(error: any) { | ||
logger.error(error); | ||
return { status: 500, data: { message: `Error during OpenMRS Sync`} }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Router } from 'express'; | ||
import { requestHandler } from '../utils/request'; | ||
import { sync } from '../controllers/openmrs' | ||
|
||
const router = Router(); | ||
|
||
router.get( | ||
'/sync', | ||
requestHandler((req) => sync()) | ||
); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import request from 'supertest'; | ||
import app from '../../..'; | ||
import * as openmrs_sync from '../../utils/openmrs_sync'; | ||
import axios from 'axios'; | ||
import { logger } from '../../../logger'; | ||
|
||
jest.mock('axios'); | ||
jest.mock('../../../logger'); | ||
|
||
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters