feat: integrate CampusScope.filter() into all business services (12 services + 12 modules)
This commit is contained in:
@@ -4,12 +4,13 @@ import { Student } from '../entities/student.entity';
|
||||
import { Deposit } from '../entities/deposit.entity';
|
||||
import { DepositInstallment } from '../entities/deposit-installment.entity';
|
||||
import { DepositsService } from './deposits.service';
|
||||
import { DepartmentsModule } from '../departments/departments.module';
|
||||
import { DepositsController } from './deposits.controller';
|
||||
import { OperationLogsModule } from '../operation-logs/operation-logs.module';
|
||||
import { NotificationsModule } from '../notifications/notifications.module';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Deposit, DepositInstallment, Student]), OperationLogsModule, NotificationsModule],
|
||||
imports: [TypeOrmModule.forFeature([Deposit, DepositInstallment, Student]), OperationLogsModule, NotificationsModule, DepartmentsModule],
|
||||
controllers: [DepositsController],
|
||||
providers: [DepositsService],
|
||||
exports: [DepositsService],
|
||||
|
||||
@@ -3,22 +3,27 @@ import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Deposit } from '../entities/deposit.entity';
|
||||
import { DepositInstallment } from '../entities/deposit-installment.entity';
|
||||
import { CampusScope } from '../common/campus-scope';
|
||||
import { CreateDepositDto, RefundDepositDto, CreateDepositWithInstallmentsDto } from './dto/deposit.dto';
|
||||
|
||||
@Injectable()
|
||||
export class DepositsService {
|
||||
|
||||
constructor(
|
||||
@InjectRepository(Deposit) private repo: Repository<Deposit>,
|
||||
@InjectRepository(DepositInstallment)
|
||||
private installmentRepo: Repository<DepositInstallment>,
|
||||
private readonly scope: CampusScope,
|
||||
) {}
|
||||
|
||||
async findAll(query?: { studentId?: number; status?: string }) {
|
||||
const scopeIds = await this.scope.getScopeDepartmentIds();
|
||||
const qb = this.repo
|
||||
.createQueryBuilder('d')
|
||||
.leftJoinAndSelect('d.student', 'student')
|
||||
.leftJoinAndSelect('d.installments', 'installments')
|
||||
.orderBy('d.createdAt', 'DESC');
|
||||
if (scopeIds) qb.andWhere('d.departmentId IN (:...scopeIds)', { scopeIds });
|
||||
if (query?.studentId) qb.andWhere('d.studentId = :studentId', { studentId: query.studentId });
|
||||
if (query?.status) qb.andWhere('d.status = :status', { status: query.status });
|
||||
return qb.getMany();
|
||||
@@ -145,12 +150,12 @@ export class DepositsService {
|
||||
|
||||
return this.repo.save(deposit);
|
||||
}
|
||||
|
||||
async findPendingRefunds() {
|
||||
const baseWhere = await this.scope.filter({});
|
||||
return this.repo.find({
|
||||
where: [
|
||||
{ refundStatus: 'pending' },
|
||||
{ refundStatus: 'head_teacher_approved' },
|
||||
{ ...baseWhere, refundStatus: 'pending' },
|
||||
{ ...baseWhere, refundStatus: 'head_teacher_approved' },
|
||||
],
|
||||
relations: ['student', 'installments'],
|
||||
order: { refundRequestedAt: 'DESC' },
|
||||
@@ -165,13 +170,14 @@ export class DepositsService {
|
||||
}
|
||||
|
||||
async getStats() {
|
||||
const result = await this.repo
|
||||
const scopeIds = await this.scope.getScopeDepartmentIds();
|
||||
const qb = this.repo
|
||||
.createQueryBuilder('d')
|
||||
.select('d.status', 'status')
|
||||
.addSelect('COUNT(*)', 'count')
|
||||
.addSelect('SUM(d.amount)', 'totalAmount')
|
||||
.groupBy('d.status')
|
||||
.getRawMany();
|
||||
return result;
|
||||
.addSelect('SUM(d.amount)', 'totalAmount');
|
||||
if (scopeIds) qb.andWhere('d.departmentId IN (:...scopeIds)', { scopeIds });
|
||||
qb.groupBy('d.status');
|
||||
return qb.getRawMany();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user