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

79 lines
3.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

namespace Brizco.Core.EntityServices.Handlers.Complexes;
public class CreateComplexCoreCommandHandler(ISender sender, RoleManager<ApplicationRole> roleManager) : IRequestHandler<CreateComplexCoreCommand , ComplexSDto>
{
public async Task<ComplexSDto> Handle(CreateComplexCoreCommand request, CancellationToken cancellationToken)
{
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()}"
};
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 viewOwnerRole = new ApplicationRole
{
ComplexId = complex.Id,
EnglishName = ApplicationRoles.ViewerOwner,
PersianName = "ناظر",
Description = "ناظر مجموعه",
Name = $"{ApplicationRoles.ViewerOwner}_{complex.Id.ToString()}"
};
var createViewerResult = await roleManager.CreateAsync(viewOwnerRole);
if (!createViewerResult.Succeeded)
throw new AppException(string.Join('|', createViewerResult.Errors));
foreach (var claim in ApplicationClaims.ViewerOwnerClaims)
await roleManager.AddClaimAsync(viewOwnerRole, claim);
var superVisorRole = new ApplicationRole
{
ComplexId = complex.Id,
EnglishName = ApplicationRoles.SuperVisor,
PersianName = "سوپروایزر",
Description = "انجام فعالیت مدیریت کارکنان و وظیفه ها",
Name = $"{ApplicationRoles.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 = ApplicationRoles.Staff,
PersianName = "کارمند",
Description = "انجام فعالیت ها و وظیفه ها",
Name = $"{ApplicationRoles.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, request.ManagerUserId, new List<Guid> { managerRole.Id }), cancellationToken);
return complex;
}
}