Skip to content

Commit

Permalink
Update getFeatures endpoint to return search keys as data, in place o…
Browse files Browse the repository at this point in the history
…f original raw properties.
  • Loading branch information
NickPhura committed Jan 20, 2024
1 parent 6ba17a0 commit d544250
Show file tree
Hide file tree
Showing 9 changed files with 780 additions and 3 deletions.
74 changes: 74 additions & 0 deletions api/src/paths/submission/{submissionId}/feature-type/index.test.ts
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 api/src/paths/submission/{submissionId}/feature-type/index.ts
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();
}
};
}
2 changes: 1 addition & 1 deletion api/src/paths/submission/{submissionId}/features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export function getSubmissionFeatures(): RequestHandler {

const submissionService = new SubmissionService(connection);

const result = await submissionService.getSubmissionFeaturesBySubmissionId(submissionId);
const result = await submissionService.getSubmissionFeaturesWithSearchKeyValuesBySubmissionId(submissionId);

await connection.commit();

Expand Down
Loading

0 comments on commit d544250

Please sign in to comment.