Enhance employee and user management with linked user functionality

- Introduced `EmployeeUserWrite` type to manage user details associated with employees.
- Updated `EmployeesService` and `EmployeesRepository` to support user assignment and retrieval by user ID.
- Enhanced DTOs to include user information for employee creation and updates.
- Implemented validation to ensure proper handling of user data during employee operations.
- Added unit and e2e tests to validate the new functionality and ensure data integrity in user-employee relationships.
- Modified existing controllers to accommodate the new user linkage features in employee management.
This commit is contained in:
shancheas
2026-08-27 12:22:25 +07:00
parent 8a61c94078
commit 790725e227
16 changed files with 736 additions and 18 deletions
@@ -66,7 +66,9 @@ export class EmployeesRepository {
.where(eq(employees.id, id))
.limit(1);
const row = rows[0];
return row ? this.toDomain(row, await this.loadAssignedUser(row.userId)) : null;
return row
? this.toDomain(row, await this.loadAssignedUser(row.userId))
: null;
}
async findByCode(code: string): Promise<Employee | null> {
@@ -76,7 +78,21 @@ export class EmployeesRepository {
.where(eq(employees.code, code))
.limit(1);
const row = rows[0];
return row ? this.toDomain(row, await this.loadAssignedUser(row.userId)) : null;
return row
? this.toDomain(row, await this.loadAssignedUser(row.userId))
: null;
}
async findByUserId(userId: string): Promise<Employee | null> {
const rows = await this.db
.select()
.from(employees)
.where(eq(employees.userId, userId))
.limit(1);
const row = rows[0];
return row
? this.toDomain(row, await this.loadAssignedUser(row.userId))
: null;
}
async create(input: CreateEmployeeInput): Promise<Employee> {
@@ -321,7 +337,10 @@ export class EmployeesRepository {
throw error;
}
private unwrapDbError(error: unknown): { code?: string; constraint?: string } {
private unwrapDbError(error: unknown): {
code?: string;
constraint?: string;
} {
let current: unknown = error;
for (let i = 0; i < 5; i++) {
if (!current || typeof current !== 'object') {