using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.WebUtilities; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using MTWorkHR.Application.Identity; using MTWorkHR.Application.Mapper; using MTWorkHR.Application.Models; using MTWorkHR.Core.Global; using MTWorkHR.Core.IRepositories; using MTWorkHR.Core.UnitOfWork; using MTWorkHR.Application.Services.Interfaces; using MTWorkHR.Core.Email; using MTWorkHR.Core.Entities; using MTWorkHR.Infrastructure.UnitOfWorks; using MTWorkHR.Infrastructure.Entities; using System.Transactions; using MTWorkHR.Core.Entities.Base; using System.Threading.Tasks; namespace MTWorkHR.Application.Services { public class CompanyService :BaseService, ICompanyService { private readonly IUnitOfWork _unitOfWork; private readonly AppSettingsConfiguration _configuration; private readonly GlobalInfo _globalInfo; private readonly IUserService _userService; private readonly IFileService _fileService; private readonly ApplicationUserManager _userManager; public CompanyService(ApplicationUserManager userManager, IUnitOfWork unitOfWork, GlobalInfo globalInfo, AppSettingsConfiguration configuration, IUserService userService, IFileService fileService) : base(unitOfWork) { _unitOfWork = unitOfWork; _configuration = configuration; _globalInfo = globalInfo; _userService = userService; _fileService = fileService; _userManager = userManager; } public override async Task GetById(long CompanyId) { var entity = await _unitOfWork.Company.GetByIdAsync(CompanyId); var companyResponse = MapperObject.Mapper.Map(entity); var userDto = await _userService.GetById(entity.UserId); companyResponse.CompanyUser = MapperObject.Mapper.Map(userDto); return companyResponse; } public async Task> GetAllCompanies() { var Companys = await _unitOfWork.Company.GetAllAsync(); var response = MapperObject.Mapper.Map>(Companys); return response; } public override async Task Create(CompanyDto input) { input.UserId = await CreateCompanyUser(input); var entity = MapperObject.Mapper.Map(input); if (entity is null) { throw new AppException(ExceptionEnum.MapperIssue); } var task = await _unitOfWork.Company.AddAsync(entity); await _unitOfWork.CompleteAsync(); var response = MapperObject.Mapper.Map(task); return response; } public async Task CreateCompanyUser(CompanyDto input) { var companyUser = MapperObject.Mapper.Map(input.CompanyUser); //UserDto companyUser = new UserDto //{ // UserType = UserTypeEnum.Business, // FirstName = input.AuthorizedName, // IdNumber = input.IdNumber, // Email = input.Email, // PassportNumber = input.PassportNumber, // UserName = input.Email, // PhoneNumber = input.PhoneNumber, // UserAddress = input.Address, //}; var UserAttachments = new List(); if (input.CommercialRegAttach != null) { UserAttachments.Add(new AttachmentDto { FileData = input.CommercialRegAttach, OriginalName = input.CommercialRegAttach?.Name, FileName = input.CommercialRegAttach?.FileName, AttachmentTypeId = 6 }); } if (input.PassportAttach != null) { UserAttachments.Add(new AttachmentDto { FileData = input.PassportAttach, OriginalName = input.PassportAttach?.Name, FileName = input.PassportAttach?.FileName, AttachmentTypeId = 2 }); } if (input.TaxDeclarationAttach != null) { UserAttachments.Add(new AttachmentDto { FileData = input.TaxDeclarationAttach, OriginalName = input.TaxDeclarationAttach?.Name, FileName = input.TaxDeclarationAttach?.FileName, AttachmentTypeId = 7 }); } if (input.ExperienceCertificateAttach != null) { UserAttachments.Add(new AttachmentDto { FileData = input.ExperienceCertificateAttach, OriginalName = input.ExperienceCertificateAttach?.Name, FileName = input.ExperienceCertificateAttach?.FileName, AttachmentTypeId = 4 }); } if (input.IdAttach != null) { UserAttachments.Add(new AttachmentDto { FileData = input.IdAttach, OriginalName = input.IdAttach?.Name, FileName = input.IdAttach?.FileName, AttachmentTypeId = 8 }); } if (!await _fileService.CopyFileToActualFolder(UserAttachments.ToList())) throw new AppException(ExceptionEnum.CouldNotMoveFiles); // var userResp = await _userService.Create(companyUser); var user = MapperObject.Mapper.Map(companyUser); if (user.UserType == 0) { user.UserType = (int)UserTypeEnum.Business; } var result = await _userManager.CreateAsync(user, companyUser.Password); if (!result.Succeeded) { if (result.Errors != null && result.Errors.Count() > 0) { var msg = result.Errors.Select(a => a.Description).Aggregate((a, b) => a + " /r/n " + b); throw new AppException(msg); } throw new AppException(ExceptionEnum.RecordCreationFailed); } return user.Id; } public override async Task Update(CompanyDto input) { var companyUser = MapperObject.Mapper.Map(input.CompanyUser); await _userService.Update(companyUser); var entity = await _unitOfWork.Company.GetByIdAsync(input.Id); if (entity == null) throw new AppException(ExceptionEnum.RecordNotExist); MapperObject.Mapper.Map(input, entity, typeof(CompanyDto), typeof(Company)); await _unitOfWork.CompleteAsync(); return input; } public override async Task Delete(long id) { var entity = await _unitOfWork.Company.GetByIdAsync(id); await _userService.Delete(entity.UserId); // delete user first await _unitOfWork.Company.DeleteAsync(entity); } } }