-
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.
Submission Meta Endpoint / Dataloader
Return meta (security state, publish state, etc) of a submission (previously, status information was being returned in the submission GET endpoint, which can now be removed since it is being returned by this new endpoint).
- Loading branch information
Showing
16 changed files
with
446 additions
and
283 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
73 changes: 73 additions & 0 deletions
73
api/src/paths/submission/{submissionId}/features/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,73 @@ | ||
import chai, { expect } from 'chai'; | ||
import { describe } from 'mocha'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import * as index from '.'; | ||
import * as db from '../../../../database/db'; | ||
import { HTTP400, HTTPError } from '../../../../errors/http-error'; | ||
import { SubmissionService } from '../../../../services/submission-service'; | ||
import { getMockDBConnection, getRequestHandlerMocks } from '../../../../__mocks__/db'; | ||
|
||
chai.use(sinonChai); | ||
|
||
describe('index', () => { | ||
describe('getSubmissionFeatures', () => { | ||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('throws error if submissionService throws error', async () => { | ||
const dbConnectionObj = getMockDBConnection(); | ||
|
||
sinon.stub(db, 'getDBConnection').returns(dbConnectionObj); | ||
|
||
const getSubmissionAndFeaturesBySubmissionIdStub = sinon | ||
.stub(SubmissionService.prototype, 'getSubmissionFeaturesBySubmissionId') | ||
.throws(new HTTP400('Error', ['Error'])); | ||
|
||
const requestHandler = index.getSubmissionFeatures(); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
|
||
mockReq.params = { | ||
submissionId: '1' | ||
}; | ||
|
||
try { | ||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect.fail(); | ||
} catch (error) { | ||
expect(getSubmissionAndFeaturesBySubmissionIdStub).to.have.been.calledOnce; | ||
expect((error as HTTPError).status).to.equal(400); | ||
expect((error as HTTPError).message).to.equal('Error'); | ||
} | ||
}); | ||
|
||
it('should return 200 on success', async () => { | ||
const dbConnectionObj = getMockDBConnection(); | ||
|
||
sinon.stub(db, 'getDBConnection').returns(dbConnectionObj); | ||
|
||
const mockResponse = [] as unknown as any; | ||
|
||
const getSubmissionAndFeaturesBySubmissionIdStub = sinon | ||
.stub(SubmissionService.prototype, 'getSubmissionFeaturesBySubmissionId') | ||
.resolves(mockResponse); | ||
|
||
const requestHandler = index.getSubmissionFeatures(); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
|
||
mockReq.params = { | ||
submissionId: '1' | ||
}; | ||
|
||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect(getSubmissionAndFeaturesBySubmissionIdStub).to.have.been.calledOnce; | ||
expect(mockRes.statusValue).to.eql(200); | ||
expect(mockRes.jsonValue).to.eql(mockResponse); | ||
}); | ||
}); | ||
}); |
174 changes: 174 additions & 0 deletions
174
api/src/paths/submission/{submissionId}/features/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,174 @@ | ||
import { RequestHandler } from 'express'; | ||
import { Operation } from 'express-openapi'; | ||
import { getAPIUserDBConnection, getDBConnection } 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 = [getSubmissionFeatures()]; | ||
|
||
GET.apiDoc = { | ||
description: 'Retrieves a submission record from the submission table', | ||
tags: ['eml'], | ||
security: [ | ||
{ | ||
OptionalBearer: [] | ||
} | ||
], | ||
parameters: [ | ||
{ | ||
description: 'Submission ID.', | ||
in: 'path', | ||
name: 'submissionId', | ||
schema: { | ||
type: 'integer', | ||
minimum: 1 | ||
}, | ||
required: true | ||
} | ||
], | ||
responses: { | ||
200: { | ||
description: 'A submission record and all child submission feature records.', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
required: ['feature_type_name', 'feature_type_display_name', 'features'], | ||
properties: { | ||
feature_type_name: { | ||
type: 'string' | ||
}, | ||
feature_type_display_name: { | ||
type: 'string' | ||
}, | ||
features: { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
required: [ | ||
'submission_feature_id', | ||
'submission_id', | ||
'feature_type_id', | ||
'data', | ||
'parent_submission_feature_id', | ||
'record_effective_date', | ||
'record_end_date', | ||
'create_date', | ||
'create_user', | ||
'update_date', | ||
'update_user', | ||
'revision_count', | ||
'feature_type_name', | ||
'feature_type_display_name', | ||
'submission_feature_security_ids' | ||
], | ||
properties: { | ||
submission_feature_id: { | ||
type: 'integer', | ||
minimum: 1 | ||
}, | ||
submission_id: { | ||
type: 'integer', | ||
minimum: 1 | ||
}, | ||
feature_type_id: { | ||
type: 'integer', | ||
minimum: 1 | ||
}, | ||
data: { | ||
type: 'object', | ||
properties: {} | ||
}, | ||
parent_submission_feature_id: { | ||
type: 'integer', | ||
minimum: 1, | ||
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 | ||
}, | ||
feature_type_name: { | ||
type: 'string' | ||
}, | ||
feature_type_display_name: { | ||
type: 'string' | ||
}, | ||
submission_feature_security_ids: { | ||
type: 'array', | ||
items: { | ||
type: 'integer', | ||
minimum: 1 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
...defaultErrorResponses | ||
} | ||
}; | ||
|
||
/** | ||
* Retrieves all child submission feature records. | ||
* | ||
* @returns {RequestHandler} | ||
*/ | ||
export function getSubmissionFeatures(): RequestHandler { | ||
return async (req, res) => { | ||
const connection = req['keycloak_token'] ? getDBConnection(req['keycloak_token']) : getAPIUserDBConnection(); | ||
|
||
const submissionId = Number(req.params.submissionId); | ||
|
||
try { | ||
await connection.open(); | ||
|
||
const submissionService = new SubmissionService(connection); | ||
|
||
const result = await submissionService.getSubmissionFeaturesBySubmissionId(submissionId); | ||
|
||
await connection.commit(); | ||
|
||
res.status(200).json(result); | ||
} 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.