diff --git a/application/enquiry/src/infrastructure/api/index.ts b/application/enquiry/src/infrastructure/api/index.ts index d1a690e..d8f6fcd 100644 --- a/application/enquiry/src/infrastructure/api/index.ts +++ b/application/enquiry/src/infrastructure/api/index.ts @@ -2,28 +2,28 @@ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ import { SecretsManager } from '@aws-sdk/client-secrets-manager'; import express, { Request, Router } from 'express'; -import mysql from 'mysql2/promise'; import moment from 'moment'; -import vehicleQueryFunctionFactory from '../../app/vehicleQueryFunctionFactory'; +import mysql from 'mysql2/promise'; +import evlFeedQueryFunctionFactory from '../../app/evlFeedQueryFunctionFactory'; import testResultsQueryFunctionFactory from '../../app/testResultsQueryFunctionFactory'; -import { getResultsDetails, getVehicleDetails, getFeedDetails } from '../../domain/enquiryService'; +import tflFeedQueryFunctionFactory from '../../app/tflFeedQueryFunctionFactory'; +import vehicleQueryFunctionFactory from '../../app/vehicleQueryFunctionFactory'; +import { getFeedDetails, getResultsDetails, getVehicleDetails } from '../../domain/enquiryService'; +import NotFoundError from '../../errors/NotFoundError'; import ParametersError from '../../errors/ParametersError'; +import EvlEvent from '../../interfaces/EvlEvent'; +import { FeedName } from '../../interfaces/FeedTypes'; import ResultsEvent from '../../interfaces/ResultsEvent'; +import SecretsManagerServiceInterface from '../../interfaces/SecretsManagerService'; import VehicleEvent from '../../interfaces/VehicleEvent'; -import EvlEvent from '../../interfaces/EvlEvent'; +import EvlFeedData from '../../interfaces/queryResults/evlFeedData'; +import TflFeedData from '../../interfaces/queryResults/tflFeedData'; +import logger from '../../utils/logger'; +import { processTFLFeedData } from '../../utils/tflHelpers'; import DatabaseService from '../databaseService'; -import SecretsManagerService from '../secretsManagerService'; -import NotFoundError from '../../errors/NotFoundError'; -import SecretsManagerServiceInterface from '../../interfaces/SecretsManagerService'; import LocalSecretsManagerService from '../localSecretsManagerService'; -import evlFeedQueryFunctionFactory from '../../app/evlFeedQueryFunctionFactory'; import { uploadToS3 } from '../s3BucketService'; -import logger from '../../utils/logger'; -import tflFeedQueryFunctionFactory from '../../app/tflFeedQueryFunctionFactory'; -import { processTFLFeedData } from '../../utils/tflHelpers'; -import { FeedName } from '../../interfaces/FeedTypes'; -import EvlFeedData from '../../interfaces/queryResults/evlFeedData'; -import TflFeedData from '../../interfaces/queryResults/tflFeedData'; +import SecretsManagerService from '../secretsManagerService'; const app = express(); const router: Router = express.Router(); @@ -216,6 +216,6 @@ router.all('*', (_request, res) => { res.status(404).send(); }); -app.use('/*/enquiry/', router); +app.use('/', router); export { app }; diff --git a/application/enquiry/tests/unit/handler.test.ts b/application/enquiry/tests/unit/handler.test.ts index 5be77a8..e37d5cb 100644 --- a/application/enquiry/tests/unit/handler.test.ts +++ b/application/enquiry/tests/unit/handler.test.ts @@ -1,7 +1,7 @@ import { APIGatewayEvent, Context } from 'aws-lambda'; +import Version from '../../local/data/version.json'; import { handler } from '../../src/handler'; import * as Utils from '../../src/utils'; -import Version from '../../local/data/version.json'; describe('Application entry', () => { const OLD_ENV = process.env; @@ -25,7 +25,7 @@ describe('Application entry', () => { describe('Handler', () => { it('should call the express wrapper', async () => { - event = { body: 'Test Body', path: '/v1/enquiry/' }; + event = { body: 'Test Body', path: '/' }; const response = await handler(event, context); expect(response.statusCode).toEqual(200); @@ -35,7 +35,7 @@ describe('Application entry', () => { describe('when the service is running', () => { describe('without proxy', () => { it("should return a body response when the handler has event with the '/' as path", async () => { - event = { httpMethod: 'GET', path: '/v1/enquiry/' }; + event = { httpMethod: 'GET', path: '/' }; const response = await handler(event, context); const parsedBody = JSON.parse(response.body ?? '') as { ok: boolean }; @@ -61,7 +61,7 @@ describe('Application entry', () => { it("should call the service/lambda when the path contains '/version' and return the app version following the semver convention", async () => { event = { ...Version, - path: '/v1/enquiry/version', + path: '/version', }; const response = await handler(event, context); diff --git a/application/enquiry/tests/unit/infrastructure/api/index.test.ts b/application/enquiry/tests/unit/infrastructure/api/index.test.ts index a67b151..6a9e8e8 100644 --- a/application/enquiry/tests/unit/infrastructure/api/index.test.ts +++ b/application/enquiry/tests/unit/infrastructure/api/index.test.ts @@ -1,14 +1,14 @@ import supertest from 'supertest'; -import { app } from '../../../../src/infrastructure/api'; import * as enquiryService from '../../../../src/domain/enquiryService'; +import NotFoundError from '../../../../src/errors/NotFoundError'; +import ParametersError from '../../../../src/errors/ParametersError'; +import { app } from '../../../../src/infrastructure/api'; import DatabaseService from '../../../../src/infrastructure/databaseService'; -import VehicleDetails from '../../../../src/interfaces/queryResults/technical/vehicleDetails'; +import * as upload from '../../../../src/infrastructure/s3BucketService'; import DatabaseServiceInterface from '../../../../src/interfaces/DatabaseService'; -import ParametersError from '../../../../src/errors/ParametersError'; -import TestResult from '../../../../src/interfaces/queryResults/test/testResult'; -import NotFoundError from '../../../../src/errors/NotFoundError'; import EvlFeedData from '../../../../src/interfaces/queryResults/evlFeedData'; -import * as upload from '../../../../src/infrastructure/s3BucketService'; +import VehicleDetails from '../../../../src/interfaces/queryResults/technical/vehicleDetails'; +import TestResult from '../../../../src/interfaces/queryResults/test/testResult'; import TflFeedData from '../../../../src/interfaces/queryResults/tflFeedData'; // TODO Define Mock strategy @@ -19,7 +19,7 @@ describe('API', () => { describe('GET', () => { it("should return '{ok: true}' when hitting '/' route", async () => { - const result = await supertest(app).get('/v1/enquiry/'); + const result = await supertest(app).get('/'); const resultContent = JSON.parse(result.text) as { ok: boolean }; expect(result.status).toEqual(200); @@ -28,7 +28,7 @@ describe('API', () => { }); it(`should return '{version: ${process.env.API_VERSION}}' when hitting '/version' route`, async () => { - const result = await supertest(app).get('/v1/enquiry/version'); + const result = await supertest(app).get('/version'); const resultContent = JSON.parse(result.text) as { version: string }; expect(result.status).toEqual(200); @@ -37,7 +37,7 @@ describe('API', () => { }); it('returns a 404 if the method is not supported', async () => { - const resultPost = await supertest(app).post('/v1/enquiry/not-a-route'); + const resultPost = await supertest(app).post('/not-a-route'); expect(resultPost.status).toEqual(404); }); @@ -53,7 +53,7 @@ describe('API', () => { }; DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(enquiryService, 'getVehicleDetails').mockResolvedValue(vehicleDetails as VehicleDetails); - const result = await supertest(app).get('/v1/enquiry/vehicle?vinNumber=123456789'); + const result = await supertest(app).get('/vehicle?vinNumber=123456789'); const resultContent = JSON.parse(result.text) as VehicleDetails; expect(resultContent.vin).toEqual('vin1'); @@ -62,7 +62,7 @@ describe('API', () => { it('returns the error message if there is an error', async () => { DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(enquiryService, 'getVehicleDetails').mockRejectedValue(new Error('This is an error')); - const result = await supertest(app).get('/v1/enquiry/vehicle?vinNumber=123456789'); + const result = await supertest(app).get('/vehicle?vinNumber=123456789'); expect(result.text).toEqual('This is an error'); }); @@ -70,7 +70,7 @@ describe('API', () => { it('sets the status to 400 for a parameters error', async () => { DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(enquiryService, 'getVehicleDetails').mockRejectedValue(new ParametersError('This is an error')); - const result = await supertest(app).get('/v1/enquiry/vehicle?vinNumber=123456789'); + const result = await supertest(app).get('/vehicle?vinNumber=123456789'); expect(result.status).toEqual(400); }); @@ -78,7 +78,7 @@ describe('API', () => { it('sets the status to 404 for a not found error', async () => { DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(enquiryService, 'getVehicleDetails').mockRejectedValue(new NotFoundError()); - const result = await supertest(app).get('/v1/enquiry/vehicle?vinNumber=123456789'); + const result = await supertest(app).get('/vehicle?vinNumber=123456789'); expect(result.status).toEqual(404); }); @@ -86,7 +86,7 @@ describe('API', () => { it('sets the status to 500 for a generic error', async () => { DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(enquiryService, 'getVehicleDetails').mockRejectedValue(new Error('This is an error')); - const result = await supertest(app).get('/v1/enquiry/vehicle?vinNumber=123456789'); + const result = await supertest(app).get('/vehicle?vinNumber=123456789'); expect(result.status).toEqual(500); }); @@ -95,25 +95,25 @@ describe('API', () => { process.env.IS_OFFLINE = 'true'; DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(enquiryService, 'getVehicleDetails').mockRejectedValue(new Error('This is an error')); - const result = await supertest(app).get('/v1/enquiry/vehicle?vinNumber=123456789'); + const result = await supertest(app).get('/vehicle?vinNumber=123456789'); expect(result.status).toEqual(500); }); it('returns a 405 if the method is not supported', async () => { - const resultPost = await supertest(app).post('/v1/enquiry/vehicle?vinNumber=123456789'); + const resultPost = await supertest(app).post('/vehicle?vinNumber=123456789'); expect(resultPost.status).toEqual(405); - const resultPut = await supertest(app).put('/v1/enquiry/vehicle?vinNumber=123456789'); + const resultPut = await supertest(app).put('/vehicle?vinNumber=123456789'); expect(resultPut.status).toEqual(405); - const resultPatch = await supertest(app).patch('/v1/enquiry/vehicle?vinNumber=123456789'); + const resultPatch = await supertest(app).patch('/vehicle?vinNumber=123456789'); expect(resultPatch.status).toEqual(405); - const resultDelete = await supertest(app).delete('/v1/enquiry/vehicle?vinNumber=123456789'); + const resultDelete = await supertest(app).delete('/vehicle?vinNumber=123456789'); expect(resultDelete.status).toEqual(405); }); @@ -126,7 +126,7 @@ describe('API', () => { } as TestResult; DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(enquiryService, 'getResultsDetails').mockResolvedValue([resultDetails]); - const result = await supertest(app).get('/v1/enquiry/testResults?vinNumber=123456789'); + const result = await supertest(app).get('/testResults?vinNumber=123456789'); const resultContent = JSON.parse(result.text) as [TestResult]; expect(resultContent[0].testStatus).toEqual('Success'); @@ -135,7 +135,7 @@ describe('API', () => { it('returns the error message if there is an error', async () => { DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(enquiryService, 'getResultsDetails').mockRejectedValue(new Error('This is an error')); - const result = await supertest(app).get('/v1/enquiry/testResults?vinNumber=123456789'); + const result = await supertest(app).get('/testResults?vinNumber=123456789'); expect(result.text).toEqual('This is an error'); }); @@ -143,7 +143,7 @@ describe('API', () => { it('sets the status to 400 for a parameters error', async () => { DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(enquiryService, 'getResultsDetails').mockRejectedValue(new ParametersError('This is an error')); - const result = await supertest(app).get('/v1/enquiry/testResults?vinNumber=123456789'); + const result = await supertest(app).get('/testResults?vinNumber=123456789'); expect(result.status).toEqual(400); }); @@ -151,7 +151,7 @@ describe('API', () => { it('sets the status to 404 for a not found error', async () => { DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(enquiryService, 'getResultsDetails').mockRejectedValue(new NotFoundError('This is an error')); - const result = await supertest(app).get('/v1/enquiry/testResults?vinNumber=123456789'); + const result = await supertest(app).get('/testResults?vinNumber=123456789'); expect(result.status).toEqual(404); }); @@ -159,25 +159,25 @@ describe('API', () => { it('sets the status to 500 for a generic error', async () => { DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(enquiryService, 'getResultsDetails').mockRejectedValue(new Error('This is an error')); - const result = await supertest(app).get('/v1/enquiry/testResults?vinNumber=123456789'); + const result = await supertest(app).get('/testResults?vinNumber=123456789'); expect(result.status).toEqual(500); }); it('returns a 405 if the method is not supported', async () => { - const resultPost = await supertest(app).post('/v1/enquiry/testResults?vinNumber=123456789'); + const resultPost = await supertest(app).post('/testResults?vinNumber=123456789'); expect(resultPost.status).toEqual(405); - const resultPut = await supertest(app).put('/v1/enquiry/testResults?vinNumber=123456789'); + const resultPut = await supertest(app).put('/testResults?vinNumber=123456789'); expect(resultPut.status).toEqual(405); - const resultPatch = await supertest(app).patch('/v1/enquiry/testResults?vinNumber=123456789'); + const resultPatch = await supertest(app).patch('/testResults?vinNumber=123456789'); expect(resultPatch.status).toEqual(405); - const resultDelete = await supertest(app).delete('/v1/enquiry/testResults?vinNumber=123456789'); + const resultDelete = await supertest(app).delete('/testResults?vinNumber=123456789'); expect(resultDelete.status).toEqual(405); }); @@ -193,14 +193,14 @@ describe('API', () => { DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(upload, 'uploadToS3').mockImplementation((_data, _fileName, callback) => new Promise((resolve) => resolve(callback()))); jest.spyOn(enquiryService, 'getFeedDetails').mockResolvedValue([evlFeedData]); - const result = await supertest(app).get('/v1/enquiry/evl'); + const result = await supertest(app).get('/evl'); expect(result.status).toEqual(200); }); it('returns the error message if there is an error', async () => { DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(enquiryService, 'getFeedDetails').mockRejectedValue(new Error('This is an error')); - const result = await supertest(app).get('/v1/enquiry/evl'); + const result = await supertest(app).get('/evl'); expect(result.text).toEqual('Error Generating EVL Feed Data: This is an error'); }); @@ -208,7 +208,7 @@ describe('API', () => { it('sets the status to 400 for a parameters error', async () => { DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(enquiryService, 'getFeedDetails').mockRejectedValue(new ParametersError('This is an error')); - const result = await supertest(app).get('/v1/enquiry/evl'); + const result = await supertest(app).get('/evl'); expect(result.status).toEqual(400); }); @@ -216,7 +216,7 @@ describe('API', () => { it('sets the status to 404 for a not found error', async () => { DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(enquiryService, 'getFeedDetails').mockRejectedValue(new NotFoundError('This is an error')); - const result = await supertest(app).get('/v1/enquiry/evl'); + const result = await supertest(app).get('/evl'); expect(result.status).toEqual(404); }); @@ -224,7 +224,7 @@ describe('API', () => { it('sets the status to 500 for a generic error', async () => { DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(enquiryService, 'getFeedDetails').mockRejectedValue(new Error('This is an error')); - const result = await supertest(app).get('/v1/enquiry/evl'); + const result = await supertest(app).get('/evl'); expect(result.status).toEqual(500); }); @@ -247,14 +247,14 @@ describe('API', () => { DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(upload, 'uploadToS3').mockImplementation((_data, _fileName, callback) => new Promise((resolve) => resolve(callback()))); jest.spyOn(enquiryService, 'getFeedDetails').mockResolvedValue([tflFeedData]); - const result = await supertest(app).get('/v1/enquiry/tfl'); + const result = await supertest(app).get('/tfl'); expect(result.status).toEqual(200); }); it('returns the error message if there is an error', async () => { DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(enquiryService, 'getFeedDetails').mockRejectedValue(new Error('This is an error')); - const result = await supertest(app).get('/v1/enquiry/tfl'); + const result = await supertest(app).get('/tfl'); expect(result.text).toEqual('Error Generating TFL Feed Data: This is an error'); }); @@ -262,7 +262,7 @@ describe('API', () => { it('sets the status to 400 for a parameters error', async () => { DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(enquiryService, 'getFeedDetails').mockRejectedValue(new ParametersError('This is an error')); - const result = await supertest(app).get('/v1/enquiry/tfl'); + const result = await supertest(app).get('/tfl'); expect(result.status).toEqual(400); }); @@ -271,7 +271,7 @@ describe('API', () => { DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(upload, 'uploadToS3').mockImplementation((_data, _fileName, callback) => new Promise((resolve) => resolve(callback()))); jest.spyOn(enquiryService, 'getFeedDetails').mockRejectedValue(new NotFoundError('This is an error')); - const result = await supertest(app).get('/v1/enquiry/tfl'); + const result = await supertest(app).get('/tfl'); expect(result.status).toEqual(200); }); @@ -279,7 +279,7 @@ describe('API', () => { it('sets the status to 500 for a generic error', async () => { DatabaseService.build = jest.fn().mockResolvedValue({} as DatabaseServiceInterface); jest.spyOn(enquiryService, 'getFeedDetails').mockRejectedValue(new Error('This is an error')); - const result = await supertest(app).get('/v1/enquiry/tfl'); + const result = await supertest(app).get('/tfl'); expect(result.status).toEqual(500); });