Skip to content

Commit

Permalink
refactor(database): remove unused code related to userWithEmail model
Browse files Browse the repository at this point in the history
  • Loading branch information
clintonbrito committed Sep 18, 2024
1 parent 6e749e0 commit 8f63d4d
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 21 deletions.
1 change: 0 additions & 1 deletion src/infra/database/prisma/schema/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ enum Frequency {
}

model UserWithEmail {
// id String @id(map: "pk_user_with_email") @unique(map: "uq_user_with_email_id") @db.Uuid()
id String @id @default(uuid()) @db.VarChar(36)
email String @unique(map: "uq_user_with_email_email") @db.VarChar(256)
password String @db.VarChar(100)
Expand Down
2 changes: 0 additions & 2 deletions src/routes/user/user-routes.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { UserRoutes } from './user-routes'
export class UserRoutesModule implements NestModule {
configure (consumer: MiddlewareConsumer): void {
consumer
// .apply()
// .forRoutes({ path: '/user/signup/email', method: RequestMethod.POST })
.apply(AuthNestMiddleware)
.forRoutes({ path: '/user', method: RequestMethod.POST })
.apply(WebhookValidatorNestMiddleware)
Expand Down
10 changes: 9 additions & 1 deletion src/users/sign-up/sign-up-with-email.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { SignUpService } from './sign-up-with-email.service'
import { SharedModule } from '@/shared/shared.module'
import type { INestApplication } from '@nestjs/common'
import request from 'supertest'
import { UuidAdapter } from '@/infra/uuid-adapter/uuid-adapter'
import { JwtSignAdapter } from '@/infra/cryptography/jwt-sign-adapter'
import { PrismaService } from '@/shared/prisma/prisma.service'

describe('SignUpController', () => {
let app: INestApplication
Expand All @@ -13,7 +16,12 @@ describe('SignUpController', () => {
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [SignUpController],
providers: [SignUpService],
providers: [
SignUpService,
UuidAdapter,
JwtSignAdapter,
PrismaService
],
imports: [SharedModule]
}).compile()

Expand Down
12 changes: 5 additions & 7 deletions src/users/sign-up/sign-up-with-email.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { SignUpService } from './sign-up-with-email.service'
import { ConflictException } from '@nestjs/common'
import { PrismaService } from '@/shared/prisma/prisma.service'
import bcrypt from 'bcrypt'
import { UuidAdapter } from '@/infra/uuid-adapter/uuid-adapter'
import { JwtSignAdapter } from '@/infra/cryptography/jwt-sign-adapter'

describe('SignUpService', () => {
let service: SignUpService
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let prismaService: PrismaService

const SALTED_ROUNDS = 10

Expand Down Expand Up @@ -35,15 +35,13 @@ describe('SignUpService', () => {
providers: [
SignUpService,
{ provide: PrismaService, useValue: mockPrismaService },
{ provide: 'UuidAdapter', useValue: mockUuidAdapter },
{ provide: UuidAdapter, useValue: mockUuidAdapter },
{ provide: 'HashAdapter', useValue: mockHashAdapter },
{ provide: 'JwtSignAdapter', useValue: mockJwtSignAdapter }
{ provide: JwtSignAdapter, useValue: mockJwtSignAdapter }
]
// imports: [SharedModule]
}).compile()

service = module.get<SignUpService>(SignUpService)
prismaService = module.get<PrismaService>(PrismaService)
})

afterEach(() => {
Expand All @@ -57,7 +55,7 @@ describe('SignUpService', () => {
it('should create a new user and return a token', async () => {
const bcryptHashSpy = jest.spyOn(bcrypt, 'hashSync').mockReturnValueOnce('hashed-password')

mockPrismaService.userWithEmail.findUnique.mockResolvedValueOnce(null) // Mock to ensure that the user does not exist
mockPrismaService.userWithEmail.findUnique.mockResolvedValueOnce(null)
mockPrismaService.userWithEmail.create.mockResolvedValueOnce({ id: 'some-uuid' })

const result = await service.create({ email: 'newuser@example.com', password: '123456' })
Expand Down
15 changes: 7 additions & 8 deletions src/users/sign-up/sign-up-with-email.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,35 @@ import { UuidAdapter } from '@/infra/uuid-adapter/uuid-adapter'
import { PrismaService } from '@/shared/prisma/prisma.service'
import { ConflictException, Injectable } from '@nestjs/common'
import { SignUpWithEmailDto } from './sign-up-with-email-dto'
import env from '@/configs/env'

const SALT_ROUNDS = 10

@Injectable()
export class SignUpService {
PrismaService: any
constructor (private readonly prismaService: PrismaService) {}
constructor (
private readonly prismaService: PrismaService,
private readonly uuidAdapter: UuidAdapter,
private readonly jwtSignAdapter: JwtSignAdapter
) {}

async create ({ email, password }: SignUpWithEmailDto): Promise<{ token: string }> {
const signUpData = new SignUpWithEmailDto(email, password)

const user = await this.prismaService.userWithEmail.findUnique({ where: { email } })

if (user) {
throw new ConflictException(`Já existe um email cadastrado com o ${email} informado`)
}

const uuidAdapter = new UuidAdapter()
const hashedPassword = await bcrypt.hashSync(password, SALT_ROUNDS)
await this.prismaService.userWithEmail.create({
data: {
id: uuidAdapter.build(),
id: this.uuidAdapter.build(),
email: signUpData.email,
password: hashedPassword
}
})

const jwtSignAdapter = new JwtSignAdapter(env.jwtSecretKey)
const token = jwtSignAdapter.execute(email)
const token = this.jwtSignAdapter.execute(email)

return token
}
Expand Down
10 changes: 9 additions & 1 deletion src/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ import { SharedModule } from '@/shared/shared.module'
import { Module } from '@nestjs/common'
import { SignUpController } from './sign-up/sign-up-with-email.controller'
import { SignUpService } from './sign-up/sign-up-with-email.service'
import { PrismaService } from '@/shared/prisma/prisma.service'
import { UuidAdapter } from '@/infra/uuid-adapter/uuid-adapter'
import { JwtSignAdapter } from '@/infra/cryptography/jwt-sign-adapter'

@Module({
controllers: [SignUpController],
providers: [SignUpService],
providers: [
SignUpService,
PrismaService,
UuidAdapter,
JwtSignAdapter
],
imports: [SharedModule]
})
export class UsersModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import type { Validation } from '@/contracts'
import type { Either } from '@/shared/either'
import { ZodHelper } from '@/helpers/zod/zod-helper'
import { z } from 'zod'
import type { SignUpWithEmailDto } from '@/users/sign-up/sign-up-with-email-dto'

export class SignUpWithEmailZodValidation implements Validation {
validate (input: any): Either<Error, void> {
validate (input: SignUpWithEmailDto): Either<Error, void> {
const schema = z.object({
email: z.string().email(),
password: z.string().min(6)
Expand Down

0 comments on commit 8f63d4d

Please sign in to comment.