namespace Brizco.Core.EntityServices.Handlers.Complexes; public class CreateComplexCoreCommandHandler(ISender sender, RoleManager roleManager) : IRequestHandler { public async Task 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 { managerRole.Id }), cancellationToken); return complex; } }