Skip to content

Commit

Permalink
Merge pull request #32 from bcgov/feat/uniform-response
Browse files Browse the repository at this point in the history
Uniform Response
  • Loading branch information
hannah-macdonald1 authored Nov 20, 2024
2 parents 5dfaa24 + 07017e4 commit f6cb5dd
Show file tree
Hide file tree
Showing 30 changed files with 225 additions and 388 deletions.
2 changes: 2 additions & 0 deletions src/common/constants/parameter-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const VIEW_MODE = 'Catalog';
const CHILD_LINKS = 'None';
const CONTENT_TYPE = 'application/json';
const UNIFORM_RESPONSE = 'y';
const uniformResponseParamName = 'uniformresponse';
const recordCountHeaderName = 'total-record-count';
const sinceParamName = 'since';

Expand All @@ -18,6 +19,7 @@ export {
CHILD_LINKS,
CONTENT_TYPE,
UNIFORM_RESPONSE,
uniformResponseParamName,
recordCountHeaderName,
sinceParamName,
idRegex,
Expand Down
43 changes: 24 additions & 19 deletions src/controllers/cases/cases.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
import { CasesController } from './cases.controller';
import { CasesService } from './cases.service';
import {
SupportNetworkEntity,
SupportNetworkSingleResponseCaseExample,
NestedSupportNetworkEntity,
SupportNetworkListResponseCaseExample,
} from '../../entities/support-network.entity';
import { FilterQueryParams } from '../../dto/filter-query-params.dto';
import { IdPathParams } from '../../dto/id-path-params.dto';
Expand All @@ -17,8 +17,7 @@ import { UtilitiesService } from '../../helpers/utilities/utilities.service';
import { InPersonVisitsService } from '../../helpers/in-person-visits/in-person-visits.service';
import { RequestPreparerService } from '../../external-api/request-preparer/request-preparer.service';
import {
InPersonVisitsEntity,
InPersonVisitsSingleResponseCaseExample,
InPersonVisitsListResponseCaseExample,
NestedInPersonVisitsEntity,
PostInPersonVisitResponseExample,
} from '../../entities/in-person-visits.entity';
Expand All @@ -28,8 +27,8 @@ import {
} from '../../common/constants/parameter-constants';
import { AttachmentsService } from '../../helpers/attachments/attachments.service';
import {
AttachmentsSingleResponseCaseExample,
AttachmentsEntity,
AttachmentsListResponseCaseExample,
NestedAttachmentsEntity,
} from '../../entities/attachments.entity';
import { VisitDetails } from '../../common/constants/enumerations';
import { getMockReq, getMockRes } from '@jest-mock/express';
Expand Down Expand Up @@ -74,19 +73,21 @@ describe('CasesController', () => {
describe('getSingleCaseSupportNetworkInformationRecord tests', () => {
it.each([
[
SupportNetworkSingleResponseCaseExample,
SupportNetworkListResponseCaseExample,
{ [idName]: 'test' } as IdPathParams,
{
[sinceParamName]: '2020-02-02',
[startRowNumParamName]: 0,
} as FilterQueryParams,
],
])(
'should return single values given good input',
'should return nested values given good input',
async (data, idPathParams, filterQueryParams) => {
const casesServiceSpy = jest
.spyOn(casesService, 'getSingleCaseSupportNetworkInformationRecord')
.mockReturnValueOnce(Promise.resolve(new SupportNetworkEntity(data)));
.mockReturnValueOnce(
Promise.resolve(new NestedSupportNetworkEntity(data)),
);

const result =
await controller.getSingleCaseSupportNetworkInformationRecord(
Expand All @@ -99,27 +100,29 @@ describe('CasesController', () => {
res,
filterQueryParams,
);
expect(result).toEqual(new SupportNetworkEntity(data));
expect(result).toEqual(new NestedSupportNetworkEntity(data));
},
);
});

describe('getSingleCaseInPersonVisitRecord tests', () => {
it.each([
[
InPersonVisitsSingleResponseCaseExample,
InPersonVisitsListResponseCaseExample,
{ [idName]: 'test' } as IdPathParams,
{
[sinceParamName]: '2020-02-02',
[startRowNumParamName]: 0,
} as FilterQueryParams,
],
])(
'should return single values given good input',
'should return nested values given good input',
async (data, idPathParams, filterQueryParams) => {
const casesServiceSpy = jest
.spyOn(casesService, 'getSingleCaseInPersonVisitRecord')
.mockReturnValueOnce(Promise.resolve(new InPersonVisitsEntity(data)));
.mockReturnValueOnce(
Promise.resolve(new NestedInPersonVisitsEntity(data)),
);

const result = await controller.getSingleCaseInPersonVisitRecord(
idPathParams,
Expand All @@ -131,7 +134,7 @@ describe('CasesController', () => {
res,
filterQueryParams,
);
expect(result).toEqual(new InPersonVisitsEntity(data));
expect(result).toEqual(new NestedInPersonVisitsEntity(data));
},
);
});
Expand All @@ -149,7 +152,7 @@ describe('CasesController', () => {
PostInPersonVisitResponseExample,
],
])(
'should return single values given good input',
'should return nested values given good input',
async (body, idPathParams, idir, data) => {
const casesServiceSpy = jest
.spyOn(casesService, 'postSingleCaseInPersonVisitRecord')
Expand All @@ -171,19 +174,21 @@ describe('CasesController', () => {
describe('getSingleCaseAttachmentRecord tests', () => {
it.each([
[
AttachmentsSingleResponseCaseExample,
AttachmentsListResponseCaseExample,
{ [idName]: 'test' } as IdPathParams,
{
[sinceParamName]: '2020-02-02',
[startRowNumParamName]: 0,
} as FilterQueryParams,
],
])(
'should return single values given good input',
'should return nested values given good input',
async (data, idPathParams, filterQueryParams) => {
const caseServiceSpy = jest
.spyOn(casesService, 'getSingleCaseAttachmentRecord')
.mockReturnValueOnce(Promise.resolve(new AttachmentsEntity(data)));
.mockReturnValueOnce(
Promise.resolve(new NestedAttachmentsEntity(data)),
);

const result = await controller.getSingleCaseAttachmentRecord(
idPathParams,
Expand All @@ -195,7 +200,7 @@ describe('CasesController', () => {
res,
filterQueryParams,
);
expect(result).toEqual(new AttachmentsEntity(data));
expect(result).toEqual(new NestedAttachmentsEntity(data));
},
);
});
Expand Down
48 changes: 12 additions & 36 deletions src/controllers/cases/cases.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import {
import { CasesService } from './cases.service';
import {
NestedSupportNetworkEntity,
SupportNetworkEntity,
SupportNetworkListResponseCaseExample,
SupportNetworkSingleResponseCaseExample,
} from '../../entities/support-network.entity';
import { IdPathParams } from '../../dto/id-path-params.dto';
import { FilterQueryParams } from '../../dto/filter-query-params.dto';
Expand All @@ -39,16 +37,12 @@ import {
import { ApiInternalServerErrorEntity } from '../../entities/api-internal-server-error.entity';
import { AuthGuard } from '../../common/guards/auth/auth.guard';
import {
InPersonVisitsEntity,
InPersonVisitsListResponseCaseExample,
InPersonVisitsSingleResponseCaseExample,
NestedInPersonVisitsEntity,
PostInPersonVisitResponseExample,
} from '../../entities/in-person-visits.entity';
import {
AttachmentsEntity,
AttachmentsListResponseCaseExample,
AttachmentsSingleResponseCaseExample,
NestedAttachmentsEntity,
} from '../../entities/attachments.entity';
import { PostInPersonVisitDto } from '../../dto/post-in-person-visit.dto';
Expand Down Expand Up @@ -80,23 +74,17 @@ export class CasesController {
@ApiQuery({ name: recordCountNeededParamName, required: false })
@ApiQuery({ name: pageSizeParamName, required: false })
@ApiQuery({ name: startRowNumParamName, required: false })
@ApiExtraModels(SupportNetworkEntity, NestedSupportNetworkEntity)
@ApiExtraModels(NestedSupportNetworkEntity)
@ApiNoContentResponse(noContentResponseSwagger)
@ApiOkResponse({
headers: totalRecordCountHeadersSwagger,
content: {
[CONTENT_TYPE]: {
schema: {
oneOf: [
{ $ref: getSchemaPath(SupportNetworkEntity) },
{ $ref: getSchemaPath(NestedSupportNetworkEntity) },
],
$ref: getSchemaPath(NestedSupportNetworkEntity),
},
examples: {
SupportNetworkSingleResponse: {
value: SupportNetworkSingleResponseCaseExample,
},
SupportNetworkListResponse: {
SupportNetworkResponse: {
value: SupportNetworkListResponseCaseExample,
},
},
Expand All @@ -122,7 +110,7 @@ export class CasesController {
}),
)
filter?: FilterQueryParams,
): Promise<SupportNetworkEntity | NestedSupportNetworkEntity> {
): Promise<NestedSupportNetworkEntity> {
return await this.casesService.getSingleCaseSupportNetworkInformationRecord(
id,
res,
Expand All @@ -140,23 +128,17 @@ export class CasesController {
@ApiQuery({ name: recordCountNeededParamName, required: false })
@ApiQuery({ name: pageSizeParamName, required: false })
@ApiQuery({ name: startRowNumParamName, required: false })
@ApiExtraModels(InPersonVisitsEntity, NestedInPersonVisitsEntity)
@ApiExtraModels(NestedInPersonVisitsEntity)
@ApiNoContentResponse(noContentResponseSwagger)
@ApiOkResponse({
headers: totalRecordCountHeadersSwagger,
content: {
[CONTENT_TYPE]: {
schema: {
oneOf: [
{ $ref: getSchemaPath(InPersonVisitsEntity) },
{ $ref: getSchemaPath(NestedInPersonVisitsEntity) },
],
$ref: getSchemaPath(NestedInPersonVisitsEntity),
},
examples: {
InPersonVisitsSingleResponse: {
value: InPersonVisitsSingleResponseCaseExample,
},
InPersonVisitsListResponse: {
InPersonVisitsResponse: {
value: InPersonVisitsListResponseCaseExample,
},
},
Expand All @@ -182,7 +164,7 @@ export class CasesController {
}),
)
filter?: FilterQueryParams,
): Promise<InPersonVisitsEntity | NestedInPersonVisitsEntity> {
): Promise<NestedInPersonVisitsEntity> {
return await this.casesService.getSingleCaseInPersonVisitRecord(
id,
res,
Expand Down Expand Up @@ -243,23 +225,17 @@ export class CasesController {
@ApiQuery({ name: recordCountNeededParamName, required: false })
@ApiQuery({ name: pageSizeParamName, required: false })
@ApiQuery({ name: startRowNumParamName, required: false })
@ApiExtraModels(AttachmentsEntity, NestedAttachmentsEntity)
@ApiExtraModels(NestedAttachmentsEntity)
@ApiNoContentResponse(noContentResponseSwagger)
@ApiOkResponse({
headers: totalRecordCountHeadersSwagger,
content: {
[CONTENT_TYPE]: {
schema: {
oneOf: [
{ $ref: getSchemaPath(AttachmentsEntity) },
{ $ref: getSchemaPath(NestedAttachmentsEntity) },
],
$ref: getSchemaPath(NestedAttachmentsEntity),
},
examples: {
AttachmentsSingleResponse: {
value: AttachmentsSingleResponseCaseExample,
},
AttachmentsListResponse: {
AttachmentsResponse: {
value: AttachmentsListResponseCaseExample,
},
},
Expand All @@ -285,7 +261,7 @@ export class CasesController {
}),
)
filter?: FilterQueryParams,
): Promise<AttachmentsEntity | NestedAttachmentsEntity> {
): Promise<NestedAttachmentsEntity> {
return await this.casesService.getSingleCaseAttachmentRecord(
id,
res,
Expand Down
Loading

0 comments on commit f6cb5dd

Please sign in to comment.