CompanyService.cs 12 KB

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