30 lines
1.3 KiB
TypeScript
30 lines
1.3 KiB
TypeScript
import { Injectable } from "@nestjs/common";
|
|
import { Prisma, PrismaClient } from "@prisma/client";
|
|
import { PrismaPostgresService } from "prisma/postgres/prisma-postgres.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: 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 } };
|
|
}
|
|
} |