Skip to content

Commit

Permalink
chore:create a cron function that will run every hour to clean up exp…
Browse files Browse the repository at this point in the history
…ired otps
  • Loading branch information
Lakshay saini committed Jan 4, 2024
1 parent 091e49b commit 5ffb54d
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
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()
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.

0 comments on commit 5ffb54d

Please sign in to comment.