123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using Microsoft.AspNetCore.Identity;
- using Microsoft.AspNetCore.WebUtilities;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Configuration;
- using MTWorkHR.Application.Identity;
- using MTWorkHR.Application.Mapper;
- using MTWorkHR.Application.Models;
- using MTWorkHR.Core.Global;
- using MTWorkHR.Core.IRepositories;
- using MTWorkHR.Core.UnitOfWork;
- using MTWorkHR.Infrastructure.Entities;
- using MTWorkHR.Application.Services.Interfaces;
- using MTWorkHR.Core.Email;
- using MTWorkHR.Core.Entities;
- using MTWorkHR.Infrastructure.UnitOfWorks;
- using MTWorkHR.Core.Entities.User;
- using Azure;
- using System.Security.Claims;
- using MTWorkHR.Core.Entities.Base;
- namespace MTWorkHR.Application.Services
- {
- public class OrderRequestService : BaseService<OrderRequest, OrderRequestDto, OrderRequestDto>, IOrderRequestService
- {
- private readonly IUnitOfWork _unitOfWork;
- private readonly IMailSender _emailSender;
- private readonly ApplicationUserManager _userManager;
- public OrderRequestService(IUnitOfWork unitOfWork, IMailSender emailSender, ApplicationUserManager userManager) : base(unitOfWork)
- {
- _unitOfWork = unitOfWork;
- _emailSender = emailSender;
- _userManager = userManager;
- }
- public override async Task<OrderRequestDto> GetById(long id)
- {
- var entity = await _unitOfWork.OrderRequest.GetByIdWithAllChildren(id);
- var response = MapperObject.Mapper.Map<OrderRequestDto>(entity);
- return response;
- }
- public override async Task<OrderRequestDto> Create(OrderRequestDto input)
- {
- var period = DateTime.Now.Year;
- var allocation = await _unitOfWork.OrderAllocation.GetUserAllocations(input.RequestingEmployeeId, input.OrderTypeId, input.LeaveTypeId, period);
- if (allocation is null)
- {
- throw new AppException(ExceptionEnum.RecordNotExist, "You do not have any allocations for this leave type.");
- }
- else
- {
- int daysRequested = (int)(input.EndDate - input.StartDate).TotalDays;
- if (daysRequested > allocation.NumberOfDays)
- {
- throw new AppException(ExceptionEnum.RecordNotExist, "You do not have enough days for this request");
- }
- }
- var orderRequest = MapperObject.Mapper.Map<OrderRequest>(input);
- orderRequest = await _unitOfWork.OrderRequest.AddAsync(orderRequest);
- await _unitOfWork.CompleteAsync();
- try
- {
- var requestingEmployee = await _userManager.Users.FirstOrDefaultAsync(x => x.Id == input.RequestingEmployeeId);
- var sendMailResult = await _emailSender.SendEmail(new EmailMessage
- {
- To = requestingEmployee.Email,
- Body = $"Your leave request for {input.StartDate:D} to {input.EndDate:D} " +
- $"has been submitted successfully.",
- Subject = "Leave Request Submitted",
- userId = input.RequestingEmployeeId
- });
- if (!sendMailResult)
- {
- throw new AppException("User created, but could not send the email!");
- }
- }
- catch (Exception ex)
- {
- //// Log or handle error, but don't throw...
- }
- var response = MapperObject.Mapper.Map<OrderRequestDto>(orderRequest);
- return response;
- }
- public async Task<OrderRequestDto> ChangeStatus(long id, int statusId )
- {
- var orderRequest = await _unitOfWork.OrderRequest.GetByIdAsync(id);
- orderRequest.OrderStatus = (ApprovalStatusEnum)statusId;
- if (orderRequest.OrderStatus == ApprovalStatusEnum.Approved)
- {
- var allocation = await _unitOfWork.OrderAllocation.GetUserAllocations(orderRequest.RequestingEmployeeId, orderRequest.OrderTypeId, orderRequest.LeaveTypeId, DateTime.Now.Year);
- int daysRequested = !orderRequest.EndDate.HasValue ? 1 : (int)(orderRequest.EndDate.Value - orderRequest.StartDate).TotalDays;
- allocation.NumberOfDays -= daysRequested;
- }
- await _unitOfWork.CompleteAsync();
- var response = MapperObject.Mapper.Map<OrderRequestDto>(orderRequest);
- return response;
- }
- }
- }
|