targo-backend/src/modules/pay-periods/services/pay-periods-command.service.ts

41 lines
1.7 KiB
TypeScript

import { Injectable, NotFoundException } from "@nestjs/common";
import { TimesheetsCommandService } from "src/modules/timesheets/services/timesheets-command.service";
import { PrismaService } from "src/prisma/prisma.service";
@Injectable()
export class PayPeriodsCommandService {
constructor(
private readonly prisma: PrismaService,
private readonly timesheetsApproval: TimesheetsCommandService,
) {}
async approvalPayPeriod(year: number , periodNumber: number): Promise<void> {
const period = await this.prisma.payPeriods.findUnique({
where: { period_number: periodNumber },
});
if (!period) throw new NotFoundException(`PayPeriod #${periodNumber} not found`);
//fetches timesheet of selected period if the timesheet as atleast 1 shift or 1 expense
const timesheetList = await this.prisma.timesheets.findMany({
where: {
OR: [
{ shift: {some: { date: { gte: period.start_date,
lte: period.end_date,
},
}},
},
{ expense: { some: { date: { gte: period.start_date,
lte: period.end_date,
},
}},
},
],
},
});
//approval of both timesheet (cascading to the approval of related shifts and expenses)
for(const timesheet of timesheetList) {
await this.timesheetsApproval.updateApproval(timesheet.id, true);
}
}
}