< 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: 97_27801654829
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 51
Line coverage: 100%
Branch coverage
70%
Covered branches: 7
Total branches: 10
Branch coverage: 70%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor()50%44100%
.cctor()100%11100%
Create()83.33%66100%

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
 186public class CreateRoleService(SykiDbContext ctx) : ISykiService
 7{
 8    private class Validator : AbstractValidator<CreateRoleIn>
 9    {
 210        public Validator()
 11        {
 212            RuleFor(x => x.Name).NotEmpty().WithError(InvalidRoleName.I);
 213            RuleFor(x => x.Name).MaximumLength(50).WithError(InvalidRoleName.I);
 14
 215            RuleFor(x => x.Description).NotEmpty().WithError(InvalidRoleDescription.I);
 216            RuleFor(x => x.Description).MaximumLength(200).WithError(InvalidRoleDescription.I);
 17
 218            RuleFor(x => x.BaseType).IsInEnum().WithError(InvalidRoleBaseType.I);
 19
 220            RuleFor(x => x.Permissions)
 25221                .Must(x => x != null && x.IsAllDistinct() && x.IsSubsetOf(SykiPermissions.Permissions.ConvertAll(p => p.
 222                .WithError(InvalidPermissionsList.I);
 23
 224            RuleFor(x => x)
 2025                .Must(x => x.Permissions.All(id => SykiPermissions.IsAllowedFor(id, x.BaseType)))
 226                .WithError(InvalidPermissionsForUserType.I);
 227        }
 28    }
 229    private static readonly Validator V = new();
 30
 31    public async Task<OneOf<CreateRoleOut, SykiError>> Create(CreateRoleIn data)
 32    {
 2633        if (V.Run(data, out var error)) return error;
 34
 1035        var institutionId = ctx.RequestUser.InstitutionId;
 1036        var upperCaseName = data.Name.Normalize().ToUpperInvariant();
 1037        var roleAlreadyExists = await ctx.Roles.AnyAsync(x => x.OwnerId == institutionId && x.NormalizedName == upperCas
 1238        if (roleAlreadyExists) return RoleNameAlreadyExists.I;
 39
 840        var role = new SykiRole(institutionId, data.Name, data.Description, data.BaseType, data.Permissions);
 841        var rolePermissionsOk = role.IsSubsetOf(ctx.RequestUser.Permissions);
 842        if (!rolePermissionsOk) return InvalidRolePermissions.I;
 43
 844        var orgRole = new InstitutionRole(institutionId, role);
 845        ctx.AddRange(role, orgRole);
 846        ctx.RecordSuccess(UserActivityType.CreateRole_Success, metadata: new { role.Id });
 847        await ctx.SaveChangesAsync();
 48
 849        return new CreateRoleOut { Id = role.Id };
 1850    }
 51}