1
|
|
|
import { |
2
|
|
|
Body, |
3
|
|
|
Controller, |
4
|
|
|
Get, |
5
|
|
|
Inject, |
6
|
|
|
NotFoundException, |
7
|
|
|
Param, |
8
|
|
|
Post, |
9
|
|
|
Render, |
10
|
|
|
Res, |
11
|
|
|
UseGuards |
12
|
|
|
} from '@nestjs/common'; |
13
|
|
|
import { Response } from 'express'; |
14
|
|
|
import { ICommandBus } from 'src/Application/ICommandBus'; |
15
|
|
|
import { IQueryBus } from 'src/Application/IQueryBus'; |
16
|
|
|
import { UpdateUserCommand } from 'src/Application/HumanResource/User/Command/UpdateUserCommand'; |
17
|
|
|
import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO'; |
18
|
|
|
import { UserAdministrativeDTO } from '../DTO/UserAdministrativeDTO'; |
19
|
|
|
import { IsAuthenticatedGuard } from '../Security/IsAuthenticatedGuard'; |
20
|
|
|
import { WithName } from 'src/Infrastructure/Common/ExtendedRouting/WithName'; |
21
|
|
|
import { GetUserByIdQuery } from 'src/Application/HumanResource/User/Query/GetUserByIdQuery'; |
22
|
|
|
import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver'; |
23
|
|
|
import { UserRole } from 'src/Domain/HumanResource/User/User.entity'; |
24
|
|
|
import { |
25
|
|
|
ContractType, |
26
|
|
|
WorkingTimeType |
27
|
|
|
} from 'src/Domain/HumanResource/User/UserAdministrative.entity'; |
28
|
|
|
import { GetUserAdministrativeByIdQuery } from 'src/Application/HumanResource/User/Query/GetUserAdministrativeByIdQuery'; |
29
|
|
|
|
30
|
|
|
@Controller('app/people/users') |
31
|
|
|
@UseGuards(IsAuthenticatedGuard) |
32
|
|
|
export class EditUserController { |
33
|
|
|
constructor( |
34
|
|
|
@Inject('ICommandBus') |
35
|
|
|
private readonly commandBus: ICommandBus, |
36
|
|
|
@Inject('IQueryBus') |
37
|
|
|
private readonly queryBus: IQueryBus, |
38
|
|
|
private readonly resolver: RouteNameResolver |
39
|
|
|
) {} |
40
|
|
|
|
41
|
|
|
@Get(':id') |
42
|
|
|
@WithName('people_users_edit') |
43
|
|
|
@Render('pages/users/edit.njk') |
44
|
|
|
public async get(@Param() dto: IdDTO) { |
45
|
|
|
const user = await this.queryBus.execute( |
46
|
|
|
new GetUserAdministrativeByIdQuery(dto.id) |
47
|
|
|
); |
48
|
|
|
const roles = Object.values(UserRole); |
49
|
|
|
const contracts = Object.values(ContractType); |
50
|
|
|
const workingTimes = Object.values(WorkingTimeType); |
51
|
|
|
|
52
|
|
|
return { user, roles, contracts, workingTimes }; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
@Post(':id') |
56
|
|
|
public async post( |
57
|
|
|
@Param() dto: IdDTO, |
58
|
|
|
@Body() userAdministrativeDto: UserAdministrativeDTO, |
59
|
|
|
@Res() res: Response |
60
|
|
|
) { |
61
|
|
|
const { |
62
|
|
|
role, |
63
|
|
|
annualEarnings, |
64
|
|
|
contract, |
65
|
|
|
workingTime, |
66
|
|
|
executivePosition, |
67
|
|
|
healthInsurance, |
68
|
|
|
joiningDate, |
69
|
|
|
leavingDate, |
70
|
|
|
transportFee, |
71
|
|
|
sustainableMobilityFee |
72
|
|
|
} = userAdministrativeDto; |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
await this.commandBus.execute( |
76
|
|
|
new UpdateUserCommand( |
77
|
|
|
dto.id, |
78
|
|
|
role, |
79
|
|
|
annualEarnings, |
80
|
|
|
contract, |
81
|
|
|
workingTime, |
82
|
|
|
executivePosition, |
83
|
|
|
healthInsurance, |
84
|
|
|
joiningDate, |
85
|
|
|
leavingDate ? leavingDate : null, |
86
|
|
|
transportFee, |
87
|
|
|
sustainableMobilityFee |
88
|
|
|
) |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
res.redirect(303, this.resolver.resolve('people_users_list')); |
92
|
|
|
} catch (e) { |
93
|
|
|
throw new NotFoundException(e.message); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|