CompanyService.cs 14 KB

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