CompanyService.cs 14 KB

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