2024-07-06 21:20:10 +03:30
|
|
|
|
using Brizco.Domain.Entities.Users;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Brizco.Core.EntityServices;
|
2023-09-20 11:54:12 +03:30
|
|
|
|
|
|
|
|
|
|
public class ComplexService : IComplexService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ISender _sender;
|
|
|
|
|
|
private readonly RoleManager<ApplicationRole> _roleManager;
|
|
|
|
|
|
|
|
|
|
|
|
public ComplexService(ISender sender,RoleManager<ApplicationRole> roleManager)
|
|
|
|
|
|
{
|
|
|
|
|
|
_sender = sender;
|
|
|
|
|
|
_roleManager = roleManager;
|
|
|
|
|
|
}
|
|
|
|
|
|
public async Task<ComplexSDto> CreateComplexAsync(string complexName,
|
|
|
|
|
|
string complexAddress,
|
|
|
|
|
|
string complexSuppPhone,
|
|
|
|
|
|
Guid managerUserId,
|
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
var complex = await _sender.Send(new CreateComplexCommand(complexName,
|
|
|
|
|
|
complexAddress,
|
|
|
|
|
|
complexSuppPhone));
|
|
|
|
|
|
|
|
|
|
|
|
var managerRole = new ApplicationRole
|
|
|
|
|
|
{
|
|
|
|
|
|
ComplexId = complex.Id,
|
2024-04-25 02:33:17 +03:30
|
|
|
|
EnglishName = ApplicationRoles.Manager,
|
2023-09-20 11:54:12 +03:30
|
|
|
|
PersianName = "مدیریت",
|
|
|
|
|
|
Description = "مدیریت مجموعه",
|
2024-04-25 02:33:17 +03:30
|
|
|
|
Name = $"{ApplicationRoles.Manager}_{complex.Id.ToString()}"
|
2023-09-20 11:54:12 +03:30
|
|
|
|
};
|
|
|
|
|
|
var createRoleResult = await _roleManager.CreateAsync(managerRole);
|
|
|
|
|
|
if (!createRoleResult.Succeeded)
|
|
|
|
|
|
throw new AppException(string.Join('|', createRoleResult.Errors));
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var claim in ApplicationClaims.ManagerClaims)
|
|
|
|
|
|
await _roleManager.AddClaimAsync(managerRole, claim);
|
|
|
|
|
|
|
2024-02-21 02:07:41 +03:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var viewOwnerRole = new ApplicationRole
|
|
|
|
|
|
{
|
|
|
|
|
|
ComplexId = complex.Id,
|
2024-04-25 02:33:17 +03:30
|
|
|
|
EnglishName = ApplicationRoles.ViewerOwner,
|
2024-02-21 02:07:41 +03:30
|
|
|
|
PersianName = "ناظر",
|
|
|
|
|
|
Description = "ناظر مجموعه",
|
2024-04-25 02:33:17 +03:30
|
|
|
|
Name = $"{ApplicationRoles.ViewerOwner}_{complex.Id.ToString()}"
|
2024-02-21 02:07:41 +03:30
|
|
|
|
};
|
|
|
|
|
|
var createViewerResult = await _roleManager.CreateAsync(viewOwnerRole);
|
|
|
|
|
|
if (!createViewerResult.Succeeded)
|
|
|
|
|
|
throw new AppException(string.Join('|', createViewerResult.Errors));
|
|
|
|
|
|
|
2024-02-27 14:20:41 +03:30
|
|
|
|
foreach (var claim in ApplicationClaims.ViewerOwnerClaims)
|
2024-02-21 02:07:41 +03:30
|
|
|
|
await _roleManager.AddClaimAsync(viewOwnerRole, claim);
|
|
|
|
|
|
|
2023-11-13 14:42:49 +03:30
|
|
|
|
var superVisorRole = new ApplicationRole
|
|
|
|
|
|
{
|
|
|
|
|
|
ComplexId = complex.Id,
|
2024-04-25 02:33:17 +03:30
|
|
|
|
EnglishName = ApplicationRoles.SuperVisor,
|
2023-11-13 14:42:49 +03:30
|
|
|
|
PersianName = "سوپروایزر",
|
|
|
|
|
|
Description = "انجام فعالیت مدیریت کارکنان و وظیفه ها",
|
2024-04-25 02:33:17 +03:30
|
|
|
|
Name = $"{ApplicationRoles.SuperVisor}_{complex.Id.ToString()}"
|
2023-11-13 14:42:49 +03:30
|
|
|
|
};
|
|
|
|
|
|
var superVisorRoleResult = await _roleManager.CreateAsync(superVisorRole);
|
|
|
|
|
|
if (!superVisorRoleResult.Succeeded)
|
|
|
|
|
|
throw new AppException(string.Join('|', superVisorRoleResult.Errors));
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var claim in ApplicationClaims.SuperVisorClaims)
|
|
|
|
|
|
await _roleManager.AddClaimAsync(superVisorRole, claim);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var staffRole = new ApplicationRole
|
|
|
|
|
|
{
|
|
|
|
|
|
ComplexId = complex.Id,
|
2024-04-25 02:33:17 +03:30
|
|
|
|
EnglishName = ApplicationRoles.Staff,
|
2023-11-13 14:42:49 +03:30
|
|
|
|
PersianName = "کارمند",
|
|
|
|
|
|
Description = "انجام فعالیت ها و وظیفه ها",
|
2024-04-25 02:33:17 +03:30
|
|
|
|
Name = $"{ApplicationRoles.Staff}_{complex.Id.ToString()}"
|
2023-11-13 14:42:49 +03:30
|
|
|
|
};
|
|
|
|
|
|
var staffRoleResult = await _roleManager.CreateAsync(staffRole);
|
|
|
|
|
|
if (!staffRoleResult.Succeeded)
|
|
|
|
|
|
throw new AppException(string.Join('|', staffRoleResult.Errors));
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var claim in ApplicationClaims.StaffClaims)
|
|
|
|
|
|
await _roleManager.AddClaimAsync(staffRole, claim);
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-10-13 16:25:34 +03:30
|
|
|
|
var complexUser = await _sender.Send(new CreateComplexUserCommand(complex.Id, managerUserId, new List<Guid>{ managerRole.Id }), cancellationToken);
|
2023-09-20 11:54:12 +03:30
|
|
|
|
|
|
|
|
|
|
return complex;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|