From 57116b14cc4c7d103d4dadc6dbd24d8bef9aeff0 Mon Sep 17 00:00:00 2001 From: SamSys Date: Sat, 13 Dec 2025 20:04:57 +0330 Subject: [PATCH] Update pmUser and create completed --- .../Account/IAccountApplication.cs | 1 + .../AccountApplication.cs | 101 +++++------------- .../RoleApplication.cs | 28 +++-- .../PmDomains/PmUserAgg/IPmUserRepository.cs | 1 + .../PmRepositories/PmUserRepository.cs | 1 + .../GetSingleUserQueryHandler.cs | 2 +- .../DependencyInjection.cs | 4 +- .../Services/User/PmUserCommandService.cs | 34 ++++++ .../Services/User/PmUserQueryService.cs | 35 +++++- .../PmUser/Commands/CreatePmUserDto.cs | 3 + .../PmUser/Commands/EditPmUserDto.cs | 3 + .../PmUser/Commands/IPmUserCommandService.cs | 7 ++ .../PmUser/IPmUserCommandService.cs | 6 -- .../PmUser/IPmUserQueryService.cs | 6 -- .../PmUser/Queries}/GetPmUserDto.cs | 7 +- .../PmUser/Queries/IPmUserQueryService.cs | 8 ++ 16 files changed, 144 insertions(+), 103 deletions(-) create mode 100644 ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Services/User/PmUserCommandService.cs create mode 100644 Shared.Contracts/PmUser/Commands/CreatePmUserDto.cs create mode 100644 Shared.Contracts/PmUser/Commands/EditPmUserDto.cs create mode 100644 Shared.Contracts/PmUser/Commands/IPmUserCommandService.cs delete mode 100644 Shared.Contracts/PmUser/IPmUserCommandService.cs delete mode 100644 Shared.Contracts/PmUser/IPmUserQueryService.cs rename {AccountManagement.Application.Contracts/ProgramManager => Shared.Contracts/PmUser/Queries}/GetPmUserDto.cs (86%) create mode 100644 Shared.Contracts/PmUser/Queries/IPmUserQueryService.cs diff --git a/AccountManagement.Application.Contracts/Account/IAccountApplication.cs b/AccountManagement.Application.Contracts/Account/IAccountApplication.cs index badb8c23..10b107ad 100644 --- a/AccountManagement.Application.Contracts/Account/IAccountApplication.cs +++ b/AccountManagement.Application.Contracts/Account/IAccountApplication.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using AccountManagement.Application.Contracts.ProgramManager; +using Shared.Contracts.PmUser.Queries; namespace AccountManagement.Application.Contracts.Account; diff --git a/AccountManagement.Application/AccountApplication.cs b/AccountManagement.Application/AccountApplication.cs index 8676a795..0736293d 100644 --- a/AccountManagement.Application/AccountApplication.cs +++ b/AccountManagement.Application/AccountApplication.cs @@ -34,8 +34,9 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using AccountManagement.Application.Contracts.ProgramManager; -using Shared.Contracts.PmUser; +using Shared.Contracts.PmUser.Commands; using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database; +using Shared.Contracts.PmUser.Queries; //using AccountManagement.Domain.RoleAgg; @@ -60,9 +61,10 @@ public class AccountApplication : IAccountApplication private readonly IPmUserRepository _pmUserRepository; private readonly IUnitOfWork _unitOfWork; private readonly IPmUserQueryService _pmUserQueryService; + private readonly IPmUserCommandService _pmUserCommandService; public AccountApplication(IAccountRepository accountRepository, IPasswordHasher passwordHasher, - IFileUploader fileUploader, IAuthHelper authHelper, IRoleRepository roleRepository, IWorker worker, ISmsService smsService, ICameraAccountRepository cameraAccountRepository, IPositionRepository positionRepository, IAccountLeftworkRepository accountLeftworkRepository, IWorkshopRepository workshopRepository, ISubAccountRepository subAccountRepository, ISubAccountRoleRepository subAccountRoleRepository, IWorkshopSubAccountRepository workshopSubAccountRepository, ISubAccountPermissionSubtitle1Repository accountPermissionSubtitle1Repository, IUnitOfWork unitOfWork, IPmUserRepository pmUserRepository, IPmUserQueryService pmUserQueryService) + IFileUploader fileUploader, IAuthHelper authHelper, IRoleRepository roleRepository, IWorker worker, ISmsService smsService, ICameraAccountRepository cameraAccountRepository, IPositionRepository positionRepository, IAccountLeftworkRepository accountLeftworkRepository, IWorkshopRepository workshopRepository, ISubAccountRepository subAccountRepository, ISubAccountRoleRepository subAccountRoleRepository, IWorkshopSubAccountRepository workshopSubAccountRepository, ISubAccountPermissionSubtitle1Repository accountPermissionSubtitle1Repository, IUnitOfWork unitOfWork, IPmUserRepository pmUserRepository, IPmUserQueryService pmUserQueryService, IPmUserCommandService pmUserCommandService) { _authHelper = authHelper; _roleRepository = roleRepository; @@ -78,6 +80,7 @@ public class AccountApplication : IAccountApplication _unitOfWork = unitOfWork; _pmUserRepository = pmUserRepository; _pmUserQueryService = pmUserQueryService; + _pmUserCommandService = pmUserCommandService; _fileUploader = fileUploader; _passwordHasher = passwordHasher; _accountRepository = accountRepository; @@ -168,41 +171,17 @@ public class AccountApplication : IAccountApplication if (command.IsProgramManagerUser) { - - try + var pmUserRoles = command.UserRoles.Where(x => x > 0).ToList(); + var createPm = await _pmUserCommandService.Create(new CreatePmUserDto(command.Fullname, command.Username, account.Password, command.Mobile, + null, account.id, pmUserRoles)); + if (!createPm.isSuccess) { - if (_pmUserRepository.Exists(x => x.FullName == command.Fullname)) - { - _unitOfWork.RollbackAccountContext(); - return operation.Failed("نام و خانوادگی تکراری است"); - } - - if (_pmUserRepository.Exists(x => x.UserName == command.Username)) - { - _unitOfWork.RollbackAccountContext(); - return operation.Failed("نام کاربری تکراری است"); - } - - if (_pmUserRepository.Exists(x => !string.IsNullOrWhiteSpace(x.Mobile) && x.Mobile == command.Mobile)) - { - _unitOfWork.RollbackAccountContext(); - return operation.Failed("این شماره همراه قبلا به فرد دیگری اختصاص داده شده است"); - } - - - - var userRoles = command.UserRoles.Where(x => x > 0).Select(x => new PmRoleUser(x)).ToList(); - var create = new PmUser(command.Fullname, command.Username, command.Password, command.Mobile, - null, account.id, userRoles); - await _pmUserRepository.CreateAsync(create); - await _pmUserRepository.SaveChangesAsync(); - } - catch (Exception e) - { - _unitOfWork.RollbackAccountContext(); - return operation.Failed("خطا در ایجاد کاربر پروگرام منیجر"); + _unitOfWork.RollbackAccountContext(); + return operation.Failed("خطا در ویرایش کاربر پروگرام منیجر"); } + + //var url = "api/user/create"; //var key = SecretKeys.ProgramManagerInternalApi; @@ -287,31 +266,27 @@ public class AccountApplication : IAccountApplication // $"api/user/{account.id}", // key //); - var userResult = _pmUserRepository.GetByPmUsertoEditbyAccountId(account.id).GetAwaiter().GetResult(); - + var userResult =await _pmUserQueryService.GetPmUserDataByAccountId(account.id); + var pmUserRoles = command.UserRoles.Where(x => x > 0).ToList(); //اگر کاربر در پروگرام منیجر قبلا ایجاد شده - if (userResult != null) + if (userResult.Id >0) { if (!command.UserRoles.Any()) { _unitOfWork.RollbackAccountContext(); return operation.Failed("حداقل یک نقش باید انتخاب شود"); } - - try - { - - var userRoles = command.UserRoles.Where(x => x > 0).Select(x => new PmRoleUser(x)).ToList(); - userResult.Edit(command.Fullname, command.Username, command.Mobile, userRoles, command.IsProgramManagerUser); - await _pmUserRepository.SaveChangesAsync(); - } - catch (Exception) + + var editPm =await _pmUserCommandService.Edit(new EditPmUserDto(command.Fullname, command.Username, command.Mobile, account.id, pmUserRoles, + command.IsProgramManagerUser)); + if (!editPm.isSuccess) { _unitOfWork.RollbackAccountContext(); return operation.Failed("خطا در ویرایش کاربر پروگرام منیجر"); } + //var parameters = new EditUserCommand( // command.Fullname, // command.Username, @@ -352,39 +327,19 @@ public class AccountApplication : IAccountApplication return operation.Failed("حداقل یک نقش باید انتخاب شود"); } - if (_pmUserRepository.Exists(x => x.FullName == command.Fullname)) - { - _unitOfWork.RollbackAccountContext(); - return operation.Failed("نام و خانوادگی تکراری است"); - } + - if (_pmUserRepository.Exists(x => x.UserName == command.Username)) - { - _unitOfWork.RollbackAccountContext(); - return operation.Failed("نام کاربری تکراری است"); - } + - if (_pmUserRepository.Exists(x => !string.IsNullOrWhiteSpace(x.Mobile) && x.Mobile == command.Mobile)) + + var createPm = await _pmUserCommandService.Create(new CreatePmUserDto(command.Fullname, command.Username, account.Password, command.Mobile, + null, account.id, pmUserRoles)); + if (!createPm.isSuccess) { - _unitOfWork.RollbackAccountContext(); - return operation.Failed("این شماره همراه قبلا به فرد دیگری اختصاص داده شده است"); - } - - - try - { - var userRoles = command.UserRoles.Where(x => x > 0).Select(x => new PmRoleUser(x)).ToList(); - var create = new PmUser(command.Fullname, command.Username, account.Password, command.Mobile, - null, account.id, userRoles); - await _pmUserRepository.CreateAsync(create); - await _pmUserRepository.SaveChangesAsync(); - } - catch (Exception) - { - _unitOfWork.RollbackAccountContext(); return operation.Failed("خطا در ویرایش کاربر پروگرام منیجر"); } + //var parameters = new CreateProgramManagerUser( diff --git a/AccountManagement.Application/RoleApplication.cs b/AccountManagement.Application/RoleApplication.cs index 02d027c0..eb25b1dd 100644 --- a/AccountManagement.Application/RoleApplication.cs +++ b/AccountManagement.Application/RoleApplication.cs @@ -188,22 +188,28 @@ public class RoleApplication : IRoleApplication //این نقش را سمت پروگرام منیجر بساز if (pmPermissions.Any()) { - - - try - { - var pmPermissionsData = pmPermissions.Where(x => x > 0).Select(x => new PmPermission(x)).ToList(); - var pmRole = new PmRole(command.Name, command.Id, pmPermissionsData); - await _pmRoleRepository.CreateAsync(pmRole); - await _pmRoleRepository.SaveChangesAsync(); - - } - catch (System.Exception) + var pmRole = new CreatePmRoleDto { RoleName = command.Name, Permissions = pmPermissions, GozareshgirRoleId = role.id }; + var res = await _pmRoleCommandService.Create(pmRole); + if (!res.Item1) { _unitOfWork.RollbackAccountContext(); return operation.Failed("خطا در ویرایش دسترسی ها در پروگرام منیجر"); } + //try + //{ + // var pmPermissionsData = pmPermissions.Where(x => x > 0).Select(x => new PmPermission(x)).ToList(); + // var pmRole = new PmRole(command.Name, command.Id, pmPermissionsData); + // await _pmRoleRepository.CreateAsync(pmRole); + // await _pmRoleRepository.SaveChangesAsync(); + + //} + //catch (System.Exception) + //{ + // _unitOfWork.RollbackAccountContext(); + // return operation.Failed("خطا در ویرایش دسترسی ها در پروگرام منیجر"); + //} + //var parameters = new CreateProgramManagerRole //{ // RoleName = command.Name, diff --git a/AccountManagement.Domain/PmDomains/PmUserAgg/IPmUserRepository.cs b/AccountManagement.Domain/PmDomains/PmUserAgg/IPmUserRepository.cs index c28c3db1..52c28fc1 100644 --- a/AccountManagement.Domain/PmDomains/PmUserAgg/IPmUserRepository.cs +++ b/AccountManagement.Domain/PmDomains/PmUserAgg/IPmUserRepository.cs @@ -1,6 +1,7 @@ using _0_Framework.Domain; using AccountManagement.Application.Contracts.ProgramManager; using System.Threading.Tasks; +using Shared.Contracts.PmUser.Queries; namespace AccountManagement.Domain.PmDomains.PmUserAgg; diff --git a/AccountMangement.Infrastructure.EFCore/Repository/PmRepositories/PmUserRepository.cs b/AccountMangement.Infrastructure.EFCore/Repository/PmRepositories/PmUserRepository.cs index 70a378df..e4915a65 100644 --- a/AccountMangement.Infrastructure.EFCore/Repository/PmRepositories/PmUserRepository.cs +++ b/AccountMangement.Infrastructure.EFCore/Repository/PmRepositories/PmUserRepository.cs @@ -6,6 +6,7 @@ using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Shared.Contracts.PmUser.Queries; namespace AccountMangement.Infrastructure.EFCore.Repository.PmRepositories; diff --git a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Users/Queries/GetSingleUser/GetSingleUserQueryHandler.cs b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Users/Queries/GetSingleUser/GetSingleUserQueryHandler.cs index 39fd66b3..10bc7650 100644 --- a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Users/Queries/GetSingleUser/GetSingleUserQueryHandler.cs +++ b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Users/Queries/GetSingleUser/GetSingleUserQueryHandler.cs @@ -111,7 +111,7 @@ public record GetSingleUserResponse /// public List Roles { get; set; } - public List RoleListDto { get; set; } + public List? RoleListDto { get; set; } }; diff --git a/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/DependencyInjection.cs b/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/DependencyInjection.cs index 8451e848..1543fb97 100644 --- a/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/DependencyInjection.cs +++ b/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/DependencyInjection.cs @@ -27,7 +27,8 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Shared.Contracts.PmRole.Commands; using Shared.Contracts.PmRole.Queries; -using Shared.Contracts.PmUser; +using Shared.Contracts.PmUser.Commands; +using Shared.Contracts.PmUser.Queries; namespace GozareshgirProgramManager.Infrastructure; @@ -96,6 +97,7 @@ public static class DependencyInjection services.AddTransient(); services.AddTransient(); + services.AddTransient(); #endregion diff --git a/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Services/User/PmUserCommandService.cs b/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Services/User/PmUserCommandService.cs new file mode 100644 index 00000000..12155bc0 --- /dev/null +++ b/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Services/User/PmUserCommandService.cs @@ -0,0 +1,34 @@ +using GozareshgirProgramManager.Application.Modules.Users.Commands.CreateUser; +using GozareshgirProgramManager.Application.Modules.Users.Commands.EditUser; +using MediatR; +using Shared.Contracts.PmUser.Commands; + +namespace GozareshgirProgramManager.Infrastructure.Services.User; + +public class PmUserCommandService : IPmUserCommandService +{ + public readonly IMediator _mediator; + + public PmUserCommandService(IMediator mediator) + { + _mediator = mediator; + } + + public async Task<(bool isSuccess, string pmUserDto)> Create(CreatePmUserDto command) + { + var request = new CreateUserCommand(command.FullName, command.UserName, command.Password, command.Mobile, + command.Email, command.AccountId, command.Roles); + + var res = await _mediator.Send(request); + return (res.IsSuccess, res.ErrorMessage); + } + + public async Task<(bool isSuccess, string pmUserDto)> Edit(EditPmUserDto command) + { + var request = new EditUserCommand(command.FullName, command.UserName, command.Mobile, command.AccountId, + command.Roles, command.IsActive); + + var res = await _mediator.Send(request); + return (res.IsSuccess, res.ErrorMessage); + } +} \ No newline at end of file diff --git a/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Services/User/PmUserQueryService.cs b/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Services/User/PmUserQueryService.cs index 1d6f9f04..4c5811bc 100644 --- a/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Services/User/PmUserQueryService.cs +++ b/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Services/User/PmUserQueryService.cs @@ -1,6 +1,8 @@ +using System.Diagnostics; using GozareshgirProgramManager.Application.Modules.Users.Queries.GetSingleUser; using MediatR; -using Shared.Contracts.PmUser; +using Shared.Contracts.PmUser.Queries; +using RoleListDto = Shared.Contracts.PmUser.Queries.RoleListDto; namespace GozareshgirProgramManager.Infrastructure.Services.User; @@ -19,4 +21,35 @@ public class PmUserQueryService : IPmUserQueryService var result = await _mediator.Send(query); return result.Data?.Id ?? null; } + + public async Task GetPmUserDataByAccountId(long accountId) + { + var query = new GetSingleUserQuery(accountId.ToString()); + var result = await _mediator.Send(query); + + if (result.IsSuccess) + { + var RoleDto = result.Data?.RoleListDto.Select(x => new RoleListDto() + { + RoleName = x.RoleName, + RoleId = x.RoleId, + Permissions = x.Permissions + }).ToList(); + var res = new GetPmUserDto() + { + IsActive = result.Data.IsActive, + AccountId = result.Data.AccountId, + Id = result.Data.Id, + Roles = result.Data.Roles, + RoleListDto = RoleDto, + }; + + return res; + } + else + { + return new GetPmUserDto(); + } + + } } \ No newline at end of file diff --git a/Shared.Contracts/PmUser/Commands/CreatePmUserDto.cs b/Shared.Contracts/PmUser/Commands/CreatePmUserDto.cs new file mode 100644 index 00000000..e1ea2d66 --- /dev/null +++ b/Shared.Contracts/PmUser/Commands/CreatePmUserDto.cs @@ -0,0 +1,3 @@ +namespace Shared.Contracts.PmUser.Commands; + +public record CreatePmUserDto(string FullName, string UserName, string Password, string Mobile, string? Email, long? AccountId, List Roles); \ No newline at end of file diff --git a/Shared.Contracts/PmUser/Commands/EditPmUserDto.cs b/Shared.Contracts/PmUser/Commands/EditPmUserDto.cs new file mode 100644 index 00000000..e09febb4 --- /dev/null +++ b/Shared.Contracts/PmUser/Commands/EditPmUserDto.cs @@ -0,0 +1,3 @@ +namespace Shared.Contracts.PmUser.Commands; + +public record EditPmUserDto(string FullName, string UserName, string Mobile, long AccountId, List Roles, bool IsActive); \ No newline at end of file diff --git a/Shared.Contracts/PmUser/Commands/IPmUserCommandService.cs b/Shared.Contracts/PmUser/Commands/IPmUserCommandService.cs new file mode 100644 index 00000000..d7504257 --- /dev/null +++ b/Shared.Contracts/PmUser/Commands/IPmUserCommandService.cs @@ -0,0 +1,7 @@ +namespace Shared.Contracts.PmUser.Commands; + +public interface IPmUserCommandService +{ + Task<(bool isSuccess, string pmUserDto)> Create(CreatePmUserDto command); + Task<(bool isSuccess, string pmUserDto)> Edit(EditPmUserDto command); +} \ No newline at end of file diff --git a/Shared.Contracts/PmUser/IPmUserCommandService.cs b/Shared.Contracts/PmUser/IPmUserCommandService.cs deleted file mode 100644 index 193bcb71..00000000 --- a/Shared.Contracts/PmUser/IPmUserCommandService.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Shared.Contracts.PmUser; - -public interface IPmUserCommandService -{ - Task<(bool isSuccess, string pmUserDto)> Create(); -} \ No newline at end of file diff --git a/Shared.Contracts/PmUser/IPmUserQueryService.cs b/Shared.Contracts/PmUser/IPmUserQueryService.cs deleted file mode 100644 index 479355de..00000000 --- a/Shared.Contracts/PmUser/IPmUserQueryService.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Shared.Contracts.PmUser; - -public interface IPmUserQueryService -{ - Task GetCurrentPmUserIdFromAccountId(long accountId); -} \ No newline at end of file diff --git a/AccountManagement.Application.Contracts/ProgramManager/GetPmUserDto.cs b/Shared.Contracts/PmUser/Queries/GetPmUserDto.cs similarity index 86% rename from AccountManagement.Application.Contracts/ProgramManager/GetPmUserDto.cs rename to Shared.Contracts/PmUser/Queries/GetPmUserDto.cs index d5901f72..af50f51e 100644 --- a/AccountManagement.Application.Contracts/ProgramManager/GetPmUserDto.cs +++ b/Shared.Contracts/PmUser/Queries/GetPmUserDto.cs @@ -1,9 +1,8 @@ -using System.Collections.Generic; - -namespace AccountManagement.Application.Contracts.ProgramManager; +namespace Shared.Contracts.PmUser.Queries; public class GetPmUserDto { + public long Id { get; set; } /// /// نام و نام خانوادگی /// @@ -43,7 +42,7 @@ public class GetPmUserDto public List Roles { get; set; } - public List RoleListDto { get; set; } + public List? RoleListDto { get; set; } } diff --git a/Shared.Contracts/PmUser/Queries/IPmUserQueryService.cs b/Shared.Contracts/PmUser/Queries/IPmUserQueryService.cs new file mode 100644 index 00000000..8a2d7aba --- /dev/null +++ b/Shared.Contracts/PmUser/Queries/IPmUserQueryService.cs @@ -0,0 +1,8 @@ +namespace Shared.Contracts.PmUser.Queries; + +public interface IPmUserQueryService +{ + Task GetCurrentPmUserIdFromAccountId(long accountId); + + Task GetPmUserDataByAccountId(long accountId); +} \ No newline at end of file