-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add project, environment module
- Loading branch information
Showing
69 changed files
with
5,336 additions
and
6,732 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,35 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { MockAuthRepository } from '../repository/mock.repository' | ||
import { AUTH_REPOSITORY } from '../repository/interface.repository' | ||
import { AuthService } from '../service/auth.service' | ||
import { RESEND_SERVICE } from '../../resend/services/resend.service.interface' | ||
import { MockResend } from '../../resend/services/mock.resend' | ||
import { JwtService } from '@nestjs/jwt' | ||
import { PrismaService } from '../../prisma/prisma.service' | ||
import { AuthController } from './auth.controller' | ||
import { USER_REPOSITORY } from '../../user/repository/interface.repository' | ||
import { MockUserRepository } from '../../user/repository/mock.repository' | ||
|
||
describe('AuthController', () => { | ||
let controller: AuthController | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AuthController], | ||
providers: [ | ||
AuthService, | ||
{ provide: RESEND_SERVICE, useClass: MockResend }, | ||
{ provide: AUTH_REPOSITORY, useClass: MockAuthRepository }, | ||
{ provide: USER_REPOSITORY, useClass: MockUserRepository }, | ||
JwtService, | ||
PrismaService | ||
] | ||
}).compile() | ||
|
||
controller = module.get<AuthController>(AuthController) | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined() | ||
}) | ||
}) |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 13 additions & 4 deletions
17
apps/api/src/auth/auth.guard.ts → apps/api/src/auth/guard/auth.guard.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Otp, User } from '@prisma/client' | ||
import { IAuthRepository } from './interface.repository' | ||
import { PrismaService } from '../../prisma/prisma.service' | ||
|
||
export class AuthRepository implements IAuthRepository { | ||
constructor(private readonly prisma: PrismaService) {} | ||
|
||
async isOtpValid(email: User['email'], otp: string): Promise<boolean> { | ||
const timeNow = new Date() | ||
return ( | ||
(await this.prisma.otp.count({ | ||
where: { | ||
code: otp, | ||
user: { | ||
}, | ||
expiresAt: { | ||
gt: timeNow | ||
} | ||
} | ||
})) > 0 | ||
) | ||
} | ||
|
||
async createOtp( | ||
email: User['email'], | ||
otp: string, | ||
expiresAfter: number | ||
): Promise<Otp> { | ||
const timeNow = new Date() | ||
return await this.prisma.otp.create({ | ||
data: { | ||
code: otp, | ||
expiresAt: new Date(timeNow.getTime() + expiresAfter), | ||
user: { | ||
connect: { | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
|
||
async deleteOtp(email: User['email'], otp: string): Promise<void> { | ||
await this.prisma.otp.delete({ | ||
where: { | ||
code: otp, | ||
AND: { | ||
user: { | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,37 @@ | ||
import { User, Otp } from '@prisma/client' | ||
|
||
export const AUTH_REPOSITORY = 'AUTH_REPOSITORY' | ||
|
||
/** | ||
* Interface for the Auth Repository. | ||
*/ | ||
export interface IAuthRepository { | ||
/** | ||
* Checks if an OTP is valid for the given email. | ||
* @param {User['email']} email - The email against which to check the OTP. | ||
* @param {string} otp - The OTP code to check. | ||
* @returns {Promise<boolean>} - A promise that resolves to true if the OTP is valid, false otherwise. | ||
*/ | ||
isOtpValid(email: User['email'], otp: string): Promise<boolean> | ||
|
||
/** | ||
* Creates an OTP for the given email. | ||
* @param {User['email']} email - The email to create the OTP for. | ||
* @param {string} otp - The OTP code. | ||
* @param {number} expiresAfter - The number of milliseconds after which the OTP expires. | ||
* @returns {Promise<Otp>} - A promise that resolves to the created OTP. | ||
*/ | ||
createOtp( | ||
email: User['email'], | ||
otp: string, | ||
expiresAfter: number | ||
): Promise<Otp> | ||
|
||
/** | ||
* Deletes an OTP. | ||
* @param {User['email']} email - The email of the OTP to delete. | ||
* @param {string} otp - The OTP code to delete. | ||
* @returns {Promise<void>} - A promise that resolves when the OTP is successfully deleted. | ||
*/ | ||
deleteOtp(email: User['email'], otp: string): Promise<void> | ||
} |
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,23 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { IAuthRepository } from './interface.repository' | ||
|
||
export class MockAuthRepository implements IAuthRepository { | ||
isOtpValid(email: string, otp: string): Promise<boolean> { | ||
throw new Error('Method not implemented.') | ||
} | ||
createOtp( | ||
email: string, | ||
otp: string, | ||
expiresAfter: number | ||
): Promise<{ | ||
code: string | ||
userId: string | ||
createdAt: Date | ||
expiresAt: Date | ||
}> { | ||
throw new Error('Method not implemented.') | ||
} | ||
deleteOtp(email: string, otp: string): Promise<void> { | ||
throw new Error('Method not implemented.') | ||
} | ||
} |
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,33 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { AuthService } from './auth.service' | ||
import { MockResend } from '../../resend/services/mock.resend' | ||
import { RESEND_SERVICE } from '../../resend/services/resend.service.interface' | ||
import { JwtService } from '@nestjs/jwt' | ||
import { PrismaService } from '../../prisma/prisma.service' | ||
import { AUTH_REPOSITORY } from '../repository/interface.repository' | ||
import { MockAuthRepository } from '../repository/mock.repository' | ||
import { USER_REPOSITORY } from '../../user/repository/interface.repository' | ||
import { MockUserRepository } from '../../user/repository/mock.repository' | ||
|
||
describe('AuthService', () => { | ||
let service: AuthService | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
AuthService, | ||
{ provide: RESEND_SERVICE, useClass: MockResend }, | ||
{ provide: AUTH_REPOSITORY, useClass: MockAuthRepository }, | ||
{ provide: USER_REPOSITORY, useClass: MockUserRepository }, | ||
JwtService, | ||
PrismaService | ||
] | ||
}).compile() | ||
|
||
service = module.get<AuthService>(AuthService) | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined() | ||
}) | ||
}) |
Oops, something went wrong.