Skip to content

Commit

Permalink
feat: finish endpoint to get an user by id
Browse files Browse the repository at this point in the history
  • Loading branch information
hdev14 committed Jan 18, 2024
1 parent 56b4f0a commit a0c90ce
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/api/middlewares/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { validationResult } from "express-validator";

export default function validator(request: Request, response: Response, next: NextFunction) {
const result = validationResult(request);
console.log(result);

if (!result.isEmpty()) {
return response.status(400).json({ errors: result.array() })
Expand Down
2 changes: 1 addition & 1 deletion src/api/routers/alerts.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('Alerts endpoints', () => {
describe.skip('Alerts endpoints', () => {
it('', () => {
console.log(globalThis.db_client);
console.log(globalThis.request);
Expand Down
41 changes: 39 additions & 2 deletions src/api/routers/users.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,50 @@ describe('Users endpoints', () => {
};

const response = await globalThis.request
.post('/v1/users')
.post('/api/users')
.set('Content-Type', 'application/json')
.send(body);

console.log(response.body.errors);
expect(response.status).toEqual(400);
expect(response.body.errors).toHaveLength(4);
});
});

describe('GET: /users/:id', () => {
const user_id = faker.string.uuid();

beforeAll(async () => {
await globalThis.db_client.query(
'INSERT INTO users (id, email, name, password, phone_number) VALUES ($1, $2, $3, $4, $5)',
[user_id, faker.internet.email(), faker.person.fullName(), faker.string.alphanumeric(10), faker.string.numeric(11)]
);
});

it('returns an user by id', async () => {
expect.assertions(5);

const response = await globalThis.request
.get(`/api/users/${user_id}`)
.set('Content-Type', 'application/json')
.send();

expect(response.status).toEqual(200);
expect(response.body.id).toEqual(user_id);
expect(response.body.name).toBeDefined();
expect(response.body.email).toBeDefined();
expect(response.body.phone_number).toBeDefined();
});

it("returns not found if user doesn't exist", async () => {
expect.assertions(2);

const response = await globalThis.request
.get(`/api/users/${faker.string.uuid()}`)
.set('Content-Type', 'application/json')
.send();

expect(response.status).toEqual(404);
expect(response.body.message).toEqual('User not found');
});
});
});
10 changes: 8 additions & 2 deletions src/api/routers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import UserService from '@b3_stock_alerts/UserService';
import { NextFunction, Request, Response, Router } from 'express';
import { checkSchema } from 'express-validator';
import { create_user } from './validations';
import NotFoundError from '@shared/NotFoundError';

const router = Router();
const userService = new UserService(new PgUserRepository(), new BcryptEncryptor());
Expand Down Expand Up @@ -32,9 +33,14 @@ router.post('/users',
}
);

router.get('/users/:id', (_request: Request, response: Response, next: NextFunction) => {
router.get('/users/:id', async (request: Request, response: Response, next: NextFunction) => {
try {
return response.status(204).json();
const result = await userService.getUser(request.params.id);
if (result.error instanceof NotFoundError) {
return response.status(404).json({ message: result.error.message });
}

return response.status(200).json(result.data);
} catch (e) {
return next(e);
}
Expand Down

0 comments on commit a0c90ce

Please sign in to comment.