Skip to content

Commit

Permalink
chore(cb2-10273): remove duplicate doc retrieval code
Browse files Browse the repository at this point in the history
  • Loading branch information
me-matt authored Jan 10, 2024
1 parent 510f0e9 commit c3419d1
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 112 deletions.
71 changes: 71 additions & 0 deletions src/domain/documentRequestFactory.ts
Original file line number Diff line number Diff line change
@@ -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<APIGatewayProxyResult> => {
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: '',
});
};
132 changes: 20 additions & 112 deletions src/infrastructure/api/index.ts
Original file line number Diff line number Diff line change
@@ -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<x> psth
app.get('/', (_request, res) => {
Expand All @@ -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) => {
Expand Down

0 comments on commit c3419d1

Please sign in to comment.