-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCreateLeaveAllocationCommandHandler.cs
More file actions
84 lines (78 loc) · 3.68 KB
/
Copy pathCreateLeaveAllocationCommandHandler.cs
File metadata and controls
84 lines (78 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using HR.LeaveManagement.Application.DTOs.LeaveAllocation.Validators;
using HR.LeaveManagement.Application.Exceptions;
using HR.LeaveManagement.Application.Features.LeaveAllocations.Requests.Commands;
using HR.LeaveManagement.Application.Features.LeaveTypes.Requests.Commands;
using HR.LeaveManagement.Domain;
using RCommon.Mediator.Subscribers;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using HR.LeaveManagement.Application.Responses;
using System.Linq;
using HR.LeaveManagement.Application.Contracts.Identity;
using RCommon.Persistence;
using HR.LeaveManagement.Domain.Specifications;
using RCommon.Persistence.Crud;
using RCommon.ApplicationServices.Validation;
namespace HR.LeaveManagement.Application.Features.LeaveAllocations.Handlers.Commands
{
public class CreateLeaveAllocationCommandHandler : IAppRequestHandler<CreateLeaveAllocationCommand, BaseCommandResponse>
{
private readonly IGraphRepository<LeaveType> _leaveTypeRepository;
private readonly IGraphRepository<LeaveAllocation> _leaveAllocationRepository;
private readonly IUserService _userService;
private readonly IValidationService _validationService;
public CreateLeaveAllocationCommandHandler(IGraphRepository<LeaveType> leaveTypeRepository,
IGraphRepository<LeaveAllocation> leaveAllocationRepository,
IUserService userService,
IValidationService validationService)
{
this._leaveTypeRepository = leaveTypeRepository;
this._leaveAllocationRepository = leaveAllocationRepository;
this._leaveAllocationRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
this._leaveTypeRepository.DataStoreName = DataStoreNamesConst.LeaveManagement;
this._userService = userService;
_validationService = validationService;
}
public async Task<BaseCommandResponse> HandleAsync(CreateLeaveAllocationCommand request, CancellationToken cancellationToken)
{
var response = new BaseCommandResponse();
var validationResult = await _validationService.ValidateAsync(request.LeaveAllocationDto);
if (validationResult.IsValid == false)
{
response.Success = false;
response.Message = "Allocations Failed";
response.Errors = validationResult.Errors.Select(q => q.ErrorMessage).ToList();
}
else
{
var leaveType = await _leaveTypeRepository.FindAsync(request.LeaveAllocationDto.LeaveTypeId);
var employees = await _userService.GetEmployees();
var period = DateTime.Now.Year;
var allocations = new List<LeaveAllocation>();
foreach (var emp in employees)
{
var allocationCount = await _leaveAllocationRepository.GetCountAsync(new AllocationExistsSpec(emp.Id, leaveType.Id, period));
if (allocationCount > 0)
continue;
allocations.Add(new LeaveAllocation
{
EmployeeId = emp.Id,
LeaveTypeId = leaveType.Id,
NumberOfDays = leaveType.DefaultDays,
Period = period
});
}
foreach (var item in allocations)
{
await _leaveAllocationRepository.AddAsync(item);
}
response.Success = true;
response.Message = "Allocations Successful";
}
return response;
}
}
}