-
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.
Update getFeatures endpoint to return search keys as data, in place o…
…f original raw properties.
- Loading branch information
Showing
9 changed files
with
780 additions
and
3 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
api/src/paths/submission/{submissionId}/feature-type/index.test.ts
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,74 @@ | ||
import chai, { expect } from 'chai'; | ||
import { describe } from 'mocha'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import { getSubmissionFeatureTypes } from '.'; | ||
import * as db from '../../../../database/db'; | ||
import { FeatureTypeRecord } from '../../../../repositories/submission-repository'; | ||
import { SubmissionService } from '../../../../services/submission-service'; | ||
import { getMockDBConnection, getRequestHandlerMocks } from '../../../../__mocks__/db'; | ||
|
||
chai.use(sinonChai); | ||
|
||
describe('getSubmissionFeatureTypes', () => { | ||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('throws error if submissionService throws error', async () => { | ||
const dbConnectionObj = getMockDBConnection(); | ||
|
||
sinon.stub(db, 'getAPIUserDBConnection').returns(dbConnectionObj); | ||
|
||
const getSubmissionFeatureTypesStub = sinon | ||
.stub(SubmissionService.prototype, 'getSubmissionFeatureTypes') | ||
.throws(new Error('test error')); | ||
|
||
const requestHandler = getSubmissionFeatureTypes(); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
|
||
const submissionId = 1; | ||
|
||
mockReq.params = { | ||
submissionId: String(submissionId) | ||
}; | ||
|
||
try { | ||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect.fail(); | ||
} catch (error) { | ||
expect(getSubmissionFeatureTypesStub).to.have.been.calledOnceWith(submissionId); | ||
expect((error as Error).message).to.equal('test error'); | ||
} | ||
}); | ||
|
||
it('should return 200 on success', async () => { | ||
const dbConnectionObj = getMockDBConnection(); | ||
|
||
sinon.stub(db, 'getAPIUserDBConnection').returns(dbConnectionObj); | ||
|
||
const mockFeatureTypes: FeatureTypeRecord[] = []; | ||
|
||
const getSubmissionFeatureTypesStub = sinon | ||
.stub(SubmissionService.prototype, 'getSubmissionFeatureTypes') | ||
.resolves(mockFeatureTypes); | ||
|
||
const requestHandler = getSubmissionFeatureTypes(); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
|
||
const submissionId = 1; | ||
|
||
mockReq.params = { | ||
submissionId: String(submissionId) | ||
}; | ||
|
||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect(getSubmissionFeatureTypesStub).to.have.been.calledOnceWith(submissionId); | ||
expect(mockRes.statusValue).to.eql(200); | ||
expect(mockRes.jsonValue).to.eql({ feature_types: mockFeatureTypes }); | ||
}); | ||
}); |
148 changes: 148 additions & 0 deletions
148
api/src/paths/submission/{submissionId}/feature-type/index.ts
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,148 @@ | ||
import { RequestHandler } from 'express'; | ||
import { Operation } from 'express-openapi'; | ||
import { getAPIUserDBConnection } from '../../../../database/db'; | ||
import { defaultErrorResponses } from '../../../../openapi/schemas/http-responses'; | ||
import { SubmissionService } from '../../../../services/submission-service'; | ||
import { getLogger } from '../../../../utils/logger'; | ||
|
||
const defaultLog = getLogger('paths/submission/{submissionId}'); | ||
|
||
export const GET: Operation = [getSubmissionFeatureTypes()]; | ||
|
||
GET.apiDoc = { | ||
description: 'Retrieves a sorted and distinct array of feature type records for a submission.', | ||
tags: ['submission'], | ||
parameters: [ | ||
{ | ||
description: 'Submission ID.', | ||
in: 'path', | ||
name: 'submissionId', | ||
schema: { | ||
type: 'integer', | ||
minimum: 1 | ||
}, | ||
required: true | ||
} | ||
], | ||
responses: { | ||
200: { | ||
description: 'A sorted and distinct array of feature type records.', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
required: ['features_types'], | ||
properties: { | ||
features_types: { | ||
description: 'A sorted and distinct array of feature type records.', | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
required: [ | ||
'feature_type_id', | ||
'name', | ||
'display_name', | ||
'description', | ||
'record_effective_date', | ||
'record_end_date', | ||
'create_date', | ||
'create_user', | ||
'update_date', | ||
'update_user', | ||
'revision_count' | ||
], | ||
properties: { | ||
feature_type_id: { | ||
type: 'integer', | ||
minimum: 1 | ||
}, | ||
name: { | ||
type: 'string', | ||
maxLength: 100 | ||
}, | ||
display_name: { | ||
type: 'string', | ||
maxLength: 100 | ||
}, | ||
description: { | ||
type: 'string', | ||
nullable: true, | ||
maxLength: 500 | ||
}, | ||
sort: { | ||
type: 'number', | ||
nullable: true | ||
}, | ||
record_effective_date: { | ||
type: 'string' | ||
}, | ||
record_end_date: { | ||
type: 'string', | ||
nullable: true | ||
}, | ||
create_date: { | ||
type: 'string' | ||
}, | ||
create_user: { | ||
type: 'integer', | ||
minimum: 1 | ||
}, | ||
update_date: { | ||
type: 'string', | ||
nullable: true | ||
}, | ||
update_user: { | ||
type: 'integer', | ||
minimum: 1, | ||
nullable: true | ||
}, | ||
revision_count: { | ||
type: 'integer', | ||
minimum: 0 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
...defaultErrorResponses | ||
} | ||
}; | ||
|
||
/** | ||
* Retrieves a sorted and distinct list of all feature type records for a submission. | ||
* | ||
* @export | ||
* @return {*} {RequestHandler} | ||
*/ | ||
export function getSubmissionFeatureTypes(): RequestHandler { | ||
return async (req, res) => { | ||
const connection = getAPIUserDBConnection(); | ||
|
||
const submissionId = Number(req.params.submissionId); | ||
|
||
try { | ||
await connection.open(); | ||
|
||
const submissionService = new SubmissionService(connection); | ||
|
||
const featureTypes = await submissionService.getSubmissionFeatureTypes(submissionId); | ||
|
||
await connection.commit(); | ||
|
||
res.status(200).json({ feature_types: featureTypes }); | ||
} catch (error) { | ||
defaultLog.error({ label: 'getSubmissionFeatures', 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
Oops, something went wrong.