Update pmUser and create completed

This commit is contained in:
SamSys
2025-12-13 20:04:57 +03:30
parent f75d267ef5
commit 57116b14cc
16 changed files with 144 additions and 103 deletions

View File

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

View File

@@ -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,12 +266,12 @@ 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())
{
@@ -300,18 +279,14 @@ public class AccountApplication : IAccountApplication
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,41 +327,21 @@ 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))
{
_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)
{
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("خطا در ویرایش کاربر پروگرام منیجر");
}
//var parameters = new CreateProgramManagerUser(
// command.Fullname,
// command.Username,

View File

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

View File

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

View File

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

View File

@@ -111,7 +111,7 @@ public record GetSingleUserResponse
/// </summary>
public List<long> Roles { get; set; }
public List<RoleListDto> RoleListDto { get; set; }
public List<RoleListDto>? RoleListDto { get; set; }
};

View File

@@ -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<IPmRoleCommandService, PmRoleCommandService>();
services.AddTransient<IPmUserQueryService, PmUserQueryService>();
services.AddTransient<IPmUserCommandService, PmUserCommandService>();
#endregion

View File

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

View File

@@ -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<GetPmUserDto> 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();
}
}
}

View File

@@ -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<long> Roles);

View File

@@ -0,0 +1,3 @@
namespace Shared.Contracts.PmUser.Commands;
public record EditPmUserDto(string FullName, string UserName, string Mobile, long AccountId, List<long> Roles, bool IsActive);

View File

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

View File

@@ -1,6 +0,0 @@
namespace Shared.Contracts.PmUser;
public interface IPmUserCommandService
{
Task<(bool isSuccess, string pmUserDto)> Create();
}

View File

@@ -1,6 +0,0 @@
namespace Shared.Contracts.PmUser;
public interface IPmUserQueryService
{
Task<long?> GetCurrentPmUserIdFromAccountId(long accountId);
}

View File

@@ -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; }
/// <summary>
/// نام و نام خانوادگی
/// </summary>
@@ -43,7 +42,7 @@ public class GetPmUserDto
public List<long> Roles { get; set; }
public List<RoleListDto> RoleListDto { get; set; }
public List<RoleListDto>? RoleListDto { get; set; }
}

View File

@@ -0,0 +1,8 @@
namespace Shared.Contracts.PmUser.Queries;
public interface IPmUserQueryService
{
Task<long?> GetCurrentPmUserIdFromAccountId(long accountId);
Task<GetPmUserDto> GetPmUserDataByAccountId(long accountId);
}