Skip to content

Commit

Permalink
feat: Check if the sort by and direction are well defined if defined
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinszuchet committed May 9, 2023
1 parent f62d4f6 commit 469b1aa
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
24 changes: 22 additions & 2 deletions src/controllers/handlers/lists-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,32 @@ export async function getListsHandler(
const sortBy = url.searchParams.get('sortBy') as ListSortBy | undefined
const sortDirection = url.searchParams.get('sortDirection') as ListSortDirection | undefined

if (sortBy && !Object.values(ListSortBy).includes(sortBy)) {
return {
status: StatusCode.BAD_REQUEST,
body: {
ok: false,
message: 'The sort by parameter is not defined as date or name.'
}
}
}

if (sortDirection && !Object.values(ListSortDirection).includes(sortDirection)) {
return {
status: StatusCode.BAD_REQUEST,
body: {
ok: false,
message: 'The sort direction parameter is not defined as asc or desc.'
}
}
}

const listsResult = await listsComponent.getLists({
userAddress,
limit,
offset,
sortBy: sortBy && Object.values(ListSortBy).includes(sortBy) ? sortBy : undefined,
sortDirection: sortDirection && Object.values(ListSortDirection).includes(sortDirection) ? sortDirection : undefined
sortBy,
sortDirection
})
const { lists, count } = fromDBGetListsToListsWithCount(listsResult)

Expand Down
4 changes: 2 additions & 2 deletions src/ports/lists/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function createListsComponent(
}

async function getLists(params: GetListsParameters): Promise<DBGetListsWithCount[]> {
const { userAddress, limit, offset, sortBy = ListSortBy.DATE, sortDirection = ListSortDirection.DESC } = params
const { userAddress, limit, offset, sortBy = ListSortBy.CREATED_AT, sortDirection = ListSortDirection.DESC } = params
const query = SQL`
SELECT l.*, COUNT(*) OVER() as lists_count, user_address = ${DEFAULT_LIST_USER_ADDRESS} as is_default_list
FROM favorites.lists l
Expand All @@ -132,7 +132,7 @@ export function createListsComponent(
const orderByQuery = SQL`ORDER BY is_default_list DESC`

switch (sortBy) {
case ListSortBy.DATE:
case ListSortBy.CREATED_AT:
orderByQuery.append(SQL`, created_at ${sortDirection}`)
break
case ListSortBy.NAME:
Expand Down
2 changes: 1 addition & 1 deletion src/ports/lists/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export type AddListRequestBody = {
}

export enum ListSortBy {
DATE = 'created_at',
CREATED_AT = 'createdAt',
NAME = 'name'
}

Expand Down
2 changes: 1 addition & 1 deletion test/unit/lists-component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ describe('when getting lists', () => {
offset: 0,
limit: 10,
userAddress: '0xuseraddress',
sortBy: ListSortBy.DATE,
sortBy: ListSortBy.CREATED_AT,
sortDirection
})
).resolves.toEqual(dbGetLists)
Expand Down
46 changes: 46 additions & 0 deletions test/unit/lists-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,52 @@ describe('when getting the lists', () => {
})
})

describe('and the sort by parameter has an incorrect value', () => {
beforeEach(() => {
url = new URL('http://localhost/v1/lists?sortBy=incorrectValue')
})

it('should return a bad request response', () => {
return expect(
getListsHandler({
url,
components,
verification
})
).resolves.toEqual({
status: StatusCode.BAD_REQUEST,
body: {
ok: false,
message: 'The sort by parameter is not defined as date or name.',
data: undefined
}
})
})
})

describe('and the sort direction parameter has an incorrect value', () => {
beforeEach(() => {
url = new URL('http://localhost/v1/lists?sortBy=name&sortDirection=incorrectValue')
})

it('should return a bad request response', () => {
return expect(
getListsHandler({
url,
components,
verification
})
).resolves.toEqual({
status: StatusCode.BAD_REQUEST,
body: {
ok: false,
message: 'The sort direction parameter is not defined as asc or desc.',
data: undefined
}
})
})
})

describe('and the process to get the lists fails', () => {
let error: Error

Expand Down

0 comments on commit 469b1aa

Please sign in to comment.