| 1 | import { InjectModel } from '@nestjs/mongoose' |
| 2 | import { Model } from 'mongoose' |
| 3 | import { ApiKey } from '../schemas' |
| 4 | import { BaseRepository } from './base.repository' |
| 5 | |
| 6 | export class ApiKeyRepository extends BaseRepository<ApiKey> { |
| 7 | constructor( |
| 8 | @InjectModel(ApiKey.name) apiKeyModel: Model<ApiKey>, |
| 9 | ) { |
| 10 | super(apiKeyModel) |
| 11 | } |
| 12 | |
| 13 | async getByKeyHash(keyHash: string) { |
| 14 | return await this.findOne({ keyHash }) |
| 15 | } |
| 16 | |
| 17 | async listByUserId(userId: string) { |
| 18 | return await this.find({ userId }) |
| 19 | } |
| 20 | |
| 21 | async updateLastUsedAt(id: string) { |
| 22 | await this.updateById(id, { lastUsedAt: new Date() }) |
| 23 | } |
| 24 | |
| 25 | async deleteByIdAndUserId(id: string, userId: string) { |
| 26 | await this.deleteOne({ _id: id, userId }) |
| 27 | } |
| 28 | } |
| 29 |