Skip to content

Commit

Permalink
Feat(User, Routes): Get authenticated user details route
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur-Poffo committed Feb 25, 2024
1 parent 2e7bbf7 commit 4f76f30
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/domain/course-management/enterprise/entities/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ export class Course extends Entity<CourseProps> {
const course = new Course(
{
...props,
coverImageKey: null,
bannerImageKey: null,
createdAt: props.createdAt ?? new Date()
},
id
Expand Down
1 change: 1 addition & 0 deletions src/infra/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const app = fastify()
// PLugins

app.register(fastifyCors, {
origin: [env.CLIENT_URL],
credentials: true,
exposedHeaders: ['set-cookie']
})
Expand Down
1 change: 1 addition & 0 deletions src/infra/env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const envSchema = z.object({
NODE_ENV: z.enum(['development', 'test', 'production']).default('production'),
PORT: z.coerce.number().default(3333),
DATABASE_URL: z.string(),
CLIENT_URL: z.string().default('http://localhost:5173'),
CLOUDFLARE_ACCOUNT_ID: z.string(),
AWS_BUCKET_NAME: z.string(),
AWS_ACCESS_KEY_ID: z.string(),
Expand Down
33 changes: 33 additions & 0 deletions src/infra/http/controllers/get-authenticated-user-details.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ResourceNotFoundError } from '@/core/errors/errors/resource-not-found-error'
import { makeUserMapper } from '@/infra/database/prisma/mappers/factories/make-user-mapper'
import { makeGetUserInfoUseCase } from '@/infra/use-cases/factories/make-get-user-info-use-case'
import { type FastifyReply, type FastifyRequest } from 'fastify'
import { UserPresenter } from '../presenters/user-presenter'

export async function getAuthenticatedUserDetailsController(request: FastifyRequest, reply: FastifyReply) {
const { sub: userId } = request.user

const getUserInfoUseCase = makeGetUserInfoUseCase()

const result = await getUserInfoUseCase.exec({
id: userId
})

if (result.isLeft()) {
const error = result.value

switch (error.constructor) {
case ResourceNotFoundError:
return await reply.status(404).send({ message: error.message })
default:
return await reply.status(500).send({ message: error.message })
}
}

const userMapper = makeUserMapper()
const user = await userMapper.toPrisma(result.value.user)

return await reply.status(200).send({
user: UserPresenter.toHTTP(user)
})
}
2 changes: 2 additions & 0 deletions src/infra/http/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { type FastifyInstance } from 'fastify'
import { authenticateUserController } from '../controllers/authenticate'
import { deleteUserController } from '../controllers/delete-user'
import { editUserDetailsController } from '../controllers/edit-user-details'
import { getAuthenticatedUserDetailsController } from '../controllers/get-authenticated-user-details'
import { getUserDetailsController } from '../controllers/get-user-details'
import { registerUserController } from '../controllers/register-user'
import { verifyJwt } from '../middlewares/verify-jwt'

export async function userRoutes(app: FastifyInstance) {
app.get('/users', { onRequest: [verifyJwt] }, getAuthenticatedUserDetailsController)
app.get('/users/:userId', getUserDetailsController)

app.post('/users', registerUserController)
Expand Down

0 comments on commit 4f76f30

Please sign in to comment.