targo-backend/src/common/mappers/email-id.mapper.ts

39 lines
1.4 KiB
TypeScript

import { PrismaPostgresService } from "prisma/postgres/prisma-postgres.service";
import { Injectable } from "@nestjs/common";
import { Result } from "src/common/errors/result-error.factory";
import { Prisma, PrismaClient } from "prisma/postgres/generated/prisma/client/postgres/client";
type Tx = Prisma.TransactionClient | PrismaClient;
@Injectable()
export class EmailToIdResolver {
constructor(private readonly prisma: PrismaPostgresService) { }
// find employee_id using email
readonly findIdByEmail = async (email: string, client?: Tx
): Promise<Result<number, string>> => {
const db = (client ?? this.prisma) as PrismaClient;
const employee = await db.employees.findFirst({
where: { user: { email } },
select: { id: true },
});
if (!employee) return { success: false, error: `EMPLOYEE_NOT_FOUND` };
return { data: employee.id, success: true };
}
// find user_id using email
readonly resolveUserIdWithEmail = async (email: string, client?: Tx
): Promise<Result<string, string>> => {
const db = (client ?? this.prisma) as PrismaClient;
const user = await db.users.findFirst({
where: { email },
select: { id: true },
});
if (!user) return { success: false, error: `EMPLOYEE_NOT_FOUND` };
return { success: true, data: user.id };
}
readonly findFullNameByEmail
}