237 lines
7.1 KiB
TypeScript
237 lines
7.1 KiB
TypeScript
import { Injectable } from "@nestjs/common";
|
|
import { Users } from "@prisma/client";
|
|
import { Result } from "src/common/errors/result-error.factory";
|
|
import { EmailToIdResolver } from "src/common/mappers/email-id.mapper";
|
|
import { module_list, Modules, toBooleanFromString, toStringFromBoolean } from "src/common/mappers/module-access.mapper";
|
|
import { toStringFromDate } from "src/common/utils/date-utils";
|
|
import { EmployeeDetailedDto } from "src/identity-and-account/employees/dtos/employee-detailed.dto";
|
|
import { EmployeeDto } from "src/identity-and-account/employees/dtos/employee.dto";
|
|
import { PrismaService } from "src/prisma/prisma.service";
|
|
|
|
@Injectable()
|
|
export class EmployeesService {
|
|
constructor(
|
|
private readonly prisma: PrismaService,
|
|
private readonly emailResolver: EmailToIdResolver,
|
|
) { }
|
|
|
|
async findListEmployees(): Promise<Result<EmployeeDto[], string>> {
|
|
const employee_list = await 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,
|
|
external_payroll_id: 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 ?? '',
|
|
external_payroll_id: r.external_payroll_id,
|
|
employee_full_name: `${r.user.first_name} ${r.user.last_name}`,
|
|
supervisor_full_name: `${r.supervisor?.user.first_name} ${r.supervisor?.user.last_name}`,
|
|
})),
|
|
)
|
|
return { success: true, data: employee_list }
|
|
}
|
|
|
|
async findOwnProfile(email: string): Promise<Result<Partial<EmployeeDetailedDto>, string>> {
|
|
const user_id = await this.emailResolver.resolveUserIdWithEmail(email);
|
|
if (!user_id.success) return { success: false, error: 'INVALID_USER' };
|
|
|
|
const existing_profile = await this.prisma.employees.findUnique({
|
|
where: { user_id: user_id.data },
|
|
select: {
|
|
user: {
|
|
select: {
|
|
first_name: true,
|
|
last_name: true,
|
|
email: true,
|
|
phone_number: true,
|
|
residence: true,
|
|
},
|
|
},
|
|
first_work_day: true,
|
|
company_code: true,
|
|
job_title: true,
|
|
external_payroll_id: true,
|
|
supervisor: {
|
|
select: {
|
|
id: true, user: {
|
|
select: {
|
|
first_name: true,
|
|
last_name: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
if (!existing_profile) return { success: false, error: 'EMPLOYEE_NOT_FOUND' };
|
|
|
|
let company_name = 'Solucom';
|
|
if (existing_profile.company_code === 271583) {
|
|
company_name = 'Targo';
|
|
}
|
|
|
|
return {
|
|
success: true, data: {
|
|
first_name: existing_profile.user.first_name,
|
|
last_name: existing_profile.user.last_name,
|
|
email: existing_profile.user.email,
|
|
supervisor_full_name: `${existing_profile.supervisor?.user.first_name} ${existing_profile.supervisor?.user.last_name}`,
|
|
company_name: company_name,
|
|
job_title: existing_profile.job_title ?? '',
|
|
external_payroll_id: existing_profile.external_payroll_id,
|
|
phone_number: existing_profile.user.phone_number,
|
|
residence: existing_profile.user.phone_number,
|
|
first_work_day: toStringFromDate(existing_profile.first_work_day),
|
|
}
|
|
}
|
|
}
|
|
|
|
async findOneDetailedProfile(email: string, employee_email?: string): Promise<Result<EmployeeDetailedDto, string>> {
|
|
const account_email = employee_email ?? email;
|
|
const user_id = await this.emailResolver.resolveUserIdWithEmail(account_email);
|
|
if (!user_id.success) return { success: false, error: 'INVALID_USER' };
|
|
|
|
const employee = await this.prisma.employees.findUnique({
|
|
where: { user_id: user_id.data },
|
|
select: {
|
|
user: {
|
|
select: {
|
|
first_name: true,
|
|
last_name: true,
|
|
email: true,
|
|
phone_number: true,
|
|
residence: true,
|
|
user_module_access: {
|
|
select: {
|
|
dashboard: true,
|
|
employee_list: true,
|
|
employee_management: true,
|
|
personal_profile: true,
|
|
timesheets: true,
|
|
timesheets_approval: 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,
|
|
external_payroll_id: true,
|
|
is_supervisor: true,
|
|
}
|
|
});
|
|
if (!employee) return { success: false, error: `EMPLOYEE_NOT_FOUND` };
|
|
if (!employee.user) return { success: false, error: 'USER_NOT_FOUND' };
|
|
|
|
let module_access_array: Modules[] = [];
|
|
if (employee.user.user_module_access) {
|
|
const stringfy_module_access = toStringFromBoolean(employee.user.user_module_access);
|
|
module_access_array = module_list
|
|
.map(mod => stringfy_module_access[mod])
|
|
.filter((value): value is Modules => value !== null && value !== undefined);
|
|
}
|
|
|
|
let company_name = 'Solucom';
|
|
if (employee.company_code === 271583) {
|
|
company_name = 'Targo';
|
|
}
|
|
return {
|
|
success: true,
|
|
data: {
|
|
first_name: employee.user.first_name,
|
|
last_name: employee.user.last_name,
|
|
email: employee.user.email,
|
|
residence: employee.user.residence ?? '',
|
|
phone_number: employee.user.phone_number,
|
|
company_name: company_name,
|
|
is_supervisor: employee.is_supervisor ?? false,
|
|
job_title: employee.job_title ?? '',
|
|
external_payroll_id: employee.external_payroll_id,
|
|
employee_full_name: `${employee.user.first_name} ${employee.user.last_name}`,
|
|
first_work_day: toStringFromDate(employee.first_work_day),
|
|
last_work_day: employee.last_work_day ? toStringFromDate(employee.last_work_day) : undefined,
|
|
supervisor_full_name: employee.supervisor ? `${employee.supervisor?.user.first_name}, ${employee.supervisor?.user.last_name}` : '',
|
|
user_module_access: module_access_array
|
|
},
|
|
};
|
|
}
|
|
|
|
async createEmployee(dto: EmployeeDetailedDto): Promise<Result<boolean, string>> {
|
|
let company_code = 271585;
|
|
if (dto.company_name === 'Targo') {
|
|
company_code = 271583;
|
|
}
|
|
const normalized_access = toBooleanFromString(dto.user_module_access)
|
|
|
|
await this.prisma.$transaction(async (tx) => {
|
|
const user: Users = await tx.users.create({
|
|
data: {
|
|
first_name: dto.first_name,
|
|
last_name: dto.last_name,
|
|
email: dto.email,
|
|
phone_number: dto.phone_number,
|
|
residence: dto.residence,
|
|
user_module_access: {
|
|
create: {
|
|
dashboard: normalized_access.dashboard,
|
|
employee_list: normalized_access.employee_list,
|
|
employee_management: normalized_access.employee_management,
|
|
personal_profile: normalized_access.personal_profile,
|
|
timesheets: normalized_access.timesheets,
|
|
timesheets_approval: normalized_access.timesheets_approval,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
return tx.employees.create({
|
|
data: {
|
|
user_id: user.id,
|
|
company_code: company_code,
|
|
job_title: dto.job_title,
|
|
first_work_day: dto.first_work_day,
|
|
last_work_day: dto.last_work_day,
|
|
is_supervisor: dto.is_supervisor,
|
|
},
|
|
})
|
|
});
|
|
return { success: true, data: true }
|
|
}
|
|
|
|
// async updateEmployeeProfile = () => {
|
|
|
|
// }
|
|
} |