< Summary - Estud

Information
Class: Estud.Back.Features.Identity.UpdateRole.UpdateRoleService
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Features/Identity/UpdateRole/UpdateRoleService.cs
Tag: 114_29044117136
Line coverage
100%
Covered lines: 32
Uncovered lines: 0
Coverable lines: 32
Total lines: 56
Line coverage: 100%
Branch coverage
75%
Covered branches: 9
Total branches: 12
Branch coverage: 75%
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%
Update()87.5%88100%

File(s)

/home/runner/work/syki/syki/Back/Features/Identity/UpdateRole/UpdateRoleService.cs

#LineLine coverage
 1using Estud.Back.Auth.Permissions;
 2
 3namespace Estud.Back.Features.Identity.UpdateRole;
 4
 145public class UpdateRoleService(EstudDbContext ctx) : IEstudService
 6{
 7    private class Validator : AbstractValidator<UpdateRoleIn>
 8    {
 29        public Validator()
 10        {
 211            RuleFor(x => x.Name).NotEmpty().WithError(InvalidRoleName.I);
 212            RuleFor(x => x.Name).MaximumLength(50).WithError(InvalidRoleName.I);
 13
 214            RuleFor(x => x.Description).NotEmpty().WithError(InvalidRoleDescription.I);
 215            RuleFor(x => x.Description).MaximumLength(200).WithError(InvalidRoleDescription.I);
 16
 217            RuleFor(x => x.BaseType).IsInEnum().WithError(InvalidRoleBaseType.I);
 18
 219            RuleFor(x => x.Permissions)
 22420                .Must(x => x != null && x.IsAllDistinct() && x.IsSubsetOf(EstudPermissions.Permissions.ConvertAll(p => p
 221                .WithError(InvalidPermissionsList.I);
 22
 223            RuleFor(x => x)
 1624                .Must(x => x.Permissions.All(id => EstudPermissions.IsAllowedFor(id, x.BaseType)))
 225                .WithError(InvalidPermissionsForUserType.I);
 226        }
 27    }
 228    private static readonly Validator V = new();
 29
 30    public async Task<OneOf<UpdateRoleOut, EstudError>> Update(UpdateRoleIn data)
 31    {
 2232        if (V.Run(data, out var error)) return error;
 33
 634        var institutionId = ctx.RequestUser.InstitutionId;
 35
 636        var role = await ctx.Roles.FirstOrDefaultAsync(r => r.OwnerId == institutionId && r.Id == data.Id);
 837        if (role == null) return RoleNotFound.I;
 38
 439        var upperCaseName = data.Name.Normalize().ToUpperInvariant();
 440        var nameConflict = await ctx.Roles.AnyAsync(r => r.OwnerId == institutionId && r.NormalizedName == upperCaseName
 641        if (nameConflict) return RoleNameAlreadyExists.I;
 42
 243        role.Name = data.Name;
 244        role.NormalizedName = upperCaseName;
 245        role.Description = data.Description;
 246        role.BaseType = data.BaseType;
 247        role.Permissions = data.Permissions;
 48
 249        var rolePermissionsOk = role.IsSubsetOf(ctx.RequestUser.Permissions);
 250        if (!rolePermissionsOk) return InvalidRolePermissions.I;
 51
 252        await ctx.SaveChangesAsync();
 53
 254        return new UpdateRoleOut { Id = role.Id };
 1455    }
 56}