-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SIMSBIOHUB-638: Add Vantage to Techniques (Backend) (#1423)
* A new vantage tables * Update technique qualitative tables, migrate existing data. * Add migration to remove duplicate constraint from previous PR. --------- Co-authored-by: Nick Phura <[email protected]>
- Loading branch information
1 parent
0a9a57f
commit f0cb44e
Showing
17 changed files
with
983 additions
and
29 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
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,81 @@ | ||
import chai, { expect } from 'chai'; | ||
import { describe } from 'mocha'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import * as db from '../../../database/db'; | ||
import { HTTPError } from '../../../errors/http-error'; | ||
import { VantageModeService } from '../../../services/vantage-mode-service'; | ||
import { getMockDBConnection, getRequestHandlerMocks } from '../../../__mocks__/db'; | ||
import { getVantageModes } from './vantage-mode'; | ||
|
||
chai.use(sinonChai); | ||
|
||
describe('getVantageModes', () => { | ||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('should return vantage modes for method lookup ids', async () => { | ||
const mockVantageModeResponse = [ | ||
{ vantage_mode_id: 1, vantage_id: 101, name: 'Mode A', description: 'Description for Mode A' }, | ||
{ vantage_mode_id: 2, vantage_id: 102, name: 'Mode B', description: 'Description for Mode B' } | ||
]; | ||
|
||
const mockDBConnection = getMockDBConnection({ | ||
open: sinon.stub(), | ||
commit: sinon.stub(), | ||
release: sinon.stub() | ||
}); | ||
|
||
sinon.stub(db, 'getDBConnection').returns(mockDBConnection); | ||
|
||
const getVantageModesByMethodLookupIdsStub = sinon | ||
.stub(VantageModeService.prototype, 'getVantageModesByMethodLookupIds') | ||
.resolves(mockVantageModeResponse); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
mockReq.query = { methodLookupId: ['1', '2'] }; | ||
|
||
const requestHandler = getVantageModes(); | ||
|
||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect(getVantageModesByMethodLookupIdsStub).to.have.been.calledOnceWith([1, 2]); | ||
expect(mockRes.jsonValue).to.eql(mockVantageModeResponse); | ||
|
||
expect(mockDBConnection.open).to.have.been.calledOnce; | ||
expect(mockDBConnection.commit).to.have.been.calledOnce; | ||
expect(mockDBConnection.release).to.have.been.calledOnce; | ||
}); | ||
|
||
it('should catch and handle errors', async () => { | ||
const mockDBConnection = getMockDBConnection({ | ||
open: sinon.stub(), | ||
commit: sinon.stub(), | ||
release: sinon.stub(), | ||
rollback: sinon.stub() | ||
}); | ||
|
||
sinon.stub(db, 'getDBConnection').returns(mockDBConnection); | ||
|
||
const getVantageModesByMethodLookupIdsStub = sinon | ||
.stub(VantageModeService.prototype, 'getVantageModesByMethodLookupIds') | ||
.rejects(new Error('Test database error')); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
mockReq.query = { methodLookupId: ['1', '2'] }; | ||
|
||
const requestHandler = getVantageModes(); | ||
|
||
try { | ||
await requestHandler(mockReq, mockRes, mockNext); | ||
expect.fail('Expected method to throw'); | ||
} catch (error) { | ||
expect(mockDBConnection.open).to.have.been.calledOnce; | ||
expect(mockDBConnection.rollback).to.have.been.calledOnce; | ||
expect(mockDBConnection.release).to.have.been.calledOnce; | ||
expect(getVantageModesByMethodLookupIdsStub).to.have.been.calledOnceWith([1, 2]); | ||
expect((error as HTTPError).message).to.equal('Test database error'); | ||
} | ||
}); | ||
}); |
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,85 @@ | ||
import { RequestHandler } from 'express'; | ||
import { Operation } from 'express-openapi'; | ||
import { getAPIUserDBConnection } from '../../../database/db'; | ||
import { vantageModeSchema } from '../../../openapi/schemas/technique'; | ||
import { VantageModeService } from '../../../services/vantage-mode-service'; | ||
import { getLogger } from '../../../utils/logger'; | ||
|
||
const defaultLog = getLogger('paths/reference/get/vantage-mode'); | ||
|
||
export const GET: Operation = [getVantageModes()]; | ||
|
||
GET.apiDoc = { | ||
description: 'Find vantage modes applicable to method lookup options', | ||
tags: ['reference'], | ||
parameters: [ | ||
{ | ||
in: 'query', | ||
name: 'methodLookupId', | ||
schema: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
}, | ||
minItems: 1 | ||
}, | ||
required: true | ||
} | ||
], | ||
responses: { | ||
200: { | ||
description: 'Vantages for a method lookup id.', | ||
content: { | ||
'application/json': { | ||
schema: vantageModeSchema | ||
} | ||
} | ||
}, | ||
400: { | ||
$ref: '#/components/responses/400' | ||
}, | ||
401: { | ||
$ref: '#/components/responses/401' | ||
}, | ||
403: { | ||
$ref: '#/components/responses/403' | ||
}, | ||
500: { | ||
$ref: '#/components/responses/500' | ||
}, | ||
default: { | ||
$ref: '#/components/responses/default' | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* Get all vantage modes possible for multiple method lookup ids. | ||
* | ||
* @returns {RequestHandler} | ||
*/ | ||
export function getVantageModes(): RequestHandler { | ||
return async (req, res) => { | ||
const connection = getAPIUserDBConnection(); | ||
|
||
try { | ||
const methodLookupIds = (req.query.methodLookupId as string[]).map(Number); | ||
|
||
await connection.open(); | ||
|
||
const vantageModeService = new VantageModeService(connection); | ||
|
||
const response = await vantageModeService.getVantageModesByMethodLookupIds(methodLookupIds); | ||
|
||
await connection.commit(); | ||
|
||
return res.status(200).json(response); | ||
} catch (error) { | ||
defaultLog.error({ label: 'getVantageModes', message: 'error', error }); | ||
await connection.rollback(); | ||
throw error; | ||
} finally { | ||
connection.release(); | ||
} | ||
}; | ||
} |
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
Oops, something went wrong.