123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- 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<Company, CompanyDto, CompanyDto>, 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<CompanyDto> GetById(long CompanyId)
- {
- var entity = await _unitOfWork.Company.GetByIdAsync(CompanyId);
- var companyResponse = MapperObject.Mapper.Map<CompanyDto>(entity);
- var userDto = await _userService.GetById(entity.UserId);
- companyResponse.CompanyUser = MapperObject.Mapper.Map<CompanyUserDto>(userDto);
- return companyResponse;
- }
- public async Task<List<CompanyDto>> GetAllCompanies()
- {
- var Companys = await _unitOfWork.Company.GetAllAsync();
- var response = MapperObject.Mapper.Map<List<CompanyDto>>(Companys);
- return response;
- }
- public override async Task<CompanyDto> Create(CompanyDto input)
- {
- input.UserId = await CreateCompanyUser(input);
- var entity = MapperObject.Mapper.Map<Company>(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<CompanyDto>(task);
- return response;
- }
- public async Task<string> CreateCompanyUser(CompanyDto input)
- {
- var companyUser = MapperObject.Mapper.Map<UserDto>(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<AttachmentDto>();
- 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<ApplicationUser>(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<CompanyDto> Update(CompanyDto input)
- {
- var companyUser = MapperObject.Mapper.Map<UserDto>(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);
- }
- }
- }
|