CompanyService.cs 13 KB

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