Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: create a cron job function that will run every hour to clean up expired otps #73

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions apps/api/src/auth/repository/auth.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,15 @@ export class AuthRepository implements IAuthRepository {
}
})
}

async deleteExpiredOtps(): Promise<void> {
const timeNow = new Date()
await this.prisma.otp.deleteMany({
where: {
expiresAt: {
lte: new Date(timeNow.getTime())
}
}
})
}
}
6 changes: 6 additions & 0 deletions apps/api/src/auth/repository/interface.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ export interface IAuthRepository {
* @returns {Promise<void>} - A promise that resolves when the OTP is successfully deleted.
*/
deleteOtp(email: User['email'], otp: string): Promise<void>

/**
* Cron Job that runs every hours to delete expired otp
* @returns {Promise<void>} - A promise that resolves when the Expired OTPs are successfully deleted.
*/
deleteExpiredOtps(): Promise<void>
}
4 changes: 4 additions & 0 deletions apps/api/src/auth/repository/mock.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ export class MockAuthRepository implements IAuthRepository {
deleteOtp(email: string, otp: string): Promise<void> {
throw new Error('Method not implemented.')
}

deleteExpiredOtps(): Promise<void> {
throw new Error('Method not implemented.')
}
}
11 changes: 11 additions & 0 deletions apps/api/src/auth/service/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@nestjs/common'
import { randomUUID } from 'crypto'
import { JwtService } from '@nestjs/jwt'
import { Cron, CronExpression } from '@nestjs/schedule'
import { UserAuthenticatedResponse } from '../auth.types'
import {
IMailService,
Expand Down Expand Up @@ -84,4 +85,14 @@ export class AuthService {
token: await this.jwt.signAsync({ id: user.id })
}
}

@Cron(CronExpression.EVERY_HOUR)
async cleanUpExpiredOtps() {
try {
await this.authRepository.deleteExpiredOtps()
rajdip-b marked this conversation as resolved.
Show resolved Hide resolved
this.logger.log('Expired OTPs cleaned up successfully.')
} catch (error) {
this.logger.error(`Error cleaning up expired OTPs: ${error.message}`)
}
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"@nestjs/jwt": "^10.2.0",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.3.0",
"@nestjs/schedule": "^4.0.0",
"@nestjs/swagger": "^7.1.17",
"@prisma/client": "^5.7.1",
"@supabase/supabase-js": "^2.39.2",
Expand Down
38 changes: 38 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading