Browse Source

attachment change, UseCors

zinab_elgendy 8 months ago
parent
commit
e536b9a8c8

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

@@ -41,9 +41,9 @@ namespace MTWorkHR.API.Controllers
 
         [HttpPost("forgetPasswordMail")]
         [ProducesResponseType(StatusCodes.Status200OK)]
-        public async Task ForgetPasswordMail(string userId)
+        public async Task ForgetPasswordMail(string email)
         {
-            await _userService.ForgetPasswordMail(userId);
+            await _userService.ForgetPasswordMail(email);
         }
 
         [HttpPost("forgetPassword")]

+ 2 - 0
MTWorkHR.API/Controllers/LookupController.cs

@@ -2,6 +2,7 @@
 
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
+using MTWorkHR.Application.Filters;
 using MTWorkHR.Application.Identity;
 using MTWorkHR.Application.Models;
 using MTWorkHR.Application.Services.Interfaces;
@@ -11,6 +12,7 @@ namespace MTWorkHR.API.Controllers
 {
     [Route("api/[controller]")]
     [ApiController]
+   // [AppAuthorize]
     public class LookupController : ControllerBase
     {
         private readonly ILookupService _LookupService;

+ 6 - 1
MTWorkHR.API/Program.cs

@@ -91,6 +91,7 @@ builder.Services.AddSwaggerGen(swagger =>
                     }
                 });
 });
+
 //--------------------------
 var app = builder.Build();
 
@@ -102,7 +103,11 @@ app.UseSwagger();
 app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MTWorkHR.API v1"));
 // }
 
-
+app.UseCors(x => x
+    .AllowAnyMethod()
+    .AllowAnyHeader()
+    .SetIsOriginAllowed(origin => true) // allow any origin
+    .AllowCredentials()); // allow credentials
 app.UseHttpsRedirection();
 app.UseAuthentication();
 app.UseAuthorization();

+ 3 - 3
MTWorkHR.Application/Dtos/Identity/AttachmentDto.cs

@@ -7,15 +7,15 @@ namespace MTWorkHR.Application.Models
 {
     public class AttachmentDto : EntityDto
     {
-        public long AttachmentTypeId { get; set; }
+        public long? AttachmentTypeId { get; set; }
         public long TaskId { get; set; }
 
         public string? AttachmentTypeName { get; set; }
 
         public IFormFile? FileData { get; set; }
-        public string FileName { get; set; }
+        public string? FileName { get; set; }
 
-        public string OriginalName { get; set; }
+        public string? OriginalName { get; set; }
 
     }
 }

+ 7 - 1
MTWorkHR.Application/Dtos/Identity/UserDto.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;
@@ -36,6 +37,11 @@ namespace MTWorkHR.Application.Models
 
         public decimal TaxNumber { get; set; }
         public decimal IncomeTaxValue { get; set; }
+        public IFormFile? CVAttach { get; set; }
+        public IFormFile? PassportAttach { get; set; }
+        public IFormFile? EduCertificateAttach { get; set; }
+        public IFormFile? ExperienceCertificateAttach { get; set; }
+        public IFormFile? ProfCertificateAttach { get; set; }
 
         public IList<UserRoleDto>? UserRoles { get; set; }
         public IList<AttachmentDto>? UserAttachments{ get; set; }

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

@@ -81,7 +81,7 @@ namespace MTWorkHR.Identity.Services
             var roleClaims = roles.Select(r => new Claim(ClaimTypes.Role, r)).ToList();
             var claims = new[]
             {
-                new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
+                new Claim(JwtRegisteredClaimNames.Name, user.UserName),
                 new Claim(JwtRegisteredClaimNames.Email, user.Email),
                 new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                 new Claim("uid", user.Id)

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

@@ -18,6 +18,7 @@ using System.Web;
 using System.Data;
 using MTWorkHR.Core.IDto;
 using System.Linq.Dynamic.Core;
+using MTWorkHR.Core.Entities.Base;
 
 namespace MTWorkHR.Application.Services
 {
@@ -164,7 +165,28 @@ namespace MTWorkHR.Application.Services
                 throw new AppException(ExceptionEnum.RecordAlreadyExist);
             //loop for given list of attachment, and move each file from Temp path to Actual path
             //  _fileService.UploadFiles(files);
-
+            if (input.UserAttachments == null )
+                input.UserAttachments = new List<AttachmentDto>();
+            if(input.CVAttach != null)
+            {
+                input.UserAttachments.Add(new AttachmentDto { FileData = input.CVAttach, OriginalName = input.CVAttach?.Name,FileName = input.CVAttach?.FileName, AttachmentTypeId = 1 });
+            }
+            if (input.PassportAttach != null)
+            {
+                input.UserAttachments.Add(new AttachmentDto { FileData = input.PassportAttach, OriginalName = input.PassportAttach?.Name, FileName = input.PassportAttach?.FileName, AttachmentTypeId = 2 });
+            }
+            if (input.EduCertificateAttach != null)
+            {
+                input.UserAttachments.Add(new AttachmentDto { FileData = input.EduCertificateAttach, OriginalName = input.EduCertificateAttach?.Name, FileName = input.EduCertificateAttach?.FileName, AttachmentTypeId = 3 });
+            }
+            if (input.ExperienceCertificateAttach != null)
+            {
+                input.UserAttachments.Add(new AttachmentDto { FileData = input.ExperienceCertificateAttach, OriginalName = input.ExperienceCertificateAttach?.Name, FileName = input.ExperienceCertificateAttach?.FileName, AttachmentTypeId = 4 });
+            }
+            if (input.ProfCertificateAttach != null)
+            {
+                input.UserAttachments.Add(new AttachmentDto { FileData = input.ProfCertificateAttach, OriginalName = input.ProfCertificateAttach?.Name, FileName = input.ProfCertificateAttach?.FileName, AttachmentTypeId = 5 });
+            }
             if (!await _fileService.CopyFileToActualFolder(input.UserAttachments.ToList()))
                 throw new AppException(ExceptionEnum.CouldNotMoveFiles);
             var user = MapperObject.Mapper.Map<ApplicationUser>(input);
@@ -360,19 +382,27 @@ namespace MTWorkHR.Application.Services
 
             return true;
         }
-        public async Task ForgetPasswordMail(string userId)
+        public async Task ForgetPasswordMail(string email)
         {
-            var resultPassReset = await GetResetPasswordURL(userId);
 
-            await _emailSender.SendEmail(new EmailMessage
+            var foundUser = await _userManager.FindByEmailAsync(email);
+            if (foundUser != null)
             {
-                Subject = "Register Confirmation",
-                To = resultPassReset.Item2,
-                Body = "Forget Your Password, link will expired after 24 hours",
-                url = resultPassReset.Item1,
-                userId = userId
-            });
+                var resultPassReset = await GetResetPasswordURL(foundUser.Id);
 
+                await _emailSender.SendEmail(new EmailMessage
+                {
+                    Subject = "Register Confirmation",
+                    To = resultPassReset.Item2,
+                    Body = "Forget Your Password, link will expired after 24 hours",
+                    url = resultPassReset.Item1,
+                    userId = foundUser.Id
+                });
+            }
+            else
+            {
+                throw new AppException(ExceptionEnum.RecordNotExist);
+            }
         }
 
         public async Task StopUser(string userId)

+ 2 - 2
MTWorkHR.Core/Entities/User/UserTaskAttachment.cs

@@ -11,7 +11,7 @@ namespace MTWorkHR.Core.Entities
 
         [ForeignKey("TaskId ")]
         public UserTask UserTask { get; set; }
-        public long AttachmentTypeId { get; set; }
+        public long? AttachmentTypeId { get; set; }
 
         [ForeignKey("AttachmentTypeId")]
         public AttachmentType AttachmentType { get; set; }
@@ -20,6 +20,6 @@ namespace MTWorkHR.Core.Entities
         public string? FileName { get; set; }
 
         [MaxLength(250)]
-        public string OriginalName { get; set; }
+        public string? OriginalName { get; set; }
     }
 }

+ 22 - 1
MTWorkHR.Infrastructure/Configurations/AttachmentTypeConfiguration.cs

@@ -26,10 +26,31 @@ namespace MTWorkHR.Infrastructure.Configurations
                 },
                 new AttachmentType
                 {
-                Id = 2,
+                    Id = 2,
                     NameEn = "Identification",
                     NameAr = "الهوية"
                 }
+                ,
+                new AttachmentType
+                {
+                    Id = 3,
+                    NameEn = "Education Certification",
+                    NameAr = "شهادة التعليم"
+                }
+                ,
+                new AttachmentType
+                {
+                    Id = 4,
+                    NameEn = "Experience Certification",
+                    NameAr = "شهادة الخبرة"
+                }
+                ,
+                new AttachmentType
+                {
+                    Id = 5,
+                    NameEn = "Professional Certification",
+                    NameAr = "شهادة الاحتراف"
+                }
                ) ;
         }
     }

File diff suppressed because it is too large
+ 2886 - 0
MTWorkHR.Infrastructure/Migrations/20240516101716_altrAttach.Designer.cs


+ 108 - 0
MTWorkHR.Infrastructure/Migrations/20240516101716_altrAttach.cs

@@ -0,0 +1,108 @@
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
+
+namespace MTWorkHR.Infrastructure.Migrations
+{
+    /// <inheritdoc />
+    public partial class altrAttach : Migration
+    {
+        /// <inheritdoc />
+        protected override void Up(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.DropForeignKey(
+                name: "FK_UserTaskAttachments_AttachmentTypes_AttachmentTypeId",
+                table: "UserTaskAttachments");
+
+            migrationBuilder.AlterColumn<string>(
+                name: "OriginalName",
+                table: "UserTaskAttachments",
+                type: "nvarchar(250)",
+                maxLength: 250,
+                nullable: true,
+                oldClrType: typeof(string),
+                oldType: "nvarchar(250)",
+                oldMaxLength: 250);
+
+            migrationBuilder.AlterColumn<long>(
+                name: "AttachmentTypeId",
+                table: "UserTaskAttachments",
+                type: "bigint",
+                nullable: true,
+                oldClrType: typeof(long),
+                oldType: "bigint");
+
+            migrationBuilder.InsertData(
+                table: "AttachmentTypes",
+                columns: new[] { "Id", "IsRequired", "NameAr", "NameEn" },
+                values: new object[,]
+                {
+                    { 3L, false, "شهادة التعليم", "Education Certification" },
+                    { 4L, false, "شهادة الخبرة", "Experience Certification" },
+                    { 5L, false, "شهادة الاحتراف", "Professional Certification" }
+                });
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_UserTaskAttachments_AttachmentTypes_AttachmentTypeId",
+                table: "UserTaskAttachments",
+                column: "AttachmentTypeId",
+                principalTable: "AttachmentTypes",
+                principalColumn: "Id");
+        }
+
+        /// <inheritdoc />
+        protected override void Down(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.DropForeignKey(
+                name: "FK_UserTaskAttachments_AttachmentTypes_AttachmentTypeId",
+                table: "UserTaskAttachments");
+
+            migrationBuilder.DeleteData(
+                table: "AttachmentTypes",
+                keyColumn: "Id",
+                keyValue: 3L);
+
+            migrationBuilder.DeleteData(
+                table: "AttachmentTypes",
+                keyColumn: "Id",
+                keyValue: 4L);
+
+            migrationBuilder.DeleteData(
+                table: "AttachmentTypes",
+                keyColumn: "Id",
+                keyValue: 5L);
+
+            migrationBuilder.AlterColumn<string>(
+                name: "OriginalName",
+                table: "UserTaskAttachments",
+                type: "nvarchar(250)",
+                maxLength: 250,
+                nullable: false,
+                defaultValue: "",
+                oldClrType: typeof(string),
+                oldType: "nvarchar(250)",
+                oldMaxLength: 250,
+                oldNullable: true);
+
+            migrationBuilder.AlterColumn<long>(
+                name: "AttachmentTypeId",
+                table: "UserTaskAttachments",
+                type: "bigint",
+                nullable: false,
+                defaultValue: 0L,
+                oldClrType: typeof(long),
+                oldType: "bigint",
+                oldNullable: true);
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_UserTaskAttachments_AttachmentTypes_AttachmentTypeId",
+                table: "UserTaskAttachments",
+                column: "AttachmentTypeId",
+                principalTable: "AttachmentTypes",
+                principalColumn: "Id",
+                onDelete: ReferentialAction.Cascade);
+        }
+    }
+}

+ 23 - 5
MTWorkHR.Infrastructure/Migrations/HRDataContextModelSnapshot.cs

@@ -257,6 +257,27 @@ namespace MTWorkHR.Infrastructure.Migrations
                             IsRequired = false,
                             NameAr = "الهوية",
                             NameEn = "Identification"
+                        },
+                        new
+                        {
+                            Id = 3L,
+                            IsRequired = false,
+                            NameAr = "شهادة التعليم",
+                            NameEn = "Education Certification"
+                        },
+                        new
+                        {
+                            Id = 4L,
+                            IsRequired = false,
+                            NameAr = "شهادة الخبرة",
+                            NameEn = "Experience Certification"
+                        },
+                        new
+                        {
+                            Id = 5L,
+                            IsRequired = false,
+                            NameAr = "شهادة الاحتراف",
+                            NameEn = "Professional Certification"
                         });
                 });
 
@@ -1845,7 +1866,7 @@ namespace MTWorkHR.Infrastructure.Migrations
 
                     SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<long>("Id"));
 
-                    b.Property<long>("AttachmentTypeId")
+                    b.Property<long?>("AttachmentTypeId")
                         .HasColumnType("bigint");
 
                     b.Property<DateTime>("CreateDate")
@@ -1862,7 +1883,6 @@ namespace MTWorkHR.Infrastructure.Migrations
                         .HasColumnType("nvarchar(250)");
 
                     b.Property<string>("OriginalName")
-                        .IsRequired()
                         .HasMaxLength(250)
                         .HasColumnType("nvarchar(250)");
 
@@ -2661,9 +2681,7 @@ namespace MTWorkHR.Infrastructure.Migrations
                 {
                     b.HasOne("MTWorkHR.Core.Entities.Base.AttachmentType", "AttachmentType")
                         .WithMany()
-                        .HasForeignKey("AttachmentTypeId")
-                        .OnDelete(DeleteBehavior.Cascade)
-                        .IsRequired();
+                        .HasForeignKey("AttachmentTypeId");
 
                     b.HasOne("MTWorkHR.Core.Entities.UserTask", "UserTask")
                         .WithMany("TaskAttachments")