OrderRequestService.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 Azure;
  18. using System.Security.Claims;
  19. using MTWorkHR.Core.Entities.Base;
  20. namespace MTWorkHR.Application.Services
  21. {
  22. public class OrderRequestService : BaseService<OrderRequest, OrderRequestDto, OrderRequestDto>, IOrderRequestService
  23. {
  24. private readonly IUnitOfWork _unitOfWork;
  25. private readonly IMailSender _emailSender;
  26. private readonly ApplicationUserManager _userManager;
  27. public OrderRequestService(IUnitOfWork unitOfWork, IMailSender emailSender, ApplicationUserManager userManager) : base(unitOfWork)
  28. {
  29. _unitOfWork = unitOfWork;
  30. _emailSender = emailSender;
  31. _userManager = userManager;
  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<OrderRequestDto> Create(OrderRequestDto input)
  40. {
  41. var period = DateTime.Now.Year;
  42. var allocation = await _unitOfWork.OrderAllocation.GetUserAllocations(input.RequestingEmployeeId, input.OrderTypeId, input.LeaveTypeId, period);
  43. if (allocation is null)
  44. {
  45. throw new AppException(ExceptionEnum.RecordNotExist, "You do not have any allocations for this leave type.");
  46. }
  47. else
  48. {
  49. int daysRequested = (int)(input.EndDate - input.StartDate).TotalDays;
  50. if (daysRequested > allocation.NumberOfDays)
  51. {
  52. throw new AppException(ExceptionEnum.RecordNotExist, "You do not have enough days for this request");
  53. }
  54. }
  55. var orderRequest = MapperObject.Mapper.Map<OrderRequest>(input);
  56. orderRequest = await _unitOfWork.OrderRequest.AddAsync(orderRequest);
  57. await _unitOfWork.CompleteAsync();
  58. try
  59. {
  60. var requestingEmployee = await _userManager.Users.FirstOrDefaultAsync(x => x.Id == input.RequestingEmployeeId);
  61. var sendMailResult = await _emailSender.SendEmail(new EmailMessage
  62. {
  63. To = requestingEmployee.Email,
  64. Body = $"Your leave request for {input.StartDate:D} to {input.EndDate:D} " +
  65. $"has been submitted successfully.",
  66. Subject = "Leave Request Submitted",
  67. userId = input.RequestingEmployeeId
  68. });
  69. if (!sendMailResult)
  70. {
  71. throw new AppException("User created, but could not send the email!");
  72. }
  73. }
  74. catch (Exception ex)
  75. {
  76. //// Log or handle error, but don't throw...
  77. }
  78. var response = MapperObject.Mapper.Map<OrderRequestDto>(orderRequest);
  79. return response;
  80. }
  81. public async Task<OrderRequestDto> ChangeStatus(long id, int statusId )
  82. {
  83. var orderRequest = await _unitOfWork.OrderRequest.GetByIdAsync(id);
  84. orderRequest.OrderStatus = (ApprovalStatusEnum)statusId;
  85. if (orderRequest.OrderStatus == ApprovalStatusEnum.Approved)
  86. {
  87. var allocation = await _unitOfWork.OrderAllocation.GetUserAllocations(orderRequest.RequestingEmployeeId, orderRequest.OrderTypeId, orderRequest.LeaveTypeId, DateTime.Now.Year);
  88. int daysRequested = !orderRequest.EndDate.HasValue ? 1 : (int)(orderRequest.EndDate.Value - orderRequest.StartDate).TotalDays;
  89. allocation.NumberOfDays -= daysRequested;
  90. }
  91. await _unitOfWork.CompleteAsync();
  92. var response = MapperObject.Mapper.Map<OrderRequestDto>(orderRequest);
  93. return response;
  94. }
  95. }
  96. }