1
|
import {Component, OnInit, ViewChild} from '@angular/core';
|
2
|
import {MatDialog} from '@angular/material';
|
3
|
import {AddVacationDialogComponent} from '../../add-vacation-dialog/add-vacation-dialog.component';
|
4
|
import {UsersService} from '../../services/api/users.service';
|
5
|
import {AuthorizationRequest, VacationRequest} from '../../models/requests.model';
|
6
|
import {UserService} from '../../services/api/user.service';
|
7
|
import {UserProfile} from '../../models/user.model';
|
8
|
import {LocalizationService} from '../../localization/localization.service';
|
9
|
import {RequestStatus, RequestTypes, UserType} from '../../enums/common.enum';
|
10
|
import {Calendar} from '../../models/calendar.model';
|
11
|
import {DateToolsService} from '../../services/util/date-tools.service';
|
12
|
|
13
|
@Component({
|
14
|
selector: 'app-employer-dashboard',
|
15
|
templateUrl: './employer-dashboard.component.html',
|
16
|
styleUrls: ['./employer-dashboard.component.sass']
|
17
|
})
|
18
|
export class EmployerDashboardComponent implements OnInit {
|
19
|
@ViewChild('dayPicker') calendar;
|
20
|
|
21
|
private profile: UserProfile;
|
22
|
private authorizationRequests: AuthorizationRequest[];
|
23
|
private vacationRequests: VacationRequest[];
|
24
|
private oncomingVacation: Calendar[];
|
25
|
|
26
|
private selectedMonth: Date;
|
27
|
|
28
|
constructor(
|
29
|
public dialog: MatDialog,
|
30
|
private localizationService: LocalizationService,
|
31
|
private dateToolsService: DateToolsService,
|
32
|
private userService: UserService,
|
33
|
private usersService: UsersService
|
34
|
) { }
|
35
|
|
36
|
ngOnInit() {
|
37
|
this.selectedMonth = this.dateToolsService.toStartOfMonth(new Date());
|
38
|
|
39
|
this.userService.getLoggedUserProfile()
|
40
|
.subscribe((data: UserProfile) => {
|
41
|
this.profile = data;
|
42
|
if (this.isEmployer()) {
|
43
|
this.loadAuthorizationRequests();
|
44
|
this.loadVacationRequests();
|
45
|
}
|
46
|
});
|
47
|
|
48
|
this.loadMonthVacation(this.selectedMonth);
|
49
|
this.loadOncomingVacation();
|
50
|
}
|
51
|
|
52
|
userApproved(requestId: number, approved: boolean) {
|
53
|
this.requestApproved(requestId, RequestTypes.AUTHORIZATION, approved)
|
54
|
.subscribe(() => this.loadAuthorizationRequests());
|
55
|
}
|
56
|
|
57
|
vacationApproved(requestId: number, approved: boolean) {
|
58
|
this.requestApproved(requestId, RequestTypes.VACATION, approved)
|
59
|
.subscribe(() => this.loadVacationRequests());
|
60
|
}
|
61
|
|
62
|
requestApproved(requestId: number, requestType: RequestTypes, approved: boolean) {
|
63
|
const request = {
|
64
|
id: requestId,
|
65
|
status: approved ? RequestStatus.ACCEPTED : RequestStatus.REJECTED
|
66
|
};
|
67
|
|
68
|
return this.userService.putUserRequestWithLanguage(request, requestType, this.localizationService.getCurrentLanguage());
|
69
|
}
|
70
|
|
71
|
removeVacation(vac: Calendar) {
|
72
|
this.userService.deleteCalendar(vac.id, this.localizationService.getCurrentLanguage())
|
73
|
.subscribe(() => {
|
74
|
this.loadOncomingVacation();
|
75
|
this.loadMonthVacation(this.selectedMonth);
|
76
|
this.loadProfile();
|
77
|
});
|
78
|
}
|
79
|
|
80
|
onDateSelect( date: Date ) {
|
81
|
this.dialog
|
82
|
.open(AddVacationDialogComponent, {
|
83
|
data: {
|
84
|
date
|
85
|
}
|
86
|
})
|
87
|
.afterClosed().subscribe(data => {
|
88
|
if (data && data.isConfirmed) {
|
89
|
this.userService.postCalendarWithLanguage(
|
90
|
{
|
91
|
date: data.date,
|
92
|
from: data.fromTime,
|
93
|
to: data.toTime,
|
94
|
type: data.vacationType
|
95
|
},
|
96
|
this.localizationService.getCurrentLanguage()
|
97
|
).subscribe(() => {
|
98
|
this.loadMonthVacation(this.selectedMonth);
|
99
|
this.loadOncomingVacation();
|
100
|
this.loadProfile();
|
101
|
});
|
102
|
}
|
103
|
});
|
104
|
}
|
105
|
|
106
|
onSelectedMonthChange(monthStart: Date) {
|
107
|
this.selectedMonth = monthStart;
|
108
|
this.loadMonthVacation(monthStart);
|
109
|
}
|
110
|
|
111
|
isEmployer(): boolean {
|
112
|
if (this.profile) {
|
113
|
return this.profile.role === UserType.EMPLOYER;
|
114
|
} else {
|
115
|
return false;
|
116
|
}
|
117
|
}
|
118
|
|
119
|
private loadProfile() {
|
120
|
this.userService.getLoggedUserProfile()
|
121
|
.subscribe((data: UserProfile) => this.profile = data);
|
122
|
}
|
123
|
|
124
|
private loadAuthorizationRequests() {
|
125
|
this.usersService.getAuthorizationRequestsWithLanguage(this.localizationService.getCurrentLanguage(), RequestStatus.PENDING)
|
126
|
.subscribe((data: AuthorizationRequest[]) => this.authorizationRequests = data);
|
127
|
}
|
128
|
|
129
|
private loadVacationRequests() {
|
130
|
this.usersService.getVacationRequestsWithLanguage(this.localizationService.getCurrentLanguage(), RequestStatus.PENDING)
|
131
|
.subscribe((data: VacationRequest[]) => this.vacationRequests = data);
|
132
|
}
|
133
|
|
134
|
private loadMonthVacation(month: Date) {
|
135
|
const fromDate = this.dateToolsService.toStartOfMonth(month);
|
136
|
const toDate = this.dateToolsService.toEndOfMonth(fromDate);
|
137
|
|
138
|
this.userService.getLoggedUserCalendarWithOptions(fromDate, toDate, this.localizationService.getCurrentLanguage(), RequestStatus.ACCEPTED)
|
139
|
.subscribe((data: Calendar[]) => {
|
140
|
if (data) {
|
141
|
this.calendar.setVacation(data);
|
142
|
}
|
143
|
});
|
144
|
}
|
145
|
|
146
|
private loadOncomingVacation() {
|
147
|
const fromDate = new Date();
|
148
|
|
149
|
this.userService.getLoggedUserCalendarWithOptions(fromDate, null, this.localizationService.getCurrentLanguage(), null)
|
150
|
.subscribe((data: Calendar[]) => this.oncomingVacation = data);
|
151
|
}
|
152
|
}
|