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
+70
View File
@@ -155,4 +155,74 @@ describe('Users (e2e)', () => {
.send({ ids: [id] })
.expect(200);
});
it('creates and updates a linked employee from the user payload', async () => {
const suffix = Date.now().toString().slice(-6);
const employee = await request(app.getHttpServer())
.post('/employees')
.set('Authorization', `Bearer ${adminAccessToken}`)
.send({
code: `UE_${suffix}`,
name: 'Ada Lovelace',
phone: '+6281234567890',
position: 'sales',
})
.expect(201);
const employeeId = (employee.body as { id: string }).id;
const created = await request(app.getHttpServer())
.post('/users')
.set('Authorization', `Bearer ${adminAccessToken}`)
.send({
username: `usr_emp_${suffix}`,
password: 'password123',
employeeId,
})
.expect(201);
expect(created.body.employee).toMatchObject({
id: employeeId,
code: `UE_${suffix}`,
name: 'Ada Lovelace',
});
const userId = (created.body as { id: string }).id;
const linked = await request(app.getHttpServer())
.get(`/employees/${employeeId}`)
.set('Authorization', `Bearer ${adminAccessToken}`)
.expect(200);
expect(linked.body.user).toMatchObject({
id: userId,
username: `usr_emp_${suffix}`,
});
const existing = await request(app.getHttpServer())
.post('/employees')
.set('Authorization', `Bearer ${adminAccessToken}`)
.send({
code: `UL_${suffix}`,
name: 'Grace Hopper',
phone: '+6281234567891',
position: 'crew',
})
.expect(201);
const existingId = (existing.body as { id: string }).id;
const reassigned = await request(app.getHttpServer())
.patch(`/users/${userId}`)
.set('Authorization', `Bearer ${adminAccessToken}`)
.send({ employeeId: existingId })
.expect(200);
expect(reassigned.body.employee).toMatchObject({
id: existingId,
name: 'Grace Hopper',
});
const unlinked = await request(app.getHttpServer())
.patch(`/users/${userId}`)
.set('Authorization', `Bearer ${adminAccessToken}`)
.send({ employeeId: null })
.expect(200);
expect(unlinked.body.employee).toBeNull();
});
});