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}`,
});
});
});
+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();
});
});