Skip to content

Commit

Permalink
Merge pull request #258 from CtrI-Alt-Del/getSuppliers
Browse files Browse the repository at this point in the history
Get Suppliers
  • Loading branch information
JohnPetros authored Nov 30, 2024
2 parents 8f8c233 + f6c05a8 commit c3c372e
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { IHttp } from '@stocker/core/interfaces'
import { GetSupplierUseCase } from '@stocker/core/use-cases'

import { suppliersRepository } from '@/database'

type RouteParams = {
supplierId: string
}

export class GetSupplierController {
async handle(http: IHttp) {
const { supplierId } = http.getRouteParams<RouteParams>()
const useCase = new GetSupplierUseCase(suppliersRepository)
const supplierDto = await useCase.execute({ supplierId })

return http.send(supplierDto)
}
}
1 change: 1 addition & 0 deletions apps/server/src/api/controllers/suppliers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { RegisterSupplierController } from './register-supplier-controller'
export { UpdateSupplierController } from './update-supplier-controller'
export { DeleteSuppliersController } from './delete-suppliers-controller'
export { ListSuppliersController } from './list-suppliers-controller'
export { GetSupplierController } from './get-supplier-controller'
11 changes: 11 additions & 0 deletions apps/server/src/app/fastify/routes/supplier-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
UpdateSupplierController,
DeleteSuppliersController,
ListSuppliersController,
GetSupplierController
} from '@/api/controllers/suppliers'
import { FastifyHttp } from '../fastify-http'
import { FastifyHandler } from '../fastify-handler'
Expand All @@ -15,6 +16,7 @@ export const SuppliersRoutes = async (app: FastifyInstance) => {
const updateSupplierController = new UpdateSupplierController()
const deleteSupplierController = new DeleteSuppliersController()
const listSuppliersController = new ListSuppliersController()
const getSupplierController = new GetSupplierController()
const verifyJwtMiddleware = new FastifyHandler(new VerifyJwtMiddleware())
const verifyManagerRoleMiddleware = new FastifyHandler(
new VerifyUserRoleMiddleware('manager'),
Expand All @@ -25,6 +27,9 @@ export const SuppliersRoutes = async (app: FastifyInstance) => {
const preHandlers = [verifyJwtMiddleware, verifyManagerRoleMiddleware].map((handler) =>
handler.handle.bind(handler),
)
const preHandlersEmployee = [verifyJwtMiddleware, verifyEmployeeRoleMiddleware].map((handler) =>
handler.handle.bind(handler),
)

app.post('/', { preHandler: preHandlers }, async (request, response) => {
const http = new FastifyHttp(request, response)
Expand Down Expand Up @@ -54,4 +59,10 @@ export const SuppliersRoutes = async (app: FastifyInstance) => {
const http = new FastifyHttp(request, response)
return deleteSupplierController.handle(http)
})

app.get('/:supplierId', { preHandler: preHandlersEmployee }, async (request, response) => {
const http = new FastifyHttp(request, response)
return getSupplierController.handle(http)
},
)
}
23 changes: 23 additions & 0 deletions packages/core/src/use-cases/suppliers/get-supplier-use-case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { ISuppliersRepository } from '../../interfaces'
import { NotFoundError } from '../../errors'

type Request = {
supplierId: string
}

export class GetSupplierUseCase {
private readonly suppliersRepository: ISuppliersRepository
constructor(suppliersRepository: ISuppliersRepository) {
this.suppliersRepository = suppliersRepository
}

async execute({ supplierId }: Request) {
const supplier = await this.suppliersRepository.findById(supplierId)

if (!supplier) {
throw new NotFoundError('Fornecedor não encontrado')
}

return supplier.dto
}
}
1 change: 1 addition & 0 deletions packages/core/src/use-cases/suppliers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { RegisterSupplierUseCase } from './register-supplier-use-case'
export { UpdateSupplierUseCase } from './update-supplier-use-case'
export { DeleteSuppliersUseCase } from './delete-supplier-use-case'
export { ListSuplliersUseCase } from './list-suppliers-use-case'
export { GetSupplierUseCase } from './get-supplier-use-case'
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { beforeEach, describe, expect, it } from 'vitest'
import { GetSupplierUseCase } from '../get-supplier-use-case'
import { SuppliersRepositoryMock } from '../../../../__tests__/mocks/repositories'
import { NotAllowedError, NotFoundError } from '../../../errors'
import { SuppliersFaker } from '../../../../__tests__/fakers'

let useCase: GetSupplierUseCase
let suppliersRepository: SuppliersRepositoryMock

describe('Get supplier use case', () => {
beforeEach(() => {
suppliersRepository = new SuppliersRepositoryMock()
useCase = new GetSupplierUseCase(suppliersRepository)
})

it('should not get a supplier if the supplier does not exist', async () => {
const fakeSupplier = SuppliersFaker.fake()

expect(async () => {
await useCase.execute({ supplierId: fakeSupplier.id })
}).rejects.toThrowError(NotFoundError)
})

it('should get a supplier', async () => {
const fakeSupplier = SuppliersFaker.fake()
await suppliersRepository.add(fakeSupplier)

const supplier = await useCase.execute({ supplierId: fakeSupplier.id })

expect(supplier).toEqual(fakeSupplier.dto)
})
})

0 comments on commit c3c372e

Please sign in to comment.