private async Task FinalizeCreateCampaignAsync(ulong subjectId, ulong targetRoleId, string comment) { PromotionActionSummary campaignResultAction; PromotionActionSummary commentResultAction; using (var campaignTransaction = await PromotionCampaignRepository.BeginCreateTransactionAsync()) using (var commentTransaction = await PromotionCommentRepository.BeginCreateTransactionAsync()) { campaignResultAction = await PromotionCampaignRepository.CreateAsync(new PromotionCampaignCreationData() { GuildId = AuthorizationService.CurrentGuildId.Value, SubjectId = subjectId, TargetRoleId = targetRoleId, CreatedById = AuthorizationService.CurrentUserId.Value }); commentResultAction = await PromotionCommentRepository.CreateAsync(new PromotionCommentCreationData() { GuildId = AuthorizationService.CurrentGuildId.Value, CampaignId = campaignResultAction.Campaign.Id, Sentiment = PromotionSentiment.Approve, Content = comment, CreatedById = AuthorizationService.CurrentUserId.Value }); campaignTransaction.Commit(); commentTransaction.Commit(); } PublishActionNotificationAsync(campaignResultAction); PublishActionNotificationAsync(commentResultAction); }
/// <inheritdoc /> public async Task AddCommentAsync(long campaignId, PromotionSentiment sentiment, string content) { AuthorizationService.RequireAuthenticatedUser(); AuthorizationService.RequireClaims(AuthorizationClaim.PromotionsComment); ValidateComment(content); if (await PromotionCommentRepository.AnyAsync(new PromotionCommentSearchCriteria() { CampaignId = campaignId, CreatedById = AuthorizationService.CurrentUserId.Value, IsModified = false })) { throw new InvalidOperationException("Only one comment can be made per user, per campaign"); } var campaign = await PromotionCampaignRepository.ReadDetailsAsync(campaignId); if (campaign.Subject.Id == AuthorizationService.CurrentUserId) { throw new InvalidOperationException("You aren't allowed to comment on your own campaign"); } if (!(campaign.CloseAction is null)) { throw new InvalidOperationException($"Campaign {campaignId} has already been closed"); } var rankRoles = await GetRankRolesAsync(AuthorizationService.CurrentGuildId.Value); if (!await CheckIfUserIsRankOrHigherAsync(rankRoles, AuthorizationService.CurrentUserId.Value, campaign.TargetRole.Id)) { throw new InvalidOperationException($"Commenting on a promotion campaign requires a rank at least as high as the proposed target rank"); } PromotionActionSummary resultAction; using (var transaction = await PromotionCommentRepository.BeginCreateTransactionAsync()) { resultAction = await PromotionCommentRepository.CreateAsync(new PromotionCommentCreationData() { GuildId = campaign.GuildId, CampaignId = campaignId, Sentiment = sentiment, Content = content, CreatedById = AuthorizationService.CurrentUserId.Value }); transaction.Commit(); } PublishActionNotificationAsync(resultAction); }
/// <inheritdoc /> public async Task AddCommentAsync(long campaignId, PromotionSentiment sentiment, string content) { AuthorizationService.RequireClaims(AuthorizationClaim.PromotionsComment); if (content == null || content.Length <= 3) { throw new InvalidOperationException("Comment content must be longer than 3 characters."); } using (var transaction = await PromotionCommentRepository.BeginCreateTransactionAsync()) { if (await PromotionCommentRepository.AnyAsync(new PromotionCommentSearchCriteria() { CampaignId = campaignId, CreatedById = AuthorizationService.CurrentUserId.Value })) { throw new InvalidOperationException("Only one comment can be made per user, per campaign"); } var campaign = await PromotionCampaignRepository.ReadDetailsAsync(campaignId); if (!(campaign.CloseAction is null)) { throw new InvalidOperationException($"Campaign {campaignId} has already been closed"); } var rankRoles = await GetRankRolesAsync(AuthorizationService.CurrentGuildId.Value); if (!await CheckIfUserIsRankOrHigher(rankRoles, AuthorizationService.CurrentUserId.Value, campaign.TargetRole.Id)) { throw new InvalidOperationException($"Commenting on a promotion campaign requires a rank at least as high as the proposed target rank"); } await PromotionCommentRepository.CreateAsync(new PromotionCommentCreationData() { GuildId = campaign.GuildId, CampaignId = campaignId, Sentiment = sentiment, Content = content, CreatedById = AuthorizationService.CurrentUserId.Value }); transaction.Commit(); } }
/// <inheritdoc /> public async Task CreateCampaignAsync(ulong subjectId, ulong targetRoleId, string comment) { AuthorizationService.RequireAuthenticatedGuild(); AuthorizationService.RequireAuthenticatedUser(); AuthorizationService.RequireClaims(AuthorizationClaim.PromotionsCreateCampaign); using (var campaignTransaction = await PromotionCampaignRepository.BeginCreateTransactionAsync()) using (var commentTransaction = await PromotionCommentRepository.BeginCreateTransactionAsync()) { var rankRoles = await GetRankRolesAsync(AuthorizationService.CurrentGuildId.Value); var targetRankRoleIndex = rankRoles .Select((x, i) => (role: x, index: (int?)i)) .FirstOrDefault(x => x.role.Id == targetRoleId) .index; if (targetRankRoleIndex is null) throw new InvalidOperationException($"Role {targetRoleId} is not a defined promotion rank"); var targetRankRole = rankRoles[targetRankRoleIndex.Value]; if (await PromotionCampaignRepository.AnyAsync(new PromotionCampaignSearchCriteria() { GuildId = AuthorizationService.CurrentGuildId.Value, SubjectId = subjectId, TargetRoleId = targetRankRole.Id, IsClosed = false })) throw new InvalidOperationException($"An active campaign already exists for user {subjectId} to be promoted to {targetRoleId}"); var subject = await UserService.GetGuildUserAsync(AuthorizationService.CurrentGuildId.Value, subjectId); if (subject.RoleIds.Contains(targetRoleId)) throw new InvalidOperationException($"User {subjectId} is already a member of role {targetRoleId}"); if(targetRankRoleIndex > 0) { var previousRankRole = rankRoles[targetRankRoleIndex.Value - 1]; if (!subject.RoleIds.Contains(previousRankRole.Id)) throw new InvalidOperationException($"The proposed promotion would skip over rank {previousRankRole.Name}"); } else if (subject.RoleIds.Intersect(rankRoles.Select(x => x.Id)).Any()) throw new InvalidOperationException($"User {subjectId} is already ranked"); if(!(await CheckIfUserIsRankOrHigher(rankRoles, AuthorizationService.CurrentUserId.Value, targetRankRole.Id))) throw new InvalidOperationException($"Creating a promotion campaign requires a rank at least as high as the proposed target rank"); var campaignId = await PromotionCampaignRepository.CreateAsync(new PromotionCampaignCreationData() { GuildId = AuthorizationService.CurrentGuildId.Value, SubjectId = subjectId, TargetRoleId = targetRankRole.Id, CreatedById = AuthorizationService.CurrentUserId.Value }); await PromotionCommentRepository.CreateAsync(new PromotionCommentCreationData() { GuildId = AuthorizationService.CurrentGuildId.Value, CampaignId = campaignId, Sentiment = PromotionSentiment.Approve, Content = comment, CreatedById = AuthorizationService.CurrentUserId.Value }); campaignTransaction.Commit(); commentTransaction.Commit(); } }