Skip to content

Commit

Permalink
fix(cb2-10707): repair CT third mark validation (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
naathanbrown authored Apr 22, 2024
1 parent a1d5dfc commit 55b9867
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/handler/updateVrm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import {
updateVehicle,
} from '../services/database';
import { donorVehicle } from '../services/donorVehicle';
import { publish } from '../services/sns';
import { getUserDetails } from '../services/user';
import { formatErrorMessage } from '../util/errorMessage';
import { addHttpHeaders } from '../util/httpHeaders';
import logger from '../util/logger';
import { validateUpdateVrmRequest, validateVrm, validateVrmExists } from '../validators/update';
import { formatErrorMessage } from '../util/errorMessage';
import { publish } from '../services/sns';

export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
try {
Expand Down Expand Up @@ -48,9 +48,10 @@ export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayPr

if (!thirdMark?.length) {
const newVrmExistsOnActiveRecord = await validateVrmExists(newVrm);
if (newVrmExistsOnActiveRecord) {
return newVrmExistsOnActiveRecord;
}
if (newVrmExistsOnActiveRecord) return newVrmExistsOnActiveRecord;
} else {
const thirdMarkVrmExistsOnActiveRecord = await validateVrmExists(thirdMark);
if (thirdMarkVrmExistsOnActiveRecord) return thirdMarkVrmExistsOnActiveRecord;
}

const [donorVehicleRecord, error] = await donorVehicle(newVrm, thirdMark) as [TechRecordType<'get'>, APIGatewayProxyResult];
Expand Down
20 changes: 19 additions & 1 deletion tests/unit/handler/updateVrm.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ const mockPublish = jest.fn();
import { APIGatewayProxyEvent } from 'aws-lambda';
import { handler } from '../../../src/handler/updateVrm';
import * as UserDetails from '../../../src/services/user';
import { ERRORS } from '../../../src/util/enum';
import { formatErrorMessage } from '../../../src/util/errorMessage';
import { addHttpHeaders } from '../../../src/util/httpHeaders';
import carData from '../../resources/techRecordCarPost.json';
import { mockToken } from '../util/mockToken';
import { ERRORS } from '../../../src/util/enum';

jest.mock('../../../src/services/database.ts', () => ({
getBySystemNumberAndCreatedTimestamp: mockGetBySystemNumberAndCreatedTimestamp,
Expand Down Expand Up @@ -184,6 +184,24 @@ describe('update vrm handler', () => {
expect(result.statusCode).toBe(400);
expect(result.body).toBe('Primary VRM SJG1020 already exists');
});
it('should fail when the third mark is in use', async () => {
request.body = JSON.stringify({ newVrm: 'SJG1020', isCherishedTransfer: true, thirdMark: 'L101RS' });
mockGetBySystemNumberAndCreatedTimestamp.mockReturnValueOnce({
techRecord_manufactureYear: 'null',
primaryVrm: 'SJG1020',
techRecord_make: 'null',
vin: 'DP76UMK4DQLTOT400021',
techRecord_statusCode: 'current',
systemNumber: 'XYZEP5JYOMM00020',
techRecord_vehicleType: 'car',
createdTimestamp: '2019-06-24T10:26:54.903Z',
techRecord_model: 'null',
});
mockValidateVrmExists.mockReturnValueOnce(addHttpHeaders({ statusCode: 400, body: 'Primary VRM L101RS already exists' }));
const result = await handler(request as unknown as APIGatewayProxyEvent);
expect(result.statusCode).toBe(400);
expect(result.body).toBe('Primary VRM L101RS already exists');
});
it('should return an error when there is an error with the donor record', async () => {
request.body = JSON.stringify({ newVrm: 'SJG1020', isCherishedTransfer: true, thirdMark: 'testing' });
mockGetBySystemNumberAndCreatedTimestamp.mockReturnValueOnce({
Expand Down

0 comments on commit 55b9867

Please sign in to comment.