Add ProgramManager Context

This commit is contained in:
SamSys
2025-12-07 15:21:53 +03:30
parent fb97d3453d
commit acd96bcdc7
22 changed files with 781 additions and 406 deletions

View File

@@ -1,36 +1,40 @@
using System;
using System.Collections;
using _0_Framework.Application;
using _0_Framework.Application;
using _0_Framework.Application.Sms;
using _0_Framework.Exceptions;
using AccountManagement.Application.Contracts.Account;
using AccountManagement.Application.Contracts.ProgramManagerApiResult;
using AccountManagement.Domain.AccountAgg;
using AccountManagement.Domain.AccountLeftWorkAgg;
using AccountManagement.Domain.CameraAccountAgg;
using AccountManagement.Domain.InternalApiCaller;
using AccountManagement.Domain.PmDomains.PmRoleUserAgg;
using AccountManagement.Domain.PmDomains.PmUserAgg;
using AccountManagement.Domain.PositionAgg;
using AccountManagement.Domain.RoleAgg;
using AccountManagement.Domain.SubAccountAgg;
using AccountManagement.Domain.SubAccountPermissionSubtitle1Agg;
using AccountManagement.Domain.SubAccountRoleAgg;
using Company.Domain._common;
using Company.Domain.WorkshopAgg;
using Company.Domain.WorkshopSubAccountAgg;
using CompanyManagment.App.Contracts.Workshop;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.JsonPatch.Operations;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using _0_Framework.Application.Sms;
using AccountManagement.Domain.AccountLeftWorkAgg;
using AccountManagement.Domain.CameraAccountAgg;
using AccountManagement.Domain.RoleAgg;
using CompanyManagment.App.Contracts.Workshop;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Rendering;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using Company.Domain.WorkshopAgg;
using System.Security.Claims;
using System.Text;
using _0_Framework.Exceptions;
using AccountManagement.Domain.PositionAgg;
using AccountManagement.Domain.SubAccountAgg;
using AccountManagement.Domain.SubAccountPermissionSubtitle1Agg;
using AccountManagement.Domain.SubAccountRoleAgg;
using Company.Domain.WorkshopSubAccountAgg;
using Newtonsoft.Json;
using Microsoft.EntityFrameworkCore;
using Company.Domain._common;
using AccountManagement.Domain.InternalApiCaller;
using AccountManagement.Application.Contracts.ProgramManagerApiResult;
using System.Threading;
using System.Threading.Tasks;
using AccountManagement.Application.Contracts.ProgramManager;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
//using AccountManagement.Domain.RoleAgg;
@@ -52,10 +56,11 @@ public class AccountApplication : IAccountApplication
private readonly ISubAccountRoleRepository _subAccountRoleRepository;
private readonly IWorkshopSubAccountRepository _workshopSubAccountRepository;
private readonly ISubAccountPermissionSubtitle1Repository _accountPermissionSubtitle1Repository;
private readonly IPmUserRepository _pmUserRepository;
private readonly IUnitOfWork _unitOfWork;
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)
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)
{
_authHelper = authHelper;
_roleRepository = roleRepository;
@@ -69,6 +74,7 @@ public class AccountApplication : IAccountApplication
_workshopSubAccountRepository = workshopSubAccountRepository;
_accountPermissionSubtitle1Repository = accountPermissionSubtitle1Repository;
_unitOfWork = unitOfWork;
_pmUserRepository = pmUserRepository;
_fileUploader = fileUploader;
_passwordHasher = passwordHasher;
_accountRepository = accountRepository;
@@ -132,7 +138,7 @@ public class AccountApplication : IAccountApplication
};
}
public OperationResult Create(CreateAccount command)
public async Task<OperationResult> Create(CreateAccount command)
{
var operation = new OperationResult();
@@ -159,36 +165,61 @@ public class AccountApplication : IAccountApplication
if (command.IsProgramManagerUser)
{
var parameters = new CreateProgramManagerUser(
command.Fullname,
command.Username,
password,
command.Mobile,
command.Email,
account.id,
command.UserRoles
);
var url = "api/user/create";
var key = SecretKeys.ProgramManagerInternalApi;
var response = InternalApiCaller.PostAsync<CreateProgramManagerUser, ApiResponse>(
url,
key,
parameters
);
if (!response.Success)
try
{
_unitOfWork.RollbackAccountContext();
return operation.Failed(response.Error);
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("خطا در ایجاد کاربر پروگرام منیجر");
}
if (!response.Result.isSuccess)
{
_unitOfWork.RollbackAccountContext();
return operation.Failed(response.Result.errorMessage);
}
//var url = "api/user/create";
//var key = SecretKeys.ProgramManagerInternalApi;
//var response = InternalApiCaller.PostAsync<CreateProgramManagerUser, ApiResponse>(
// url,
// key,
// parameters
//);
//if (!response.Success)
//{
// _unitOfWork.RollbackAccountContext();
// return operation.Failed(response.Error);
//}
//if (!response.Result.isSuccess)
//{
// _unitOfWork.RollbackAccountContext();
// return operation.Failed(response.Result.errorMessage);
//}
}
_unitOfWork.CommitAccountContext();
@@ -230,7 +261,7 @@ public class AccountApplication : IAccountApplication
return opreation.Succcedded(register.id, message: "ثبت نام شما با موفقیت انجام شد");
}
public OperationResult Edit(EditAccount command)
public async Task<OperationResult> Edit(EditAccount command)
{
var operation = new OperationResult();
var account = _accountRepository.Get(command.Id);
@@ -249,47 +280,62 @@ public class AccountApplication : IAccountApplication
_accountRepository.SaveChanges();
var key = SecretKeys.ProgramManagerInternalApi;
var apiResult = InternalApiCaller.GetAsync<SingleUserResponseResult>(
$"api/user/{account.id}",
key
);
//var apiResult = InternalApiCaller.GetAsync<SingleUserResponseResult>(
// $"api/user/{account.id}",
// key
//);
var userResult = _pmUserRepository.GetByPmUsertoEditbyAccountId(account.id).GetAwaiter().GetResult();
//اگر کاربر در پروگرام منیجر قبلا ایجاد شده
if (apiResult.Success && apiResult.Result.Data.accountId == account.id)
if (userResult != null)
{
if (!command.UserRoles.Any())
{
_unitOfWork.RollbackAccountContext();
return operation.Failed("حداقل یک نقش باید انتخاب شود");
}
try
{
var parameters = new EditUserCommand(
command.Fullname,
command.Username,
command.Mobile,
account.id,
command.UserRoles,
command.IsProgramManagerUser
);
var url = "api/user/edit";
var response = InternalApiCaller.PostAsync<EditUserCommand, ApiResponse>(
url,
key,
parameters
);
if (!response.Success)
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)
{
_unitOfWork.RollbackAccountContext();
return operation.Failed(response.Error);
return operation.Failed("خطا در ویرایش کاربر پروگرام منیجر");
}
//var parameters = new EditUserCommand(
// command.Fullname,
// command.Username,
// command.Mobile,
// account.id,
// command.UserRoles,
// command.IsProgramManagerUser
//);
//var url = "api/user/edit";
//var response = InternalApiCaller.PostAsync<EditUserCommand, ApiResponse>(
// url,
// key,
// parameters
//);
if (!response.Result.isSuccess)
{
_unitOfWork.RollbackAccountContext();
return operation.Failed(response.Error);
}
//if (!response.Success)
//{
// _unitOfWork.RollbackAccountContext();
// return operation.Failed(response.Error);
//}
//if (!response.Result.isSuccess)
//{
// _unitOfWork.RollbackAccountContext();
// return operation.Failed(response.Error);
//}
}
else //اگر کاربر قبلا ایجاد نشده
@@ -298,38 +344,77 @@ public class AccountApplication : IAccountApplication
if (command.IsProgramManagerUser)
{
if (!command.UserRoles.Any())
{
_unitOfWork.RollbackAccountContext();
return operation.Failed("حداقل یک نقش باید انتخاب شود");
var parameters = new CreateProgramManagerUser(
command.Fullname,
command.Username,
account.Password,
command.Mobile,
command.Email,
account.id,
command.UserRoles
);
var url = "api/user/Create";
var response = InternalApiCaller.PostAsync<CreateProgramManagerUser, ApiResponse>(
url,
key,
parameters
);
if (!response.Success)
{
_unitOfWork.RollbackAccountContext();
return operation.Failed(response.Error);
}
if (!response.Result.isSuccess)
if (_pmUserRepository.Exists(x => x.FullName == command.Fullname))
{
_unitOfWork.RollbackAccountContext();
return operation.Failed(response.Error);
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)
{
_unitOfWork.RollbackAccountContext();
return operation.Failed("خطا در ویرایش کاربر پروگرام منیجر");
}
//var parameters = new CreateProgramManagerUser(
// command.Fullname,
// command.Username,
// account.Password,
// command.Mobile,
// command.Email,
// account.id,
// command.UserRoles
//);
//var url = "api/user/Create";
//var response = InternalApiCaller.PostAsync<CreateProgramManagerUser, ApiResponse>(
// url,
// key,
// parameters
//);
//if (!response.Success)
//{
// _unitOfWork.RollbackAccountContext();
// return operation.Failed(response.Error);
//}
//if (!response.Result.isSuccess)
//{
// _unitOfWork.RollbackAccountContext();
// return operation.Failed(response.Error);
//}
}
}
@@ -968,4 +1053,9 @@ public class AccountApplication : IAccountApplication
_authHelper.CameraSignIn(authViewModel);
}
public async Task<GetPmUserDto> GetPmUserByAccountId(long accountId)
{
return await _pmUserRepository.GetPmUserByAccountId(accountId);
}
}

View File

@@ -3,25 +3,36 @@ 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)
public RoleApplication(IRoleRepository roleRepository, IUnitOfWork unitOfWork, IPmRoleRepository pmRoleRepository, IPmUserRepository pmUserRepository)
{
_roleRepository = roleRepository;
_unitOfWork = unitOfWork;
_pmRoleRepository = pmRoleRepository;
_pmUserRepository = pmUserRepository;
}
public OperationResult Create(CreateRole command)
public async Task<OperationResult> Create(CreateRole command)
{
var operation = new OperationResult();
if (_roleRepository.Exists(x => x.Name == command.Name))
@@ -36,35 +47,49 @@ public class RoleApplication : IRoleApplication
var pmPermissions = command.PmPermissions.Where(x => x > 0).ToList();
if (pmPermissions.Any())
{
var parameters = new CreateProgramManagerRole
try
{
RoleName = command.Name,
Permissions = pmPermissions,
GozareshgirRoleId = role.id
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();
};
var url = "api/role";
var key = SecretKeys.ProgramManagerInternalApi;
var response = InternalApiCaller.PostAsync<CreateProgramManagerRole, ApiResponse>(
url,
key,
parameters
);
if (!response.Success)
}
catch (System.Exception)
{
_unitOfWork.RollbackAccountContext();
return operation.Failed("ارتباط با اپلیکیش پروگرام منیجر برقرار نشد");
return operation.Failed("خطا در ویرایش دسترسی ها در پروگرام منیجر");
}
if (!response.Result.isSuccess)
{
_unitOfWork.RollbackAccountContext();
return operation.Failed(response.Result.errorMessage);
}
//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);
//}
}
@@ -74,7 +99,7 @@ public class RoleApplication : IRoleApplication
return operation.Succcedded();
}
public OperationResult Edit(EditRole command)
public async Task<OperationResult> Edit(EditRole command)
{
var operation = new OperationResult();
var role = _roleRepository.Get(command.Id);
@@ -89,7 +114,7 @@ public class RoleApplication : IRoleApplication
var permissions = command.Permissions.Where(x => x > 0).Select(x => new Permission(x)).ToList();
_unitOfWork.BeginAccountContext();
role.Edit(command.Name, permissions);
@@ -99,117 +124,120 @@ public class RoleApplication : IRoleApplication
//یافتن نقش در پروگرام منیجر
var apiResult = InternalApiCaller.GetAsync<RoleResponse>(
"api/role",
key,
new Dictionary<string, object>
{
{ "RoleName", "" },
//var apiResult = InternalApiCaller.GetAsync<RoleResponse>(
// "api/role",
// key,
// new Dictionary<string, object>
// {
// { "RoleName", "" },
{ "GozareshgirRoleId", command.Id}
}
);
// { "GozareshgirRoleId", command.Id}
// }
//);
var pmRoleResult = await _pmRoleRepository.GetPmRoleToEdit(command.Id);
if (apiResult.Success)
//اگر این نقش در پروگرام منیجر وجود داشت ویرایش کن
if (pmRoleResult != null)
{
if (apiResult.Result.isSuccess)
try
{
//اگر این نقش در پروگرام منیجر وجود داشت ویرایش کن
if (apiResult.Result.data.role.Any())
{
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())
{
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);
}
}
}
var pmpermissionsData = pmPermissions.Where(x => x > 0).Select(x => new PmPermission(x)).ToList();
pmRoleResult.Edit(command.Name, pmpermissionsData);
await _pmRoleRepository.SaveChangesAsync();
}
else
catch (System.Exception)
{
_unitOfWork.RollbackAccountContext();
return operation.Failed("ارتباط با اپلیکیش پروگرام منیجر برقرار نشد");
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
else //اگر نقش در پروگرام منیجر وجود نداشت
{
_unitOfWork.RollbackAccountContext();
return operation.Failed("ارتباط با اپلیکیش پروگرام منیجر برقرار نشد");
//اگر تیک پرمیشن های پروگرام منیجر زده شده
//این نقش را سمت پروگرام منیجر بساز
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();
}
@@ -222,4 +250,23 @@ public class RoleApplication : IRoleApplication
{
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);
}
}