Conditions | 3 |
Total Lines | 71 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import { Inject } from '@nestjs/common'; |
||
32 | |||
33 | public async execute(command: CreateLeaveRequestCommand): Promise<string> { |
||
34 | const { |
||
35 | user, |
||
36 | endDate, |
||
37 | endsAllDay, |
||
38 | type, |
||
39 | startDate, |
||
40 | startsAllDay, |
||
41 | comment |
||
42 | } = command; |
||
43 | |||
44 | if ( |
||
45 | true === |
||
46 | (await this.doesLeaveRequestExistForPeriod.isSatisfiedBy( |
||
47 | user, |
||
48 | startDate, |
||
49 | endDate |
||
50 | )) |
||
51 | ) { |
||
52 | throw new LeaveRequestAlreadyExistForThisPeriodException(); |
||
53 | } |
||
54 | |||
55 | if ( |
||
56 | true === |
||
57 | (await this.doesLeaveExistForPeriod.isSatisfiedBy( |
||
58 | user, |
||
59 | startDate, |
||
60 | endDate |
||
61 | )) |
||
62 | ) { |
||
63 | throw new EventsOrLeavesAlreadyExistForThisPeriodException(); |
||
64 | } |
||
65 | |||
66 | const leaveRequest = await this.leaveRequestRepository.save( |
||
67 | new LeaveRequest( |
||
68 | user, |
||
69 | type, |
||
70 | startDate, |
||
71 | startsAllDay, |
||
72 | endDate, |
||
73 | endsAllDay, |
||
74 | comment |
||
75 | ) |
||
76 | ); |
||
77 | |||
78 | this.commandBus.execute( |
||
79 | new CreateNotificationCommand( |
||
80 | NotificationType.POST, |
||
81 | this.translator.translate( |
||
82 | 'leave-requests-create-notification-message', |
||
83 | { |
||
84 | userFirstName: leaveRequest.getUser().getFirstName(), |
||
85 | startDate: leaveRequest.getStartDate(), |
||
86 | endDate: leaveRequest.getEndDate(), |
||
87 | duration: this.dateUtils.getLeaveDuration( |
||
88 | leaveRequest.getStartDate(), |
||
89 | leaveRequest.isStartsAllDay(), |
||
90 | leaveRequest.getEndDate(), |
||
91 | leaveRequest.isEndsAllDay() |
||
92 | ), |
||
93 | link: `${this.configService.get<string>( |
||
94 | 'PERMACOOP_BASE_URL' |
||
95 | )}/app/people/leaves/${leaveRequest.getId()}` |
||
96 | } |
||
97 | ), |
||
98 | leaveRequest |
||
99 | ) |
||
100 | ); |
||
101 | |||
102 | return leaveRequest.getId(); |
||
103 | } |
||
105 |