Skip to content

Commit

Permalink
Fix addressing comments and removing prisma repository.
Browse files Browse the repository at this point in the history
  • Loading branch information
maxyuri13 authored and MatheusSanchez committed Jan 25, 2024
1 parent bb6eed1 commit 64c2f88
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 50 deletions.
25 changes: 0 additions & 25 deletions src/repositories/prisma/prisma-users-repository.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
import { expect, describe, it } from "vitest";
import { CreateUseCase } from "./createUseCase";
import { compare } from "bcryptjs";
import { InMemoryUserRepository } from "../repositories/in-memory-db/inMemoryUserRepository";
import { UserAlreadyExistsError } from "./errors/user-already-exists-error";
import { expect, describe, it, beforeEach } from 'vitest';
import { CreateUserUseCase } from './createUserUseCase';
import { compare } from 'bcryptjs';
import { InMemoryUserRepository } from '../repositories/in-memory-db/inMemoryUserRepository';
import { UserAlreadyExistsError } from './errors/user-already-exists-error';
import { UserRepository } from '../repositories/user-repository';

let usersRepository: UserRepository;
let createUserUseCase: CreateUserUseCase;

describe('Register Use Case', () => {

beforeEach(() => {
usersRepository = new InMemoryUserRepository()
createUserUseCase = new CreateUserUseCase(usersRepository)
})

it('should be able to register', async () => {
const usersRepository = new InMemoryUserRepository
const createUseCase = new CreateUseCase(usersRepository)

const { user } = await createUseCase.execute({
const { user } = await createUserUseCase.execute({
name: 'John',
surname: 'Doe',
email: '[email protected]',
password: '123456',
})

expect(user.id).toEqual(expect.any(String))
expect(user.email).toEqual('[email protected]')
})

it('should hash user password upon registration', async () => {
const usersRepository = new InMemoryUserRepository
const createUseCase = new CreateUseCase(usersRepository)

const { user } = await createUseCase.execute({

const { user } = await createUserUseCase.execute({
name: 'John',
surname: 'Doe',
email: '[email protected]',
Expand All @@ -39,20 +45,18 @@ describe('Register Use Case', () => {
})

it('should not be able to register with same email twice', async () => {
const usersRepository = new InMemoryUserRepository
const createUseCase = new CreateUseCase(usersRepository)

const email = '[email protected]'

await createUseCase.execute({
await createUserUseCase.execute({
name: 'John',
surname: 'Doe',
email,
password: '123456',
})

await expect(() =>
createUseCase.execute({
createUserUseCase.execute({
name: 'John',
surname: 'Doe',
email,
Expand All @@ -63,4 +67,3 @@ describe('Register Use Case', () => {
})

})

Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@ import { UserRepository } from '../repositories/user-repository'
import { UserAlreadyExistsError } from './errors/user-already-exists-error'
import { User } from '@prisma/client'

interface CreateUseCaseRequest {
interface CreateUserUseCaseRequest {
name: string
surname: string
email: string
password: string
}

interface CreateUseCaseResponse {
interface CreateUserUseCaseResponse {
user: User
}

export class CreateUseCase {
export class CreateUserUseCase {
constructor(private usersRepository: UserRepository) {}

async execute({
name,
surname,
email,
password,
}: CreateUseCaseRequest): Promise<CreateUseCaseResponse> {
const password_hash = await hash(password, 6)

}: CreateUserUseCaseRequest): Promise<CreateUserUseCaseResponse> {

const userWithSameEmail = await this.usersRepository.findByEmail(email)

if (userWithSameEmail) {
throw new UserAlreadyExistsError()
}

const password_hash = await hash(password, 6)

const user = await this.usersRepository.create({
name,
surname,
Expand All @@ -45,3 +45,5 @@ export class CreateUseCase {
}
}



0 comments on commit 64c2f88

Please sign in to comment.