diff --git a/src/domain/documentRequestFactory.ts b/src/domain/documentRequestFactory.ts new file mode 100644 index 0000000..20f826e --- /dev/null +++ b/src/domain/documentRequestFactory.ts @@ -0,0 +1,71 @@ +import AWS, { S3 } from 'aws-sdk'; +import { APIGatewayProxyResult } from 'aws-lambda'; +import getCertificate from './getCertificate'; +import getLetter from './getLetter'; +import getPlate from './getPlate'; + +const { + NODE_ENV, BUCKET, BRANCH, +} = process.env; + +export default async (vin: string, testNumber: string, plateSerialNumber: string, systemNumber: string): Promise => { + const s3 = new S3( + process.env.IS_OFFLINE && { + s3ForcePathStyle: true, + // You will need to create your s3local profile (~/.aws/credentials) if you are not using any + accessKeyId: 'S3RVER', + secretAccessKey: 'S3RVER', + endpoint: new AWS.Endpoint('http://localhost:4569'), + }, + ); + + const isCertificate = vin && testNumber && !plateSerialNumber && !systemNumber; + const isPlate = plateSerialNumber && !vin && !testNumber && !systemNumber; + const isLetter = !plateSerialNumber && vin && !testNumber && systemNumber; + + if (isCertificate) { + console.info('Calling cert service'); + return getCertificate( + { + vin, + testNumber, + }, + s3, + `cvs-cert-${BUCKET}`, + BRANCH, + NODE_ENV, + ); + } + + if (isPlate) { + console.info('Calling plate service'); + return getPlate( + { + plateSerialNumber, + }, + s3, + `cvs-cert-${BUCKET}`, + BRANCH, + NODE_ENV, + ); + } + + if (isLetter) { + console.info('Calling letter service'); + return getLetter( + { + vin, + systemNumber, + }, + s3, + `cvs-cert-${BUCKET}`, + BRANCH, + NODE_ENV, + ); + } + + return Promise.resolve({ + statusCode: 400, + body: '', + }); +}; diff --git a/src/infrastructure/api/index.ts b/src/infrastructure/api/index.ts index 3aafa61..e381cd1 100644 --- a/src/infrastructure/api/index.ts +++ b/src/infrastructure/api/index.ts @@ -1,14 +1,9 @@ -import AWS, { S3 } from 'aws-sdk'; import express, { Request, Response } from 'express'; -import getCertificate from '../../domain/getCertificate'; -import getLetter from '../../domain/getLetter'; -import getPlate from '../../domain/getPlate'; +import documentRequestFactory from '../../domain/documentRequestFactory'; const app = express(); -const { - API_VERSION, NODE_ENV, BUCKET, BRANCH, -} = process.env; +const { API_VERSION } = process.env; // Debug router before we start proxying requests from /v psth app.get('/', (_request, res) => { @@ -20,111 +15,24 @@ app.get('/version', (_request, res) => { }); app.get('/document-retrieval', (req: Request, res: Response) => { - if (req.query.vinNumber && req.query.testNumber && !req.query.plateSerialNumber && !req.query.systemNumber) { - console.info('Calling cert service'); - - getCertificate( - { - vin: req.query.vinNumber as string, - testNumber: req.query.testNumber as string, - }, - new S3( - process.env.IS_OFFLINE && { - s3ForcePathStyle: true, - // You will need to create your s3local profile (~/.aws/credentials) if you are not using any - accessKeyId: 'S3RVER', - secretAccessKey: 'S3RVER', - endpoint: new AWS.Endpoint('http://localhost:4569'), - }, - ), - `cvs-cert-${BUCKET}`, - BRANCH, - NODE_ENV, - ) - .then(({ statusCode, headers, body }) => { - res.status(statusCode); - - if (headers) { - res.header(headers); - } - - res.send(body); - }) - .catch((e: Error) => { - console.error(e.message); - res.status(500).send(e.message); - }); - } else if (req.query.plateSerialNumber && !req.query.vinNumber && !req.query.testNumber && !req.query.systemNumber) { - console.info('Calling plate service'); - - getPlate( - { - plateSerialNumber: req.query.plateSerialNumber as string, - }, - new S3( - process.env.IS_OFFLINE && { - s3ForcePathStyle: true, - // You will need to create your s3local profile (~/.aws/credentials) if you are not using any - accessKeyId: 'S3RVER', - secretAccessKey: 'S3RVER', - endpoint: new AWS.Endpoint('http://localhost:4569'), - }, - ), - `cvs-cert-${BUCKET}`, - BRANCH, - NODE_ENV, - ) - .then(({ statusCode, headers, body }) => { - res.status(statusCode); - - if (headers) { - res.header(headers); - } - - res.send(body); - }) - .catch((e: Error) => { - console.error(e.message); - res.status(500).send(e.message); - }); - } else if (!req.query.plateSerialNumber && req.query.vinNumber && !req.query.testNumber && req.query.systemNumber) { - console.info('Calling letter service'); - - - getLetter( - { - vin: req.query.vinNumber as string, - systemNumber: req.query.systemNumber as string, - }, - new S3( - process.env.IS_OFFLINE && { - s3ForcePathStyle: true, - // You will need to create your s3local profile (~/.aws/credentials) if you are not using any - accessKeyId: 'S3RVER', - secretAccessKey: 'S3RVER', - endpoint: new AWS.Endpoint('http://localhost:4569'), - }, - ), - `cvs-cert-${BUCKET}`, - BRANCH, - NODE_ENV, - ) - .then(({ statusCode, headers, body }) => { - res.status(statusCode); - - if (headers) { - res.header(headers); - } - - res.send(body); - }) - .catch((e: Error) => { - console.error(e.message); - res.status(500).send(e.message); - }); - } else { - res.status(400).end(); - } + const { + vinNumber, plateSerialNumber, testNumber, systemNumber, + } = req.query; + + documentRequestFactory(vinNumber as string, testNumber as string, plateSerialNumber as string, systemNumber as string) + .then(({ statusCode, headers, body }) => { + res.status(statusCode); + + if (headers) { + res.header(headers); + } + + res.send(body); + }) + .catch((e: Error) => { + console.error(e.message); + res.status(500).send(e.message); + }); }); app.all('/document-retrieval', (_request, res: Response) => {