Api/Brizco.Core/EntityServices/Handlers/Complexes/CreateComplexCoreCommandHan...

79 lines
3.3 KiB
C#
Raw Normal View History

2024-08-11 19:20:09 +03:30
namespace Brizco.Core.EntityServices.Handlers.Complexes;
2024-07-06 21:20:10 +03:30
2024-08-11 19:20:09 +03:30
public class CreateComplexCoreCommandHandler(ISender sender, RoleManager<ApplicationRole> roleManager) : IRequestHandler<CreateComplexCoreCommand , ComplexSDto>
{
2024-08-11 19:20:09 +03:30
public async Task<ComplexSDto> Handle(CreateComplexCoreCommand request, CancellationToken cancellationToken)
{
2024-08-11 19:20:09 +03:30
var complex = await sender.Send(new CreateComplexCommand(request.Name,
request.Address,
request.SupportPhone), cancellationToken);
var managerRole = new ApplicationRole
{
ComplexId = complex.Id,
EnglishName = ApplicationRoles.Manager,
PersianName = "مدیریت",
Description = "مدیریت مجموعه",
Name = $"{ApplicationRoles.Manager}_{complex.Id.ToString()}"
};
2024-08-09 21:42:04 +03:30
var createRoleResult = await roleManager.CreateAsync(managerRole);
if (!createRoleResult.Succeeded)
throw new AppException(string.Join('|', createRoleResult.Errors));
foreach (var claim in ApplicationClaims.ManagerClaims)
2024-08-09 21:42:04 +03:30
await roleManager.AddClaimAsync(managerRole, claim);
var viewOwnerRole = new ApplicationRole
{
ComplexId = complex.Id,
EnglishName = ApplicationRoles.ViewerOwner,
PersianName = "ناظر",
Description = "ناظر مجموعه",
Name = $"{ApplicationRoles.ViewerOwner}_{complex.Id.ToString()}"
};
2024-08-09 21:42:04 +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-08-09 21:42:04 +03:30
await roleManager.AddClaimAsync(viewOwnerRole, claim);
2023-11-13 14:42:49 +03:30
var superVisorRole = new ApplicationRole
{
ComplexId = complex.Id,
EnglishName = ApplicationRoles.SuperVisor,
2023-11-13 14:42:49 +03:30
PersianName = "سوپروایزر",
Description = "انجام فعالیت مدیریت کارکنان و وظیفه ها",
Name = $"{ApplicationRoles.SuperVisor}_{complex.Id.ToString()}"
2023-11-13 14:42:49 +03:30
};
2024-08-09 21:42:04 +03:30
var superVisorRoleResult = await roleManager.CreateAsync(superVisorRole);
2023-11-13 14:42:49 +03:30
if (!superVisorRoleResult.Succeeded)
throw new AppException(string.Join('|', superVisorRoleResult.Errors));
foreach (var claim in ApplicationClaims.SuperVisorClaims)
2024-08-09 21:42:04 +03:30
await roleManager.AddClaimAsync(superVisorRole, claim);
2023-11-13 14:42:49 +03:30
var staffRole = new ApplicationRole
{
ComplexId = complex.Id,
EnglishName = ApplicationRoles.Staff,
2023-11-13 14:42:49 +03:30
PersianName = "کارمند",
Description = "انجام فعالیت ها و وظیفه ها",
Name = $"{ApplicationRoles.Staff}_{complex.Id.ToString()}"
2023-11-13 14:42:49 +03:30
};
2024-08-09 21:42:04 +03:30
var staffRoleResult = await roleManager.CreateAsync(staffRole);
2023-11-13 14:42:49 +03:30
if (!staffRoleResult.Succeeded)
throw new AppException(string.Join('|', staffRoleResult.Errors));
foreach (var claim in ApplicationClaims.StaffClaims)
2024-08-09 21:42:04 +03:30
await roleManager.AddClaimAsync(staffRole, claim);
2023-11-13 14:42:49 +03:30
2024-08-11 19:20:09 +03:30
var complexUser = await sender.Send(new CreateComplexUserCommand(complex.Id, request.ManagerUserId, new List<Guid> { managerRole.Id }), cancellationToken);
return complex;
}
}