import { Injectable, NotFoundException } from "@nestjs/common"; import { Prisma, PrismaClient } from "@prisma/client"; import { PrismaService } from "src/prisma/prisma.service"; import { EmailToIdResolver } from "./email-id.mapper"; import { Result } from "src/common/errors/result-error.factory"; import { weekStartSunday } from "src/common/utils/date-utils"; 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); console.log('start date: ', start_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} }; } }