OrderRequestService.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Microsoft.AspNetCore.Identity;
  2. using Microsoft.AspNetCore.WebUtilities;
  3. using Microsoft.EntityFrameworkCore;
  4. using Microsoft.Extensions.Configuration;
  5. using MTWorkHR.Application.Identity;
  6. using MTWorkHR.Application.Mapper;
  7. using MTWorkHR.Application.Models;
  8. using MTWorkHR.Core.Global;
  9. using MTWorkHR.Core.IRepositories;
  10. using MTWorkHR.Core.UnitOfWork;
  11. using MTWorkHR.Infrastructure.Entities;
  12. using MTWorkHR.Application.Services.Interfaces;
  13. using MTWorkHR.Core.Email;
  14. using MTWorkHR.Core.Entities;
  15. using MTWorkHR.Infrastructure.UnitOfWorks;
  16. using MTWorkHR.Core.Entities.User;
  17. using System.Linq.Dynamic.Core;
  18. namespace MTWorkHR.Application.Services
  19. {
  20. public class OrderRequestService : BaseService<OrderRequest, OrderRequestDto, OrderRequestDto>, IOrderRequestService
  21. {
  22. private readonly IUnitOfWork _unitOfWork;
  23. private readonly IMailSender _emailSender;
  24. private readonly ApplicationUserManager _userManager;
  25. private readonly GlobalInfo _globalInfo;
  26. private readonly IUserService _userService;
  27. public OrderRequestService(IUnitOfWork unitOfWork, IMailSender emailSender, ApplicationUserManager userManager, GlobalInfo globalInfo, IUserService userService) : base(unitOfWork)
  28. {
  29. _unitOfWork = unitOfWork;
  30. _emailSender = emailSender;
  31. _userManager = userManager;
  32. _globalInfo = globalInfo;
  33. _userService = userService;
  34. }
  35. public override async Task<OrderRequestDto> GetById(long id)
  36. {
  37. var entity = await _unitOfWork.OrderRequest.GetByIdWithAllChildren(id);
  38. var response = MapperObject.Mapper.Map<OrderRequestDto>(entity);
  39. response.Employee = await _userService.GetUserWithAttachmentById(entity.RequestingEmployeeId);
  40. return response;
  41. }
  42. public override async Task<PagingResultDto<OrderRequestDto>> GetAll(PagingInputDto PagingInputDto)
  43. {
  44. var res = await _unitOfWork.OrderRequest.GetAllWithChildrenAsync();
  45. var query = res.Item1;
  46. if (_globalInfo.UserType != UserTypeEnum.Business)
  47. {
  48. query = query.Where(m => m.RequestingEmployeeId != null && m.RequestingEmployeeId == _globalInfo.UserId);
  49. }
  50. var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
  51. var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
  52. var total = await query.CountAsync();
  53. var list = MapperObject.Mapper
  54. .Map<IList<OrderRequestDto>>(await page.ToListAsync());
  55. var response = new PagingResultDto<OrderRequestDto>
  56. {
  57. Result = list,
  58. Total = total
  59. };
  60. return response;
  61. }
  62. public override async Task<OrderRequestDto> Create(OrderRequestDto input)
  63. {
  64. var period = DateTime.Now.Year;
  65. if(string.IsNullOrEmpty( input.RequestingEmployeeId))
  66. {
  67. input.RequestingEmployeeId = _globalInfo.UserId;
  68. }
  69. var allocation = await _unitOfWork.OrderAllocation.GetUserAllocations(input.RequestingEmployeeId, input.OrderTypeId, input.LeaveTypeId, period);
  70. if (allocation is null)
  71. {
  72. throw new AppException(ExceptionEnum.RecordNotExist, "You do not have any allocations for this leave type.");
  73. }
  74. else
  75. {
  76. int daysRequested = (int)(input.EndDate - input.StartDate).TotalDays;
  77. if (daysRequested > allocation.NumberOfDays)
  78. {
  79. throw new AppException(ExceptionEnum.RecordNotExist, "You do not have enough days for this request");
  80. }
  81. }
  82. var orderRequest = MapperObject.Mapper.Map<OrderRequest>(input);
  83. orderRequest = await _unitOfWork.OrderRequest.AddAsync(orderRequest);
  84. await _unitOfWork.CompleteAsync();
  85. try
  86. {
  87. var requestingEmployee = await _userManager.Users.FirstOrDefaultAsync(x => x.Id == input.RequestingEmployeeId);
  88. var sendMailResult = await _emailSender.SendEmail(new EmailMessage
  89. {
  90. To = requestingEmployee.Email,
  91. Body = $"Your leave request for {input.StartDate:D} to {input.EndDate:D} " +
  92. $"has been submitted successfully.",
  93. Subject = "Leave Request Submitted",
  94. userId = input.RequestingEmployeeId
  95. });
  96. if (!sendMailResult)
  97. {
  98. throw new AppException("User created, but could not send the email!");
  99. }
  100. }
  101. catch (Exception ex)
  102. {
  103. //// Log or handle error, but don't throw...
  104. }
  105. var response = MapperObject.Mapper.Map<OrderRequestDto>(orderRequest);
  106. return response;
  107. }
  108. public async Task<OrderRequestDto> ChangeStatus(long id, int statusId )
  109. {
  110. var orderRequest = await _unitOfWork.OrderRequest.GetByIdAsync(id);
  111. orderRequest.OrderStatus = (ApprovalStatusEnum)statusId;
  112. if (orderRequest.OrderStatus == ApprovalStatusEnum.Approved)
  113. {
  114. var allocation = await _unitOfWork.OrderAllocation.GetUserAllocations(orderRequest.RequestingEmployeeId, orderRequest.OrderTypeId, orderRequest.LeaveTypeId, DateTime.Now.Year);
  115. int daysRequested = !orderRequest.EndDate.HasValue ? 1 : (int)(orderRequest.EndDate.Value - orderRequest.StartDate).TotalDays;
  116. allocation.NumberOfDays -= daysRequested;
  117. }
  118. await _unitOfWork.CompleteAsync();
  119. var response = MapperObject.Mapper.Map<OrderRequestDto>(orderRequest);
  120. return response;
  121. }
  122. }
  123. }