-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(infra): controllers of driver and administrator registration and…
… search for pending requests In the http layer created driver and administrator registration controllers and e2e tests, and a fetch pending solicitations controller and e2e test. In the persistence layer, factories were created for use cases of these controllers.
- Loading branch information
1 parent
d693de6
commit c8b25cf
Showing
17 changed files
with
252 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/infra/database/prisma/factories/make-fetch-pending-solicitations-use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { FetchPendingSolicitationsUseCase } from '@account/application/use-cases/fetch-pending-solicitations' | ||
import { PrismaSolicitatitonsRepository } from '../repositories/prisma-solicitations-repository' | ||
|
||
export function makeFetchPendingSolicitationsUseCase() { | ||
const solicitationsRepository = new PrismaSolicitatitonsRepository() | ||
const fetchPendingSolicitationsUseCase = new FetchPendingSolicitationsUseCase( | ||
solicitationsRepository, | ||
) | ||
|
||
return fetchPendingSolicitationsUseCase | ||
} |
11 changes: 11 additions & 0 deletions
11
src/infra/database/prisma/factories/make-register-admin-use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { RegisterAdminUseCase } from '@account/application/use-cases/register-admin' | ||
import { PrismaUsersRepository } from '../repositories/prisma-users-repository' | ||
import { BcryptHasher } from '@infra/cryptography/bcrypt-hasher' | ||
|
||
export function makeRegisterAdminUseCase() { | ||
const usersRepository = new PrismaUsersRepository() | ||
const hasher = new BcryptHasher() | ||
const registerAdminUseCase = new RegisterAdminUseCase(usersRepository, hasher) | ||
|
||
return registerAdminUseCase | ||
} |
14 changes: 14 additions & 0 deletions
14
src/infra/database/prisma/factories/make-register-driver-use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { RegisterDriverUseCase } from '@account/application/use-cases/register-driver' | ||
import { PrismaUsersRepository } from '../repositories/prisma-users-repository' | ||
import { BcryptHasher } from '@infra/cryptography/bcrypt-hasher' | ||
|
||
export function makeRegisterDriverUseCase() { | ||
const usersRepository = new PrismaUsersRepository() | ||
const hasher = new BcryptHasher() | ||
const registerDriverUseCase = new RegisterDriverUseCase( | ||
usersRepository, | ||
hasher, | ||
) | ||
|
||
return registerDriverUseCase | ||
} |
2 changes: 1 addition & 1 deletion
2
src/infra/database/prisma/factories/make-signup-student-use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/infra/http/controllers/fetch-pending-solicitations-controller.e2e.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { databaseE2ETests } from 'prisma/vitest-environment-prisma/setup-e2e' | ||
import { app } from 'src/app' | ||
import request from 'supertest' | ||
import { makePrismaSolicitation } from 'test/factories/make-solicitation' | ||
|
||
describe('Fetch pending solicitations (e2e)', () => { | ||
beforeAll(async () => { | ||
await app.ready() | ||
}) | ||
|
||
afterAll(async () => { | ||
await app.close() | ||
}) | ||
|
||
test('[GET] /solicitations', async () => { | ||
await makePrismaSolicitation() | ||
await makePrismaSolicitation() | ||
|
||
const response = await request(app.server).get('/solicitations').send() | ||
|
||
expect(response.statusCode).toEqual(200) | ||
|
||
const solicitationsOnDatabase = | ||
await databaseE2ETests.solicitation.findMany() | ||
|
||
expect(solicitationsOnDatabase).toHaveLength(2) | ||
}) | ||
}) |
19 changes: 19 additions & 0 deletions
19
src/infra/http/controllers/fetch-pending-solicitations-controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { makeFetchPendingSolicitationsUseCase } from '@infra/database/prisma/factories/make-fetch-pending-solicitations-use-case' | ||
import { FastifyReply, FastifyRequest } from 'fastify' | ||
|
||
export async function fetchPendingSolicitations( | ||
request: FastifyRequest, | ||
reply: FastifyReply, | ||
) { | ||
const fetchPendingSolicitationsUseCase = | ||
makeFetchPendingSolicitationsUseCase() | ||
|
||
const result = await fetchPendingSolicitationsUseCase.execute() | ||
|
||
if (result.isFailure()) { | ||
return reply.status(400).send() | ||
} | ||
const { solicitations } = result.value | ||
|
||
return reply.status(200).send(solicitations) | ||
} |
32 changes: 32 additions & 0 deletions
32
src/infra/http/controllers/register-admin-controller.e2e.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { databaseE2ETests } from 'prisma/vitest-environment-prisma/setup-e2e' | ||
import { app } from 'src/app' | ||
import request from 'supertest' | ||
|
||
describe('Register Admin (e2e)', () => { | ||
beforeAll(async () => { | ||
await app.ready() | ||
}) | ||
|
||
afterAll(async () => { | ||
await app.close() | ||
}) | ||
|
||
test('[POST] /admins/register', async () => { | ||
const response = await request(app.server).post('/admins/register').send({ | ||
completeName: 'John Doe', | ||
email: '[email protected]', | ||
password: '123456', | ||
phone: '(00) 98765-4321', | ||
}) | ||
|
||
expect(response.statusCode).toEqual(201) | ||
|
||
const adminOnDatabase = await databaseE2ETests.user.findUnique({ | ||
where: { | ||
email: '[email protected]', | ||
}, | ||
}) | ||
|
||
expect(adminOnDatabase).toBeTruthy() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { UserAlreadyExistsError } from '@account/application/use-cases/errors/user-already-exists-error' | ||
import { makeRegisterAdminUseCase } from '@infra/database/prisma/factories/make-register-admin-use-case' | ||
import { FastifyReply, FastifyRequest } from 'fastify' | ||
import { z } from 'zod' | ||
|
||
export async function registerAdmin( | ||
request: FastifyRequest, | ||
reply: FastifyReply, | ||
) { | ||
const registerAdminBodySchema = z.object({ | ||
completeName: z.string(), | ||
email: z.string().email(), | ||
password: z.coerce.string().min(6), | ||
phone: z.string(), | ||
}) | ||
|
||
const { completeName, email, password, phone } = | ||
registerAdminBodySchema.parse(request.body) | ||
|
||
try { | ||
const registerAdminUseCase = makeRegisterAdminUseCase() | ||
|
||
await registerAdminUseCase.execute({ | ||
completeName, | ||
email, | ||
password, | ||
phone, | ||
}) | ||
|
||
return reply.status(201).send() | ||
} catch (error) { | ||
if (error instanceof UserAlreadyExistsError) { | ||
return reply.status(409).send({ message: error.message }) | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/infra/http/controllers/register-driver-controller.e2e.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { databaseE2ETests } from 'prisma/vitest-environment-prisma/setup-e2e' | ||
import { app } from 'src/app' | ||
import request from 'supertest' | ||
|
||
describe('Register driver (e2e)', () => { | ||
beforeAll(async () => { | ||
await app.ready() | ||
}) | ||
|
||
afterAll(async () => { | ||
await app.close() | ||
}) | ||
|
||
test('[POST] /drivers/register', async () => { | ||
const response = await request(app.server).post('/drivers/register').send({ | ||
completeName: 'John Doe', | ||
email: '[email protected]', | ||
password: '123456', | ||
phone: '(00) 98765-4321', | ||
}) | ||
|
||
expect(response.statusCode).toEqual(201) | ||
|
||
const driverOnDatabase = await databaseE2ETests.user.findUnique({ | ||
where: { | ||
email: '[email protected]', | ||
}, | ||
}) | ||
|
||
expect(driverOnDatabase).toBeTruthy() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { FastifyReply, FastifyRequest } from 'fastify' | ||
import { UserAlreadyExistsError } from '@account/application/use-cases/errors/user-already-exists-error' | ||
import { makeRegisterDriverUseCase } from '@infra/database/prisma/factories/make-register-driver-use-case' | ||
import { z } from 'zod' | ||
|
||
export async function registerDriver( | ||
request: FastifyRequest, | ||
reply: FastifyReply, | ||
) { | ||
const registerDriverBodySchema = z.object({ | ||
completeName: z.string(), | ||
email: z.string().email(), | ||
password: z.coerce.string().min(6), | ||
phone: z.string(), | ||
}) | ||
|
||
const { completeName, email, password, phone } = | ||
registerDriverBodySchema.parse(request.body) | ||
|
||
try { | ||
const registerDriverUseCase = makeRegisterDriverUseCase() | ||
|
||
await registerDriverUseCase.execute({ | ||
completeName, | ||
email, | ||
password, | ||
phone, | ||
}) | ||
|
||
return reply.status(201).send() | ||
} catch (error) { | ||
if (error instanceof UserAlreadyExistsError) { | ||
return reply.status(409).send({ message: error.message }) | ||
} | ||
|
||
throw error | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
import { FastifyInstance } from 'fastify' | ||
import { signUpStudent } from './signup-student-controller' | ||
import { registerStudent } from './register-student-controller' | ||
import { registerDriver } from './register-driver-controller' | ||
import { registerAdmin } from './register-admin-controller' | ||
import { fetchPendingSolicitations } from './fetch-pending-solicitations-controller' | ||
|
||
export async function routes(app: FastifyInstance) { | ||
app.post('/signup', signUpStudent) | ||
|
||
app.post('/solicitations/:solicitationId/register', registerStudent) | ||
app.post('/drivers/register', registerDriver) | ||
app.post('/admins/register', registerAdmin) | ||
|
||
app.get('/solicitations', fetchPendingSolicitations) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters