Skip to content

Commit

Permalink
Update project serverless v3 get regions (#323)
Browse files Browse the repository at this point in the history
* #319 - create the GET regions endpoint flow

* #319 - fix findregions query must has draft as false by default

* #319 - put a one week cache max age value in the GET regions endpoints

* #319 - created the unit test to the get all regions function
  • Loading branch information
malaquiasdev authored Mar 27, 2021
1 parent 69c8189 commit 7952548
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 23 deletions.
2 changes: 1 addition & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ provider:

functions:
- ${file(src/functions/create-new-region/function.yml)}
- ${file(src/functions/get-regions/function.yml)}
- ${file(src/functions/get-all-regions/function.yml)}

resources:
- ${file(resources/api-gateway.yml)}
Expand Down
12 changes: 12 additions & 0 deletions src/components/dynamodb/querys/find-regions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import AWS from 'aws-sdk';
import { convertRecordsToObject } from '../utils/convert-records-to-object';

const dynamoDB = new AWS.DynamoDB();

async function findRegions(tableName) {
const statement = `SELECT * FROM "${tableName}" WHERE "draft" = 'false'`;
const { Items } = await dynamoDB.executeStatement({ Statement: statement }).promise();
return convertRecordsToObject(Items);
}

export { findRegions };
59 changes: 59 additions & 0 deletions src/functions/get-all-regions/adapter/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-disable max-lines */
import { adapterRegionsResponse } from '../index';

describe('# Functions - GetAllRegions - adapter', () => {
it('## should return a success response 200 OK with a list of regions and no cache max age', async () => {
const data = [
{
uf: 'CE',
label: 'Ceará',
draft: 'false',
},
{
uf: 'BA',
label: 'Bahia',
draft: 'false',
},
];
const compareBody = {
regions: [
{ label: 'Ceará', uf: 'CE' },
{ label: 'Bahia', uf: 'BA' },
],
};
const regions = await adapterRegionsResponse(data, 0);
expect(typeof regions).toBe('object');
expect(regions.statusCode).toBe(200);
expect(regions.body).toMatchObject(compareBody);
expect(regions.headers.maxAge).toBe(0);
});

it('## should return a success response 200 OK with a list of regions and cache value', async () => {
const data = [
{
uf: 'BA',
label: 'Bahia',
draft: 'false',
},
];
const compareBody = {
regions: [{ label: 'Bahia', uf: 'BA' }],
};
const regions = await adapterRegionsResponse(data, 800);

expect(typeof regions).toBe('object');
expect(regions.statusCode).toBe(200);
expect(regions.body).toMatchObject(compareBody);
expect(regions.headers.maxAge).toBe(800);
});

it('## should return a success response 204 No Content because data is empty', async () => {
const data = [];
const regions = await adapterRegionsResponse(data, 800);

expect(typeof regions).toBe('object');
expect(regions.statusCode).toBe(204);
expect(regions).toEqual(expect.not.objectContaining({ body: {} }));
expect(regions).toEqual(expect.not.objectContaining({ headers: {} }));
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function buildNoContentReponse() {
return {
statusCode: 204,
};
}

export { buildNoContentReponse };
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function buildSuccessRegionsResponse(regions, cacheMaxAge) {
return {
body: {
regions,
},
statusCode: 200,
headers: {
maxAge: cacheMaxAge,
},
};
}

export { buildSuccessRegionsResponse };
13 changes: 13 additions & 0 deletions src/functions/get-all-regions/adapter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { isRegionsEmpty } from './is-region-empty';
import { buildNoContentReponse } from './build-no-content-response';
import { removeNoUnsedProperties } from './remove-no-unsed-properties';
import { buildSuccessRegionsResponse } from './build-success-regions-response';

function adapterRegionsResponse(data, cacheMaxAge) {
if (isRegionsEmpty(data)) {
return buildNoContentReponse();
}
return buildSuccessRegionsResponse(removeNoUnsedProperties(data), cacheMaxAge);
}

export { adapterRegionsResponse };
5 changes: 5 additions & 0 deletions src/functions/get-all-regions/adapter/is-region-empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function isRegionsEmpty(regions) {
return Array.isArray(regions) && regions.length === 0;
}

export { isRegionsEmpty };
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function removeNoUnsedProperties(regions) {
return regions.map(({ uf, label }) => {
return {
uf,
label,
};
});
}

export { removeNoUnsedProperties };
6 changes: 6 additions & 0 deletions src/functions/get-all-regions/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const config = {
regionTableName: process.env.regionTableName,
cacheMaxAge: process.env.CACHE_MAX_AGE || 0,
};

export { config };
9 changes: 9 additions & 0 deletions src/functions/get-all-regions/function.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
get-all-regions:
handler: src/functions/get-all-regions/handler.handlerGetAllRegions
events:
- http:
path: regions
method: get
cors: true
environment:
CACHE_MAX_AGE: 604800
20 changes: 20 additions & 0 deletions src/functions/get-all-regions/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { config } from './config';
import { findRegions } from '../../components/dynamodb/querys/find-regions';
import { handlerSuccess } from '../../libs/http/response/handler-success';
import { handlerResponseError } from '../../libs/http/response/handler-error';
import { getAllRegions } from './use-case';
import { adapterRegionsResponse } from './adapter';

async function handlerGetAllRegions(event, context) {
try {
const regions = await getAllRegions({ findRegions, tableName: config.regionTableName });
return handlerSuccess(
adapterRegionsResponse(regions, config.cacheMaxAge),
context.awsRequestId,
);
} catch (error) {
return handlerResponseError(error, context.awsRequestId);
}
}

export { handlerGetAllRegions };
28 changes: 28 additions & 0 deletions src/functions/get-all-regions/use-case/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { getAllRegions } from '../index';

describe('# Functions - GetAllRegions - use-case', () => {
it('## should return a list of regions', async () => {
const mock = [
{
uf: 'CE',
label: 'Ceará',
draft: 'false',
},
{
uf: 'BA',
label: 'Bahia',
draft: 'false',
},
];

const findRegions = async tableName => {
return mock;
};
const tableName = 'mocked-table-data';

const regions = await getAllRegions({ findRegions, tableName });

expect(Array.isArray(regions)).toBe(true);
expect(regions).toMatchObject(mock);
});
});
5 changes: 5 additions & 0 deletions src/functions/get-all-regions/use-case/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
async function getAllRegions({ findRegions, tableName }) {
return findRegions(tableName);
}

export { getAllRegions };
7 changes: 0 additions & 7 deletions src/functions/get-regions/function.yml

This file was deleted.

15 changes: 0 additions & 15 deletions src/functions/get-regions/handler.js

This file was deleted.

0 comments on commit 7952548

Please sign in to comment.