CompanyService.cs 15 KB

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