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, ) {} async findByUserId(userId: string): Promise { return this.repo.findOne({ where: { userId } }); } async findByCode(code: string): Promise { return this.repo.findOne({ where: { referralCode: code.toUpperCase() } }); } async existsByCode(code: string): Promise { const count = await this.repo.count({ where: { referralCode: code.toUpperCase() } }); return count > 0; } async existsByUserId(userId: string): Promise { 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 { return this.repo.save(profile); } }