| | 1 | | using Microsoft.AspNetCore.Identity; |
| | 2 | | using MUNity.Database.Context; |
| | 3 | | using MUNity.Database.Models.User; |
| | 4 | | using MUNity.Schema.Conference; |
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Security.Claims; |
| | 9 | | using System.Text; |
| | 10 | | using System.Threading.Tasks; |
| | 11 | | using MUNity.Schema.Extensions; |
| | 12 | | using MUNity.Database.Models.Conference; |
| | 13 | | using Microsoft.EntityFrameworkCore; |
| | 14 | |
|
| | 15 | | namespace MUNity.Services |
| | 16 | | { |
| | 17 | | public class DelegationService |
| | 18 | | { |
| | 19 | | private MunityContext context; |
| | 20 | |
|
| | 21 | | private UserConferenceAuthService authService; |
| | 22 | |
|
| | 23 | | public async Task<ManageDelegationsInfo> GetManageDelegationsInfo(string conferenceId, ClaimsPrincipal claim) |
| 0 | 24 | | { |
| 0 | 25 | | var isAllowed = await authService.IsUserAllowedToEditConference(conferenceId, claim); |
| 0 | 26 | | if (!isAllowed) |
| 0 | 27 | | { |
| 0 | 28 | | return null; |
| | 29 | | } |
| | 30 | |
|
| 0 | 31 | | ManageDelegationsInfo info = context.Conferences |
| 0 | 32 | | .Include(n => n.Delegations) |
| 0 | 33 | | .ThenInclude(n => n.Roles) |
| 0 | 34 | | .AsSingleQuery() |
| 0 | 35 | | .Select(conf => new ManageDelegationsInfo() |
| 0 | 36 | | { |
| 0 | 37 | | ConferenceId = conf.ConferenceId, |
| 0 | 38 | | ConferenceName = conf.Name, |
| 0 | 39 | | ConferenceShort = conf.ConferenceShort, |
| 0 | 40 | | OrganizationId = conf.ConferenceProject.ProjectOrganization.OrganizationId, |
| 0 | 41 | | OrganizationName = conf.ConferenceProject.ProjectOrganization.OrganizationName, |
| 0 | 42 | | OrganizationShort = conf.ConferenceProject.ProjectOrganization.OrganizationShort, |
| 0 | 43 | | ProjectId = conf.ConferenceProject.ProjectId, |
| 0 | 44 | | ProjectName = conf.ConferenceProject.ProjectName, |
| 0 | 45 | | ProjectShort = conf.ConferenceProject.ProjectShort, |
| 0 | 46 | | Delegations = conf.Delegations.Select(del => new ManageDelegationInfo() |
| 0 | 47 | | { |
| 0 | 48 | | DelegationId = del.DelegationId, |
| 0 | 49 | | DelegationName = del.Name, |
| 0 | 50 | | DelegationShort = del.DelegationShort, |
| 0 | 51 | | Roles = del.Roles.Select(role => new ManageDelegationRoleInfo() |
| 0 | 52 | | { |
| 0 | 53 | | ApplicationState = role.ApplicationState, |
| 0 | 54 | | HasParicipant = role.Participations.Any(), |
| 0 | 55 | | RoleCommitteeId = role.Committee.CommitteeId, |
| 0 | 56 | | RoleCommitteeName = role.Committee.Name, |
| 0 | 57 | | RoleId = role.RoleId, |
| 0 | 58 | | RoleName = role.RoleName |
| 0 | 59 | | }).ToList() |
| 0 | 60 | | }).ToList() |
| 0 | 61 | | }).FirstOrDefault(n => n.ConferenceId == conferenceId); |
| 0 | 62 | | return info; |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public async Task<CreateDelegationResponse> CreateDelegationAsync(CreateDelegationRequest request, ClaimsPrincip |
| 0 | 66 | | { |
| 0 | 67 | | var response = new CreateDelegationResponse(); |
| 0 | 68 | | var isAllowed = await authService.IsUserAllowedToEditConference(request.ConferenceId, claim); |
| 0 | 69 | | if (!isAllowed) |
| 0 | 70 | | { |
| 0 | 71 | | response.AddNoPermissionError(); |
| 0 | 72 | | return response; |
| | 73 | | } |
| | 74 | |
|
| 0 | 75 | | var conference = context.Conferences.FirstOrDefault(n => n.ConferenceId == request.ConferenceId); |
| 0 | 76 | | if (conference == null) |
| 0 | 77 | | { |
| 0 | 78 | | response.AddNotFoundError(nameof(request.ConferenceId)); |
| 0 | 79 | | return response; |
| | 80 | | } |
| | 81 | |
|
| 0 | 82 | | var delegation = new Delegation() |
| 0 | 83 | | { |
| 0 | 84 | | Conference = conference, |
| 0 | 85 | | DelegationShort = request.DelegationShort, |
| 0 | 86 | | FullName = request.DelegationFullName, |
| 0 | 87 | | Name = request.DelegationName |
| 0 | 88 | | }; |
| | 89 | |
|
| 0 | 90 | | string easyId = conference.ConferenceId + "-" + Util.IdGenerator.AsPrimaryKey(request.DelegationShort); |
| 0 | 91 | | if (context.Delegations.All(n => n.DelegationId != easyId)) |
| 0 | 92 | | delegation.DelegationId = easyId; |
| | 93 | |
|
| 0 | 94 | | context.Delegations.Add(delegation); |
| 0 | 95 | | context.SaveChanges(); |
| | 96 | |
|
| 0 | 97 | | return response; |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | public async Task<List<ManageDelegationRoleInfo>> GetAvailableRoles(string conferenceId, ClaimsPrincipal claim) |
| 0 | 101 | | { |
| 0 | 102 | | var isAllowed = await authService.IsUserAllowedToEditConference(conferenceId, claim); |
| 0 | 103 | | if (!isAllowed) |
| 0 | 104 | | return new List<ManageDelegationRoleInfo>(); |
| | 105 | |
|
| 0 | 106 | | var roles = context.Delegates |
| 0 | 107 | | .Where(n => n.Delegation == null && n.Conference.ConferenceId == conferenceId) |
| 0 | 108 | | .OrderBy(n => n.RoleName) |
| 0 | 109 | | .Select(n => new ManageDelegationRoleInfo() |
| 0 | 110 | | { |
| 0 | 111 | | ApplicationState = n.ApplicationState, |
| 0 | 112 | | HasParicipant = n.Participations.Any(), |
| 0 | 113 | | RoleCommitteeId = n.Committee.CommitteeId, |
| 0 | 114 | | RoleCommitteeName = n.Committee.Name, |
| 0 | 115 | | RoleId = n.RoleId, |
| 0 | 116 | | RoleName = n.RoleName |
| 0 | 117 | | }).ToList(); |
| 0 | 118 | | return roles; |
| 0 | 119 | | } |
| | 120 | |
|
| | 121 | | public async Task<bool> AddRoleToDelegation(int roleId, string delegationId, ClaimsPrincipal claim) |
| 0 | 122 | | { |
| 0 | 123 | | var role = context.Delegates |
| 0 | 124 | | .Include(n => n.Conference) |
| 0 | 125 | | .FirstOrDefault(n => n.RoleId == roleId); |
| 0 | 126 | | if (role == null || role.Conference == null) |
| 0 | 127 | | return false; |
| | 128 | |
|
| 0 | 129 | | var isAllowed = await authService.IsUserAllowedToEditConference(role.Conference.ConferenceId, claim); |
| 0 | 130 | | if (!isAllowed) |
| 0 | 131 | | return false; |
| | 132 | |
|
| 0 | 133 | | var delegation = context.Delegations |
| 0 | 134 | | .Include(n => n.Conference) |
| 0 | 135 | | .FirstOrDefault(n => n.DelegationId == delegationId); |
| | 136 | |
|
| 0 | 137 | | if (role.Conference.ConferenceId != delegation.Conference.ConferenceId) |
| 0 | 138 | | return false; |
| | 139 | |
|
| 0 | 140 | | role.Delegation = delegation; |
| 0 | 141 | | context.SaveChanges(); |
| 0 | 142 | | return true; |
| 0 | 143 | | } |
| | 144 | |
|
| 0 | 145 | | public DelegationService(MunityContext context, UserConferenceAuthService authService) |
| 0 | 146 | | { |
| 0 | 147 | | this.context = context; |
| 0 | 148 | | this.authService = authService; |
| 0 | 149 | | } |
| | 150 | | } |
| | 151 | | } |