< Summary - Syki

Information
Class: Syki.Back.Features.Identity.CreateRole.CreateRoleService
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Features/Identity/CreateRole/CreateRoleService.cs
Tag: 56_26538939494
Line coverage
0%
Covered lines: 0
Uncovered lines: 24
Coverable lines: 24
Total lines: 44
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
.ctor()0%2040%
.cctor()100%210%
Create()0%4260%

File(s)

/home/runner/work/syki/syki/Back/Features/Identity/CreateRole/CreateRoleService.cs

#LineLine coverage
 1using Syki.Back.Domain.Identity;
 2using Syki.Back.Auth.Permissions;
 3
 4namespace Syki.Back.Features.Identity.CreateRole;
 5
 06public class CreateRoleService(SykiDbContext ctx) : ISykiService
 7{
 8    private class Validator : AbstractValidator<CreateRoleIn>
 9    {
 010        public Validator()
 11        {
 012            RuleFor(x => x.Name).NotEmpty().WithError(InvalidRoleName.I);
 013            RuleFor(x => x.Name).MaximumLength(50).WithError(InvalidRoleName.I);
 14
 015            RuleFor(x => x.Description).NotEmpty().WithError(InvalidRoleDescription.I);
 016            RuleFor(x => x.Description).MaximumLength(200).WithError(InvalidRoleDescription.I);
 17
 018            RuleFor(x => x.Permissions)
 019                .Must(x => x != null && x.IsAllDistinct() && x.IsSubsetOf(SykiPermissions.Permissions.ConvertAll(p => p.
 020                .WithError(InvalidPermissionsList.I);
 021        }
 22    }
 023    private static readonly Validator V = new();
 24
 25    public async Task<OneOf<CreateRoleOut, SykiError>> Create(CreateRoleIn data)
 26    {
 027        if (V.Run(data, out var error)) return error;
 28
 029        var institutionId = ctx.RequestUser.InstitutionId;
 030        var upperCaseName = data.Name.Normalize().ToUpperInvariant();
 031        var roleAlreadyExists = await ctx.Roles.AnyAsync(x => x.OwnerId == institutionId && x.NormalizedName == upperCas
 032        if (roleAlreadyExists) return RoleNameAlreadyExists.I;
 33
 034        var role = new SykiRole(institutionId, data.Name, data.Description, data.Permissions);
 035        var rolePermissionsOk = role.IsSubsetOf(ctx.RequestUser.Permissions);
 036        if (!rolePermissionsOk) return InvalidRolePermissions.I;
 37
 038        var orgRole = new InstitutionRole(institutionId, role);
 039        ctx.AddRange(role, orgRole);
 040        await ctx.SaveChangesAsync();
 41
 042        return new CreateRoleOut { Id = role.Id };
 043    }
 44}