-
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.
Merge pull request #85 from Quest-Finder/refactor/user-sign-up-email
refactor(user-with-email): move to nestjs architecture
- Loading branch information
Showing
9 changed files
with
159 additions
and
42 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type UserWithEmailModel = { | ||
id: string | ||
email: string | ||
password: string | ||
} |
79 changes: 79 additions & 0 deletions
79
src/users/repository/user-with-email/user-with-email-repository.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,79 @@ | ||
import { PrismaService } from '@/shared/prisma/prisma.service' | ||
import { Test, type TestingModule } from '@nestjs/testing' | ||
import { type UserWithEmailModel } from '../entity/user-with-email.model' | ||
import { UserWithEmailRepository } from './user-with-email-repository' | ||
|
||
const makeUserWithEmail = (): UserWithEmailModel => { | ||
return { | ||
email: 'valid@email.com', | ||
id: 'valid-id', | ||
password: 'encoded_password' | ||
} | ||
} | ||
|
||
describe('UserWithEmailRepository', () => { | ||
let repository: UserWithEmailRepository | ||
let prismaService: PrismaService | ||
beforeAll(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [UserWithEmailRepository, PrismaService] | ||
}).compile() | ||
repository = module.get<UserWithEmailRepository>(UserWithEmailRepository) | ||
prismaService = module.get<PrismaService>(PrismaService) | ||
}) | ||
|
||
beforeEach(async () => { | ||
await prismaService.userPreferenceRpgStyle.deleteMany() | ||
await prismaService.userPreferenceDayPeriod.deleteMany() | ||
await prismaService.userPreferenceGamePlace.deleteMany() | ||
await prismaService.userPreferencePlayersRange.deleteMany() | ||
await prismaService.externalAuthMapping.deleteMany() | ||
await prismaService.userPreference.deleteMany() | ||
await prismaService.userSocialMedia.deleteMany() | ||
await prismaService.userConfig.deleteMany() | ||
await prismaService.userBadge.deleteMany() | ||
await prismaService.user.deleteMany() | ||
await prismaService.address.deleteMany() | ||
await prismaService.cityState.deleteMany() | ||
await prismaService.userWithEmail.deleteMany() | ||
await prismaService.playerProfile.deleteMany() | ||
await prismaService.rpgStyle.deleteMany() | ||
await prismaService.badge.deleteMany() | ||
await prismaService.socialMedia.deleteMany() | ||
}) | ||
|
||
afterAll(async () => { | ||
await prismaService.$disconnect() | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(repository).toBeDefined() | ||
}) | ||
|
||
describe('Find UserWithEmail by email', () => { | ||
it('should return undefined when email not found', async () => { | ||
const response = await repository.findByEmail('invalid@email.com') | ||
expect(response).not.toBeTruthy() | ||
}) | ||
|
||
it('should return a valid user', async () => { | ||
await prismaService.userWithEmail.create({ | ||
data: makeUserWithEmail() | ||
}) | ||
const result = await repository.findByEmail(makeUserWithEmail().email) | ||
expect(result).toEqual(expect.objectContaining(makeUserWithEmail())) | ||
}) | ||
}) | ||
|
||
describe('Save a UserWithEmail ', () => { | ||
it('should save a user with email', async () => { | ||
const inputData = { | ||
email: 'invalid@email.com', | ||
password: 'hashed_password' | ||
} | ||
const savedUser = await repository.save(inputData) | ||
expect(savedUser.id).toBeTruthy() | ||
expect(savedUser).toEqual(expect.objectContaining(inputData)) | ||
}) | ||
}) | ||
}) |
30 changes: 30 additions & 0 deletions
30
src/users/repository/user-with-email/user-with-email-repository.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,30 @@ | ||
import { PrismaService } from '@/shared/prisma/prisma.service' | ||
import { Injectable } from '@nestjs/common' | ||
import { v4 } from 'uuid' | ||
import { type UserWithEmailModel } from '../entity/user-with-email.model' | ||
|
||
export type UserWithEmailData = Omit<UserWithEmailModel, 'id'> | ||
|
||
@Injectable() | ||
export class UserWithEmailRepository { | ||
constructor (private readonly prismaService: PrismaService) { } | ||
|
||
async findByEmail (email: string): Promise<UserWithEmailModel | undefined> { | ||
const result = await this.prismaService.userWithEmail.findUnique({ | ||
where: { | ||
} | ||
}) | ||
return result ?? undefined | ||
} | ||
|
||
async save (data: UserWithEmailData): Promise<UserWithEmailModel> { | ||
const result = await this.prismaService.userWithEmail.create({ | ||
data: { | ||
id: v4(), | ||
...data | ||
} | ||
}) | ||
return result | ||
} | ||
} |
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
18 changes: 7 additions & 11 deletions
18
...ers/sign-up/sign-up-with-email.service.ts → ...-with-email/sign-up-with-email.service.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
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