targo-backend/src/modules/employees/services/employees.service.ts

230 lines
6.9 KiB
TypeScript

import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';
import { EmployeeListItemDto } from '../dtos/list-employee.dto';
import { EmployeeProfileItemDto } from '../dtos/profil-employee.dto';
@Injectable()
export class EmployeesService {
constructor(private readonly prisma: PrismaService) { }
findListEmployees(): Promise<EmployeeListItemDto[]> {
return this.prisma.employees.findMany({
select: {
user: {
select: {
first_name: true,
last_name: true,
email: true,
},
},
supervisor: {
select: {
user: {
select: {
first_name: true,
last_name: true,
},
},
},
},
job_title: true,
company_code: true,
}
}).then(rows => rows.map(r => ({
first_name: r.user.first_name,
last_name: r.user.last_name,
email: r.user.email,
company_name: r.company_code,
job_title: r.job_title,
employee_full_name: `${r.user.first_name} ${r.user.last_name}`,
supervisor_full_name: r.supervisor ? `${r.supervisor.user.first_name} ${r.supervisor.user.last_name}` : null,
})),
);
}
async findOneProfile(email: string): Promise<EmployeeProfileItemDto> {
const emp = await this.prisma.employees.findFirst({
where: { user: { email } },
select: {
user: {
select: {
first_name: true,
last_name: true,
email: true,
phone_number: true,
residence: true,
},
},
supervisor: {
select: {
user: {
select: {
first_name: true,
last_name: true,
},
},
},
},
job_title: true,
company_code: true,
first_work_day: true,
last_work_day: true,
}
});
if (!emp) throw new NotFoundException(`Employee with email ${email} not found`);
return {
first_name: emp.user.first_name,
last_name: emp.user.last_name,
email: emp.user.email,
residence: emp.user.residence,
phone_number: emp.user.phone_number,
company_name: emp.company_code,
job_title: emp.job_title,
employee_full_name: `${emp.user.first_name} ${emp.user.last_name}`,
first_work_day: emp.first_work_day.toISOString().slice(0, 10),
last_work_day: emp.last_work_day ? emp.last_work_day.toISOString().slice(0, 10) : null,
supervisor_full_name: emp.supervisor ? `${emp.supervisor.user.first_name}, ${emp.supervisor.user.last_name}` : null,
};
}
//_____________________________________________________________________________________________
// Deprecated or unused methods
//_____________________________________________________________________________________________
// async create(dto: CreateEmployeeDto): Promise<Employees> {
// const {
// first_name,
// last_name,
// email,
// phone_number,
// residence,
// external_payroll_id,
// company_code,
// job_title,
// first_work_day,
// last_work_day,
// is_supervisor,
// } = dto;
// return this.prisma.$transaction(async (transaction) => {
// const user: Users = await transaction.users.create({
// data: {
// first_name,
// last_name,
// email,
// phone_number,
// residence,
// },
// });
// return transaction.employees.create({
// data: {
// user_id: user.id,
// external_payroll_id,
// company_code,
// job_title,
// first_work_day,
// last_work_day,
// is_supervisor,
// },
// });
// });
// }
// findAll(): Promise<Employees[]> {
// return this.prisma.employees.findMany({
// include: { user: true },
// });
// }
// async findOne(email: string): Promise<Employees> {
// const emp = await this.prisma.employees.findFirst({
// where: { user: { email } },
// include: { user: true },
// });
// //add search for archived employees
// if (!emp) {
// throw new NotFoundException(`Employee with email: ${email} not found`);
// }
// return emp;
// }
// async update(
// email: string,
// dto: UpdateEmployeeDto,
// ): Promise<Employees> {
// const emp = await this.findOne(email);
// const {
// first_name,
// last_name,
// phone_number,
// residence,
// external_payroll_id,
// company_code,
// job_title,
// first_work_day,
// last_work_day,
// is_supervisor,
// email: new_email,
// } = dto;
// return this.prisma.$transaction(async (transaction) => {
// if(
// first_name !== undefined ||
// last_name !== undefined ||
// new_email !== undefined ||
// phone_number !== undefined ||
// residence !== undefined
// ){
// await transaction.users.update({
// where: { id: emp.user_id },
// data: {
// ...(first_name !== undefined && { first_name }),
// ...(last_name !== undefined && { last_name }),
// ...(email !== undefined && { email }),
// ...(phone_number !== undefined && { phone_number }),
// ...(residence !== undefined && { residence }),
// },
// });
// }
// const updated = await transaction.employees.update({
// where: { id: emp.id },
// data: {
// ...(external_payroll_id !== undefined && { external_payroll_id }),
// ...(company_code !== undefined && { company_code }),
// ...(first_work_day !== undefined && { first_work_day }),
// ...(last_work_day !== undefined && { last_work_day }),
// ...(job_title !== undefined && { job_title }),
// ...(is_supervisor !== undefined && { is_supervisor }),
// },
// });
// return updated;
// });
// }
// async remove(email: string): Promise<Employees> {
// const emp = await this.findOne(email);
// return this.prisma.$transaction(async (transaction) => {
// await transaction.employees.updateMany({
// where: { supervisor_id: emp.id },
// data: { supervisor_id: null },
// });
// const deleted_employee = await transaction.employees.delete({
// where: {id: emp.id },
// });
// await transaction.users.delete({
// where: { id: emp.user_id },
// });
// return deleted_employee;
// });
// }
}