< Summary

Class:MUNity.Services.ConferenceService
Assembly:MUNity.Services
File(s):C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNityServices\Services\ConferenceService.cs
Covered lines:0
Uncovered lines:423
Coverable lines:423
Total lines:542
Line coverage:0% (0 of 423)
Covered branches:0
Total branches:66
Branch coverage:0% (0 of 66)
Covered methods:0
Total methods:12
Method coverage:0% (0 of 12)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
CreateConference(...)0%60%
CreateDefaultConferenceAuths(...)100%10%
GetTeamDashboard(...)0%20%
GetParticipatingConferencesAsync()0%20%
GetManageInfo(...)100%10%
CreateCommitteeAsync()0%140%
GetCommitteeSeatsInfo()0%60%
CreateCommitteeSeat()0%120%
CreateFreeSeat()0%140%
GetRolesInfo()0%20%
GetConferenceBoardInfosAsync()0%80%
.ctor(...)100%10%

File(s)

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

#LineLine coverage
 1using Microsoft.AspNetCore.Identity;
 2using Microsoft.EntityFrameworkCore;
 3using MUNity.Database.Context;
 4using MUNity.Database.Models.Conference;
 5using MUNity.Database.Models.Conference.Roles;
 6using MUNity.Database.Models.User;
 7using MUNity.Schema.Conference;
 8using System;
 9using System.Collections.Generic;
 10using System.Linq;
 11using System.Security.Claims;
 12using System.Text;
 13using System.Threading.Tasks;
 14using MUNity.Database.General;
 15using MUNity.Schema.Extensions;
 16using Microsoft.Extensions.Logging;
 17
 18namespace MUNity.Services
 19{
 20    public class ConferenceService
 21    {
 22        private readonly MunityContext context;
 23
 24        private UserConferenceAuthService authService;
 25
 26        private ILogger<ConferenceService> _logger;
 27
 28        public CreateConferenceResponse CreateConference(CreateConferenceRequest request, ClaimsPrincipal claim)
 029        {
 030            var response = new CreateConferenceResponse();
 031            var user = context.Users.FirstOrDefault(n => n.UserName == claim.Identity.Name);
 032            if (user == null)
 033            {
 034                response.AddNoPermissionError("You are not allowed to create a conference.");
 035                return response;
 36            }
 37
 038            var project = context.Projects.FirstOrDefault(n => n.ProjectId == request.ProjectId);
 039            if (project == null)
 040            {
 041                response.AddInvalidDataError("The Project was not found.", nameof(request.ProjectId));
 042                return response;
 43            }
 44
 45            // Create the conference
 046            var conference = new Conference()
 047            {
 048                Name = request.Name,
 049                FullName = request.FullName,
 050                ConferenceShort = request.ConferenceShort,
 051                ConferenceProject = project,
 052                CreationDate = DateTime.UtcNow,
 053                CreationUser = user,
 054                StartDate = request.StartDate,
 055                EndDate = request.EndDate
 056            };
 57
 058            var easyId = Util.IdGenerator.AsPrimaryKey(request.ConferenceShort);
 059            if (context.Conferences.All(n => n.ConferenceId != easyId))
 060                conference.ConferenceId = easyId;
 61
 62            //if (DateTime.TryParse(request.StartDate, out DateTime start))
 63            //    conference.StartDate = start;
 64
 65            //if (DateTime.TryParse(request.EndDate, out DateTime end))
 66            //    conference.EndDate = end;
 67
 068            context.Conferences.Add(conference);
 69
 70
 071            CreateDefaultConferenceAuths(conference);
 72
 073            context.SaveChanges();
 74
 075            response.ConferenceId = conference.ConferenceId;
 76
 077            return response;
 078        }
 79
 80        private void CreateDefaultConferenceAuths(Conference conference)
 081        {
 082            var defaultOwnerAuth = new ConferenceRoleAuth()
 083            {
 084                Conference = conference,
 085                CanEditConferenceSettings = true,
 086                CanEditParticipations = true,
 087                CanSeeApplications = true,
 088                PowerLevel = 1,
 089                RoleAuthName = "Conference-Admin",
 090            };
 091            context.ConferenceRoleAuthorizations.Add(defaultOwnerAuth);
 92
 093            var defaultTeamMemberAuth = new ConferenceRoleAuth()
 094            {
 095                Conference = conference,
 096                CanEditConferenceSettings = false,
 097                CanEditParticipations = false,
 098                CanSeeApplications = true,
 099                PowerLevel = 2,
 0100                RoleAuthName = "Team-Member"
 0101            };
 0102            context.ConferenceRoleAuthorizations.Add(defaultTeamMemberAuth);
 103
 0104            var defaultParticipantAuth = new ConferenceRoleAuth()
 0105            {
 0106                Conference = conference,
 0107                CanEditConferenceSettings = false,
 0108                CanEditParticipations = false,
 0109                CanSeeApplications = false,
 0110                PowerLevel = 5,
 0111                RoleAuthName = "Participant"
 0112            };
 0113            context.ConferenceRoleAuthorizations.Add(defaultParticipantAuth);
 0114        }
 115
 116
 117
 118        public ManageTeamInfo GetTeamDashboard(string conferenceId)
 0119        {
 0120            var dashboardInfo = context.Conferences.Select(n => new ManageTeamInfo()
 0121            {
 0122                OrganizationName = n.ConferenceProject.ProjectOrganization.OrganizationName,
 0123                OrganizationShort = n.ConferenceProject.ProjectOrganization.OrganizationShort,
 0124                OrganizationId = n.ConferenceProject.ProjectOrganization.OrganizationId,
 0125                ProjectName = n.ConferenceProject.ProjectName,
 0126                ProjectShort = n.ConferenceProject.ProjectShort,
 0127                ProjectId = n.ConferenceProject.ProjectId,
 0128                ConferenceName = n.Name,
 0129                ConferenceShort = n.ConferenceShort,
 0130                ConferenceId = n.ConferenceId
 0131            }).FirstOrDefault(n => n.ConferenceId == conferenceId);
 132
 0133            if (dashboardInfo == null)
 0134                return null;
 135
 0136            dashboardInfo.RoleGroups = context.TeamRoleGroups
 0137                .Include(n => n.TeamRoles)
 0138                .ThenInclude(n => n.Participations)
 0139                .AsSingleQuery()
 0140                .Where(n => n.Conference.ConferenceId == conferenceId)
 0141                .Select(n => new TeamRoleGroupInfo()
 0142                {
 0143                    FullName = n.FullName,
 0144                    Name = n.Name,
 0145                    Short = n.TeamRoleGroupShort,
 0146                    TeamRoleGroupId = n.TeamRoleGroupId,
 0147                    Roles = n.TeamRoles.Select(a => new TeamRoleInfo()
 0148                    {
 0149                        FullName = a.RoleFullName,
 0150                        Name = a.RoleName,
 0151                        ParentId = a.ParentTeamRole.RoleId,
 0152                        ParentName = a.ParentTeamRole.RoleName,
 0153                        Short = a.RoleShort,
 0154                        TeamRoleId = a.RoleId,
 0155                        Participants = a.Participations.Select(p => new RoleParticipant()
 0156                        {
 0157                            ParticipantDisplayName = p.User.Forename + " " + p.User.Lastname,
 0158                            ParticipantUsername = p.User.UserName
 0159                        }).ToList()
 0160                    }).ToList()
 0161                }).ToList();
 162
 0163            return dashboardInfo;
 0164        }
 165
 166
 167
 168        public async Task<List<ParticipatingConferenceInfo>> GetParticipatingConferencesAsync(ClaimsPrincipal claim)
 0169        {
 0170            var user = await authService.GetUserAsync(claim);
 0171            if (user == null)
 0172                return null;
 173
 0174            var list = new List<ParticipatingConferenceInfo>();
 175            // Load Conferences this user owns
 0176            var owningConferences = context.Conferences.Where(n => n.CreationUser.UserName == user.UserName)
 0177                .Select(n => new ParticipatingConferenceInfo()
 0178                {
 0179                    ConferenceFullName = n.FullName,
 0180                    ConferenceShort = n.ConferenceShort,
 0181                    ConferenceId = n.ConferenceId,
 0182                    IsTeamMember = true
 0183                }).ToList();
 184
 0185            list.AddRange(owningConferences);
 0186            return list;
 0187        }
 188
 189        public ManageCommitteesInfo GetManageInfo(string conferenceId)
 0190        {
 0191            return context.Conferences
 0192                .Select(conf => new ManageCommitteesInfo()
 0193                {
 0194                    ConferenceId = conf.ConferenceId,
 0195                    ConferenceName = conf.Name,
 0196                    ConferenceShort = conf.ConferenceShort,
 0197                    OrganizationId = conf.ConferenceProject.ProjectOrganization.OrganizationId,
 0198                    OrganizationName = conf.ConferenceProject.ProjectOrganization.OrganizationName,
 0199                    OrganizationShort = conf.ConferenceProject.ProjectOrganization.OrganizationShort,
 0200                    ProjectId = conf.ConferenceProject.ProjectId,
 0201                    ProjectName = conf.ConferenceProject.ProjectName,
 0202                    ProjectShort = conf.ConferenceProject.ProjectShort,
 0203                    Committees = conf.Committees.Select(comm => new ManageCommitteeInfo()
 0204                    {
 0205                        CommitteeId = comm.CommitteeId,
 0206                        CommitteeName = comm.Name,
 0207                        CommitteeShort = comm.CommitteeShort,
 0208                        ParentCommitteeId = comm.ResolutlyCommittee.CommitteeId,
 0209                        ParentCommitteeName = comm.ResolutlyCommittee.Name,
 0210                        SeatCount = context.Delegates.Count(n => n.Committee.CommitteeId == comm.CommitteeId)
 0211                    }).ToList()
 0212                }).FirstOrDefault(n => n.ConferenceId == conferenceId);
 0213        }
 214
 215        public async Task<CreateCommitteeResponse> CreateCommitteeAsync(CreateCommitteeRequest request, ClaimsPrincipal 
 0216        {
 0217            var response = new CreateCommitteeResponse();
 0218            var user = await authService.GetUserAsync(claim);
 0219            var isAllowed = user != null && authService.IsUserAllowedToEditConference(request.ConferenceId, user.UserNam
 0220            if (!isAllowed)
 0221            {
 0222                response.AddNoPermissionError();
 0223                return response;
 224            }
 225
 0226            var conference = context.Conferences.FirstOrDefault(n => n.ConferenceId == request.ConferenceId);
 0227            if (conference == null)
 0228            {
 0229                response.AddConferenceNotFoundError();
 0230            }
 231
 0232            Committee parentCommittee = null;
 0233            if (!string.IsNullOrEmpty(request.ResolutlyCommitteeId))
 0234            {
 0235                parentCommittee = context.Committees.FirstOrDefault(n => n.CommitteeId == request.ResolutlyCommitteeId);
 0236                if (parentCommittee == null)
 0237                {
 0238                    response.AddCommitteeNotFoundError();
 0239                }
 0240            }
 241
 0242            if (response.HasError)
 0243                return response;
 244
 0245            var committee = new Committee()
 0246            {
 0247                Article = request.Article,
 0248                CommitteeShort = request.Short,
 0249                Conference = conference,
 0250                FullName = request.FullName,
 0251                Name = request.Name,
 0252                ResolutlyCommittee = parentCommittee
 0253            };
 254
 0255            var shortAsId = Util.IdGenerator.AsPrimaryKey(request.Short);
 0256            var easyId = conference.ConferenceId + "-" + shortAsId;
 0257            if (context.Committees.All(n => n.CommitteeId != easyId))
 0258            {
 0259                committee.CommitteeId = easyId;
 0260            }
 0261            context.Committees.Add(committee);
 0262            context.SaveChanges();
 0263            response.NewCommitteeId = committee.CommitteeId;
 0264            return response;
 0265        }
 266
 267        public async Task<CommitteeSeatsInfo> GetCommitteeSeatsInfo(string committeeId, ClaimsPrincipal claim)
 0268        {
 0269            var conference = context.Committees
 0270                .Include(n => n.Conference)
 0271                .FirstOrDefault(n => n.CommitteeId == committeeId).Conference;
 0272            if (conference == null)
 0273                return null;
 0274            var isAllowed = await authService.IsUserAllowedToEditConference(conference.ConferenceId, claim);
 0275            if (!isAllowed)
 0276                return null;
 277
 0278            var info = context.Committees
 0279                .Where(n => n.CommitteeId == committeeId)
 0280                .Select(n => new CommitteeSeatsInfo()
 0281                {
 0282                    CommitteeArticle = n.Article,
 0283                    CommitteeId = n.CommitteeId,
 0284                    CommitteeName = n.Name,
 0285                    CommitteeShort = n.CommitteeShort
 0286                }).FirstOrDefault();
 287
 0288            if (info == null)
 0289                return null;
 290
 0291            info.Countries = context.Countries
 0292                .Select(a => new CountryInfo()
 0293                {
 0294                    CountryId = a.CountryId,
 0295                    Name = a.Name
 0296                }).ToList();
 297
 0298            info.Delegations = context.Delegations
 0299                .Where(n => n.Conference.ConferenceId == conference.ConferenceId)
 0300                .Select(n => new DelegationInfo()
 0301                {
 0302                    DelegationId = n.DelegationId,
 0303                    DelegationName = n.Name
 0304                }).ToList();
 305
 0306            info.Seats = context.Delegates
 0307                .Where(n => n.Committee.CommitteeId == committeeId)
 0308                .Select(n => new CommitteeSeatInfo()
 0309                {
 0310                    CountryId = n.DelegateCountry.CountryId,
 0311                    CountryName = n.DelegateCountry.Name,
 0312                    DelegationId = n.Delegation.DelegationId,
 0313                    DelegationName = n.Delegation.Name,
 0314                    RoleId = n.RoleId,
 0315                    RoleName = n.RoleName,
 0316                    Subtypes = n.DelegateType,
 0317                    Participants = n.Participations.Select(a => new CommitteeParticipation()
 0318                    {
 0319                        DisplayName = a.User.Forename + " " + a.User.Lastname,
 0320                        ParticipationId = a.ParticipationId,
 0321                        Username = a.User.UserName
 0322                    }).ToList()
 0323                }).ToList();
 324
 0325            return info;
 0326        }
 327
 328        public async Task<CreateSeatResponse> CreateCommitteeSeat(CreateCommitteeSeatRequest request, ClaimsPrincipal cl
 0329        {
 0330            var response = new CreateSeatResponse();
 0331            var committee = context.Committees
 0332                .Include(n => n.Conference)
 0333                .FirstOrDefault(n => n.CommitteeId == request.CommitteeId);
 0334            var isAllowed = await authService.IsUserAllowedToEditConference(committee.Conference.ConferenceId, claim);
 0335            if (!isAllowed)
 0336            {
 0337                response.AddNoPermissionError();
 0338                return response;
 339            }
 340
 0341            Country country = null;
 0342            if (request.CountryId != -1)
 0343            {
 0344                country = context.Countries.FirstOrDefault(n => n.CountryId == request.CountryId);
 0345                if (country == null)
 0346                {
 0347                    response.AddNotFoundError(nameof(request.CountryId));
 0348                }
 0349            }
 350
 0351            Delegation delegation = null;
 0352            if (!string.IsNullOrEmpty(request.DelegationId))
 0353            {
 0354                delegation = context.Delegations.FirstOrDefault(n => n.DelegationId == request.DelegationId);
 0355                if (delegation == null)
 0356                {
 0357                    response.AddNotFoundError(nameof(request.DelegationId));
 0358                }
 0359            }
 360
 0361            if (response.HasError)
 0362                return response;
 363
 0364            var role = new ConferenceDelegateRole()
 0365            {
 0366                Committee = committee,
 0367                Conference = committee.Conference,
 0368                DelegateCountry = country,
 0369                DelegateType = request.Subtype,
 0370                Delegation = delegation,
 0371                RoleName = request.RoleName,
 0372                RoleFullName = request.RoleName,
 0373                Title = request.RoleName
 0374            };
 375
 0376            context.Delegates.Add(role);
 0377            await context.SaveChangesAsync();
 0378            response.CreatedRoleId = role.RoleId;
 0379            return response;
 0380        }
 381
 382        public async Task<CreateSeatResponse> CreateFreeSeat(CreateFreeSeatRequest request,
 383            ClaimsPrincipal claim)
 0384        {
 0385            var response = new CreateSeatResponse();
 0386            var isAllowed = await authService.IsUserAllowedToEditConference(request.ConferenceId, claim);
 0387            if (!isAllowed)
 0388            {
 0389                response.AddNoPermissionError();
 0390                return response;
 391            }
 392
 0393            var conference = context.Conferences.FirstOrDefault(n => n.ConferenceId == request.ConferenceId);
 0394            if (conference == null)
 0395            {
 0396                response.AddNotFoundError(nameof(request.ConferenceId));
 0397            }
 398
 0399            Country country = null;
 0400            if (request.CountryId != -1)
 0401            {
 0402                country = context.Countries.FirstOrDefault(n => n.CountryId == request.CountryId);
 0403                if (country == null)
 0404                {
 0405                    response.AddNotFoundError(nameof(request.CountryId));
 0406                }
 0407            }
 408
 0409            Delegation delegation = null;
 0410            if (!string.IsNullOrEmpty(request.DelegationId))
 0411            {
 0412                delegation = context.Delegations.FirstOrDefault(n => n.DelegationId == request.DelegationId);
 0413                if (delegation == null)
 0414                {
 0415                    response.AddNotFoundError(nameof(request.DelegationId));
 0416                }
 0417            }
 418
 0419            if (response.HasError)
 0420                return response;
 421
 0422            var role = new ConferenceDelegateRole()
 0423            {
 0424                Committee = null,
 0425                Conference = conference,
 0426                DelegateCountry = country,
 0427                DelegateType = request.Subtype,
 0428                Delegation = delegation,
 0429                RoleName = request.RoleName,
 0430                RoleFullName = request.RoleName,
 0431                Title = request.RoleName
 0432            };
 433
 0434            context.Delegates.Add(role);
 0435            await context.SaveChangesAsync();
 0436            response.CreatedRoleId = role.RoleId;
 0437            return response;
 0438        }
 439
 440        public async Task<ConferenceRolesInfo> GetRolesInfo(string conferenceId, ClaimsPrincipal claim)
 0441        {
 0442            var isAllowed = await authService.IsUserAllowedToEditConference(conferenceId, claim);
 0443            if (!isAllowed)
 0444                return null;
 445
 0446            var mdl = context.Conferences
 0447                .Include(n => n.ConferenceProject)
 0448                .ThenInclude(n => n.ProjectOrganization)
 0449                .Include(n => n.Roles)
 0450                .Include(n => n.Delegations)
 0451                .AsSingleQuery()
 0452                .AsNoTracking()
 0453                .Select(conf => new ConferenceRolesInfo()
 0454                {
 0455                    ConferenceId = conf.ConferenceId,
 0456                    ConferenceName = conf.Name,
 0457                    ConferenceShort = conf.ConferenceShort,
 0458                    OrganizationId = conf.ConferenceProject.ProjectOrganization.OrganizationId,
 0459                    OrganizationName = conf.ConferenceProject.ProjectOrganization.OrganizationName,
 0460                    OrganizationShort = conf.ConferenceProject.ProjectOrganization.OrganizationShort,
 0461                    ProjectId = conf.ConferenceProject.ProjectId,
 0462                    ProjectName = conf.ConferenceProject.ProjectName,
 0463                    ProjectShort = conf.ConferenceProject.ProjectShort,
 0464                    Roles = conf.Roles.OfType<ConferenceDelegateRole>()
 0465                        .Select(role => new ManageDelegationRoleInfo()
 0466                        {
 0467                            ApplicationState = role.ApplicationState,
 0468                            HasParicipant = role.Participations.Any(),
 0469                            RoleCommitteeId = role.Committee.CommitteeId,
 0470                            RoleCommitteeName = role.Committee.Name,
 0471                            RoleId = role.RoleId,
 0472                            RoleName = role.RoleName,
 0473                            Subtype = role.DelegateType,
 0474                            DelegationId = role.Delegation.DelegationId,
 0475                            DelegationName = role.Delegation.Name
 0476                        }).ToList(),
 0477                    Delegations = conf.Delegations.Select(del => new DelegationInfo()
 0478                    {
 0479                        DelegationId = del.DelegationId,
 0480                        DelegationName = del.Name
 0481                    }).ToList()
 0482                }).FirstOrDefault(n => n.ConferenceId == conferenceId);
 483
 0484            mdl.Countries = context.Countries.AsNoTracking().Select(n => new CountryInfo()
 0485            {
 0486                Name = n.Name,
 0487                CountryId = n.CountryId
 0488            }).ToList();
 489
 0490            return mdl;
 0491        }
 492
 493        public async Task<List<ConferenceBoardInfo>> GetConferenceBoardInfosAsync(ClaimsPrincipal claim)
 0494        {
 495
 496            try
 0497            {
 0498                var claimedUserName = claim.Identity?.Name?.ToUpper();
 0499                var infos = await this.context.Conferences.Select(n => new ConferenceBoardInfo()
 0500                {
 0501                    ConferenceFullName = n.FullName,
 0502                    ConferenceId = n.ConferenceId,
 0503                    ConferenceName = n.Name,
 0504                    ConferenceShort = n.ConferenceShort,
 0505                    DashboardText = "",
 0506                    EndDate = n.EndDate,
 0507                    StartDate = n.StartDate,
 0508                    UserIsOwner = claimedUserName != null && n.CreationUser.NormalizedUserName == claimedUserName
 0509                }).ToListAsync();
 510
 511
 0512                if (claimedUserName != null)
 0513                {
 0514                    foreach (var info in infos)
 0515                    {
 0516                        info.UserIsParticipating = context.Participations.Any(n => n.User.NormalizedUserName == claimedU
 0517                    n.Role.Conference.ConferenceId == info.ConferenceId && n.Role is ConferenceDelegateRole);
 518
 0519                        info.UserIsTeamMember = context.Participations.Any(n => n.User.NormalizedUserName == claimedUser
 0520                        n.Role.Conference.ConferenceId == info.ConferenceId && n.Role is ConferenceTeamRole);
 0521                    }
 0522                }
 0523                return infos;
 524            }
 0525            catch (Exception ex)
 0526            {
 0527                _logger.LogError(ex, "Unable to get the list of conference.");
 528
 0529            }
 0530            return null;
 531
 532
 0533        }
 534
 0535        public ConferenceService(MunityContext context, UserConferenceAuthService authService, ILogger<ConferenceService
 0536        {
 0537            this.context = context;
 0538            this.authService = authService;
 0539            this._logger = logger;
 0540        }
 541    }
 542}