mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-25 13:49:40 +00:00
46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { PageOptionsRequestDto } from '~/core/dtos';
|
|
import { SetThemeRequestDto } from '../dtos/request';
|
|
import { Theme } from '../entities';
|
|
import { Junior } from '../entities/junior.entity';
|
|
const FIRST_PAGE = 1;
|
|
@Injectable()
|
|
export class JuniorRepository {
|
|
constructor(@InjectRepository(Junior) private juniorRepository: Repository<Junior>) {}
|
|
|
|
findJuniorsByGuardianId(guardianId: string, pageOptions: PageOptionsRequestDto) {
|
|
return this.juniorRepository.findAndCount({
|
|
where: { guardianId },
|
|
relations: ['customer', 'customer.user'],
|
|
skip: (pageOptions.page - FIRST_PAGE) * pageOptions.size,
|
|
take: pageOptions.size,
|
|
});
|
|
}
|
|
|
|
findJuniorById(juniorId: string, withGuardianRelation = false, guardianId?: string) {
|
|
const relations = ['customer', 'customer.user', 'theme', 'theme.avatar'];
|
|
if (withGuardianRelation) {
|
|
relations.push('guardian', 'guardian.customer', 'guardian.customer.user');
|
|
}
|
|
return this.juniorRepository.findOne({
|
|
where: { id: juniorId, guardianId },
|
|
relations,
|
|
});
|
|
}
|
|
|
|
setTheme(body: SetThemeRequestDto, junior: Junior) {
|
|
junior.theme = Theme.create({ ...body, junior });
|
|
return this.juniorRepository.save(junior);
|
|
}
|
|
|
|
removeTheme(theme: Theme) {
|
|
return this.juniorRepository.manager.remove(theme);
|
|
}
|
|
|
|
findThemeForJunior(juniorId: string) {
|
|
return this.juniorRepository.manager.findOne(Theme, { where: { juniorId }, relations: ['avatar'] });
|
|
}
|
|
}
|