CompanyService.cs 15 KB

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