oa_based/src/modules/system/log/services/captcha-log.service.ts

51 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-02-28 08:32:35 +08:00
import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { LessThan, Repository } from 'typeorm'
import { paginate } from '~/helper/paginate'
import { CaptchaLogQueryDto } from '../dto/log.dto'
import { CaptchaLogEntity } from '../entities/captcha-log.entity'
@Injectable()
export class CaptchaLogService {
constructor(
@InjectRepository(CaptchaLogEntity)
private captchaLogRepository: Repository<CaptchaLogEntity>,
) {}
async create(
account: string,
code: string,
provider: 'sms' | 'email',
uid?: number,
): Promise<void> {
await this.captchaLogRepository.save({
account,
code,
provider,
userId: uid,
})
}
async paginate({ page, pageSize }: CaptchaLogQueryDto) {
const queryBuilder = await this.captchaLogRepository
.createQueryBuilder('captcha_log')
.orderBy('captcha_log.id', 'DESC')
return paginate<CaptchaLogEntity>(queryBuilder, {
page,
pageSize,
})
}
async clearLog(): Promise<void> {
await this.captchaLogRepository.clear()
}
async clearLogBeforeTime(time: Date): Promise<void> {
await this.captchaLogRepository.delete({ createdAt: LessThan(time) })
}
}