zinab_elgendy hace 10 meses
padre
commit
87e7c154ad

+ 1 - 1
MTWorkHR.API/Controllers/AuthController.cs

@@ -56,7 +56,7 @@ namespace MTWorkHR.API.Controllers
         [HttpGet("confirmEmail")]
         [ProducesResponseType(StatusCodes.Status200OK)]
 
-        public async Task<bool> ConfirmEmail(ForgetPasswordDto model)
+        public async Task<bool> ConfirmEmail([FromQuery] ForgetPasswordDto model)
         {
             var result = await _userService.ConfirmEmail(model);
             return result;

+ 57 - 0
MTWorkHR.API/Controllers/CompanyController.cs

@@ -0,0 +1,57 @@
+using Microsoft.AspNetCore.Authorization;
+
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using MTWorkHR.Application.Identity;
+using MTWorkHR.Application.Models;
+using MTWorkHR.Application.Services.Interfaces;
+
+namespace MTWorkHR.API.Controllers
+{
+    [Route("api/[controller]")]
+    [ApiController]
+    public class CompanyController : ControllerBase
+    {
+        private readonly ICompanyService _companyService;
+        public CompanyController(ICompanyService CompanyService) 
+        {
+            this._companyService = CompanyService;
+        }
+        [HttpGet("GetAll")]
+        public async Task<ActionResult<List<CompanyDto>>> GetAll([FromQuery] PagingInputDto pagingInput)
+        {
+            return Ok( await _companyService.GetAll(pagingInput));
+        }
+        [HttpGet("Get")]
+        public async Task<ActionResult<CompanyDto>> Get(long CompanyId)
+        {
+            return Ok(await _companyService.GetById(CompanyId));
+        }
+
+
+        [HttpPost("Create")]
+        [ProducesResponseType(StatusCodes.Status200OK)]
+        public async Task<ActionResult<CompanyDto>> Create([FromBody] CompanyDto input)
+        {
+            return await _companyService.Create(input);
+        }
+
+        [HttpPost("Update")]
+        [ProducesResponseType(StatusCodes.Status200OK)]
+
+        public async Task Update([FromBody] CompanyDto input)
+        {
+            await _companyService.Update(input);
+        }
+
+        [HttpPost("Delete")]
+        [ProducesResponseType(StatusCodes.Status200OK)]
+
+        public async Task Delete(long id)
+        {
+            await _companyService.Delete(id);
+        }
+
+       
+    }
+}

+ 2 - 1
MTWorkHR.Application/ApplicationServiceRegistration.cs

@@ -32,7 +32,8 @@ namespace MTWorkHR.Application
             services.AddScoped<IOrderAllocationService, OrderAllocationService>();
             services.AddScoped<IOrderRequestService, OrderRequestService>();
             services.AddScoped<ILookupService, LookupService>();
-            
+            services.AddScoped<ICompanyService, CompanyService>();
+
             return services;
         }
     }

+ 1 - 1
MTWorkHR.Application/Dtos/Identity/UserAddressDto.cs

@@ -3,7 +3,7 @@ using System.ComponentModel.DataAnnotations.Schema;
 
 namespace MTWorkHR.Application.Models
 {
-    public class UserAddressDto
+    public class UserAddressDto : EntityDto
     {
         public int CountryId { get; set; }
         public string City { get; set; }

+ 2 - 2
MTWorkHR.Application/Dtos/Identity/UserDto.cs

@@ -18,13 +18,13 @@ namespace MTWorkHR.Application.Models
         public string FirstName { get; set; } 
         public string LastName { get; set; }
         public string PhoneNumber { get; set; }
-        public string LinkedInLink { get; set; }
+        public string? LinkedInLink { get; set; }
         [Required]
         public UserTypeEnum UserType { get; set; }
         [Required]
         [MinLength(6)]
         public string UserName { get; set; }
-        public string Password { get; set; }
+        public string? Password { get; set; }
 
         public string? FavoriteName { get; set; }
         public string PassportNumber { get; set; }

+ 2 - 1
MTWorkHR.Application/Dtos/User/CompanyDto.cs

@@ -14,8 +14,9 @@ namespace MTWorkHR.Application.Models
 
         public string CRNumber { get; set; }
         public int TaxNumber { get; set; }
+        public string UserId { get; set; }
 
-        public UserDto CompanyUser { get; set; }
+        public UserDto? CompanyUser { get; set; }
        
     }
 }

+ 2 - 1
MTWorkHR.Application/Mapper/MappingProfile.cs

@@ -23,7 +23,7 @@ namespace MTWorkHR.Application.Mapper
                 .ForMember(m => m.UserRoles, op => op.Ignore())
                 .ForMember(m => m.Id, op => op.Ignore());
 
-            CreateMap<ApplicationUser, UserDto>();
+            CreateMap<ApplicationUser, UserDto>().ForMember(m => m.Password, op => op.Ignore());
             CreateMap<ApplicationUser, UserAllDto>()
                 .ForMember(s => s.ManagerName, o => o.MapFrom(s => s.Manager.FirstName)
                 
@@ -76,6 +76,7 @@ namespace MTWorkHR.Application.Mapper
             CreateMap<OrderRequest, OrderRequestDto>().ReverseMap().ForMember(d => d.CreateDate, o => o.Ignore());
             CreateMap<OrderType, OrderTypeDto>().ReverseMap();
             CreateMap<LeaveType, LeaveTypeDto>().ReverseMap();
+            CreateMap<Company, CompanyDto>().ReverseMap().ForMember(d => d.CreateDate, o => o.Ignore());
 
 
         }

+ 18 - 0
MTWorkHR.Application/Services/Interfaces/ICompanyService.cs

@@ -0,0 +1,18 @@
+using Microsoft.AspNetCore.Http;
+using MTWorkHR.Application.Models;
+using MTWorkHR.Core.Entities;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MTWorkHR.Application.Services.Interfaces
+{
+    public interface ICompanyService : IService<Company, CompanyDto, CompanyDto>
+    {
+      
+        Task<List<CompanyDto>> GetAllCompanies();
+        
+    }
+}

+ 94 - 0
MTWorkHR.Application/Services/User/CompanyService.cs

@@ -0,0 +1,94 @@
+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;
+
+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;
+
+        public CompanyService(IUnitOfWork unitOfWork, GlobalInfo globalInfo, AppSettingsConfiguration configuration, IUserService userService) : base(unitOfWork)
+        {
+            _unitOfWork = unitOfWork;
+            _configuration = configuration;
+            _globalInfo = globalInfo;
+            _userService = userService;
+        }
+
+     
+        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 = 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)
+        {
+            var userResp = await _userService.Create(input.CompanyUser);
+            input.UserId = userResp?.Id;
+            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 override async Task<CompanyDto> Update(CompanyDto input)
+        {
+            await _userService.Update(input.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);
+
+        }
+    }
+}

+ 26 - 10
MTWorkHR.Application/Services/User/UserService.cs

@@ -13,6 +13,9 @@ using MTWorkHR.Core.Email;
 using MTWorkHR.Core.Entities;
 using MTWorkHR.Infrastructure.UnitOfWorks;
 using MTWorkHR.Infrastructure.Entities;
+using static Org.BouncyCastle.Crypto.Engines.SM2Engine;
+using System.Web;
+using System.Data;
 
 namespace MTWorkHR.Application.Services
 {
@@ -119,15 +122,26 @@ namespace MTWorkHR.Application.Services
             input.Id = user.Id;
 
             //saving userRoles
-            var userRoles = MapperObject.Mapper.Map<List<IdentityUserRole<string>>>(input.UserRoles);
-            foreach (var role in userRoles)
+            if(input.UserRoles == null || input.UserRoles.Count == 0)
             {
-                role.UserId = user.Id;
-                if (await _roleManager.FindByIdAsync(role.RoleId) == null)
-                    throw new AppException(ExceptionEnum.RecordNotExist);
-                var roleOb = input.UserRoles?.FirstOrDefault(r => r.RoleId == role.RoleId);
-                var roleName = roleOb != null ? roleOb.RoleName : "Employee";
-                await _userManager.AddToRoleAsync(user, roleName);
+                var employeeRole = await _roleManager.FindByNameAsync("Employee");
+                if (employeeRole == null)
+                {
+                    await _userManager.AddToRoleAsync(user, "Employee");
+                }
+            }
+            else
+            {
+                var userRoles = MapperObject.Mapper.Map<List<IdentityUserRole<string>>>(input.UserRoles);
+                foreach (var role in userRoles)
+                {
+                    role.UserId = user.Id;
+                    if (await _roleManager.FindByIdAsync(role.RoleId) == null)
+                        throw new AppException(ExceptionEnum.RecordNotExist);
+                    var roleOb = input.UserRoles?.FirstOrDefault(r => r.RoleId == role.RoleId);
+                    var roleName = roleOb != null ? roleOb.RoleName : "Employee";
+                    await _userManager.AddToRoleAsync(user, roleName);
+                }
             }
 
             // await _userRole.AddRangeAsync(userRoles);
@@ -161,7 +175,7 @@ namespace MTWorkHR.Application.Services
         }
         public async Task<bool> ConfirmEmail(ForgetPasswordDto input)
         {
-            var user = await _userManager.FindByEmailAsync(input.Email);
+            var user = await _userManager.FindByIdAsync(input.UserId);
             if (user == null)
                 throw new AppException(ExceptionEnum.RecordNotExist);
             var result = await _userManager.ConfirmEmailAsync(user, input.Token);
@@ -189,11 +203,13 @@ namespace MTWorkHR.Application.Services
                 throw new AppException(ExceptionEnum.RecordNotExist);
 
             string token = await _userManager.GenerateEmailConfirmationTokenAsync(user);
+            string codeHtmlVersion = HttpUtility.UrlEncode(token);
+
             var route = "auth/ConfirmEmail";
             var origin = _configuration.JwtSettings.Audience;
             var endpointUri = new Uri(string.Concat($"{origin}/", route));
             var userURL = QueryHelpers.AddQueryString(endpointUri.ToString(), "userId", user.Id);
-            var confirmEmailUrl = QueryHelpers.AddQueryString(userURL.ToString(), "token", token);
+            var confirmEmailUrl = QueryHelpers.AddQueryString(userURL.ToString(), "token", codeHtmlVersion);
             return new Tuple<string, string>(confirmEmailUrl, user.Email);
         }
 

+ 1 - 1
MTWorkHR.Core/Entities/Auth/Company.cs

@@ -10,7 +10,7 @@ namespace MTWorkHR.Core.Entities
 {
     public class Company : FullAuditEntity
     {
-        public long UserId { get; set; }
+        public string UserId { get; set; }
         [Filter]
         public string CompanyName { get; set; }
         [Filter]

+ 2 - 1
MTWorkHR.Infrastructure/EmailService/MailSender.cs

@@ -57,7 +57,8 @@ namespace MTWorkHR.Infrastructure.EmailService
             emailObj.Subject = email.Subject;
 
             var builder = new BodyBuilder();
-            email.Body = email.Body + "  /n" + email.url;
+            email.Body = email.Body +
+                 ( string.IsNullOrEmpty( email.url ) ?"":  "Please click on <a href =\"" + email.url + "\">this link</a> to confirm your email address is correct. ");
 
             builder.HtmlBody = email.Body;
             emailObj.Body = builder.ToMessageBody();

+ 1 - 0
MTWorkHR.Infrastructure/Entities/ApplicationUser.cs

@@ -37,6 +37,7 @@ namespace MTWorkHR.Infrastructure.Entities
         public bool IsStopped { get; set; }
         public bool IsDeleted { get; set; }
         public string? DeleteUserId { get; set; }
+        public string? LinkedInLink { get; set; }
 
         public ICollection<ApplicationRole> UserRoles { get; set; }
         public ICollection<UserAttachment> UserAttachments { get; set; }

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 2249 - 0
MTWorkHR.Infrastructure/Migrations/20240327101748_altrcompany.Designer.cs


+ 34 - 0
MTWorkHR.Infrastructure/Migrations/20240327101748_altrcompany.cs

@@ -0,0 +1,34 @@
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace MTWorkHR.Infrastructure.Migrations
+{
+    /// <inheritdoc />
+    public partial class altrcompany : Migration
+    {
+        /// <inheritdoc />
+        protected override void Up(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.AlterColumn<string>(
+                name: "UserId",
+                table: "Companies",
+                type: "nvarchar(max)",
+                nullable: false,
+                oldClrType: typeof(long),
+                oldType: "bigint");
+        }
+
+        /// <inheritdoc />
+        protected override void Down(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.AlterColumn<long>(
+                name: "UserId",
+                table: "Companies",
+                type: "bigint",
+                nullable: false,
+                oldClrType: typeof(string),
+                oldType: "nvarchar(max)");
+        }
+    }
+}

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 2252 - 0
MTWorkHR.Infrastructure/Migrations/20240328090119_altrUser.Designer.cs


+ 42 - 0
MTWorkHR.Infrastructure/Migrations/20240328090119_altrUser.cs

@@ -0,0 +1,42 @@
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace MTWorkHR.Infrastructure.Migrations
+{
+    /// <inheritdoc />
+    public partial class altrUser : Migration
+    {
+        /// <inheritdoc />
+        protected override void Up(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.AddColumn<string>(
+                name: "LinkedInLink",
+                table: "AspNetUsers",
+                type: "nvarchar(max)",
+                nullable: true);
+
+            migrationBuilder.UpdateData(
+                table: "AspNetUsers",
+                keyColumn: "Id",
+                keyValue: "ADMB3B92-2311-48F8-9DEC-F9FAEF1F21UA",
+                column: "LinkedInLink",
+                value: null);
+
+            migrationBuilder.UpdateData(
+                table: "AspNetUsers",
+                keyColumn: "Id",
+                keyValue: "AL5B3B92-2311-48F8-9DEC-F9FAEF1F21UB",
+                column: "LinkedInLink",
+                value: null);
+        }
+
+        /// <inheritdoc />
+        protected override void Down(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.DropColumn(
+                name: "LinkedInLink",
+                table: "AspNetUsers");
+        }
+    }
+}

+ 6 - 2
MTWorkHR.Infrastructure/Migrations/HRDataContextModelSnapshot.cs

@@ -307,8 +307,9 @@ namespace MTWorkHR.Infrastructure.Migrations
                         .HasColumnType("nvarchar(450)")
                         .HasColumnOrder(2);
 
-                    b.Property<long>("UserId")
-                        .HasColumnType("bigint");
+                    b.Property<string>("UserId")
+                        .IsRequired()
+                        .HasColumnType("nvarchar(max)");
 
                     b.HasKey("Id");
 
@@ -1599,6 +1600,9 @@ namespace MTWorkHR.Infrastructure.Migrations
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
 
+                    b.Property<string>("LinkedInLink")
+                        .HasColumnType("nvarchar(max)");
+
                     b.Property<bool>("LockoutEnabled")
                         .HasColumnType("bit");