|
@@ -15,6 +15,7 @@ using MTWorkHR.Infrastructure.UnitOfWorks;
|
|
|
using MTWorkHR.Infrastructure.Entities;
|
|
|
using System.Transactions;
|
|
|
using MTWorkHR.Core.Entities.Base;
|
|
|
+using System.Threading.Tasks;
|
|
|
|
|
|
namespace MTWorkHR.Application.Services
|
|
|
{
|
|
@@ -24,13 +25,18 @@ namespace MTWorkHR.Application.Services
|
|
|
private readonly AppSettingsConfiguration _configuration;
|
|
|
private readonly GlobalInfo _globalInfo;
|
|
|
private readonly IUserService _userService;
|
|
|
+ private readonly IFileService _fileService;
|
|
|
+ private readonly ApplicationUserManager _userManager;
|
|
|
|
|
|
- public CompanyService(IUnitOfWork unitOfWork, GlobalInfo globalInfo, AppSettingsConfiguration configuration, IUserService userService) : base(unitOfWork)
|
|
|
+
|
|
|
+ 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;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -39,7 +45,7 @@ namespace MTWorkHR.Application.Services
|
|
|
var entity = await _unitOfWork.Company.GetByIdAsync(CompanyId);
|
|
|
var companyResponse = MapperObject.Mapper.Map<CompanyDto>(entity);
|
|
|
var userDto = await _userService.GetById(entity.UserId);
|
|
|
- companyResponse.CompanyUser = userDto;
|
|
|
+ companyResponse.CompanyUser = MapperObject.Mapper.Map<CompanyUserDto>(userDto);
|
|
|
return companyResponse;
|
|
|
}
|
|
|
|
|
@@ -52,9 +58,7 @@ namespace MTWorkHR.Application.Services
|
|
|
|
|
|
public override async Task<CompanyDto> Create(CompanyDto input)
|
|
|
{
|
|
|
- input.CompanyUser.UserType = UserTypeEnum.Business;
|
|
|
- var userResp = await _userService.Create(input.CompanyUser);
|
|
|
- input.UserId = userResp?.Id;
|
|
|
+ input.UserId = await CreateCompanyUser(input);
|
|
|
var entity = MapperObject.Mapper.Map<Company>(input);
|
|
|
if (entity is null)
|
|
|
{
|
|
@@ -67,10 +71,70 @@ namespace MTWorkHR.Application.Services
|
|
|
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)
|
|
|
{
|
|
|
- await _userService.Update(input.CompanyUser);
|
|
|
+ var companyUser = MapperObject.Mapper.Map<UserDto>(input.CompanyUser);
|
|
|
+
|
|
|
+ await _userService.Update(companyUser);
|
|
|
var entity = await _unitOfWork.Company.GetByIdAsync(input.Id);
|
|
|
|
|
|
if (entity == null)
|