CompanyService.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.Application.Services.Interfaces;
  12. using MTWorkHR.Core.Email;
  13. using MTWorkHR.Core.Entities;
  14. using MTWorkHR.Infrastructure.UnitOfWorks;
  15. using MTWorkHR.Infrastructure.Entities;
  16. using System.Transactions;
  17. using MTWorkHR.Core.Entities.Base;
  18. using System.Threading.Tasks;
  19. namespace MTWorkHR.Application.Services
  20. {
  21. public class CompanyService :BaseService<Company, CompanyDto, CompanyDto>, ICompanyService
  22. {
  23. private readonly IUnitOfWork _unitOfWork;
  24. private readonly AppSettingsConfiguration _configuration;
  25. private readonly GlobalInfo _globalInfo;
  26. private readonly IUserService _userService;
  27. private readonly IFileService _fileService;
  28. private readonly ApplicationUserManager _userManager;
  29. public CompanyService(ApplicationUserManager userManager, IUnitOfWork unitOfWork, GlobalInfo globalInfo, AppSettingsConfiguration configuration, IUserService userService, IFileService fileService) : base(unitOfWork)
  30. {
  31. _unitOfWork = unitOfWork;
  32. _configuration = configuration;
  33. _globalInfo = globalInfo;
  34. _userService = userService;
  35. _fileService = fileService;
  36. _userManager = userManager;
  37. }
  38. public override async Task<CompanyDto> GetById(long CompanyId)
  39. {
  40. var entity = await _unitOfWork.Company.GetByIdAsync(CompanyId);
  41. var companyResponse = MapperObject.Mapper.Map<CompanyDto>(entity);
  42. var userDto = await _userService.GetById(entity.UserId);
  43. companyResponse.CompanyUser = MapperObject.Mapper.Map<CompanyUserDto>(userDto);
  44. return companyResponse;
  45. }
  46. public async Task<List<CompanyDto>> GetAllCompanies()
  47. {
  48. var Companys = await _unitOfWork.Company.GetAllAsync();
  49. var response = MapperObject.Mapper.Map<List<CompanyDto>>(Companys);
  50. return response;
  51. }
  52. public override async Task<CompanyDto> Create(CompanyDto input)
  53. {
  54. input.UserId = await CreateCompanyUser(input);
  55. var entity = MapperObject.Mapper.Map<Company>(input);
  56. if (entity is null)
  57. {
  58. throw new AppException(ExceptionEnum.MapperIssue);
  59. }
  60. var task = await _unitOfWork.Company.AddAsync(entity);
  61. await _unitOfWork.CompleteAsync();
  62. var response = MapperObject.Mapper.Map<CompanyDto>(task);
  63. return response;
  64. }
  65. public async Task<string> CreateCompanyUser(CompanyDto input)
  66. {
  67. var companyUser = MapperObject.Mapper.Map<UserDto>(input.CompanyUser);
  68. //UserDto companyUser = new UserDto
  69. //{
  70. // UserType = UserTypeEnum.Business,
  71. // FirstName = input.AuthorizedName,
  72. // IdNumber = input.IdNumber,
  73. // Email = input.Email,
  74. // PassportNumber = input.PassportNumber,
  75. // UserName = input.Email,
  76. // PhoneNumber = input.PhoneNumber,
  77. // UserAddress = input.Address,
  78. //};
  79. var UserAttachments = new List<AttachmentDto>();
  80. if (input.CommercialRegAttach != null)
  81. {
  82. UserAttachments.Add(new AttachmentDto { FileData = input.CommercialRegAttach, OriginalName = input.CommercialRegAttach?.Name, FileName = input.CommercialRegAttach?.FileName, AttachmentTypeId = 6 });
  83. }
  84. if (input.PassportAttach != null)
  85. {
  86. UserAttachments.Add(new AttachmentDto { FileData = input.PassportAttach, OriginalName = input.PassportAttach?.Name, FileName = input.PassportAttach?.FileName, AttachmentTypeId = 2 });
  87. }
  88. if (input.TaxDeclarationAttach != null)
  89. {
  90. UserAttachments.Add(new AttachmentDto { FileData = input.TaxDeclarationAttach, OriginalName = input.TaxDeclarationAttach?.Name, FileName = input.TaxDeclarationAttach?.FileName, AttachmentTypeId = 7 });
  91. }
  92. if (input.ExperienceCertificateAttach != null)
  93. {
  94. UserAttachments.Add(new AttachmentDto { FileData = input.ExperienceCertificateAttach, OriginalName = input.ExperienceCertificateAttach?.Name, FileName = input.ExperienceCertificateAttach?.FileName, AttachmentTypeId = 4 });
  95. }
  96. if (input.IdAttach != null)
  97. {
  98. UserAttachments.Add(new AttachmentDto { FileData = input.IdAttach, OriginalName = input.IdAttach?.Name, FileName = input.IdAttach?.FileName, AttachmentTypeId = 8 });
  99. }
  100. if (!await _fileService.CopyFileToActualFolder(UserAttachments.ToList()))
  101. throw new AppException(ExceptionEnum.CouldNotMoveFiles);
  102. // var userResp = await _userService.Create(companyUser);
  103. var user = MapperObject.Mapper.Map<ApplicationUser>(companyUser);
  104. if (user.UserType == 0)
  105. {
  106. user.UserType = (int)UserTypeEnum.Business;
  107. }
  108. var result = await _userManager.CreateAsync(user, companyUser.Password);
  109. if (!result.Succeeded)
  110. {
  111. if (result.Errors != null && result.Errors.Count() > 0)
  112. {
  113. var msg = result.Errors.Select(a => a.Description).Aggregate((a, b) => a + " /r/n " + b);
  114. throw new AppException(msg);
  115. }
  116. throw new AppException(ExceptionEnum.RecordCreationFailed);
  117. }
  118. return user.Id;
  119. }
  120. public override async Task<CompanyDto> Update(CompanyDto input)
  121. {
  122. var companyUser = MapperObject.Mapper.Map<UserDto>(input.CompanyUser);
  123. await _userService.Update(companyUser);
  124. var entity = await _unitOfWork.Company.GetByIdAsync(input.Id);
  125. if (entity == null)
  126. throw new AppException(ExceptionEnum.RecordNotExist);
  127. MapperObject.Mapper.Map(input, entity, typeof(CompanyDto), typeof(Company));
  128. await _unitOfWork.CompleteAsync();
  129. return input;
  130. }
  131. public override async Task Delete(long id)
  132. {
  133. var entity = await _unitOfWork.Company.GetByIdAsync(id);
  134. await _userService.Delete(entity.UserId); // delete user first
  135. await _unitOfWork.Company.DeleteAsync(entity);
  136. }
  137. }
  138. }