diff --git a/src/controller/project/createProject.spec.ts b/src/controller/project/createProject.spec.ts index bb894b9..0f21082 100644 --- a/src/controller/project/createProject.spec.ts +++ b/src/controller/project/createProject.spec.ts @@ -1,7 +1,6 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest' import request from 'supertest' import { app } from '../../app' -import { randomUUID } from 'crypto' import { createAndAuthenticateUser } from '../../utils/tests/create-and-authenticate-user' let userAuth: { @@ -28,7 +27,7 @@ describe('createProject E2E', () => { } const createProjectResponse = await request(app.server) - .post(`/user/${userAuth.userId}/project`) + .post(`/user/project`) .send(createProjectBody) .set('Authorization', `Bearer ${userAuth.token}`) @@ -37,7 +36,7 @@ describe('createProject E2E', () => { expect(createProjectResponse.body.project.tags).toEqual(['react', 'node']) }) - it('should not be able to create a project without user', async () => { + it('should not be able to create a project without authenticate', async () => { const createProjectBody = { title: 'Squad40 Project', tags: ['react', 'node'], @@ -45,15 +44,15 @@ describe('createProject E2E', () => { description: 'Squad40 description', } - const userId = randomUUID() - const response = await request(app.server) - .post(`/user/${userId}/project`) + .post(`/user/project`) .send(createProjectBody) - .set('Authorization', `Bearer ${userAuth.token}`) - - expect(response.body.message).toContain('User was not Found !') - expect(response.status).toEqual(404) + expect(response.status).toEqual(401) + expect(response.body).toEqual( + expect.objectContaining({ + message: 'Unauthorized', + }), + ) }) }) diff --git a/src/controller/project/createProject.ts b/src/controller/project/createProject.ts index 24496b5..1da5b7f 100644 --- a/src/controller/project/createProject.ts +++ b/src/controller/project/createProject.ts @@ -3,7 +3,6 @@ import { z } from 'zod' import { CreateProjectUseCase } from '../../use-cases/project/createProjectUseCase' import { PrismaProjectRepository } from '../../repositories/prisma/prisma-project-repository' import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository' -import { ResourceNotFoundError } from '../../use-cases/errors/ResourceNotFoundError' export async function createProject( request: FastifyRequest, @@ -16,36 +15,23 @@ export async function createProject( description: z.string(), }) - const createProjectParamsSchema = z.object({ - userId: z.string().uuid(), - }) - const { title, tags, link, description } = createProjectBodySchema.parse( request.body, ) - const { userId } = createProjectParamsSchema.parse(request.params) - const userRepository = new PrismaUsersRepository() const projectRepository = new PrismaProjectRepository() const createProjectUseCase = new CreateProjectUseCase( projectRepository, userRepository, ) - try { - const { project } = await createProjectUseCase.execute({ - userId, - title, - tags, - link, - description, - }) - return response.status(201).send({ project }) - } catch (error) { - if (error instanceof ResourceNotFoundError) { - return response.status(404).send({ message: 'User was not Found !' }) - } + const { project } = await createProjectUseCase.execute({ + userId: request.user.sub, + title, + tags, + link, + description, + }) - throw error - } + return response.status(201).send({ project }) } diff --git a/src/controller/project/editProject.spec.ts b/src/controller/project/editProject.spec.ts index a7f6c12..b7d1e21 100644 --- a/src/controller/project/editProject.spec.ts +++ b/src/controller/project/editProject.spec.ts @@ -28,7 +28,7 @@ describe('edit Project E2E', () => { } const createProjectResponse = await request(app.server) - .post(`/user/${userAuth.userId}/project`) + .post(`/user/project`) .send(createProjectBody) .set('Authorization', `Bearer ${userAuth.token}`) diff --git a/src/controller/project/getProjectsByTags.spec.ts b/src/controller/project/getProjectsByTags.spec.ts index 21a952c..899dd6e 100644 --- a/src/controller/project/getProjectsByTags.spec.ts +++ b/src/controller/project/getProjectsByTags.spec.ts @@ -42,71 +42,64 @@ describe('Get Projets By Tags E2E', () => { tags: ['tag7', 'tag8', 'tag9'], }, ] - console.log('Creating projects') for (const project of projectsToBeCreate) { await request(app.server) - .post(`/user/${userAuth.userId}/project`) + .post(`/user/project`) .set('Authorization', `Bearer ${userAuth.token}`) - .send(project) } - console.log('Finish Creating projects') - - console.log('Get By tags projects') - const getProjectsByTagsResponse = await request(app.server) .post(`/projects/tags`) .send({ tags }) .set('Authorization', `Bearer ${userAuth.token}`) - console.log('End By tags projects') expect(getProjectsByTagsResponse.statusCode).toEqual(200) expect(getProjectsByTagsResponse.body.projects).toHaveLength(2) expect(getProjectsByTagsResponse.body.projects[0]).toEqual( expect.objectContaining({ - title: 'Project 01', + ...projectsToBeCreate[0], user: { name: 'John', surname: 'Doe', avatar_url: expect.any(String) }, }), ) expect(getProjectsByTagsResponse.body.projects[1]).toEqual( expect.objectContaining({ - title: 'Project 02', + ...projectsToBeCreate[1], user: { name: 'John', surname: 'Doe', avatar_url: expect.any(String) }, }), ) }) - // it('should return 200 and empty object when not find projects by some tag', async () => { - // const tags = ['tagNotExist', 'tagNotExist'] + it('should return 200 and empty object when not find projects by some tag', async () => { + const tags = ['tagNotExist', 'tagNotExist'] - // const getProjectsByTagsResponse = await request(app.server) - // .post(`/projects/tags`) - // .send({ tags }) - // .set('Authorization', `Bearer ${userAuth.token}`) + const getProjectsByTagsResponse = await request(app.server) + .post(`/projects/tags`) + .send({ tags }) + .set('Authorization', `Bearer ${userAuth.token}`) - // expect(getProjectsByTagsResponse.statusCode).toEqual(200) - // expect(getProjectsByTagsResponse.body.projects).toHaveLength(0) - // }) + expect(getProjectsByTagsResponse.statusCode).toEqual(200) + expect(getProjectsByTagsResponse.body.projects).toHaveLength(0) + }) - // it('should be able to get all projects NOT BEING case- sensitive', async () => { - // const tags = ['tAG7', 'TAG8', 'Tag9'] + it('should be able to get all projects NOT BEING case- sensitive', async () => { + const tags = ['tAG7', 'TAG8', 'Tag9'] - // // Projects with tags ['tag7', 'tag8', 'tag9'] are already registered - // // once the database is set up once per file. + // Projects with tags ['tag7', 'tag8', 'tag9'] are already registered + // once the database is set up once per file. - // const getProjectsByTagsResponse = await request(app.server) - // .post(`/projects/tags`) - // .send({ tags }) - // .set('Authorization', `Bearer ${userAuth.token}`) + const getProjectsByTagsResponse = await request(app.server) + .post(`/projects/tags`) + .send({ tags }) + .set('Authorization', `Bearer ${userAuth.token}`) - // expect(getProjectsByTagsResponse.statusCode).toEqual(200) - // expect(getProjectsByTagsResponse.body.projects).toHaveLength(1) - // expect(getProjectsByTagsResponse.body.projects[0]).toEqual( - // expect.objectContaining({ - // title: 'Project 03', - // user: { name: 'John', surname: 'Doe', avatar_url: expect.any(String) }, - // }), - // ) - // }) + expect(getProjectsByTagsResponse.statusCode).toEqual(200) + expect(getProjectsByTagsResponse.body.projects).toHaveLength(1) + expect(getProjectsByTagsResponse.body.projects[0]).toEqual( + expect.objectContaining({ + title: 'Project 03', + user: { name: 'John', surname: 'Doe', avatar_url: expect.any(String) }, + }), + ) + }) }) diff --git a/src/controller/project/getProjectsByUserId.spec.ts b/src/controller/project/getProjectsByUserId.spec.ts index c74852f..c847428 100644 --- a/src/controller/project/getProjectsByUserId.spec.ts +++ b/src/controller/project/getProjectsByUserId.spec.ts @@ -3,7 +3,6 @@ import request from 'supertest' import { app } from '../../app' import { ProjectRepository } from '../../repositories/project-repository' import { PrismaProjectRepository } from '../../repositories/prisma/prisma-project-repository' -import { randomUUID } from 'crypto' import { createAndAuthenticateUser } from '../../utils/tests/create-and-authenticate-user' let projectRepository: ProjectRepository @@ -50,7 +49,7 @@ describe('Get Projets By UserId E2E', () => { }) const getProjectsByUserIdResponse = await request(app.server) - .get(`/projects/${userAuth.userId}`) + .get(`/projects`) .set('Authorization', `Bearer ${userAuth.token}`) expect(getProjectsByUserIdResponse.statusCode).toEqual(200) @@ -64,16 +63,16 @@ describe('Get Projets By UserId E2E', () => { ) }) - it('should not be able to project that user does not exist', async () => { - const getProjectsByUserIdResponse = await request(app.server) - .get(`/projects/${randomUUID()}`) - .set('Authorization', `Bearer ${userAuth.token}`) + it('should not be able to get projects without authenticate', async () => { + const getProjectsByUserIdResponse = await request(app.server).get( + `/projects`, + ) - expect(getProjectsByUserIdResponse.statusCode).toEqual(404) + expect(getProjectsByUserIdResponse.statusCode).toEqual(401) expect(getProjectsByUserIdResponse.body).toEqual( expect.objectContaining({ - error: 'User was not Found !', + message: 'Unauthorized', }), ) }) diff --git a/src/controller/project/getProjectsByUserId.ts b/src/controller/project/getProjectsByUserId.ts index 058cd54..7614ff4 100644 --- a/src/controller/project/getProjectsByUserId.ts +++ b/src/controller/project/getProjectsByUserId.ts @@ -1,9 +1,7 @@ import { FastifyReply, FastifyRequest } from 'fastify' -import { z } from 'zod' import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository' import { GetProjectsByUserIdUseCase } from '../../use-cases/project/getProjectsByUserIdUseCase' import { PrismaProjectRepository } from '../../repositories/prisma/prisma-project-repository' -import { ResourceNotFoundError } from '../../use-cases/errors/ResourceNotFoundError' export async function getProjectsByUserId( request: FastifyRequest, @@ -16,18 +14,8 @@ export async function getProjectsByUserId( userRepository, ) - const GetProjectByUserIdParamsSchema = z.object({ - userId: z.string().uuid(), + const { projects } = await getProjectByUserId.execute({ + userId: request.user.sub, }) - - const { userId } = GetProjectByUserIdParamsSchema.parse(request.params) - - try { - const { projects } = await getProjectByUserId.execute({ userId }) - return response.status(200).send({ projects }) - } catch (error) { - if (error instanceof ResourceNotFoundError) { - return response.status(404).send({ error: 'User was not Found !' }) - } - } + return response.status(200).send({ projects }) } diff --git a/src/controller/project/routes.ts b/src/controller/project/routes.ts index eb130f4..19e7e9b 100644 --- a/src/controller/project/routes.ts +++ b/src/controller/project/routes.ts @@ -18,7 +18,7 @@ export async function projectRoutes(app: FastifyInstance) { }) app.post('/projects/tags', { onRequest: verifyJWT }, getProjectsByTags) - app.get('/projects/:userId', { onRequest: verifyJWT }, getProjectsByUserId) + app.get('/projects', { onRequest: verifyJWT }, getProjectsByUserId) app.get('/project/:projectId', { onRequest: verifyJWT }, getProjectsById) app.post( @@ -26,7 +26,7 @@ export async function projectRoutes(app: FastifyInstance) { { onRequest: verifyJWT }, addImageProject, ) - app.post('/user/:userId/project', { onRequest: verifyJWT }, createProject) + app.post('/user/project', { onRequest: verifyJWT }, createProject) app.put('/project/:projectId/edit', { onRequest: verifyJWT }, editProject) app.delete('/project/:projectId', { onRequest: verifyJWT }, deleteProjectById) diff --git a/src/controller/session/authUser.spec.ts b/src/controller/session/authUser.spec.ts index ea67497..1ddd307 100644 --- a/src/controller/session/authUser.spec.ts +++ b/src/controller/session/authUser.spec.ts @@ -1,10 +1,15 @@ import { afterAll, beforeAll, describe, expect, test } from 'vitest' import { app } from '../../app' import request from 'supertest' - +import { createAndAuthenticateUser } from '../../utils/tests/create-and-authenticate-user' +let userAuth: { + token: string + userId: string +} describe('User Login E2E', () => { beforeAll(async () => { await app.ready() + userAuth = await createAndAuthenticateUser(app) }) afterAll(async () => { @@ -12,17 +17,8 @@ describe('User Login E2E', () => { }) test('should be able to login', async () => { - const email = 'john_doe@email.com.br' - const name = 'John' - const surname = 'Doe' - const password = 'password' - - await request(app.server).post('/user').send({ - email, - name, - surname, - password, - }) + const email = 'johndoe@example.com' + const password = '12345678' const userData = await request(app.server) .post('/login') @@ -30,50 +26,28 @@ describe('User Login E2E', () => { expect(userData.statusCode).toEqual(200) expect(userData.body).toEqual({ - user: expect.any(Object), - token: expect.any(String), + token: userAuth.token, }) }) test('should not be able to login because the password is incorrect', async () => { - const email = 'john_doe@email.com.br' - const name = 'John' - const surname = 'Doe' - const password = 'password' - const wrongPassword = 'wrongPassword' - - await request(app.server).post('/user').send({ - email, - name, - surname, - password, - }) - + const email = 'johndoe@example.com' + const password = 'wrongpass' const userData = await request(app.server) .post('/login') - .send({ email, password: wrongPassword }) + .send({ email, password }) expect(userData.statusCode).toEqual(401) expect(userData.body.user).toEqual(expect.objectContaining({})) }) test('should not be able to login because the email is incorrect', async () => { - const email = 'john_doe@email.com.br' - const wrongEmail = 'wrong@email.com.br' - const name = 'John' - const surname = 'Doe' - const password = 'password' - - await request(app.server).post('/user').send({ - email, - name, - surname, - password, - }) + const email = 'wrongemail@example.com' + const password = '12345678' const userData = await request(app.server) .post('/login') - .send({ email: wrongEmail, password }) + .send({ email, password }) expect(userData.statusCode).toEqual(401) expect(userData.body.user).toEqual(expect.objectContaining({})) diff --git a/src/controller/session/authUser.ts b/src/controller/session/authUser.ts index b75c9b7..e077714 100644 --- a/src/controller/session/authUser.ts +++ b/src/controller/session/authUser.ts @@ -30,7 +30,7 @@ export async function authUser( }, ) - return response.status(200).send({ user, token }) + return response.status(200).send({ token }) } catch (e) { if (e instanceof InvalidCredentialsError) { return response.status(401).send() diff --git a/src/controller/user/addImageToUser.ts b/src/controller/user/addImageToUser.ts index 333c2a9..e14cac9 100644 --- a/src/controller/user/addImageToUser.ts +++ b/src/controller/user/addImageToUser.ts @@ -1,5 +1,4 @@ import { FastifyReply, FastifyRequest } from 'fastify' -import { z } from 'zod' import { ResourceNotFoundError } from '../../use-cases/errors/ResourceNotFoundError' import { AwsS3Error } from '../../use-cases/errors/AwsS3Error' import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository' @@ -11,11 +10,7 @@ export async function addImageUser( ) { const userRepository = new PrismaUsersRepository() const addImageToUserUseCase = new AddImageToUserUseCase(userRepository) - const addImageUserParamsSchema = z.object({ - userId: z.string().uuid(), - }) - const { userId } = addImageUserParamsSchema.parse(request.params) const photo = await request.file() if (photo === undefined) { @@ -23,7 +18,10 @@ export async function addImageUser( } try { - const { user } = await addImageToUserUseCase.execute({ userId, photo }) + const { user } = await addImageToUserUseCase.execute({ + userId: request.user.sub, + photo, + }) return response .status(200) .send({ user: { ...user, password_hash: undefined } }) diff --git a/src/controller/user/editUserById.spec.ts b/src/controller/user/editUserById.spec.ts index 8469036..2577057 100644 --- a/src/controller/user/editUserById.spec.ts +++ b/src/controller/user/editUserById.spec.ts @@ -1,7 +1,6 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest' import request from 'supertest' import { app } from '../../app' -import { randomUUID } from 'crypto' import { createAndAuthenticateUser } from '../../utils/tests/create-and-authenticate-user' let userAuth: { @@ -21,7 +20,7 @@ describe('edit User E2E', () => { it('should be able to edit a user', async () => { const editUserResponse = await request(app.server) - .put(`/user/${userAuth.userId}/edit`) + .put(`/user/edit`) .send({ name: 'newName', surname: 'surname', @@ -40,21 +39,18 @@ describe('edit User E2E', () => { ) }) - it('should not be able to edit a user that does not exist', async () => { - const editUserResponse = await request(app.server) - .put(`/user/${randomUUID()}/edit`) - .set('Authorization', `Bearer ${userAuth.token}`) - .send({ - name: 'newName', - surname: 'surname', - country: 'country', - }) + it('should not be able to edit a user without authentication', async () => { + const editUserResponse = await request(app.server).put(`/user/edit`).send({ + name: 'newName', + surname: 'surname', + country: 'country', + }) - expect(editUserResponse.statusCode).toEqual(404) + expect(editUserResponse.statusCode).toEqual(401) expect(editUserResponse.body).toEqual( expect.objectContaining({ - error: 'User was not Found !', + message: 'Unauthorized', }), ) }) diff --git a/src/controller/user/editUserById.ts b/src/controller/user/editUserById.ts index f80d69b..4442d4a 100644 --- a/src/controller/user/editUserById.ts +++ b/src/controller/user/editUserById.ts @@ -14,30 +14,19 @@ export async function editUserById( country: z.string(), }) - const editUserParamsSchema = z.object({ - userId: z.string().uuid(), - }) - const { name, surname, country } = editUserBodySchema.parse(request.body) - const { userId } = editUserParamsSchema.parse(request.params) const userRepository = new PrismaUsersRepository() const editUserUseCase = new EditUserUseCase(userRepository) - try { - const { user } = await editUserUseCase.execute({ - name, - surname, - country, - userId, - }) + const { user } = await editUserUseCase.execute({ + name, + surname, + country, + userId: request.user.sub, + }) - return response - .status(200) - .send({ user: { ...user, password_hash: undefined } }) - } catch (error) { - if (error instanceof ResourceNotFoundError) { - return response.status(404).send({ error: 'User was not Found !' }) - } - } + return response + .status(200) + .send({ user: { ...user, password_hash: undefined } }) } diff --git a/src/controller/user/editUserPassword.spec.ts b/src/controller/user/editUserPassword.spec.ts index 33aa835..1a09aa7 100644 --- a/src/controller/user/editUserPassword.spec.ts +++ b/src/controller/user/editUserPassword.spec.ts @@ -43,7 +43,6 @@ describe('edit User Pass E2E', () => { expect(userData.statusCode).toEqual(200) expect(userData.body).toEqual({ - user: expect.any(Object), token: expect.any(String), }) }) diff --git a/src/controller/user/editUserPassword.ts b/src/controller/user/editUserPassword.ts index 7574d4d..5a89f11 100644 --- a/src/controller/user/editUserPassword.ts +++ b/src/controller/user/editUserPassword.ts @@ -28,9 +28,7 @@ export async function editUserPassword( .status(200) .send({ user: { ...user, password_hash: undefined } }) } catch (error) { - if (error instanceof ResourceNotFoundError) { - return response.status(404).send({ error: 'User was not Found !' }) - } else if (error instanceof InvalidCredentialsError) { + if (error instanceof InvalidCredentialsError) { return response.status(401).send({ error: 'Invalid old Password!' }) } diff --git a/src/controller/user/getUserByEmail.spec.ts b/src/controller/user/getUserByEmail.spec.ts deleted file mode 100644 index 0d4afae..0000000 --- a/src/controller/user/getUserByEmail.spec.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { afterAll, beforeAll, describe, expect, test } from 'vitest' -import request from 'supertest' -import { app } from '../../app' -import { createAndAuthenticateUser } from '../../utils/tests/create-and-authenticate-user' - -let userAuth: { - token: string - userId: string -} - -describe('Get User By email E2E', () => { - beforeAll(async () => { - await app.ready() - userAuth = await createAndAuthenticateUser(app) - }) - - afterAll(async () => { - await app.close() - }) - - test('should be able to get an user by e-mail', async () => { - const getUserByEmailResponse = await request(app.server) - .get(`/user`) - .query({ email: 'johndoe@example.com' }) - .set('Authorization', `Bearer ${userAuth.token}`) - - expect(getUserByEmailResponse.statusCode).toEqual(200) - expect(getUserByEmailResponse.body.user).toEqual( - expect.objectContaining({ - email: 'johndoe@example.com', - id: userAuth.userId, - country: 'Brazil', - }), - ) - }) - - test('should not be able to get an user by e-mail that does not exist', async () => { - const email = 'userwasnotregistered@email.com' - - const getUserByEmailResponse = await request(app.server) - .get(`/user`) - .query({ email }) - .set('Authorization', `Bearer ${userAuth.token}`) - - expect(getUserByEmailResponse.statusCode).toEqual(404) - expect(getUserByEmailResponse.body.user).toEqual( - expect.objectContaining({}), - ) - }) -}) diff --git a/src/controller/user/getUserById.spec.ts b/src/controller/user/getUserById.spec.ts index 8f82d01..30468c3 100644 --- a/src/controller/user/getUserById.spec.ts +++ b/src/controller/user/getUserById.spec.ts @@ -2,17 +2,10 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest' import request from 'supertest' import { app } from '../../app' import { createAndAuthenticateUser } from '../../utils/tests/create-and-authenticate-user' -import { randomUUID } from 'crypto' - -let userAuth: { - token: string - userId: string -} describe('Get User By Id E2E', () => { beforeAll(async () => { await app.ready() - userAuth = await createAndAuthenticateUser(app) }) afterAll(async () => { @@ -20,42 +13,28 @@ describe('Get User By Id E2E', () => { }) it('should be able to get an user by ID', async () => { + const { token } = await createAndAuthenticateUser(app) const getUserByIdResponse = await request(app.server) - .get(`/user/${userAuth.userId}`) - .set('Authorization', `Bearer ${userAuth.token}`) + .get(`/user`) + .set('Authorization', `Bearer ${token}`) expect(getUserByIdResponse.statusCode).toEqual(200) expect(getUserByIdResponse.body.user).toEqual( expect.objectContaining({ - id: userAuth.userId, country: 'Brazil', + name: 'John', + surname: 'Doe', + email: 'johndoe@example.com', }), ) }) - it('should not be able to get an user by ID that does exists', async () => { - const getUserByIdResponse = await request(app.server) - .get(`/user/${randomUUID()}`) - .set('Authorization', `Bearer ${userAuth.token}`) - - expect(getUserByIdResponse.statusCode).toEqual(404) - expect(getUserByIdResponse.body.user).toEqual(expect.objectContaining({})) - }) - - it('should not be able to get an user requesting with id that is not uuid', async () => { - const id = 'id_not_uuid' - - const getUserByIdResponse = await request(app.server) - .get(`/user/${id}`) - .set('Authorization', `Bearer ${userAuth.token}`) - - expect(getUserByIdResponse.statusCode).toEqual(400) + it('should not be able to get an user without authenticate', async () => { + const getUserByIdResponse = await request(app.server).get(`/user`) + expect(getUserByIdResponse.statusCode).toEqual(401) expect(getUserByIdResponse.body).toEqual( - expect.objectContaining({ - message: expect.any(String), - issues: expect.any(Object), - }), + expect.objectContaining({ message: 'Unauthorized' }), ) }) }) diff --git a/src/controller/user/getUserById.ts b/src/controller/user/getUserById.ts index 7b4f6f0..47b923c 100644 --- a/src/controller/user/getUserById.ts +++ b/src/controller/user/getUserById.ts @@ -1,5 +1,4 @@ import { FastifyReply, FastifyRequest } from 'fastify' -import { z } from 'zod' import { GetUserByIdUseCase } from '../../use-cases/user/getUserByIdUseCase' import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository' import { ResourceNotFoundError } from '../../use-cases/errors/ResourceNotFoundError' @@ -11,22 +10,10 @@ export async function getUserById( const userRepository = new PrismaUsersRepository() const getUserByIdUseCase = new GetUserByIdUseCase(userRepository) - const getUserByIdBodySchema = z.object({ - id: z.string().uuid(), + const { user } = await getUserByIdUseCase.execute({ + id: request.user.sub, }) - - const { id } = getUserByIdBodySchema.parse(request.params) - - try { - const { user } = await getUserByIdUseCase.execute({ - id, - }) - return response - .status(200) - .send({ user: { ...user, password_hash: undefined } }) - } catch (error) { - if (error instanceof ResourceNotFoundError) { - return response.status(404).send() - } - } + return response + .status(200) + .send({ user: { ...user, password_hash: undefined } }) } diff --git a/src/controller/user/routes.ts b/src/controller/user/routes.ts index f563c12..0930a57 100644 --- a/src/controller/user/routes.ts +++ b/src/controller/user/routes.ts @@ -1,6 +1,5 @@ import { FastifyInstance } from 'fastify' import { getUserById } from './getUserById' -import { getUserByEmail } from './getUserByEmail' import { registerUser } from './registerUser' import { editUserById } from './editUserById' import { addImageUser } from './addImageToUser' @@ -16,10 +15,9 @@ export async function userRoutes(app: FastifyInstance) { }, }) app.post('/user', registerUser) - app.get('/user/:id', { onRequest: verifyJWT }, getUserById) - app.get('/user', getUserByEmail) - app.put('/user/:userId/edit', { onRequest: verifyJWT }, editUserById) + app.get('/user', { onRequest: verifyJWT }, getUserById) + app.put('/user/edit', { onRequest: verifyJWT }, editUserById) app.put('/user/edit/pass', { onRequest: verifyJWT }, editUserPassword) - app.post('/user/:userId/photo', { onRequest: verifyJWT }, addImageUser) + app.post('/user/photo', { onRequest: verifyJWT }, addImageUser) } diff --git a/src/utils/tests/create-and-authenticate-user.ts b/src/utils/tests/create-and-authenticate-user.ts index 0aa9aaa..c92072d 100644 --- a/src/utils/tests/create-and-authenticate-user.ts +++ b/src/utils/tests/create-and-authenticate-user.ts @@ -15,7 +15,18 @@ export async function createAndAuthenticateUser(app: FastifyInstance) { }) const { token } = authResponse.body - const { id: userId } = authResponse.body.user + + /* + we won't need that in the future + we need to adress this issue to remove this call to get the user id + https://github.com/MatheusSanchez/orange-back/issues/50 + */ + const getUseByEmailResponse = await request(app.server) + .get('/user') + .query({ email: 'johndoe@example.com' }) + .set('Authorization', `Bearer ${token}`) + + const { id: userId } = getUseByEmailResponse.body.user return { token, userId } }