CompanyService.cs 13 KB

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