272 lines
9.3 KiB
C#
272 lines
9.3 KiB
C#
using _0_Framework.Application;
|
||
using AccountManagement.Application.Contracts.Role;
|
||
using AccountManagement.Domain.RoleAgg;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading.Tasks;
|
||
using AccountManagement.Application.Contracts.ProgramManager;
|
||
using AccountManagement.Application.Contracts.ProgramManagerApiResult;
|
||
using AccountManagement.Domain.InternalApiCaller;
|
||
using Company.Domain._common;
|
||
using AccountManagement.Application.Contracts.Ticket;
|
||
using AccountManagement.Domain.PmDomains.PmPermissionAgg;
|
||
using AccountManagement.Domain.PmDomains.PmRoleAgg;
|
||
using AccountManagement.Domain.PmDomains.PmUserAgg;
|
||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||
using Role = AccountManagement.Domain.RoleAgg.Role;
|
||
|
||
namespace AccountManagement.Application;
|
||
|
||
public class RoleApplication : IRoleApplication
|
||
{
|
||
private readonly IRoleRepository _roleRepository;
|
||
private readonly IPmRoleRepository _pmRoleRepository;
|
||
private readonly IPmUserRepository _pmUserRepository;
|
||
private readonly IUnitOfWork _unitOfWork;
|
||
|
||
public RoleApplication(IRoleRepository roleRepository, IUnitOfWork unitOfWork, IPmRoleRepository pmRoleRepository, IPmUserRepository pmUserRepository)
|
||
{
|
||
_roleRepository = roleRepository;
|
||
_unitOfWork = unitOfWork;
|
||
_pmRoleRepository = pmRoleRepository;
|
||
_pmUserRepository = pmUserRepository;
|
||
}
|
||
|
||
public async Task<OperationResult> Create(CreateRole command)
|
||
{
|
||
var operation = new OperationResult();
|
||
if (_roleRepository.Exists(x => x.Name == command.Name))
|
||
return operation.Failed(ApplicationMessages.DuplicatedRecord);
|
||
var permissions = command.Permissions.Where(x => x > 0).Select(x => new Permission(x)).ToList();
|
||
var role = new Role(command.Name, permissions);
|
||
_unitOfWork.BeginAccountContext();
|
||
|
||
_roleRepository.Create(role);
|
||
_roleRepository.SaveChanges();
|
||
|
||
var pmPermissions = command.PmPermissions.Where(x => x > 0).ToList();
|
||
if (pmPermissions.Any())
|
||
{
|
||
try
|
||
{
|
||
var pmPermissionsData = pmPermissions.Where(x => x > 0).Select(x => new PmPermission(x)).ToList();
|
||
var pmRole = new PmRole(command.Name, role.id, pmPermissionsData);
|
||
await _pmRoleRepository.CreateAsync(pmRole);
|
||
await _pmRoleRepository.SaveChangesAsync();
|
||
|
||
}
|
||
catch (System.Exception)
|
||
{
|
||
_unitOfWork.RollbackAccountContext();
|
||
return operation.Failed("خطا در ویرایش دسترسی ها در پروگرام منیجر");
|
||
}
|
||
|
||
//var parameters = new CreateProgramManagerRole
|
||
//{
|
||
// RoleName = command.Name,
|
||
// Permissions = pmPermissions,
|
||
// GozareshgirRoleId = role.id
|
||
|
||
//};
|
||
|
||
//var url = "api/role";
|
||
//var key = SecretKeys.ProgramManagerInternalApi;
|
||
|
||
//var response = InternalApiCaller.PostAsync<CreateProgramManagerRole, ApiResponse>(
|
||
// url,
|
||
// key,
|
||
// parameters
|
||
//);
|
||
|
||
|
||
//if (!response.Success)
|
||
//{
|
||
// _unitOfWork.RollbackAccountContext();
|
||
// return operation.Failed("ارتباط با اپلیکیش پروگرام منیجر برقرار نشد");
|
||
//}
|
||
|
||
//if (!response.Result.isSuccess)
|
||
//{
|
||
// _unitOfWork.RollbackAccountContext();
|
||
// return operation.Failed(response.Result.errorMessage);
|
||
//}
|
||
}
|
||
|
||
|
||
|
||
//command.Permissions.ForEach(code => permissions.Add(new Permission(code)));
|
||
_unitOfWork.CommitAccountContext();
|
||
return operation.Succcedded();
|
||
}
|
||
|
||
public async Task<OperationResult> Edit(EditRole command)
|
||
{
|
||
var operation = new OperationResult();
|
||
var role = _roleRepository.Get(command.Id);
|
||
if (role == null)
|
||
return operation.Failed(ApplicationMessages.RecordNotFound);
|
||
|
||
if (_roleRepository.Exists(x => x.Name == command.Name && x.id != command.Id))
|
||
return operation.Failed(ApplicationMessages.DuplicatedRecord);
|
||
|
||
//var permissions = new List<Permission>();
|
||
//command.Permissions.ForEach(code => permissions.Add(new Permission(code)));
|
||
|
||
var permissions = command.Permissions.Where(x => x > 0).Select(x => new Permission(x)).ToList();
|
||
|
||
|
||
|
||
_unitOfWork.BeginAccountContext();
|
||
role.Edit(command.Name, permissions);
|
||
_roleRepository.SaveChanges();
|
||
var key = SecretKeys.ProgramManagerInternalApi;
|
||
var pmPermissions = command.PmPermissions.Where(x => x > 0).ToList();
|
||
|
||
|
||
//یافتن نقش در پروگرام منیجر
|
||
//var apiResult = InternalApiCaller.GetAsync<RoleResponse>(
|
||
// "api/role",
|
||
// key,
|
||
// new Dictionary<string, object>
|
||
// {
|
||
// { "RoleName", "" },
|
||
|
||
// { "GozareshgirRoleId", command.Id}
|
||
// }
|
||
//);
|
||
|
||
|
||
var pmRoleResult = await _pmRoleRepository.GetPmRoleToEdit(command.Id);
|
||
|
||
|
||
//اگر این نقش در پروگرام منیجر وجود داشت ویرایش کن
|
||
if (pmRoleResult != null)
|
||
{
|
||
|
||
try
|
||
{
|
||
var pmpermissionsData = pmPermissions.Where(x => x > 0).Select(x => new PmPermission(x)).ToList();
|
||
pmRoleResult.Edit(command.Name, pmpermissionsData);
|
||
await _pmRoleRepository.SaveChangesAsync();
|
||
}
|
||
catch (System.Exception)
|
||
{
|
||
_unitOfWork.RollbackAccountContext();
|
||
return operation.Failed("خطا در ویرایش دسترسی ها در پروگرام منیجر");
|
||
}
|
||
|
||
|
||
//var parameters = new CreateProgramManagerRole
|
||
//{
|
||
// RoleName = command.Name,
|
||
// Permissions = pmPermissions,
|
||
// GozareshgirRoleId = role.id
|
||
|
||
//};
|
||
//var url = "api/role/edit";
|
||
//var response = InternalApiCaller.PostAsync<CreateProgramManagerRole, ApiResponse>(
|
||
// url,
|
||
// key,
|
||
// parameters
|
||
//);
|
||
|
||
|
||
//if (!response.Success)
|
||
//{
|
||
// _unitOfWork.RollbackAccountContext();
|
||
// return operation.Failed("ارتباط با اپلیکیش پروگرام منیجر برقرار نشد");
|
||
//}
|
||
|
||
//if (!response.Result.isSuccess)
|
||
//{
|
||
// _unitOfWork.RollbackAccountContext();
|
||
// return operation.Failed(response.Result.errorMessage);
|
||
//}
|
||
}
|
||
else //اگر نقش در پروگرام منیجر وجود نداشت
|
||
{
|
||
|
||
//اگر تیک پرمیشن های پروگرام منیجر زده شده
|
||
//این نقش را سمت پروگرام منیجر بساز
|
||
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)
|
||
{
|
||
_unitOfWork.RollbackAccountContext();
|
||
return operation.Failed("خطا در ویرایش دسترسی ها در پروگرام منیجر");
|
||
}
|
||
|
||
//var parameters = new CreateProgramManagerRole
|
||
//{
|
||
// RoleName = command.Name,
|
||
// Permissions = pmPermissions,
|
||
// GozareshgirRoleId = role.id
|
||
|
||
//};
|
||
|
||
//var url = "api/role";
|
||
|
||
|
||
//var response = InternalApiCaller.PostAsync<CreateProgramManagerRole, ApiResponse>(
|
||
// url,
|
||
// key,
|
||
// parameters
|
||
//);
|
||
|
||
|
||
//if (!response.Success)
|
||
//{
|
||
// _unitOfWork.RollbackAccountContext();
|
||
// return operation.Failed("ارتباط با اپلیکیش پروگرام منیجر برقرار نشد");
|
||
//}
|
||
|
||
//if (!response.Result.isSuccess)
|
||
//{
|
||
// _unitOfWork.RollbackAccountContext();
|
||
// return operation.Failed(response.Result.errorMessage);
|
||
//}
|
||
}
|
||
}
|
||
_unitOfWork.CommitAccountContext();
|
||
return operation.Succcedded();
|
||
}
|
||
|
||
public EditRole GetDetails(long id)
|
||
{
|
||
return _roleRepository.GetDetails(id);
|
||
}
|
||
|
||
public List<RoleViewModel> List()
|
||
{
|
||
return _roleRepository.List();
|
||
}
|
||
|
||
|
||
public async Task<SelectList> GetPmRoleList(long? gozareshgirRoleId)
|
||
{
|
||
var rolse = await _pmRoleRepository.GetPmRoleList(gozareshgirRoleId);
|
||
return new SelectList(rolse, "Id", "RoleName");
|
||
|
||
|
||
}
|
||
|
||
public async Task<List<GetPmRolesDto>> GetPmRoleListToEdit(long? gozareshgirRoleId)
|
||
{
|
||
return await _pmRoleRepository.GetPmRoleList(gozareshgirRoleId);
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
} |