Просмотр исходного кода

user_update employee permission

zinab_elgendy дней назад: 4
Родитель
Сommit
3064566d84

+ 1 - 0
MTWorkHR.Application/Dtos/User/EmployeeDto.cs

@@ -11,6 +11,7 @@ namespace MTWorkHR.Application.Models
     public class EmployeeDto 
     {
         public string? Id { get; set; }
+        public string? EmployeeName { get; set; }
         public string? FirstName { get; set; }
 
         public string? LastName { get; set; }

+ 2 - 0
MTWorkHR.Application/Dtos/User/IOrderPagingInputDto.cs

@@ -10,6 +10,8 @@ namespace MTWorkHR.Application.Models
     public class OrderPagingInputDto : PagingInputDto, IOrderPagingInputDto
     {
         public DateTime? SearchDate { get; set; }
+        public string? UserName { get; set; }
+
         public string? AssignedUserId { get; set; }
         public long? OrderTypeId { get; set; }
         public long? LeaveTypeId { get; set; }

+ 2 - 2
MTWorkHR.Application/Services/Contract/ContractService.cs

@@ -199,8 +199,8 @@ namespace MTWorkHR.Application.Services
                 var filter = PagingInputDto.UserName;
 
                 list = list.Where(u =>
-                     (u.EmployeeName != null && u.EmployeeName.Contains(filter))
-                    || (u.EmployeeEmail != null && u.EmployeeEmail.Contains(filter))
+                     (u.EmployeeName != null && u.EmployeeName.ToLower().Contains(filter))
+                    || (u.EmployeeEmail != null && u.EmployeeEmail.ToLower().Contains(filter))
                 ).ToList();
             }
 

+ 2 - 0
MTWorkHR.Application/Services/Interfaces/IUserService.cs

@@ -32,6 +32,8 @@ namespace MTWorkHR.Application.Identity
         Task<List<UserAllDto>> GetAllCompanyEmployees();
         Task<BlobObject> Download(string filePath);
         Task<UserDto> GetUserWithAttachmentById(string id);
+        Task<List<UserDto>> GetUsersWithAttachmentsByIds(List<string> ids);
+        
         Task<string> GetProfileImage(string userId);
         Task<bool> Update(string userId, long companyId);
         Task Suspend(string id);

+ 67 - 33
MTWorkHR.Application/Services/User/OrderRequestService.cs

@@ -17,6 +17,7 @@ using System.Linq.Dynamic.Core;
 using Microsoft.AspNetCore.Http;
 using MTWorkHR.Core.Entities.User;
 using MTWorkHR.Core.Entities.Base;
+using System.Linq;
 
 namespace MTWorkHR.Application.Services
 {
@@ -121,7 +122,8 @@ namespace MTWorkHR.Application.Services
             }
             if (PagingInputDto.SearchDate != null)
             {
-                query = query.Where(u => u.StartDate.Date <= PagingInputDto.SearchDate.Value.Date && u.EndDate!.Value.Date >= PagingInputDto.SearchDate.Value.Date) ;
+               // query = query.Where(u => u.StartDate.Date <= PagingInputDto.SearchDate.Value.Date && u.EndDate!.Value.Date >= PagingInputDto.SearchDate.Value.Date) ;
+                query = query.Where(u => u.CreateDate.Date == PagingInputDto.SearchDate.Value.Date) ;
             }
             if (PagingInputDto.OrderTypeId != null)
             {
@@ -135,42 +137,33 @@ namespace MTWorkHR.Application.Services
             {
                 query = query.Where(u => (long?)u.OrderStatus == PagingInputDto.StatusId);
             }
-            var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
+          
 
-            var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
 
-            var total = await query.CountAsync();
 
-            var list = MapperObject.Mapper
-                .Map<IList<OrderRequestDto>>(await page.ToListAsync());
-            foreach (var item in list)
+            // UserName filter (join with users table)
+            if (!string.IsNullOrEmpty(PagingInputDto.UserName))
             {
-                if (item.RequestingEmployeeId != null)
-                {
-                    var user = await _userService.GetUserWithAttachmentById(item.RequestingEmployeeId);
-                    if (user != null)
-                    {
-                        item.Employee = new EmployeeDto { Id = item.RequestingEmployeeId, FirstName = user.FirstName, LastName = user.LastName, Email = user.Email, Position = !string.IsNullOrEmpty( user.Position )? user.Position  : user.JobTitleName };
-                        var attach = user.UserAttachments?.FirstOrDefault(a => a.AttachmentTypeId == 9);
-                        item.Employee.ProfileImagePath = attach?.FilePath;
-                       // if (attach != null)
-                        //using (var stream = new MemoryStream(attach.Content))
-                        //{
-                        //    var file = new FormFile(stream, 0, stream.Length, Path.GetFileNameWithoutExtension(attach.FileName), attach.FileName)
-                        //    {
-                        //        Headers = new HeaderDictionary(),
-                        //        ContentType = attach.ContentType,
-                        //    };
-
-                        //    System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
-                        //    {
-                        //        FileName = file.FileName
-                        //    };
-                        //    file.ContentDisposition = cd.ToString();
-                        //}
-                    }
-                }
+                var filter = PagingInputDto.UserName.ToLower();
+                query = query.Join(
+                    _userManager.Users,
+                    order => order.RequestingEmployeeId,
+                    user => user.Id,
+                    (order, user) => new { Order = order, User = user }
+                )
+                .Where(joined =>
+                    (joined.User.FirstName + " " + joined.User.LastName).ToLower().Contains(filter)
+                //  ||joined.User.Email.ToLower().Contains(filter)
+                )
+                .Select(joined => joined.Order);
             }
+            var total = await query.CountAsync();
+            var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
+
+            var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
+
+            var list = await MapToOrderRequestDtos(page);
+
             var response = new PagingResultDto<OrderRequestDto>
             {
                 Result = list,
@@ -179,6 +172,46 @@ namespace MTWorkHR.Application.Services
 
             return response;
         }
+
+
+        private async Task<List<OrderRequestDto>> MapToOrderRequestDtos(IQueryable<OrderRequest> page)
+        {
+            var list = MapperObject.Mapper.Map<List<OrderRequestDto>>(await page.ToListAsync());
+
+            // Batch load user data to improve performance
+            var employeeIds = list.Where(x => x.RequestingEmployeeId != null)
+                                .Select(x => x.RequestingEmployeeId)
+                                .Distinct()
+                                .ToList();
+
+            if (employeeIds.Any())
+            {
+                var users = await _userService.GetUsersWithAttachmentsByIds(employeeIds);
+                if (users != null)
+                {
+                    var userLookup = users.ToDictionary(u => u.Id, u => u);
+
+                    foreach (var item in list)
+                    {
+                        if (item.RequestingEmployeeId != null && userLookup.TryGetValue(item.RequestingEmployeeId, out var user))
+                        {
+                            item.Employee = new EmployeeDto
+                            {
+                                Id = user.Id,
+                                FirstName = user.FirstName,
+                                LastName = user.LastName,
+                                Email = user.Email,
+                                Position = user.Position ?? user.JobTitleName,
+                                EmployeeName = $"{user.FirstName} {user.LastName}",
+                                ProfileImagePath = user.UserAttachments?.FirstOrDefault(a => a.AttachmentTypeId == 9)?.FilePath
+                            };
+                        }
+                    }
+                }
+            }
+
+            return list;
+        }
         public override async Task<OrderRequestDto> Create(OrderRequestDto input)
         {
             var period = DateTime.Now.Year;
@@ -215,7 +248,8 @@ namespace MTWorkHR.Application.Services
                     }
                     else
                     {
-                        int daysRequested = (int)(input.EndDate - input.StartDate).TotalDays;
+                        int sub = (int)(input.EndDate.Date - input.StartDate.Date).TotalDays;
+                        int daysRequested = input.TotalDays > 0 ? (int)input.TotalDays : sub;
                         if (daysRequested > allocation.NumberOfDays)
                         {
                             throw new AppException(ExceptionEnum.NoVacationBalance, "You do not have enough days for this request");

+ 23 - 0
MTWorkHR.Application/Services/User/UserService.cs

@@ -25,6 +25,8 @@ using Microsoft.AspNetCore.Http;
 using System.Collections;
 using System.Linq;
 using System.ComponentModel.Design;
+using static iText.StyledXmlParser.Jsoup.Select.Evaluator;
+using static iText.IO.Util.IntHashtable;
 
 namespace MTWorkHR.Application.Services
 {
@@ -129,12 +131,14 @@ namespace MTWorkHR.Application.Services
                                 break;
                             case 9:
                                 response.ProfileImage = file;
+                                response.ProfileImagePath = attach.FilePath;
                                 break;
                         }
                         attach.Content = new byte[0];
                     }
                    
                 }
+            
             var attendance = await _unitOfWork.Attendance.GetAttendanceByUserId(id, DateTime.Now.Date);
             response.IsCheckedIn = attendance != null && attendance.CheckInTime.HasValue;
             response.IsCheckedOut = attendance != null && attendance.CheckOutTime.HasValue;
@@ -163,6 +167,25 @@ namespace MTWorkHR.Application.Services
             return response;
         }
 
+        public async Task<List<UserDto>> GetUsersWithAttachmentsByIds(List<string> ids)
+        {
+            // Validate input
+            if (ids == null || !ids.Any())
+            {
+                return new List<UserDto>();
+            }
+
+            // Query users with related data
+            var users = await _userManager.Users
+                .AsNoTracking() // Optional: Use if read-only data is sufficient
+                .Include(u => u.UserAttachments)
+                .Include(u => u.JobTitle)
+                .Where(u => ids.Contains(u.Id))
+                .ToListAsync();
+            var response = MapperObject.Mapper.Map<List<UserDto>>(users);
+            return response;
+        }
+
         public async Task<string> GetProfileImage(string userId)
         {
             string imagePath = "";

+ 4 - 1
MTWorkHR.Core/IDto/IOrderPagingInputDto.cs

@@ -5,7 +5,10 @@ namespace MTWorkHR.Core.IDto
     public interface IOrderPagingInputDto:IPagingInputDto
     {
         public DateTime? SearchDate { get; set; }
+        public string? UserName { get; set; }
         public string? AssignedUserId { get; set; }
-
+        public long? OrderTypeId { get; set; }
+        public long? LeaveTypeId { get; set; }
+        public long? StatusId { get; set; }
     }
 }

+ 3 - 3
MTWorkHR.Infrastructure/Configurations/RolePermissionConfiguration.cs

@@ -48,7 +48,7 @@ namespace MTWorkHR.Infrastructure.Configurations
                 // User permissions (excluding Delete and Suspend)
                 new RolePermission { Id = 20, RoleId = "EM5B3B92-2311-48F8-9DEC-F9FAEF1F211E", PermissionId = 22, PermissionName = "User" },
                 //new RolePermission { Id = 21, RoleId = "EM5B3B92-2311-48F8-9DEC-F9FAEF1F211E", PermissionId = 23, PermissionName = "User.Create" },
-                //new RolePermission { Id = 22, RoleId = "EM5B3B92-2311-48F8-9DEC-F9FAEF1F211E", PermissionId = 24, PermissionName = "User.Update" },
+                new RolePermission { Id = 22, RoleId = "EM5B3B92-2311-48F8-9DEC-F9FAEF1F211E", PermissionId = 24, PermissionName = "User.Update" },
 
                 // Role permissions
                 new RolePermission { Id = 23, RoleId = "EM5B3B92-2311-48F8-9DEC-F9FAEF1F211E", PermissionId = 27, PermissionName = "Role" },
@@ -119,8 +119,8 @@ namespace MTWorkHR.Infrastructure.Configurations
 
                 // User permissions (excluding Delete and Suspend)
                 new RolePermission { Id = 68, RoleId = "CO5B3B92-2311-48F8-9DEC-F9FAEF1F211R", PermissionId = 22, PermissionName = "User" },
-                //new RolePermission { Id = 69, RoleId = "CO5B3B92-2311-48F8-9DEC-F9FAEF1F211R", PermissionId = 23, PermissionName = "User.Create" },
-                //new RolePermission { Id = 70, RoleId = "CO5B3B92-2311-48F8-9DEC-F9FAEF1F211R", PermissionId = 24, PermissionName = "User.Update" },
+                new RolePermission { Id = 69, RoleId = "CO5B3B92-2311-48F8-9DEC-F9FAEF1F211R", PermissionId = 23, PermissionName = "User.Create" },
+                new RolePermission { Id = 70, RoleId = "CO5B3B92-2311-48F8-9DEC-F9FAEF1F211R", PermissionId = 24, PermissionName = "User.Update" },
 
                 // Role permissions
                 new RolePermission { Id = 71, RoleId = "CO5B3B92-2311-48F8-9DEC-F9FAEF1F211R", PermissionId = 27, PermissionName = "Role" },

Разница между файлами не показана из-за своего большого размера
+ 7284 - 0
MTWorkHR.Infrastructure/Migrations/20250415094408_altrEmployeePermission.Designer.cs


+ 45 - 0
MTWorkHR.Infrastructure/Migrations/20250415094408_altrEmployeePermission.cs

@@ -0,0 +1,45 @@
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
+
+namespace MTWorkHR.Infrastructure.Migrations
+{
+    /// <inheritdoc />
+    public partial class altrEmployeePermission : Migration
+    {
+        /// <inheritdoc />
+        protected override void Up(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.InsertData(
+                table: "RolePermissions",
+                columns: new[] { "Id", "PermissionId", "PermissionName", "RoleId" },
+                values: new object[,]
+                {
+                    { 22L, 24L, "User.Update", "EM5B3B92-2311-48F8-9DEC-F9FAEF1F211E" },
+                    { 69L, 23L, "User.Create", "CO5B3B92-2311-48F8-9DEC-F9FAEF1F211R" },
+                    { 70L, 24L, "User.Update", "CO5B3B92-2311-48F8-9DEC-F9FAEF1F211R" }
+                });
+        }
+
+        /// <inheritdoc />
+        protected override void Down(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.DeleteData(
+                table: "RolePermissions",
+                keyColumn: "Id",
+                keyValue: 22L);
+
+            migrationBuilder.DeleteData(
+                table: "RolePermissions",
+                keyColumn: "Id",
+                keyValue: 69L);
+
+            migrationBuilder.DeleteData(
+                table: "RolePermissions",
+                keyColumn: "Id",
+                keyValue: 70L);
+        }
+    }
+}

+ 21 - 0
MTWorkHR.Infrastructure/Migrations/HRDataContextModelSnapshot.cs

@@ -5743,6 +5743,13 @@ namespace MTWorkHR.Infrastructure.Migrations
                         },
                         new
                         {
+                            Id = 22L,
+                            PermissionId = 24L,
+                            PermissionName = "User.Update",
+                            RoleId = "EM5B3B92-2311-48F8-9DEC-F9FAEF1F211E"
+                        },
+                        new
+                        {
                             Id = 23L,
                             PermissionId = 27L,
                             PermissionName = "Role",
@@ -6023,6 +6030,20 @@ namespace MTWorkHR.Infrastructure.Migrations
                         },
                         new
                         {
+                            Id = 69L,
+                            PermissionId = 23L,
+                            PermissionName = "User.Create",
+                            RoleId = "CO5B3B92-2311-48F8-9DEC-F9FAEF1F211R"
+                        },
+                        new
+                        {
+                            Id = 70L,
+                            PermissionId = 24L,
+                            PermissionName = "User.Update",
+                            RoleId = "CO5B3B92-2311-48F8-9DEC-F9FAEF1F211R"
+                        },
+                        new
+                        {
                             Id = 71L,
                             PermissionId = 27L,
                             PermissionName = "Role",