< Summary

Class:MUNity.Services.ConferenceRoleService
Assembly:MUNity.Services
File(s):C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNityServices\Services\ConferenceRoleService.cs
Covered lines:0
Uncovered lines:153
Coverable lines:153
Total lines:232
Line coverage:0% (0 of 153)
Covered branches:0
Total branches:54
Branch coverage:0% (0 of 54)
Covered methods:0
Total methods:9
Method coverage:0% (0 of 9)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
CreateTeamRoleGroupAsync()0%120%
CreateTeamRoleAsync()0%60%
RemoveDelegateRole()0%40%
RemoveTeamRole()0%40%
SetDelegateRoleApplicationState()0%60%
SetDelegateRoleApplicationState()0%200%
GetRoleInfos()0%20%
GetTeamMembers()100%10%
.ctor(...)100%10%

File(s)

C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNityServices\Services\ConferenceRoleService.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Security.Claims;
 5using System.Text;
 6using System.Threading.Tasks;
 7using Microsoft.EntityFrameworkCore;
 8using MUNity.Base;
 9using MUNity.Database.Context;
 10using MUNity.Database.Models.Conference;
 11using MUNity.Database.Models.Conference.Roles;
 12using MUNity.Schema.Conference;
 13using MUNity.Schema.Extensions;
 14
 15namespace MUNity.Services
 16{
 17    public class ConferenceRoleService
 18    {
 19        private readonly MunityContext _context;
 20
 21        private readonly UserConferenceAuthService _authService;
 22
 23        public async Task<CreateTeamRoleGroupResponse> CreateTeamRoleGroupAsync(CreateTeamRoleGroupRequest request, Clai
 024        {
 025            var response = new CreateTeamRoleGroupResponse();
 026            var isAllowed = await _authService.IsUserAllowedToEditTeam(request.ConferenceId, claim);
 027            if (!isAllowed)
 028            {
 029                response.AddNoPermissionError();
 030                return response;
 31            }
 32
 033            var conference = _context.Conferences.FirstOrDefault(n => n.ConferenceId == request.ConferenceId);
 034            if (conference == null)
 035                response.AddNotFoundError(nameof(request.ConferenceId));
 36
 37
 038            if (_context.TeamRoleGroups.Any(n => n.Conference.ConferenceId == request.ConferenceId && n.Name == request.
 039                response.AddNotFoundError(nameof(request.GroupName));
 40
 41
 042            if (_context.TeamRoleGroups.Any(n => n.Conference.ConferenceId == request.ConferenceId && n.FullName == requ
 043                response.AddNotFoundError(nameof(request.GroupFullName));
 44
 45
 046            if (_context.TeamRoleGroups.Any(n => n.Conference.ConferenceId == request.ConferenceId && n.TeamRoleGroupSho
 047                response.AddNotFoundError(nameof(request.GroupShort));
 48
 49
 050            if (response.HasError)
 051                return response;
 52
 053            var group = new TeamRoleGroup()
 054            {
 055                Conference = conference,
 056                FullName = request.GroupFullName,
 057                GroupLevel = request.GroupLevel,
 058                Name = request.GroupName,
 059                TeamRoleGroupShort = request.GroupShort
 060            };
 61
 062            _context.TeamRoleGroups.Add(group);
 063            await _context.SaveChangesAsync();
 064            response.CreatedGroupId = group.TeamRoleGroupId;
 065            return response;
 066        }
 67
 68        public async Task<CreateTeamRoleResponse> CreateTeamRoleAsync(CreateTeamRoleRequest request, ClaimsPrincipal cla
 069        {
 070            var response = new CreateTeamRoleResponse();
 071            var group = _context.TeamRoleGroups.Include(n => n.Conference)
 072                .FirstOrDefault(n => n.TeamRoleGroupId == request.RoleGroupId);
 73
 074            if (group == null)
 075            {
 076                response.AddInvalidDataError("The given group was not found", nameof(request.RoleGroupId));
 077                return response;
 78            }
 79
 080            var isAllowed = await _authService.IsUserAllowedToEditTeam(group.Conference.ConferenceId, claim);
 081            if (!isAllowed)
 082            {
 083                response.AddNoPermissionError("You don't have permission to create a Team role");
 084                return response;
 85            }
 86
 087            ConferenceTeamRole parentRole = null;
 088            if (request.ParentRoleId != -1)
 089            {
 90                // We are also checking for the ConferenceId to make sure that the given Parent role is in
 91                // the same conference!
 092                parentRole = _context.ConferenceTeamRoles.FirstOrDefault(n =>
 093                n.RoleId == request.ParentRoleId &&
 094                n.Conference.ConferenceId == group.Conference.ConferenceId);
 095            }
 96
 97
 098            var role = new ConferenceTeamRole()
 099            {
 0100                Conference = group.Conference,
 0101                ParentTeamRole = parentRole,
 0102                RoleName = request.RoleName,
 0103                RoleShort = request.RoleShort,
 0104                RoleFullName = request.RoleFullName,
 0105                TeamRoleGroup = group,
 0106                TeamRoleLevel = 0
 0107            };
 0108            _context.ConferenceTeamRoles.Add(role);
 0109            await _context.SaveChangesAsync();
 0110            response.RoleId = role.RoleId;
 111
 0112            return response;
 0113        }
 114
 115
 116        public async Task<bool> RemoveDelegateRole(int roleId, ClaimsPrincipal claim)
 0117        {
 0118            var role = _context.Delegates
 0119                .Include(n => n.Conference)
 0120                .FirstOrDefault(n => n.RoleId == roleId);
 121
 0122            if (role == null)
 0123                return false;
 124
 0125            var isAllowed = await _authService.IsUserAllowedToEditConference(role.Conference.ConferenceId, claim);
 0126            if (!isAllowed)
 0127                return false;
 128
 0129            _context.Delegates.Remove(role);
 0130            await _context.SaveChangesAsync();
 0131            return true;
 0132        }
 133
 134        public async Task<bool> RemoveTeamRole(int roleId, ClaimsPrincipal claim)
 0135        {
 0136            var role = _context.ConferenceTeamRoles
 0137                .Include(n => n.Conference)
 0138                .FirstOrDefault(n => n.RoleId == roleId);
 139
 0140            if (role == null)
 0141                return false;
 142
 0143            var isAllowed = await _authService.IsUserAllowedToEditTeam(role.Conference.ConferenceId, claim);
 0144            if (!isAllowed)
 0145                return false;
 146
 0147            _context.ConferenceTeamRoles.Remove(role);
 0148            await _context.SaveChangesAsync();
 0149            return true;
 0150        }
 151
 152        public async Task<bool> SetDelegateRoleApplicationState(int roleId, EApplicationStates state, ClaimsPrincipal cl
 0153        {
 0154            var roleWithConference = _context.Delegates
 0155                .Include(n => n.Conference)
 0156                .FirstOrDefault(n => n.RoleId == roleId);
 157
 0158            if (roleWithConference?.Conference == null)
 0159                return false;
 160
 0161            var isAllowed = await _authService.IsUserAllowedToEditConference(roleWithConference.Conference.ConferenceId,
 0162            if (!isAllowed)
 0163                return false;
 164
 0165            roleWithConference.ApplicationState = state;
 0166            _context.SaveChanges();
 0167            return true;
 0168        }
 169
 170        public async Task<bool> SetDelegateRoleApplicationState(int roleId, string state, ClaimsPrincipal claim)
 0171        {
 0172            if (state == EApplicationStates.Closed.ToString() || state.ToLower() == "closed")
 0173            {
 0174                return await SetDelegateRoleApplicationState(roleId, EApplicationStates.Closed, claim);
 175            }
 0176            else if (state == EApplicationStates.Custom.ToString() || state.ToLower() == "custom")
 0177            {
 0178                return await SetDelegateRoleApplicationState(roleId, EApplicationStates.Custom, claim);
 179            }
 0180            else if (state == EApplicationStates.DelegationApplication.ToString() || state.ToLower() == "delegationappli
 0181            {
 0182                return await SetDelegateRoleApplicationState(roleId, EApplicationStates.DelegationApplication, claim);
 183            }
 0184            else if (state == EApplicationStates.DirectApplication.ToString() || state.ToLower() == "directapplication" 
 0185            {
 0186                return await SetDelegateRoleApplicationState(roleId, EApplicationStates.DirectApplication, claim);
 187            }
 188            else
 0189            {
 0190                throw new NotSupportedException($"{state} is not a known Application Type that can be read by string.");
 191            }
 0192        }
 193
 194        public async Task<List<ManageDelegationRoleInfo>> GetRoleInfos(string conferenceId, ClaimsPrincipal claim)
 0195        {
 0196            var isAllowed = await _authService.IsUserAllowedToEditConference(conferenceId, claim);
 0197            if (!isAllowed)
 0198                return null;
 199
 0200            return _context.Delegates.Where(n => n.Conference.ConferenceId == conferenceId)
 0201                .Select(role => new ManageDelegationRoleInfo()
 0202                {
 0203                    ApplicationState = role.ApplicationState,
 0204                    HasParicipant = role.Participations.Any(),
 0205                    RoleCommitteeId = role.Committee.CommitteeId,
 0206                    RoleCommitteeName = role.Committee.Name,
 0207                    RoleId = role.RoleId,
 0208                    RoleName = role.RoleName,
 0209                    Subtype = role.DelegateType,
 0210                    DelegationId = role.Delegation.DelegationId,
 0211                    DelegationName = role.Delegation.Name
 0212                }).ToList();
 0213        }
 214
 215        public async Task<List<TeamMemberInfo>> GetTeamMembers(string conferenceId)
 0216    {
 0217            return await _context.Participations.Where(n => n.Role is ConferenceTeamRole && n.Role.Conference.Conference
 0218                .Select(n => new TeamMemberInfo()
 0219                {
 0220                    UserName = n.User.UserName,
 0221                    DisplayName = n.User.GetDisplayNamePublic,
 0222                    RoleName = n.Role.RoleName
 0223                }).ToListAsync();
 0224    }
 225
 0226        public ConferenceRoleService(MunityContext context, UserConferenceAuthService authService)
 0227        {
 0228            _context = context;
 0229            _authService = authService;
 0230        }
 231    }
 232}