| 1 | import { Injectable } from '@nestjs/common' |
| 2 | import { InjectModel } from '@nestjs/mongoose' |
| 3 | import { AccountType } from '@yikart/common' |
| 4 | import { Model } from 'mongoose' |
| 5 | import { DB_CONNECTION_NAME } from '../common' |
| 6 | import { OAuth2Credential } from '../schemas' |
| 7 | import { BaseRepository } from './base.repository' |
| 8 | |
| 9 | @Injectable() |
| 10 | export class OAuth2CredentialRepository extends BaseRepository<OAuth2Credential> { |
| 11 | constructor( |
| 12 | @InjectModel(OAuth2Credential.name, DB_CONNECTION_NAME) private oauth2CredentialModel: Model<OAuth2Credential>, |
| 13 | ) { |
| 14 | super(oauth2CredentialModel) |
| 15 | } |
| 16 | |
| 17 | async getOne(accountId: string, platform: AccountType | 'meta') { |
| 18 | const oauth2Credential = await this.oauth2CredentialModel.findOne( |
| 19 | { |
| 20 | accountId, |
| 21 | platform, |
| 22 | }, |
| 23 | ).lean({ virtuals: true }) |
| 24 | return oauth2Credential |
| 25 | } |
| 26 | |
| 27 | async upsertOne(accountId: string, platform: AccountType | 'meta', newData: Partial<OAuth2Credential>): Promise<boolean> { |
| 28 | const persistResult = await this.oauth2CredentialModel.updateOne( |
| 29 | { |
| 30 | accountId, |
| 31 | platform, |
| 32 | }, |
| 33 | newData, |
| 34 | { |
| 35 | upsert: true, |
| 36 | }, |
| 37 | ) |
| 38 | return (persistResult.modifiedCount > 0 || persistResult.upsertedCount > 0) |
| 39 | } |
| 40 | |
| 41 | async delOne(accountId: string, platform: AccountType | 'meta') { |
| 42 | await this.oauth2CredentialModel.deleteOne({ |
| 43 | accountId, |
| 44 | platform, |
| 45 | }).exec() |
| 46 | } |
| 47 | } |
| 48 |