186 lines
8.9 KiB
TypeScript
186 lines
8.9 KiB
TypeScript
// import { EmployeeTimesheetResolver } from "src/common/mappers/timesheet.mapper";
|
|
// import { EmailToIdResolver } from "src/common/mappers/email-id.mapper";
|
|
// import { BankCodesResolver } from "src/common/mappers/bank-type-id.mapper";
|
|
// import { PrismaPostgresService } from "prisma/postgres/prisma-postgres.service";
|
|
// import { Result } from "src/common/errors/result-error.factory";
|
|
// import { Injectable } from "@nestjs/common";
|
|
// import { LeaveRequestDto } from "src/time-and-attendance/leave-requests/leave-request.dto";
|
|
// import { leave_requests_select } from "src/time-and-attendance/utils/selects.utils";
|
|
// import { toDateFromString, toStringFromDate } from "src/common/utils/date-utils";
|
|
// import { NormalizedLeaveRequest } from "src/time-and-attendance/utils/type.utils";
|
|
|
|
// @Injectable()
|
|
// export class LeaveRequestsService {
|
|
// // constructor(
|
|
// // private readonly prisma: PrismaPostgresService,
|
|
// // private readonly timesheetResolver: EmployeeTimesheetResolver,
|
|
// // private readonly emailResolver: EmailToIdResolver,
|
|
// // private readonly typeResolver: BankCodesResolver,
|
|
// // ) { }
|
|
|
|
|
|
|
|
// // async create(email: string, dto: LeaveRequestDto): Promise<Result<LeaveRequestDto, string>> {
|
|
// // try {
|
|
// // //verify if array is empty or not
|
|
// // if (!Array.isArray(dto.dates) || dto.dates.length === 0) return { success: false, error: 'no data received' };
|
|
// // //verify if email is valid or not
|
|
// // const employee = await this.emailResolver.findIdByEmail(email);
|
|
// // if (!employee.success) return { success: false, error: employee.error }
|
|
// // //normalized dto datas to match DB's
|
|
// // const normed_request = await this.normalizeRequest(dto);
|
|
// // if (!normed_request.success) return { success: false, error: normed_request.error }
|
|
|
|
// // //creates the requests
|
|
// // const request_day = await this.prisma.leaveRequests.create({
|
|
// // data: {
|
|
// // employee_id: employee.data,
|
|
// // bank_code_id: normed_request.data.bank_code_id,
|
|
// // comment: normed_request.data.comment,
|
|
// // dates: normed_request.data.dates,
|
|
// // approval_status: dto.approval_status,
|
|
// // requested_hours: dto.requested_hours,
|
|
// // leave_type: normed_request.data.leave_type,
|
|
// // },
|
|
// // select: leave_requests_select,
|
|
// // });
|
|
// // if (!request_day) return { success: false, error: 'An error occured during creation. Leave-Request is invalid' }
|
|
|
|
// // const created_request: LeaveRequestDto = {
|
|
// // email: dto.email,
|
|
// // type: request_day.leave_type.toString(),
|
|
// // dates: dto.dates,
|
|
// // comment: normed_request.data.comment,
|
|
// // approval_status: 'PENDING',
|
|
// // requested_hours: dto.requested_hours,
|
|
// // };
|
|
|
|
// // return { success: true, data: created_request };
|
|
// // } catch (error) {
|
|
// // return { success: false, error: `An error occured during creation, invalid data` }
|
|
// // }
|
|
// // }
|
|
|
|
// // async update(email: string, request_id: number, dto: LeaveRequestDto): Promise<Result<LeaveRequestDto, string>> {
|
|
// // try {
|
|
// // const employee = await this.emailResolver.findIdByEmail(email);
|
|
// // if (!employee.success) return { success: false, error: employee.error }
|
|
|
|
// // switch (dto.approval_status) {
|
|
// // case 'APPROVED': //creation of shifts and returns void
|
|
// // break;
|
|
// // case 'DENIED': //simple update and returns void
|
|
// // break;
|
|
// // case 'CANCELLED': //CANCELLED, simple update and returns void
|
|
// // break;
|
|
// // case 'PENDING': this.updatePendingRequest(dto)
|
|
// // break;
|
|
// // default: return { success: false, error: `invalid approval_status` };
|
|
// // }
|
|
|
|
// // // const updated_request: LeaveRequestDto = {
|
|
// // // email: dto.email,
|
|
// // // type: request_day.leave_type.toString(),
|
|
// // // dates: dto.dates,
|
|
// // // comment: normed_request.data.comment,
|
|
// // // approval_status: dto.approval_status,
|
|
// // // requested_hours: dto.requested_hours,
|
|
// // // }
|
|
|
|
// // } catch (error) {
|
|
// // return { success: false, error: ' An error occured during update, Invalid Leave-Request data' }
|
|
// // }
|
|
// // return { success: true, data: updated_request };
|
|
// // }
|
|
|
|
// // async delete(email: string, request_id: number): Promise<Result<number, string>> {
|
|
// // try {
|
|
// // const employee = await this.emailResolver.findIdByEmail(email);
|
|
// // if (!employee.success) return { success: false, error: employee.error }
|
|
|
|
// // const deleted = await this.prisma.leaveRequests.findUnique({
|
|
// // where: { id: request_id, employee_id: employee.data },
|
|
// // select: { id: true, dates: true },
|
|
// // });
|
|
// // if (!deleted) return { success: false, error: `Leave Request with id ${request_id} not found ` };
|
|
// // return { success: true, data: deleted.id };
|
|
|
|
// // } catch (error) {
|
|
// // return { success: false, error: `INVALID_REQUEST, leave-request with id ${request_id} not found` }
|
|
// // }
|
|
// // }
|
|
|
|
// // private normalizeRequest = async (dto: LeaveRequestDto): Promise<Result<NormalizedLeaveRequest, string>> => {
|
|
// // const bank_code = await this.typeResolver.findBankCodeIDByType(dto.type);
|
|
// // if (!bank_code.success) return { success: false, error: bank_code.error };
|
|
// // const comment = this.truncate280(dto.comment);
|
|
// // //maps enum, check if dto.type is include in the list and return a valid type
|
|
// // const leave_type_list = Object.values(LeaveTypes);
|
|
// // const leave_type = leave_type_list.includes(dto.type.toUpperCase() as LeaveTypes);
|
|
// // if (!leave_type) return { success: false, error: `Leave Request of type ${dto.type} is invalid` }
|
|
// // const valid_leave_type = dto.type.toUpperCase() as LeaveTypes;
|
|
|
|
// // //map of all dates in string format
|
|
// // const dates = dto.dates.map(toDateFromString);
|
|
// // if (!dates) return { success: false, error: 'Bad date' }
|
|
|
|
// // return { success: true, data: { comment, dates, leave_type: valid_leave_type, bank_code_id: bank_code.data } };
|
|
// // }
|
|
|
|
// // //makes sure that a string cannot exceed 280 chars
|
|
// // private truncate280 = (input: string): string => {
|
|
// // return input.length > 280 ? input.slice(0, 280) : input;
|
|
// // }
|
|
|
|
// // private updatePendingRequest = async (request_id: number, dto: LeaveRequestDto): Promise<Result<LeaveRequestDto, string>> => {
|
|
// // const normed_dto = await this.normalizeRequest(dto);
|
|
// // if (!normed_dto.success) return { success: false, error: normed_dto.error }
|
|
|
|
// // const leave_request = await this.prisma.leaveRequests.findUnique({
|
|
// // where: { id: request_id },
|
|
// // select: leave_requests_select,
|
|
// // });
|
|
// // if (!leave_request) return { success: false, error: `Leave Request with id: ${request_id} not found` }
|
|
|
|
// // const update = await this.prisma.leaveRequests.update({
|
|
// // where: { id: request_id },
|
|
// // data: {
|
|
// // bank_code_id: normed_dto.data.bank_code_id,
|
|
// // leave_type: normed_dto.data.leave_type,
|
|
// // comment: normed_dto.data.comment,
|
|
// // dates: normed_dto.data.dates,
|
|
// // requested_hours: dto.requested_hours ?? 0,
|
|
// // }
|
|
// // })
|
|
// // // (alias) class LeaveRequestDto {
|
|
// // // email: string;
|
|
// // // dates: string[];
|
|
// // // type: string;
|
|
// // // comment: string;
|
|
// // // requested_hours?: number | undefined;
|
|
// // // approval_status?: $Enums.LeaveApprovalStatus | undefined;
|
|
// // // }
|
|
// // // const dates_string = update.dates.map(toStringFromDate);
|
|
// // // const updated_request: LeaveRequestDto = {
|
|
// // // email: leave_request,
|
|
// // // type: update.leave_type,
|
|
|
|
// // // }
|
|
|
|
// // return { success: true, data: updated_request }
|
|
// // }
|
|
// }
|
|
|
|
// type leaveRequests = {
|
|
// id: number;
|
|
// bank_code_id: number;
|
|
// comment: string;
|
|
// dates: Date[];
|
|
// payable_hours: Prisma.Decimal | null;
|
|
// requested_hours: Prisma.Decimal | null;
|
|
// approval_status: LeaveApprovalStatus;
|
|
// leave_type: LeaveTypes;
|
|
// }
|
|
|
|
|