mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-25 05:42:27 +00:00
65 lines
1.9 KiB
TypeScript
65 lines
1.9 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { PageOptionsRequestDto } from '~/core/dtos';
|
|
import { CreateAllowanceRequestDto } from '../dtos/request';
|
|
import { Allowance } from '../entities';
|
|
const ONE = 1;
|
|
@Injectable()
|
|
export class AllowancesRepository {
|
|
constructor(@InjectRepository(Allowance) private readonly allowancesRepository: Repository<Allowance>) {}
|
|
|
|
createAllowance(guardianId: string, body: CreateAllowanceRequestDto) {
|
|
return this.allowancesRepository.save(
|
|
this.allowancesRepository.create({
|
|
guardianId,
|
|
name: body.name,
|
|
amount: body.amount,
|
|
frequency: body.frequency,
|
|
type: body.type,
|
|
startDate: body.startDate,
|
|
endDate: body.endDate,
|
|
numberOfTransactions: body.numberOfTransactions,
|
|
juniorId: body.juniorId,
|
|
}),
|
|
);
|
|
}
|
|
|
|
findAllowanceById(allowanceId: string, guardianId?: string) {
|
|
return this.allowancesRepository.findOne({
|
|
where: { id: allowanceId, guardianId },
|
|
relations: ['junior', 'junior.customer', 'junior.customer.profilePicture'],
|
|
});
|
|
}
|
|
|
|
findAllowances(guardianId: string, query: PageOptionsRequestDto) {
|
|
return this.allowancesRepository.findAndCount({
|
|
where: { guardianId },
|
|
relations: ['junior', 'junior.customer', 'junior.customer.profilePicture'],
|
|
take: query.size,
|
|
skip: query.size * (query.page - ONE),
|
|
});
|
|
}
|
|
|
|
deleteAllowance(guardianId: string, allowanceId: string) {
|
|
return this.allowancesRepository.softDelete({ id: allowanceId, guardianId });
|
|
}
|
|
|
|
async *findAllowancesChunks(chunkSize: number) {
|
|
let offset = 0;
|
|
while (true) {
|
|
const allowances = await this.allowancesRepository.find({
|
|
take: chunkSize,
|
|
skip: offset,
|
|
});
|
|
|
|
if (!allowances.length) {
|
|
break;
|
|
}
|
|
|
|
yield allowances;
|
|
offset += chunkSize;
|
|
}
|
|
}
|
|
}
|