CompanyService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.ProfileImage != null)
  81. {
  82. UserAttachments.Add(new AttachmentDto { FileData = input.ProfileImage, OriginalName = input.ProfileImage?.Name, FileName = input.ProfileImage?.FileName, AttachmentTypeId = 9 });
  83. }
  84. if (input.CommercialRegAttach != null)
  85. {
  86. UserAttachments.Add(new AttachmentDto { FileData = input.CommercialRegAttach, OriginalName = input.CommercialRegAttach?.Name, FileName = input.CommercialRegAttach?.FileName, AttachmentTypeId = 6 });
  87. }
  88. if (input.PassportAttach != null)
  89. {
  90. UserAttachments.Add(new AttachmentDto { FileData = input.PassportAttach, OriginalName = input.PassportAttach?.Name, FileName = input.PassportAttach?.FileName, AttachmentTypeId = 2 });
  91. }
  92. if (input.TaxDeclarationAttach != null)
  93. {
  94. UserAttachments.Add(new AttachmentDto { FileData = input.TaxDeclarationAttach, OriginalName = input.TaxDeclarationAttach?.Name, FileName = input.TaxDeclarationAttach?.FileName, AttachmentTypeId = 7 });
  95. }
  96. if (input.ExperienceCertificateAttach != null)
  97. {
  98. UserAttachments.Add(new AttachmentDto { FileData = input.ExperienceCertificateAttach, OriginalName = input.ExperienceCertificateAttach?.Name, FileName = input.ExperienceCertificateAttach?.FileName, AttachmentTypeId = 4 });
  99. }
  100. if (input.IdAttach != null)
  101. {
  102. UserAttachments.Add(new AttachmentDto { FileData = input.IdAttach, OriginalName = input.IdAttach?.Name, FileName = input.IdAttach?.FileName, AttachmentTypeId = 8 });
  103. }
  104. _fileService.CopyFileToCloud(ref UserAttachments);
  105. companyUser.UserAttachments = UserAttachments;
  106. // var userResp = await _userService.Create(companyUser);
  107. var user = MapperObject.Mapper.Map<ApplicationUser>(companyUser);
  108. if (user.UserType == 0)
  109. {
  110. user.UserType = (int)UserTypeEnum.Business;
  111. }
  112. var result = await _userManager.CreateAsync(user, companyUser.Password);
  113. if (!result.Succeeded)
  114. {
  115. if (result.Errors != null && result.Errors.Count() > 0)
  116. {
  117. var msg = result.Errors.Select(a => a.Description).Aggregate((a, b) => a + " /r/n " + b);
  118. throw new AppException(msg);
  119. }
  120. throw new AppException(ExceptionEnum.RecordCreationFailed);
  121. }
  122. return user.Id;
  123. }
  124. public override async Task<CompanyDto> Update(CompanyDto input)
  125. {
  126. var companyUser = MapperObject.Mapper.Map<UserUpdateDto>(input.CompanyUser);
  127. _unitOfWork.BeginTran();
  128. await UpdateCompanyUser(input);
  129. var entity = await _unitOfWork.Company.GetByIdAsync(input.Id);
  130. if (entity == null)
  131. throw new AppException(ExceptionEnum.RecordNotExist);
  132. MapperObject.Mapper.Map(input, entity, typeof(CompanyDto), typeof(Company));
  133. await _unitOfWork.CompleteAsync();
  134. _unitOfWork.CommitTran();
  135. return input;
  136. }
  137. public async Task<UserUpdateDto> UpdateCompanyUser(CompanyDto input)
  138. {
  139. try
  140. {
  141. var entity = _userManager.Users.Include(x => x.UserAttachments).FirstOrDefault(x => x.Id == input.UserId);
  142. if (entity == null)
  143. throw new AppException(ExceptionEnum.RecordNotExist);
  144. if (input.CompanyUser != null && input.CompanyUser.UserAttachments == null)
  145. input.CompanyUser.UserAttachments = new List<AttachmentDto>();
  146. var oldAttachList = entity.UserAttachments;
  147. if (input.ProfileImage != null)
  148. {
  149. var oldAttach = oldAttachList.Where(x => x.AttachmentTypeId == 9 || x.OriginalName == input.ProfileImage?.Name).FirstOrDefault();
  150. if (oldAttach != null) entity.UserAttachments.Remove(oldAttach);
  151. input.CompanyUser?.UserAttachments.Add(new AttachmentDto { FileData = input.ProfileImage, OriginalName = input.ProfileImage?.Name, FileName = input.ProfileImage?.FileName, AttachmentTypeId = 9 });
  152. }
  153. if (input.CommercialRegAttach != null)
  154. {
  155. var oldAttach = oldAttachList.Where(x => x.AttachmentTypeId == 1 || x.OriginalName == input.CommercialRegAttach?.Name).FirstOrDefault();
  156. if (oldAttach != null) entity.UserAttachments.Remove(oldAttach);
  157. input.CompanyUser?.UserAttachments.Add(new AttachmentDto { FileData = input.CommercialRegAttach, OriginalName = input.CommercialRegAttach?.Name, FileName = input.CommercialRegAttach?.FileName, AttachmentTypeId = 6 });
  158. }
  159. if (input.PassportAttach != null)
  160. {
  161. var oldAttach = oldAttachList.Where(x => x.AttachmentTypeId == 2 || x.OriginalName == input.PassportAttach?.Name).FirstOrDefault();
  162. if (oldAttach != null) entity.UserAttachments.Remove(oldAttach);
  163. input.CompanyUser?.UserAttachments.Add(new AttachmentDto { FileData = input.PassportAttach, OriginalName = input.PassportAttach?.Name, FileName = input.PassportAttach?.FileName, AttachmentTypeId = 2 });
  164. }
  165. if (input.TaxDeclarationAttach != null)
  166. {
  167. var oldAttach = oldAttachList.Where(x => x.AttachmentTypeId == 3 || x.OriginalName == input.TaxDeclarationAttach?.Name).FirstOrDefault();
  168. if (oldAttach != null) entity.UserAttachments.Remove(oldAttach);
  169. input.CompanyUser?.UserAttachments.Add(new AttachmentDto { FileData = input.TaxDeclarationAttach, OriginalName = input.TaxDeclarationAttach?.Name, FileName = input.TaxDeclarationAttach?.FileName, AttachmentTypeId = 7});
  170. }
  171. if (input.ExperienceCertificateAttach != null)
  172. {
  173. var oldAttach = oldAttachList.Where(x => x.AttachmentTypeId == 4 || x.OriginalName == input.ExperienceCertificateAttach?.Name).FirstOrDefault();
  174. if (oldAttach != null) entity.UserAttachments.Remove(oldAttach);
  175. input.CompanyUser?.UserAttachments.Add(new AttachmentDto { FileData = input.ExperienceCertificateAttach, OriginalName = input.ExperienceCertificateAttach?.Name, FileName = input.ExperienceCertificateAttach?.FileName, AttachmentTypeId = 4 });
  176. }
  177. if (input.IdAttach != null)
  178. {
  179. var oldAttach = oldAttachList.Where(x => x.AttachmentTypeId == 5 || x.OriginalName == input.IdAttach?.Name).FirstOrDefault();
  180. if (oldAttach != null) entity.UserAttachments.Remove(oldAttach);
  181. input.CompanyUser?.UserAttachments.Add(new AttachmentDto { FileData = input.IdAttach, OriginalName = input.IdAttach?.Name, FileName = input.IdAttach?.FileName, AttachmentTypeId = 8 });
  182. }
  183. List<AttachmentDto> attachs = input.CompanyUser?.UserAttachments.ToList();
  184. _fileService.CopyFileToCloud(ref attachs);
  185. input.CompanyUser.UserAttachments = attachs;
  186. //if (!await _fileService.CopyFileToActualFolder(input.UserAttachments.ToList()))
  187. // throw new AppException(ExceptionEnum.CouldNotMoveFiles);
  188. MapperObject.Mapper.Map(input.CompanyUser, entity);
  189. //saving user
  190. var result = await _userManager.UpdateAsync(entity);
  191. if (!result.Succeeded)
  192. throw new AppException(ExceptionEnum.RecordUpdateFailed);
  193. await _unitOfWork.CompleteAsync();
  194. }
  195. catch (Exception e)
  196. {
  197. throw e;
  198. }
  199. var userResponse = await GetById(input.Id);
  200. var user = MapperObject.Mapper.Map<UserUpdateDto>(userResponse);
  201. return user;
  202. }
  203. public override async Task Delete(long id)
  204. {
  205. var entity = await _unitOfWork.Company.GetByIdAsync(id);
  206. await _userService.Delete(entity.UserId); // delete user first
  207. await _unitOfWork.Company.DeleteAsync(entity);
  208. }
  209. }
  210. }