< Summary - Syki

Information
Class: Syki.Back.Features.Identity.UpdateRole.UpdateRoleService
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Features/Identity/UpdateRole/UpdateRoleService.cs
Tag: 97_27801654829
Line coverage
0%
Covered lines: 0
Uncovered lines: 32
Coverable lines: 32
Total lines: 56
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
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%
Update()0%7280%

File(s)

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

#LineLine coverage
 1using Syki.Back.Auth.Permissions;
 2
 3namespace Syki.Back.Features.Identity.UpdateRole;
 4
 05public class UpdateRoleService(SykiDbContext ctx) : ISykiService
 6{
 7    private class Validator : AbstractValidator<UpdateRoleIn>
 8    {
 09        public Validator()
 10        {
 011            RuleFor(x => x.Name).NotEmpty().WithError(InvalidRoleName.I);
 012            RuleFor(x => x.Name).MaximumLength(50).WithError(InvalidRoleName.I);
 13
 014            RuleFor(x => x.Description).NotEmpty().WithError(InvalidRoleDescription.I);
 015            RuleFor(x => x.Description).MaximumLength(200).WithError(InvalidRoleDescription.I);
 16
 017            RuleFor(x => x.BaseType).IsInEnum().WithError(InvalidRoleBaseType.I);
 18
 019            RuleFor(x => x.Permissions)
 020                .Must(x => x != null && x.IsAllDistinct() && x.IsSubsetOf(SykiPermissions.Permissions.ConvertAll(p => p.
 021                .WithError(InvalidPermissionsList.I);
 22
 023            RuleFor(x => x)
 024                .Must(x => x.Permissions.All(id => SykiPermissions.IsAllowedFor(id, x.BaseType)))
 025                .WithError(InvalidPermissionsForUserType.I);
 026        }
 27    }
 028    private static readonly Validator V = new();
 29
 30    public async Task<OneOf<UpdateRoleOut, SykiError>> Update(UpdateRoleIn data)
 31    {
 032        if (V.Run(data, out var error)) return error;
 33
 034        var institutionId = ctx.RequestUser.InstitutionId;
 35
 036        var role = await ctx.Roles.FirstOrDefaultAsync(r => r.OwnerId == institutionId && r.Id == data.Id);
 037        if (role == null) return RoleNotFound.I;
 38
 039        var upperCaseName = data.Name.Normalize().ToUpperInvariant();
 040        var nameConflict = await ctx.Roles.AnyAsync(r => r.OwnerId == institutionId && r.NormalizedName == upperCaseName
 041        if (nameConflict) return RoleNameAlreadyExists.I;
 42
 043        role.Name = data.Name;
 044        role.NormalizedName = upperCaseName;
 045        role.Description = data.Description;
 046        role.BaseType = data.BaseType;
 047        role.Permissions = data.Permissions;
 48
 049        var rolePermissionsOk = role.IsSubsetOf(ctx.RequestUser.Permissions);
 050        if (!rolePermissionsOk) return InvalidRolePermissions.I;
 51
 052        await ctx.SaveChangesAsync();
 53
 054        return new UpdateRoleOut { Id = role.Id };
 055    }
 56}