-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update project serverless v3 get regions (#323)
* #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
1 parent
69c8189
commit 7952548
Showing
15 changed files
with
188 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
src/functions/get-all-regions/adapter/__tests__/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {} })); | ||
}); | ||
}); |
7 changes: 7 additions & 0 deletions
7
src/functions/get-all-regions/adapter/build-no-content-response.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
function buildNoContentReponse() { | ||
return { | ||
statusCode: 204, | ||
}; | ||
} | ||
|
||
export { buildNoContentReponse }; |
13 changes: 13 additions & 0 deletions
13
src/functions/get-all-regions/adapter/build-success-regions-response.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
10 changes: 10 additions & 0 deletions
10
src/functions/get-all-regions/adapter/remove-no-unsed-properties.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
src/functions/get-all-regions/use-case/__tests__/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
async function getAllRegions({ findRegions, tableName }) { | ||
return findRegions(tableName); | ||
} | ||
|
||
export { getAllRegions }; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.