OrderRequestService.cs 5.7 KB

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