feat(employees): added funcion to get employee by email

This commit is contained in:
Matthieu Haineault 2025-08-14 11:47:47 -04:00
parent 5b1746da9d
commit 183a54a89a
5 changed files with 113 additions and 0 deletions

View File

@ -384,6 +384,45 @@
]
}
},
"/employees/profile": {
"get": {
"operationId": "EmployeesController_findOneProfile",
"parameters": [
{
"name": "email",
"required": true,
"in": "path",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Employee profile found",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/EmployeeProfileItemDto"
}
}
}
},
"400": {
"description": "Employee profile not found"
}
},
"security": [
{
"access-token": []
}
],
"summary": "Find employee profile",
"tags": [
"Employees"
]
}
},
"/timesheets": {
"post": {
"operationId": "TimesheetsController_create",
@ -2307,6 +2346,10 @@
"type": "object",
"properties": {}
},
"EmployeeProfileItemDto": {
"type": "object",
"properties": {}
},
"UpdateEmployeeDto": {
"type": "object",
"properties": {

View File

@ -6,6 +6,7 @@ import { UpdateEmployeeDto } from '../dtos/update-employee.dto';
import { RolesAllowed } from '../../../common/decorators/roles.decorators';
import { ApiBearerAuth, ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger';
import { EmployeeListItemDto } from '../dtos/list-employee.dto';
import { EmployeeProfileItemDto } from '../dtos/profil-employee.dto';
@ApiTags('Employees')
@ApiBearerAuth('access-token')
@ -50,6 +51,14 @@ export class EmployeesController {
return this.employeesService.findOne(id);
}
@Get('profile')
@ApiOperation({summary: 'Find employee profile' })
@ApiResponse({ status: 200, description: 'Employee profile found', type: EmployeeProfileItemDto })
@ApiResponse({ status: 400, description: 'Employee profile not found' })
findOneProfile(@Param('email', ParseIntPipe)email: string): Promise<EmployeeProfileItemDto> {
return this.employeesService.findOneProfile(email);
}
@Delete(':id')
@RolesAllowed(RoleEnum.ADMIN, RoleEnum.HR, RoleEnum.SUPERVISOR )
@ApiOperation({summary: 'Delete employee' })

View File

@ -1,6 +1,7 @@
export class EmployeeListItemDto {
first_name: string;
last_name: string;
email: string;
supervisor_full_name: string | null;
company_name: number | null;
job_title: string | null;

View File

@ -0,0 +1,12 @@
export class EmployeeProfileItemDto {
first_name: string;
last_name: string;
supervisor_full_name: string | null;
company_name: number | null;
job_title: string | null;
email: string | null;
phone_number: number;
first_work_day: string;
last_work_day?: string | null;
residence: string | null;
}

View File

@ -5,6 +5,7 @@ import { UpdateEmployeeDto } from '../dtos/update-employee.dto';
import { Employees, EmployeesArchive, Users } from '@prisma/client';
import { EmployeeListItemDto } from '../dtos/list-employee.dto';
import { Roles as RoleEnum } from '@prisma/client';
import { EmployeeProfileItemDto } from '../dtos/profil-employee.dto';
function toDateOrNull(v?: string | null): Date | null {
if (!v) return null;
@ -70,6 +71,7 @@ export class EmployeesService {
select: {
first_name: true,
last_name: true,
email: true,
},
},
supervisor: {
@ -88,6 +90,7 @@ export class EmployeesService {
}).then(rows => rows.map(r => ({
first_name: r.user.first_name,
last_name: r.user.last_name,
email: r.user.email,
supervisor_full_name: r.supervisor ? `${r.supervisor.user.first_name} ${r.supervisor.user.last_name}` : null,
company_name: r.company_code,
job_title: r.job_title,
@ -106,6 +109,51 @@ export class EmployeesService {
return emp;
}
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,
supervisor_full_name: emp.supervisor ? `${emp.supervisor.user.first_name}, ${emp.supervisor.user.last_name}` : null,
company_name: emp.company_code,
job_title: emp.job_title,
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,
};
}
async update(
id: number,
dto: UpdateEmployeeDto,