zinab_elgendy 8 місяців тому
батько
коміт
921f32f4ff

+ 2 - 1
MTWorkHR.API/Controllers/CompanyController.cs

@@ -31,7 +31,8 @@ namespace MTWorkHR.API.Controllers
 
         [HttpPost("Create")]
         [ProducesResponseType(StatusCodes.Status200OK)]
-        public async Task<ActionResult<CompanyDto>> Create([FromBody] CompanyDto input)
+        [Consumes("multipart/form-data")]
+        public async Task<ActionResult<CompanyDto>> Create([FromForm] CompanyDto input)
         {
             return await _companyService.Create(input);
         }

+ 3 - 1
MTWorkHR.Application/Dtos/Identity/AuthResponse.cs

@@ -1,4 +1,5 @@
-using System;
+using MTWorkHR.Core.Global;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -11,6 +12,7 @@ namespace MTWorkHR.Application.Models
         public string Id { get; set; }
         public string UserName { get; set; }
         public string Email { get; set; }
+        public UserTypeEnum UserTypeId { get; set; }
         public string Token{ get; set; }
         public DateTime Expiration { get; set; }
     }

+ 36 - 0
MTWorkHR.Application/Dtos/Identity/CompanyUserDto.cs

@@ -0,0 +1,36 @@
+using Microsoft.AspNetCore.Http;
+using MTWorkHR.Core.Global;
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MTWorkHR.Application.Models
+{
+    public class CompanyUserDto
+    {
+        public string? Id { get; set; }
+       
+        [Required]
+        public string FirstName { get; set; }
+        public string LastName { get; set; }
+       
+        public string IdNumber { get; set; }
+        [Required]
+        [EmailAddress]
+        public string Email { get; set; }
+
+        public string PassportNumber { get; set; }
+
+        public string PhoneNumber { get; set; }
+        public UserTypeEnum? UserType { get; set; }
+       
+        public string? Password { get; set; }
+      
+        public decimal? TaxNumber { get; set; }
+        public IList<AttachmentDto>? UserAttachments{ get; set; }
+        public UserAddressDto? UserAddress{ get; set; }
+    }
+}

+ 11 - 6
MTWorkHR.Application/Dtos/User/CompanyDto.cs

@@ -1,4 +1,5 @@
-using MTWorkHR.Core.Global;
+using Microsoft.AspNetCore.Http;
+using MTWorkHR.Core.Global;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;
@@ -12,11 +13,15 @@ namespace MTWorkHR.Application.Models
     {
         public string CompanyName { get; set; }
 
-        public string CRNumber { get; set; }
-        public int TaxNumber { get; set; }
-        public string UserId { get; set; }
+        public string CRNumber { get; set; } //Commercial Registration
+        public string? UserId { get; set; }
 
-        public UserDto? CompanyUser { get; set; }
-       
+
+        public CompanyUserDto? CompanyUser { get; set; }
+        public IFormFile? CommercialRegAttach { get; set; }
+        public IFormFile? TaxDeclarationAttach { get; set; }
+        public IFormFile? PassportAttach { get; set; }
+        public IFormFile? ExperienceCertificateAttach { get; set; }
+        public IFormFile? IdAttach { get; set; }
     }
 }

+ 10 - 0
MTWorkHR.Application/Mapper/MappingProfile.cs

@@ -86,6 +86,16 @@ namespace MTWorkHR.Application.Mapper
             CreateMap<University, UniversityDto>().ReverseMap();
             CreateMap<Qualification, QualificationDto>().ReverseMap();
             CreateMap<City, CityDto>().ReverseMap();
+            CreateMap<UserDto, CompanyUserDto>();
+            CreateMap<CompanyUserDto, UserDto>().ForMember(m => m.UserName, o => o.MapFrom(s => s.Email)).ReverseMap();
+
+
+            CreateMap<CompanyUserDto, ApplicationUser>()
+                .ForMember(m => m.UserRoles, op => op.Ignore())
+                .ForMember(m => m.Id, op => op.Ignore());
+
+            CreateMap<CompanyUserDto, ApplicationUser>().ForMember(m => m.UserName, o => o.MapFrom(s => s.Email));
+            CreateMap<ApplicationUser, CompanyUserDto>().ForMember(m => m.Password, op => op.Ignore());
 
         }
     }

+ 2 - 1
MTWorkHR.Application/Services/Auth/AuthService.cs

@@ -2,6 +2,7 @@
 using Microsoft.Extensions.Options;
 using Microsoft.IdentityModel.Tokens;
 using MTWorkHR.Application.Exceptions;
+using MTWorkHR.Application.Filters;
 using MTWorkHR.Application.Identity;
 using MTWorkHR.Application.Models;
 using MTWorkHR.Core.Global;
@@ -28,7 +29,7 @@ namespace MTWorkHR.Identity.Services
             var user = await _userManager.FindByEmailAsync(request.Email);
             if(user == null)
             {
-                throw new NotFoundException($"User with {request.Email} not found", request.Email);
+                throw new AppException(ExceptionEnum.RecordNotExist);
             }
             var result = await _signInManager.CheckPasswordSignInAsync(user, request.Password, false);
             if(!result.Succeeded)

+ 1 - 0
MTWorkHR.Application/Services/Interfaces/IUserTaskService.cs

@@ -8,5 +8,6 @@ namespace MTWorkHR.Application.Services.Interfaces
 {
     public interface IUserTaskService : IService<UserTask, UserTaskDto, UserTaskDto>
     {
+        Task<UserTaskDto> GetByUserId(long userId);
     }
 }

+ 6 - 0
MTWorkHR.Application/Services/Task/UserTaskService.cs

@@ -33,6 +33,12 @@ namespace MTWorkHR.Application.Services
             var response = MapperObject.Mapper.Map<UserTaskDto>(entity);
             return response;
         }
+        public async Task<UserTaskDto> GetByUserId(long userId)
+        {
+            var entity = await _unitOfWork.UserTask.GetByUserIdWithAllChildren(userId);
+            var response = MapperObject.Mapper.Map<UserTaskDto>(entity);
+            return response;
+        }
 
         //public override async Task<List<ProjectDto>> GetAll()
         //{

+ 70 - 6
MTWorkHR.Application/Services/User/CompanyService.cs

@@ -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)

+ 1 - 1
MTWorkHR.Core/IRepositories/Task/IUserTaskRepository.cs

@@ -6,6 +6,6 @@ namespace MTWorkHR.Core.IRepositories
     public interface IUserTaskRepository : IRepository<UserTask>
     {
         Task<UserTask> GetByIdWithAllChildren(long id);
-
+        Task<UserTask> GetByUserIdWithAllChildren(long userId);
     }
 }

+ 21 - 2
MTWorkHR.Infrastructure/Configurations/AttachmentTypeConfiguration.cs

@@ -27,8 +27,8 @@ namespace MTWorkHR.Infrastructure.Configurations
                 new AttachmentType
                 {
                     Id = 2,
-                    NameEn = "Identification",
-                    NameAr = "الهوية"
+                    NameEn = "Passport",
+                    NameAr = "جواز السفر"
                 }
                 ,
                 new AttachmentType
@@ -50,6 +50,25 @@ namespace MTWorkHR.Infrastructure.Configurations
                     Id = 5,
                     NameEn = "Professional Certification",
                     NameAr = "شهادة الاحتراف"
+                },
+                new AttachmentType
+                {
+                    Id = 6,
+                    NameEn = "Commercial Regestration",
+                    NameAr = "شهادة الاحتراف"
+                },
+                new AttachmentType
+                {
+                    Id = 7,
+                    NameEn = "Tax Declaration",
+                    NameAr = "البطاقة الضريبية"
+                }
+                ,
+                new AttachmentType
+                {
+                    Id = 8,
+                    NameEn = "Identification",
+                    NameAr = "الهوية"
                 }
                ) ;
         }

+ 11 - 0
MTWorkHR.Infrastructure/Repositories/Task/UserTaskRepository.cs

@@ -25,5 +25,16 @@ namespace MTWorkHR.Infrastructure.Repositories
                 .Include(x => x.Project)
                 .FirstOrDefaultAsync(x => x.Id == id);
         }
+
+
+        public async Task<UserTask> GetByUserIdWithAllChildren(long userId)
+        {
+            return await dbSet
+                .Include(x => x.TaskAttachments).ThenInclude(a => a.AttachmentType)
+                .Include(x => x.UserTaskHistories)
+                .Include(x => x.TaskStatus)
+                .Include(x => x.Project)
+                .FirstOrDefaultAsync(x => x.AssignedUserId == userId);
+        }
     }
 }