CompanyService.cs 15 KB

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