gcx/backend/services/referral-service/src/infrastructure/persistence/referral-profile.repository.ts

49 lines
1.5 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ReferralProfile } from '../../domain/entities/referral-profile.entity';
import { IReferralProfileRepository } from '../../domain/repositories/referral-profile.repository.interface';
@Injectable()
export class ReferralProfileRepository implements IReferralProfileRepository {
constructor(
@InjectRepository(ReferralProfile)
private readonly repo: Repository<ReferralProfile>,
) {}
async findByUserId(userId: string): Promise<ReferralProfile | null> {
return this.repo.findOne({ where: { userId } });
}
async findByCode(code: string): Promise<ReferralProfile | null> {
return this.repo.findOne({ where: { referralCode: code.toUpperCase() } });
}
async existsByCode(code: string): Promise<boolean> {
const count = await this.repo.count({ where: { referralCode: code.toUpperCase() } });
return count > 0;
}
async existsByUserId(userId: string): Promise<boolean> {
const count = await this.repo.count({ where: { userId } });
return count > 0;
}
async findDirectReferrals(
referrerId: string,
offset: number,
limit: number,
): Promise<[ReferralProfile[], number]> {
return this.repo.findAndCount({
where: { referrerId },
order: { createdAt: 'DESC' },
skip: offset,
take: limit,
});
}
async save(profile: ReferralProfile): Promise<ReferralProfile> {
return this.repo.save(profile);
}
}