| | | 1 | | using Chronicis.Api.Data; |
| | | 2 | | using Chronicis.Shared.DTOs; |
| | | 3 | | using Chronicis.Shared.Enums; |
| | | 4 | | using Chronicis.Shared.Extensions; |
| | | 5 | | using Chronicis.Shared.Models; |
| | | 6 | | using Microsoft.EntityFrameworkCore; |
| | | 7 | | |
| | | 8 | | namespace Chronicis.Api.Services; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Service for world invitation management |
| | | 12 | | /// </summary> |
| | | 13 | | public class WorldInvitationService : IWorldInvitationService |
| | | 14 | | { |
| | | 15 | | private readonly ChronicisDbContext _context; |
| | | 16 | | private readonly ILogger<WorldInvitationService> _logger; |
| | | 17 | | |
| | 18 | 18 | | public WorldInvitationService(ChronicisDbContext context, ILogger<WorldInvitationService> logger) |
| | | 19 | | { |
| | 18 | 20 | | _context = context; |
| | 18 | 21 | | _logger = logger; |
| | 18 | 22 | | } |
| | | 23 | | |
| | | 24 | | public async Task<List<WorldInvitationDto>> GetInvitationsAsync(Guid worldId, Guid userId) |
| | | 25 | | { |
| | | 26 | | // Only GMs can view invitations |
| | 2 | 27 | | var isGM = await _context.WorldMembers |
| | 2 | 28 | | .AnyAsync(m => m.WorldId == worldId && m.UserId == userId && m.Role == WorldRole.GM); |
| | | 29 | | |
| | 2 | 30 | | if (!isGM) |
| | 1 | 31 | | return new List<WorldInvitationDto>(); |
| | | 32 | | |
| | 1 | 33 | | var invitations = await _context.WorldInvitations |
| | 1 | 34 | | .Include(i => i.Creator) |
| | 1 | 35 | | .Where(i => i.WorldId == worldId) |
| | 1 | 36 | | .OrderByDescending(i => i.CreatedAt) |
| | 1 | 37 | | .ToListAsync(); |
| | | 38 | | |
| | 3 | 39 | | return invitations.Select(i => new WorldInvitationDto |
| | 3 | 40 | | { |
| | 3 | 41 | | Id = i.Id, |
| | 3 | 42 | | WorldId = i.WorldId, |
| | 3 | 43 | | Code = i.Code, |
| | 3 | 44 | | Role = i.Role, |
| | 3 | 45 | | CreatedBy = i.CreatedBy, |
| | 3 | 46 | | CreatorName = i.Creator?.DisplayName ?? "Unknown", |
| | 3 | 47 | | CreatedAt = i.CreatedAt, |
| | 3 | 48 | | ExpiresAt = i.ExpiresAt, |
| | 3 | 49 | | MaxUses = i.MaxUses, |
| | 3 | 50 | | UsedCount = i.UsedCount, |
| | 3 | 51 | | IsActive = i.IsActive |
| | 3 | 52 | | }).ToList(); |
| | 2 | 53 | | } |
| | | 54 | | |
| | | 55 | | public async Task<WorldInvitationDto?> CreateInvitationAsync(Guid worldId, WorldInvitationCreateDto dto, Guid userId |
| | | 56 | | { |
| | | 57 | | // Only GMs can create invitations |
| | 17 | 58 | | var isGM = await _context.WorldMembers |
| | 17 | 59 | | .AnyAsync(m => m.WorldId == worldId && m.UserId == userId && m.Role == WorldRole.GM); |
| | | 60 | | |
| | 17 | 61 | | if (!isGM) |
| | 1 | 62 | | return null; |
| | | 63 | | |
| | | 64 | | // Generate unique code |
| | | 65 | | string code; |
| | 16 | 66 | | int attempts = 0; |
| | | 67 | | do |
| | | 68 | | { |
| | 16 | 69 | | code = Utilities.InvitationCodeGenerator.GenerateCode(); |
| | 16 | 70 | | attempts++; |
| | 16 | 71 | | } while (await _context.WorldInvitations.AnyAsync(i => i.Code == code) && attempts < 10); |
| | | 72 | | |
| | 16 | 73 | | if (attempts >= 10) |
| | | 74 | | { |
| | 0 | 75 | | _logger.LogError("Failed to generate unique invitation code after 10 attempts"); |
| | 0 | 76 | | return null; |
| | | 77 | | } |
| | | 78 | | |
| | 16 | 79 | | var invitation = new WorldInvitation |
| | 16 | 80 | | { |
| | 16 | 81 | | Id = Guid.NewGuid(), |
| | 16 | 82 | | WorldId = worldId, |
| | 16 | 83 | | Code = code, |
| | 16 | 84 | | Role = dto.Role, |
| | 16 | 85 | | CreatedBy = userId, |
| | 16 | 86 | | CreatedAt = DateTime.UtcNow, |
| | 16 | 87 | | ExpiresAt = dto.ExpiresAt, |
| | 16 | 88 | | MaxUses = dto.MaxUses, |
| | 16 | 89 | | UsedCount = 0, |
| | 16 | 90 | | IsActive = true |
| | 16 | 91 | | }; |
| | | 92 | | |
| | 16 | 93 | | _context.WorldInvitations.Add(invitation); |
| | 16 | 94 | | await _context.SaveChangesAsync(); |
| | | 95 | | |
| | 16 | 96 | | _logger.LogDebugSanitized("Created invitation {Code} for world {WorldId} by user {UserId}", |
| | 16 | 97 | | code, worldId, userId); |
| | | 98 | | |
| | 16 | 99 | | var creator = await _context.Users.FindAsync(userId); |
| | | 100 | | |
| | 16 | 101 | | return new WorldInvitationDto |
| | 16 | 102 | | { |
| | 16 | 103 | | Id = invitation.Id, |
| | 16 | 104 | | WorldId = invitation.WorldId, |
| | 16 | 105 | | Code = invitation.Code, |
| | 16 | 106 | | Role = invitation.Role, |
| | 16 | 107 | | CreatedBy = invitation.CreatedBy, |
| | 16 | 108 | | CreatorName = creator?.DisplayName ?? "Unknown", |
| | 16 | 109 | | CreatedAt = invitation.CreatedAt, |
| | 16 | 110 | | ExpiresAt = invitation.ExpiresAt, |
| | 16 | 111 | | MaxUses = invitation.MaxUses, |
| | 16 | 112 | | UsedCount = invitation.UsedCount, |
| | 16 | 113 | | IsActive = invitation.IsActive |
| | 16 | 114 | | }; |
| | 17 | 115 | | } |
| | | 116 | | |
| | | 117 | | public async Task<bool> RevokeInvitationAsync(Guid worldId, Guid invitationId, Guid userId) |
| | | 118 | | { |
| | | 119 | | // Only GMs can revoke invitations |
| | 3 | 120 | | var isGM = await _context.WorldMembers |
| | 3 | 121 | | .AnyAsync(m => m.WorldId == worldId && m.UserId == userId && m.Role == WorldRole.GM); |
| | | 122 | | |
| | 3 | 123 | | if (!isGM) |
| | 1 | 124 | | return false; |
| | | 125 | | |
| | 2 | 126 | | var invitation = await _context.WorldInvitations |
| | 2 | 127 | | .FirstOrDefaultAsync(i => i.Id == invitationId && i.WorldId == worldId); |
| | | 128 | | |
| | 2 | 129 | | if (invitation == null) |
| | 0 | 130 | | return false; |
| | | 131 | | |
| | 2 | 132 | | invitation.IsActive = false; |
| | 2 | 133 | | await _context.SaveChangesAsync(); |
| | | 134 | | |
| | 2 | 135 | | _logger.LogDebug("Revoked invitation {InvitationId} for world {WorldId}", invitationId, worldId); |
| | | 136 | | |
| | 2 | 137 | | return true; |
| | 3 | 138 | | } |
| | | 139 | | |
| | | 140 | | public async Task<WorldJoinResultDto> JoinWorldAsync(string code, Guid userId) |
| | | 141 | | { |
| | 11 | 142 | | var normalizedCode = Utilities.InvitationCodeGenerator.NormalizeCode(code); |
| | | 143 | | |
| | 11 | 144 | | if (!Utilities.InvitationCodeGenerator.IsValidFormat(normalizedCode)) |
| | | 145 | | { |
| | 1 | 146 | | return new WorldJoinResultDto |
| | 1 | 147 | | { |
| | 1 | 148 | | Success = false, |
| | 1 | 149 | | ErrorMessage = "Invalid invitation code format" |
| | 1 | 150 | | }; |
| | | 151 | | } |
| | | 152 | | |
| | 10 | 153 | | var invitation = await _context.WorldInvitations |
| | 10 | 154 | | .Include(i => i.World) |
| | 10 | 155 | | .FirstOrDefaultAsync(i => i.Code == normalizedCode && i.IsActive); |
| | | 156 | | |
| | 10 | 157 | | if (invitation == null) |
| | | 158 | | { |
| | 2 | 159 | | return new WorldJoinResultDto |
| | 2 | 160 | | { |
| | 2 | 161 | | Success = false, |
| | 2 | 162 | | ErrorMessage = "Invitation not found or has been revoked" |
| | 2 | 163 | | }; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | // Check expiration |
| | 8 | 167 | | if (invitation.ExpiresAt.HasValue && invitation.ExpiresAt.Value < DateTime.UtcNow) |
| | | 168 | | { |
| | 1 | 169 | | return new WorldJoinResultDto |
| | 1 | 170 | | { |
| | 1 | 171 | | Success = false, |
| | 1 | 172 | | ErrorMessage = "This invitation has expired" |
| | 1 | 173 | | }; |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | // Check max uses |
| | 7 | 177 | | if (invitation.MaxUses.HasValue && invitation.UsedCount >= invitation.MaxUses.Value) |
| | | 178 | | { |
| | 1 | 179 | | return new WorldJoinResultDto |
| | 1 | 180 | | { |
| | 1 | 181 | | Success = false, |
| | 1 | 182 | | ErrorMessage = "This invitation has reached its maximum number of uses" |
| | 1 | 183 | | }; |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | // Check if user is already a member |
| | 6 | 187 | | var existingMember = await _context.WorldMembers |
| | 6 | 188 | | .FirstOrDefaultAsync(m => m.WorldId == invitation.WorldId && m.UserId == userId); |
| | | 189 | | |
| | 6 | 190 | | if (existingMember != null) |
| | | 191 | | { |
| | 1 | 192 | | return new WorldJoinResultDto |
| | 1 | 193 | | { |
| | 1 | 194 | | Success = false, |
| | 1 | 195 | | ErrorMessage = "You are already a member of this world" |
| | 1 | 196 | | }; |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | // Create membership |
| | 5 | 200 | | var member = new WorldMember |
| | 5 | 201 | | { |
| | 5 | 202 | | Id = Guid.NewGuid(), |
| | 5 | 203 | | WorldId = invitation.WorldId, |
| | 5 | 204 | | UserId = userId, |
| | 5 | 205 | | Role = invitation.Role, |
| | 5 | 206 | | JoinedAt = DateTime.UtcNow, |
| | 5 | 207 | | InvitedBy = invitation.CreatedBy |
| | 5 | 208 | | }; |
| | | 209 | | |
| | 5 | 210 | | _context.WorldMembers.Add(member); |
| | | 211 | | |
| | | 212 | | // Increment usage count |
| | 5 | 213 | | invitation.UsedCount++; |
| | | 214 | | |
| | 5 | 215 | | await _context.SaveChangesAsync(); |
| | | 216 | | |
| | 5 | 217 | | _logger.LogDebugSanitized("User {UserId} joined world {WorldId} via invitation {Code}", |
| | 5 | 218 | | userId, invitation.WorldId, normalizedCode); |
| | | 219 | | |
| | 5 | 220 | | return new WorldJoinResultDto |
| | 5 | 221 | | { |
| | 5 | 222 | | Success = true, |
| | 5 | 223 | | WorldId = invitation.WorldId, |
| | 5 | 224 | | WorldName = invitation.World?.Name, |
| | 5 | 225 | | AssignedRole = invitation.Role |
| | 5 | 226 | | }; |
| | 11 | 227 | | } |
| | | 228 | | } |