-
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.
- Loading branch information
1 parent
2cd77a7
commit df929f7
Showing
21 changed files
with
343 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { IsEmail, IsNotEmpty, IsString } from 'class-validator' | ||
|
||
export class CreateOtpDto { | ||
@IsEmail() | ||
email: string | ||
} |
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,4 @@ | ||
import { PartialType } from '@nestjs/swagger' | ||
import { CreateOtpDto } from './create-otp.dto' | ||
|
||
export class UpdateOtpDto extends PartialType(CreateOtpDto) {} |
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,16 @@ | ||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose' | ||
import { HydratedDocument } from 'mongoose' | ||
|
||
export type OtpDocument = HydratedDocument<Otp> | ||
|
||
@Schema({ | ||
timestamps: true, | ||
}) | ||
export class Otp { | ||
@Prop({ required: true, unique: true }) | ||
email: string | ||
@Prop({ required: true, unique: true }) | ||
otp: number | ||
} | ||
|
||
export const OtpSchema = SchemaFactory.createForClass(Otp) |
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 { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
HttpException, | ||
HttpStatus, | ||
Param, | ||
Patch, | ||
Post, | ||
} from '@nestjs/common' | ||
import { CreateOtpDto } from './dto/create-otp.dto' | ||
import { UpdateOtpDto } from './dto/update-otp.dto' | ||
import { OtpService } from './otp.service' | ||
|
||
@Controller('otp') | ||
export class OtpController { | ||
constructor(private readonly otpService: OtpService) {} | ||
|
||
@Post() | ||
async create(@Body() createOtpDto: CreateOtpDto) { | ||
try { | ||
return await this.otpService.create(createOtpDto) | ||
} catch (e) { | ||
throw new HttpException({ message: e.message }, HttpStatus.BAD_REQUEST) | ||
} | ||
} | ||
} |
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,13 @@ | ||
import { Module } from '@nestjs/common' | ||
import { MongooseModule } from '@nestjs/mongoose' | ||
import { Otp, OtpSchema } from './entities/otp.entity' | ||
import { OtpController } from './otp.controller' | ||
import { OtpService } from './otp.service' | ||
|
||
@Module({ | ||
imports: [MongooseModule.forFeature([{ name: Otp.name, schema: OtpSchema }])], | ||
controllers: [OtpController], | ||
providers: [OtpService], | ||
exports: [OtpService], | ||
}) | ||
export class OtpModule {} |
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,52 @@ | ||
import { getOtp } from '@/utils/getOtp' | ||
import { BadRequestException, Injectable } from '@nestjs/common' | ||
import { InjectModel } from '@nestjs/mongoose' | ||
import { Model } from 'mongoose' | ||
import { CreateOtpDto } from './dto/create-otp.dto' | ||
import { UpdateOtpDto } from './dto/update-otp.dto' | ||
import { Otp } from './entities/otp.entity' | ||
|
||
@Injectable() | ||
export class OtpService { | ||
constructor( | ||
@InjectModel(Otp.name) | ||
private readonly otpModel: Model<Otp>, | ||
) {} | ||
|
||
async create({ email }: CreateOtpDto) { | ||
const foundOtp = await this.findbyEmail(email) | ||
|
||
if (foundOtp) { | ||
return foundOtp.otp | ||
} | ||
|
||
const otp = await this.generateOtp(email) | ||
|
||
await this.otpModel.create({ | ||
email, | ||
otp, | ||
}) | ||
|
||
return otp | ||
} | ||
|
||
async findbyEmail(email: string) { | ||
return await this.otpModel.findOne({ email }) | ||
} | ||
|
||
async findbyOtp(otp: number) { | ||
return await this.otpModel.findOne({ otp }) | ||
} | ||
|
||
async generateOtp(email: string): Promise<number> { | ||
const otp = getOtp(6) | ||
|
||
const foundOtp = await this.findbyOtp(otp) | ||
|
||
if (foundOtp) { | ||
return await this.generateOtp(email) | ||
} | ||
|
||
return otp | ||
} | ||
} |
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,7 @@ | ||
import { IsNotEmpty, IsString } from 'class-validator' | ||
|
||
export class CreateRestoreAccountDto { | ||
@IsNotEmpty() | ||
@IsString() | ||
email: string | ||
} |
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,6 @@ | ||
import { PartialType } from '@nestjs/swagger' | ||
import { CreateRestoreAccountDto } from './create-restore-account.dto' | ||
|
||
export class UpdateRestoreAccountDto extends PartialType( | ||
CreateRestoreAccountDto, | ||
) {} |
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,9 @@ | ||
import { IsEmail, IsNotEmpty, IsNumber, IsString } from 'class-validator' | ||
|
||
export class VerificationOtpDto { | ||
@IsEmail() | ||
email: string | ||
|
||
@IsNumber() | ||
otp: number | ||
} |
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 @@ | ||
export class RestoreAccount {} |
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,53 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
Param, | ||
Patch, | ||
Post, | ||
} from '@nestjs/common' | ||
import { ApiBody, ApiTags } from '@nestjs/swagger' | ||
import { CreateRestoreAccountDto } from './dto/create-restore-account.dto' | ||
import { UpdateRestoreAccountDto } from './dto/update-restore-account.dto' | ||
import { VerificationOtpDto } from './dto/verification-otp.dto' | ||
import { RestoreAccountService } from './restore-account.service' | ||
|
||
@ApiTags('Restore account') | ||
@Controller('restore-account') | ||
export class RestoreAccountController { | ||
constructor(private readonly restoreAccountService: RestoreAccountService) {} | ||
|
||
@Post() | ||
@ApiBody({ | ||
schema: { | ||
type: 'object', | ||
properties: { | ||
email: { | ||
default: '[email protected]', | ||
}, | ||
}, | ||
}, | ||
}) | ||
async createOtp(@Body() createRestoreAccountDto: CreateRestoreAccountDto) { | ||
return await this.restoreAccountService.createOtp(createRestoreAccountDto) | ||
} | ||
|
||
@ApiBody({ | ||
schema: { | ||
type: 'object', | ||
properties: { | ||
email: { | ||
default: '[email protected]', | ||
}, | ||
otp: { | ||
default: '636421', | ||
}, | ||
}, | ||
}, | ||
}) | ||
@Post('verification') | ||
async verificationOtp(@Body() verificationOtpDto: VerificationOtpDto) { | ||
return await this.restoreAccountService.verificationOtp(verificationOtpDto) | ||
} | ||
} |
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,13 @@ | ||
import { JwtModule } from '@/jwt/jwt.module' | ||
import { OtpModule } from '@/otp/otp.module' | ||
import { UserModule } from '@/user/user.module' | ||
import { Module } from '@nestjs/common' | ||
import { RestoreAccountController } from './restore-account.controller' | ||
import { RestoreAccountService } from './restore-account.service' | ||
|
||
@Module({ | ||
imports: [OtpModule, UserModule, JwtModule], | ||
controllers: [RestoreAccountController], | ||
providers: [RestoreAccountService], | ||
}) | ||
export class RestoreAccountModule {} |
Oops, something went wrong.