| 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 { ChannelAuthIdentity } from '../schemas' |
| 7 | import { BaseRepository } from './base.repository' |
| 8 | |
| 9 | @Injectable() |
| 10 | export class ChannelAuthIdentityRepository extends BaseRepository<ChannelAuthIdentity> { |
| 11 | constructor( |
| 12 | @InjectModel(ChannelAuthIdentity.name, DB_CONNECTION_NAME) |
| 13 | channelAuthIdentityModel: Model<ChannelAuthIdentity>, |
| 14 | ) { |
| 15 | super(channelAuthIdentityModel) |
| 16 | } |
| 17 | |
| 18 | async getByPlatformAndSubjectUid(platform: AccountType, subjectUid: string) { |
| 19 | return this.findOne({ platform, subjectUid }) |
| 20 | } |
| 21 | |
| 22 | async createOrUpdateByPlatformAndSubjectUid( |
| 23 | platform: AccountType, |
| 24 | subjectUid: string, |
| 25 | userId: string, |
| 26 | ) { |
| 27 | return this.updateOne( |
| 28 | { |
| 29 | platform, |
| 30 | subjectUid, |
| 31 | }, |
| 32 | { $set: { platform, subjectUid, userId } }, |
| 33 | { upsert: true }, |
| 34 | ) |
| 35 | } |
| 36 | |
| 37 | async deleteByPlatformAndUserId(platform: AccountType, userId: string): Promise<void> { |
| 38 | await this.deleteOne({ platform, userId }) |
| 39 | } |
| 40 | |
| 41 | async deleteByPlatformAndSubjectUid(platform: AccountType, subjectUid: string): Promise<void> { |
| 42 | await this.deleteOne({ platform, subjectUid }) |
| 43 | } |
| 44 | } |
| 45 |