import { Injectable, NotFoundException } from "@nestjs/common"; import { Prisma, PrismaClient } from "@prisma/client"; import { weekStartSunday } from "src/time-and-attendance/utils/date-time.utils"; import { PrismaService } from "src/prisma/prisma.service"; import { EmailToIdResolver } from "./resolve-email-id.utils"; import { Result } from "src/common/errors/result-error.factory"; type Tx = Prisma.TransactionClient | PrismaClient; @Injectable() export class EmployeeTimesheetResolver { constructor( private readonly prisma: PrismaService, private readonly emailResolver: EmailToIdResolver, ) {} readonly findTimesheetIdByEmail = async (email: string, date: Date, client?: Tx): Promise> => { const db = client ?? this.prisma; const employee_id = await this.emailResolver.findIdByEmail(email); if(!employee_id.success) return { success: false, error: employee_id.error} const start_date = weekStartSunday(date); const timesheet = await db.timesheets.findFirst({ where: { employee_id : employee_id.data, start_date: start_date }, select: { id: true }, }); if(!timesheet) throw new NotFoundException(`timesheet not found`); return { success: true, data: {id: timesheet.id} }; } }