targo-backend/src/common/mappers/timesheet.mapper.ts
2026-02-27 10:09:24 -05:00

35 lines
1.5 KiB
TypeScript

import { Injectable } from "@nestjs/common";
import { EmailToIdResolver } from "./email-id.mapper";
import { weekStartSunday } from "src/common/utils/date-utils";
import { Result } from "src/common/errors/result-error.factory";
import { PrismaPostgresService } from "prisma/postgres/prisma-postgres.service";
import { PrismaClient } from "prisma/postgres/generated/prisma/client/postgres/internal/class";
import { Prisma } from "prisma/postgres/generated/prisma/client/postgres/client";
type Tx = Prisma.TransactionClient | PrismaClient;
@Injectable()
export class EmployeeTimesheetResolver {
constructor(
private readonly prisma: PrismaPostgresService,
private readonly emailResolver: EmailToIdResolver,
) { }
readonly findTimesheetIdByEmail = async (
email: string,
date: Date,
client?: Tx
): Promise<Result<{ id: number }, string>> => {
const db = (client ?? this.prisma) as PrismaClient;
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) return { success: false, error: 'TIMESHEET_NOT_FOUND' };
return { success: true, data: { id: timesheet.id } };
}
}