75 lines
2.8 KiB
C#
75 lines
2.8 KiB
C#
namespace Brizco.Core.EntityServices;
|
||
|
||
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,
|
||
EnglishName = "Manager",
|
||
PersianName = "مدیریت",
|
||
Description = "مدیریت مجموعه",
|
||
Name = $"Manager_{complex.Id.ToString()}"
|
||
};
|
||
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);
|
||
|
||
var superVisorRole = new ApplicationRole
|
||
{
|
||
ComplexId = complex.Id,
|
||
EnglishName = "SuperVisor",
|
||
PersianName = "سوپروایزر",
|
||
Description = "انجام فعالیت مدیریت کارکنان و وظیفه ها",
|
||
Name = $"SuperVisor_{complex.Id.ToString()}"
|
||
};
|
||
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,
|
||
EnglishName = "Staff",
|
||
PersianName = "کارمند",
|
||
Description = "انجام فعالیت ها و وظیفه ها",
|
||
Name = $"Staff_{complex.Id.ToString()}"
|
||
};
|
||
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);
|
||
|
||
|
||
var complexUser = await _sender.Send(new CreateComplexUserCommand(complex.Id, managerUserId, new List<Guid>{ managerRole.Id }), cancellationToken);
|
||
|
||
return complex;
|
||
}
|
||
} |