-
Notifications
You must be signed in to change notification settings - Fork 27
Feature/citizen reports guides #686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
65d8e29
Add citizen reports api
idormenco 8ba9f08
rename api feature
idormenco d5d2efe
refactor guides
idormenco f3c91fb
Add tests, rename folder, fix selects
idormenco 61314f9
Update EditTextGuide.tsx
idormenco dcfdd7b
Update file-uploader.tsx
idormenco f65c306
Update EditTextGuide.tsx
idormenco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...uthorization.Policies/RequirementHandlers/CitizenReportingNgoAdminAuthorizationHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| using Authorization.Policies.Requirements; | ||
| using Authorization.Policies.Specifications; | ||
| using Vote.Monitor.Domain.Entities.ElectionRoundAggregate; | ||
|
|
||
| namespace Authorization.Policies.RequirementHandlers; | ||
|
|
||
| internal class CitizenReportingNgoAdminAuthorizationHandler( | ||
| ICurrentUserProvider currentUserProvider, | ||
| ICurrentUserRoleProvider currentUserRoleProvider, | ||
| IReadRepository<ElectionRound> electionRoundRepository) | ||
| : AuthorizationHandler<CitizenReportingNgoAdminRequirement> | ||
| { | ||
| protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, | ||
| CitizenReportingNgoAdminRequirement requirement) | ||
| { | ||
| if (!currentUserRoleProvider.IsNgoAdmin()) | ||
| { | ||
| context.Fail(); | ||
| return; | ||
| } | ||
|
|
||
| var ngoId = currentUserProvider.GetNgoId(); | ||
| if (ngoId is null) | ||
| { | ||
| context.Fail(); | ||
| return; | ||
| } | ||
|
|
||
| var getMonitoringNgoSpecification = | ||
| new GetCitizenReportingMonitoringNgoSpecification(requirement.ElectionRoundId, ngoId.Value); | ||
| var result = await electionRoundRepository.FirstOrDefaultAsync(getMonitoringNgoSpecification); | ||
|
|
||
| if (result is null) | ||
| { | ||
| context.Fail(); | ||
| return; | ||
| } | ||
|
|
||
| if (result.ElectionRoundStatus == ElectionRoundStatus.Archived | ||
| || result.NgoStatus == NgoStatus.Deactivated | ||
| || result.MonitoringNgoStatus == MonitoringNgoStatus.Suspended) | ||
| { | ||
| context.Fail(); | ||
| return; | ||
| } | ||
|
|
||
| context.Succeed(requirement); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
api/src/Authorization.Policies/Requirements/CitizenReportingNgoAdminRequirement.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace Authorization.Policies.Requirements; | ||
|
|
||
| public class CitizenReportingNgoAdminRequirement(Guid electionRoundId) : IAuthorizationRequirement | ||
| { | ||
| public Guid ElectionRoundId { get; } = electionRoundId; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...rc/Authorization.Policies/Specifications/GetCitizenReportingMonitoringNgoSpecification.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| using Ardalis.Specification; | ||
| using Vote.Monitor.Domain.Entities.ElectionRoundAggregate; | ||
|
|
||
| namespace Authorization.Policies.Specifications; | ||
|
|
||
| internal sealed class GetCitizenReportingMonitoringNgoSpecification : SingleResultSpecification<ElectionRound, MonitoringNgoView> | ||
| { | ||
| public GetCitizenReportingMonitoringNgoSpecification(Guid electionRoundId, Guid ngoId) | ||
| { | ||
| Query | ||
| .Include(x => x.MonitoringNgoForCitizenReporting) | ||
| .ThenInclude(x => x.Ngo) | ||
| .Where(x => x.CitizenReportingEnabled && x.Id == electionRoundId && | ||
| x.MonitoringNgoForCitizenReporting.NgoId == ngoId) | ||
| .AsNoTracking(); | ||
|
|
||
| Query.Select(x => new MonitoringNgoView | ||
| { | ||
| ElectionRoundId = x.Id, | ||
| ElectionRoundStatus = x.Status, | ||
| NgoId = x.MonitoringNgoForCitizenReporting.NgoId, | ||
| NgoStatus = x.MonitoringNgoForCitizenReporting.Ngo.Status, | ||
| MonitoringNgoId = x.MonitoringNgoForCitizenReporting.Id, | ||
| MonitoringNgoStatus = x.MonitoringNgoForCitizenReporting.Status | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,4 +22,4 @@ public GetMonitoringNgoSpecification(Guid electionRoundId, Guid ngoId) | |
| MonitoringNgoStatus = x.Status | ||
| }); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using System.Text.Json.Serialization; | ||
| using Ardalis.SmartEnum.SystemTextJson; | ||
| using Vote.Monitor.Domain.Entities.CitizenGuideAggregate; | ||
|
|
||
| namespace Feature.Citizen.Guides; | ||
|
|
||
| public record CitizenGuideModel | ||
| { | ||
| public required Guid Id { get; init; } | ||
| public string Title { get; init; } = string.Empty; | ||
| public string? FileName { get; init; } = string.Empty; | ||
| public string? MimeType { get; init; } = string.Empty; | ||
| public string? PresignedUrl { get; init; } = string.Empty; | ||
| public int? UrlValidityInSeconds { get; init; } | ||
| public string? WebsiteUrl { get; init; } | ||
| public string? Text { get; init; } | ||
|
|
||
| [JsonConverter(typeof(SmartEnumNameConverter<CitizenGuideType, string>))] | ||
| public CitizenGuideType GuideType { get; init; } | ||
|
|
||
| public DateTime CreatedOn { get; init; } | ||
| public string CreatedBy { get; init; } | ||
| } |
11 changes: 11 additions & 0 deletions
11
api/src/Feature.Citizen.Guides/CitizenGuidesFeatureInstaller.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace Feature.Citizen.Guides; | ||
|
|
||
| public static class CitizenGuidesFeatureInstaller | ||
| { | ||
| public static IServiceCollection AddCitizenGuidesFeature(this IServiceCollection services) | ||
| { | ||
| return services; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| using System.Net; | ||
| using Authorization.Policies; | ||
| using Authorization.Policies.Requirements; | ||
| using Microsoft.AspNetCore.Authorization; | ||
| using Vote.Monitor.Core.Services.FileStorage.Contracts; | ||
| using Vote.Monitor.Domain.Entities.CitizenGuideAggregate; | ||
|
|
||
| namespace Feature.Citizen.Guides.Create; | ||
|
|
||
| public class Endpoint( | ||
| IAuthorizationService authorizationService, | ||
| IRepository<CitizenGuideAggregate> repository, | ||
| IFileStorageService fileStorageService) | ||
| : Endpoint<Request, Results<Ok<CitizenGuideModel>, NotFound, StatusCodeHttpResult>> | ||
| { | ||
| public override void Configure() | ||
| { | ||
| Post("/api/election-rounds/{electionRoundId}/citizen-guides"); | ||
| DontAutoTag(); | ||
| Options(x => x.WithTags("citizen-guides")); | ||
| AllowFileUploads(); | ||
| Policies(PolicyNames.NgoAdminsOnly); | ||
| } | ||
|
|
||
| public override async Task<Results<Ok<CitizenGuideModel>, NotFound, StatusCodeHttpResult>> ExecuteAsync( | ||
| Request req, CancellationToken ct) | ||
| { | ||
| var requirement = new CitizenReportingNgoAdminRequirement(req.ElectionRoundId); | ||
| var authorizationResult = await authorizationService.AuthorizeAsync(User, requirement); | ||
| if (!authorizationResult.Succeeded) | ||
| { | ||
| return TypedResults.NotFound(); | ||
| } | ||
|
|
||
| CitizenGuideAggregate? citizenGuide = null; | ||
| CitizenGuideModel? citizenGuideModel = null; | ||
| if (req.GuideType == CitizenGuideType.Document) | ||
| { | ||
| var uploadPath = $"elections/{req.ElectionRoundId}/citizen-guides"; | ||
|
|
||
| citizenGuide = CitizenGuide.NewDocumentGuide(req.ElectionRoundId, | ||
| req.Title, | ||
| req.Attachment!.FileName, | ||
| uploadPath, | ||
| req.Attachment.ContentType); | ||
|
|
||
| var uploadResult = await fileStorageService.UploadFileAsync(uploadPath, | ||
| fileName: citizenGuide.UploadedFileName!, | ||
| req.Attachment.OpenReadStream(), | ||
| ct); | ||
|
|
||
| if (uploadResult is UploadFileResult.Failed) | ||
| { | ||
| return TypedResults.StatusCode((int)HttpStatusCode.InternalServerError); | ||
| } | ||
|
|
||
| var result = uploadResult as UploadFileResult.Ok; | ||
| citizenGuideModel = new CitizenGuideModel | ||
| { | ||
| Title = citizenGuide.Title, | ||
| FileName = citizenGuide.FileName!, | ||
| PresignedUrl = result!.Url, | ||
| MimeType = citizenGuide.MimeType!, | ||
| UrlValidityInSeconds = result.UrlValidityInSeconds, | ||
| Id = citizenGuide.Id, | ||
| GuideType = citizenGuide.GuideType | ||
| }; | ||
| } | ||
|
|
||
| if (req.GuideType == CitizenGuideType.Website) | ||
| { | ||
| citizenGuide = CitizenGuide.NewWebsiteGuide(req.ElectionRoundId, | ||
| req.Title, | ||
| new Uri(req.WebsiteUrl!)); | ||
|
|
||
| citizenGuideModel = new CitizenGuideModel | ||
| { | ||
| Id = citizenGuide.Id, | ||
| Title = citizenGuide.Title, | ||
| WebsiteUrl = citizenGuide.WebsiteUrl, | ||
| GuideType = citizenGuide.GuideType | ||
| }; | ||
| } | ||
|
|
||
| if (req.GuideType == CitizenGuideType.Text) | ||
| { | ||
| citizenGuide = CitizenGuide.NewTextGuide(req.ElectionRoundId, | ||
| req.Title, | ||
| req.Text!); | ||
|
|
||
| citizenGuideModel = new CitizenGuideModel | ||
| { | ||
| Id = citizenGuide.Id, | ||
| Title = citizenGuide.Title, | ||
| Text = citizenGuide.Text, | ||
| GuideType = citizenGuide.GuideType | ||
| }; | ||
| } | ||
|
|
||
| await repository.AddAsync(citizenGuide!, ct); | ||
| return TypedResults.Ok(citizenGuideModel!); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Vote.Monitor.Core.Security; | ||
| using Vote.Monitor.Domain.Entities.CitizenGuideAggregate; | ||
|
|
||
| namespace Feature.Citizen.Guides.Create; | ||
|
|
||
| public class Request | ||
| { | ||
| public Guid ElectionRoundId { get; set; } | ||
|
|
||
| [FromClaim(ApplicationClaimTypes.NgoId)] | ||
| public Guid NgoId { get; set; } | ||
| public string Title { get; set; } | ||
|
|
||
| public CitizenGuideType GuideType { get; set; } | ||
|
|
||
| [FromForm] | ||
| public IFormFile? Attachment { get; set; } | ||
|
|
||
| public string? WebsiteUrl { get; set; } | ||
| public string? Text { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| using Vote.Monitor.Core.Validators; | ||
| using Vote.Monitor.Domain.Entities.CitizenGuideAggregate; | ||
|
|
||
| namespace Feature.Citizen.Guides.Create; | ||
|
|
||
| public class Validator : Validator<Request> | ||
| { | ||
| public Validator() | ||
| { | ||
| RuleFor(x => x.ElectionRoundId).NotEmpty(); | ||
| RuleFor(x => x.Title).NotEmpty().MaximumLength(256); | ||
|
|
||
| RuleFor(x => x.Attachment) | ||
| .NotEmpty()! | ||
| .FileSmallerThan(50 * 1024 * 1024) // 50 MB upload limit | ||
| .When(x => x.GuideType == CitizenGuideType.Document); | ||
|
|
||
| RuleFor(x => x.WebsiteUrl) | ||
| .NotEmpty()! | ||
| .MaximumLength(2048) | ||
| .IsValidUri() | ||
| .When(x => x.GuideType == CitizenGuideType.Website); | ||
|
|
||
| RuleFor(x => x.Text) | ||
| .NotEmpty()! | ||
| .When(x => x.GuideType == CitizenGuideType.Text); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Dereferenced variable may be null