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
+63
View File
@@ -215,4 +215,67 @@ describe('Employees (e2e)', () => {
expect(res.body).toMatchObject({ imported: 1 });
});
it('creates and updates a linked user from the employee payload', async () => {
const suffix = Date.now().toString().slice(-6);
const created = await request(app.getHttpServer())
.post('/employees')
.set('Authorization', `Bearer ${adminAccessToken}`)
.send({
code: `EU_${suffix}`,
name: 'Ada Lovelace',
phone: '+6281234567890',
position: 'sales',
user: {
username: `emp_usr_${suffix}`,
password: 'password123',
},
})
.expect(201);
expect(created.body.user).toMatchObject({
username: `emp_usr_${suffix}`,
});
const employeeId = (created.body as { id: string }).id;
const linkedUserId = (created.body.user as { id: string }).id;
const user = await request(app.getHttpServer())
.get(`/users/${linkedUserId}`)
.set('Authorization', `Bearer ${adminAccessToken}`)
.expect(200);
expect(user.body.employee).toMatchObject({
id: employeeId,
code: `EU_${suffix}`,
});
const renamed = await request(app.getHttpServer())
.patch(`/employees/${employeeId}`)
.set('Authorization', `Bearer ${adminAccessToken}`)
.send({ user: { username: `emp_ren_${suffix}` } })
.expect(200);
expect(renamed.body.user).toMatchObject({
id: linkedUserId,
username: `emp_ren_${suffix}`,
});
const existingUser = await request(app.getHttpServer())
.post('/users')
.set('Authorization', `Bearer ${adminAccessToken}`)
.send({
username: `emp_ex_${suffix}`,
password: 'password123',
})
.expect(201);
const existingUserId = (existingUser.body as { id: string }).id;
const assigned = await request(app.getHttpServer())
.patch(`/employees/${employeeId}`)
.set('Authorization', `Bearer ${adminAccessToken}`)
.send({ user: { id: existingUserId, username: `emp_as_${suffix}` } })
.expect(200);
expect(assigned.body.user).toMatchObject({
id: existingUserId,
username: `emp_as_${suffix}`,
});
});
});