137 lines
6.6 KiB
TypeScript
137 lines
6.6 KiB
TypeScript
// import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
|
|
// import { EmployeeTimesheetResolver } from "src/modules/shared/utils/resolve-timesheet.utils";
|
|
// import { getWeekEnd, getWeekStart } from "src/common/utils/date-utils";
|
|
// import { parseISODate, parseHHmm } from "./utils-helpers-others/timesheet.helpers";
|
|
// import { TimesheetsQueryService } from "./timesheets-query.service";
|
|
// import { BaseApprovalService } from "src/common/shared/base-approval.service";
|
|
// import { Prisma, Timesheets } from "@prisma/client";
|
|
// import { CreateTimesheetDto } from "./create-timesheet.dto";
|
|
// import { EmailToIdResolver } from "src/modules/shared/utils/resolve-email-id.utils";
|
|
// import { BankCodesResolver } from "src/modules/shared/utils/resolve-bank-type-id.utils";
|
|
// import { PrismaService } from "src/prisma/prisma.service";
|
|
// import { TimesheetMap } from "./utils-helpers-others/timesheet.types";
|
|
// import { Shift, Expense } from "../dtos/timesheet.dto";
|
|
|
|
// @Injectable()
|
|
// export class TimesheetsCommandService extends BaseApprovalService<Timesheets>{
|
|
// constructor(
|
|
// prisma: PrismaService,
|
|
// private readonly query: TimesheetsQueryService,
|
|
// private readonly emailResolver: EmailToIdResolver,
|
|
// private readonly timesheetResolver: EmployeeTimesheetResolver,
|
|
// private readonly bankTypeResolver: BankCodesResolver,
|
|
// ) {super(prisma);}
|
|
// //_____________________________________________________________________________________________
|
|
// // APPROVAL AND DELEGATE METHODS
|
|
// //_____________________________________________________________________________________________
|
|
// protected get delegate() {
|
|
// return this.prisma.timesheets;
|
|
// }
|
|
|
|
// protected delegateFor(transaction: Prisma.TransactionClient) {
|
|
// return transaction.timesheets;
|
|
// }
|
|
|
|
// async updateApproval(id: number, isApproved: boolean): Promise<Timesheets> {
|
|
// return this.prisma.$transaction((transaction) =>
|
|
// this.updateApprovalWithTransaction(transaction, id, isApproved),
|
|
// );
|
|
// }
|
|
|
|
// async cascadeApprovalWithtx(transaction: Prisma.TransactionClient, timesheetId: number, isApproved: boolean): Promise<Timesheets> {
|
|
// const timesheet = await this.updateApprovalWithTransaction(transaction, timesheetId, isApproved);
|
|
// await transaction.shifts.updateMany({
|
|
// where: { timesheet_id: timesheetId },
|
|
// data: { is_approved: isApproved },
|
|
// });
|
|
// await transaction.expenses.updateManyAndReturn({
|
|
// where: { timesheet_id: timesheetId },
|
|
// data: { is_approved: isApproved },
|
|
// });
|
|
// return timesheet;
|
|
// }
|
|
|
|
// /**_____________________________________________________________________________________________
|
|
// create/update/delete shifts and expenses from 1 or many timesheet(s)
|
|
|
|
// -this function receives an email and an array of timesheets
|
|
|
|
// -this function will find the timesheets with all shifts and expenses
|
|
// -this function will calculate total hours, total expenses, filtered by types,
|
|
// cumulate in daily and weekly.
|
|
|
|
// -the timesheet_id will be determined using the employee email
|
|
// -with the timesheet_id, all shifts and expenses will be fetched
|
|
|
|
// -with shift_id and expense_id, this function will compare both
|
|
// datas from the DB and from the body of the function and then:
|
|
// -it will create a shift if no shift is found in the DB
|
|
// -it will update a shift if a shift is found in the DB
|
|
// -it will delete a shift if a shift is found and no data is received from the frontend
|
|
|
|
// This function will be used for the Timesheet Page for an employee to enter, modify or delete and entry
|
|
// This function will also be used in the modal of the timesheet validation page to
|
|
// allow a supervisor to enter, modify or delete and entry of a selected employee
|
|
// _____________________________________________________________________________________________*/
|
|
|
|
// async findTimesheetsByEmailAndPayPeriod(email: string, year: number, period_no: number, timesheets: Timesheets[]): Promise<Timesheets[]> {
|
|
// const employee_id = await this.emailResolver.findIdByEmail(email);
|
|
|
|
|
|
// return timesheets;
|
|
// }
|
|
|
|
// async upsertOrDeleteShiftsByEmailAndDate(email:string, shift_ids: Shift[]) {}
|
|
|
|
// async upsertOrDeleteExpensesByEmailAndDate(email:string, expenses_id: Expense[]) {}
|
|
|
|
|
|
|
|
|
|
|
|
// //_____________________________________________________________________________________________
|
|
// //
|
|
// //_____________________________________________________________________________________________
|
|
|
|
// async createWeekShiftsAndReturnOverview(
|
|
// email:string,
|
|
// shifts: CreateTimesheetDto[],
|
|
// week_offset = 0,
|
|
// ): Promise<TimesheetMap> {
|
|
// //fetchs employee matchint user's email
|
|
// const employee_id = await this.emailResolver.findIdByEmail(email);
|
|
// if(!employee_id) throw new NotFoundException(`employee for ${ email } not found`);
|
|
|
|
// //insure that the week starts on sunday and finishes on saturday
|
|
// const base = new Date();
|
|
// base.setDate(base.getDate() + week_offset * 7);
|
|
// const start_week = getWeekStart(base, 0);
|
|
// const end_week = getWeekEnd(start_week);
|
|
|
|
// const timesheet = await this.timesheetResolver.findTimesheetIdByEmail(email, base)
|
|
// if(!timesheet) throw new NotFoundException(`no timesheet found for employe ${employee_id}`);
|
|
|
|
// //validations and insertions
|
|
// for(const shift of shifts) {
|
|
// const date = parseISODate(shift.date);
|
|
// if (date < start_week || date > end_week) throw new BadRequestException(`date ${shift.date} not in current week`);
|
|
|
|
// const bank_code = await this.bankTypeResolver.findByType(shift.type)
|
|
// if(!bank_code) throw new BadRequestException(`Invalid bank_code type: ${shift.type}`);
|
|
|
|
// await this.prisma.shifts.create({
|
|
// data: {
|
|
// timesheet_id: timesheet.id,
|
|
// bank_code_id: bank_code.id,
|
|
// date: date,
|
|
// start_time: parseHHmm(shift.start_time),
|
|
// end_time: parseHHmm(shift.end_time),
|
|
// comment: shift.comment ?? null,
|
|
// is_approved: false,
|
|
// is_remote: false,
|
|
// },
|
|
// });
|
|
// }
|
|
// return this.query.getTimesheetByEmail(email, week_offset);
|
|
// }
|
|
// }
|