|
| 1 | +using Module.Answers.Models; |
| 2 | +using Vote.Monitor.Core.Services.FileStorage.Contracts; |
| 3 | + |
| 4 | +namespace Feature.Form.Submissions.GetByIdV2; |
| 5 | + |
| 6 | +public class Endpoint( |
| 7 | + IAuthorizationService authorizationService, |
| 8 | + INpgsqlConnectionFactory dbConnectionFactory, |
| 9 | + IFileStorageService fileStorageService) : Endpoint<Request, Results<Ok<FormSubmissionView>, NotFound>> |
| 10 | +{ |
| 11 | + public override void Configure() |
| 12 | + { |
| 13 | + Get("/api/election-rounds/{electionRoundId}/form-submissions/{submissionId}:v2"); |
| 14 | + DontAutoTag(); |
| 15 | + Options(x => x.WithTags("form-submissions")); |
| 16 | + Summary(s => { s.Summary = "Gets submission by id"; }); |
| 17 | + |
| 18 | + Policies(PolicyNames.NgoAdminsOnly); |
| 19 | + } |
| 20 | + |
| 21 | + public override async Task<Results<Ok<FormSubmissionView>, NotFound>> ExecuteAsync(Request req, |
| 22 | + CancellationToken ct) |
| 23 | + { |
| 24 | + var authorizationResult = |
| 25 | + await authorizationService.AuthorizeAsync(User, new MonitoringNgoAdminRequirement(req.ElectionRoundId)); |
| 26 | + if (!authorizationResult.Succeeded) |
| 27 | + { |
| 28 | + return TypedResults.NotFound(); |
| 29 | + } |
| 30 | + |
| 31 | + var sql = """ |
| 32 | + WITH submissions AS |
| 33 | + (SELECT psi."Id" AS "SubmissionId", |
| 34 | + 'PSI' AS "FormType", |
| 35 | + 'PSI' AS "FormCode", |
| 36 | + psi."PollingStationId", |
| 37 | + psi."MonitoringObserverId", |
| 38 | + psi."Answers", |
| 39 | + (SELECT "Id" |
| 40 | + FROM "PollingStationInformationForms" |
| 41 | + WHERE "ElectionRoundId" = @electionRoundId) AS "FormId", |
| 42 | + psi."FollowUpStatus" as "FollowUpStatus", |
| 43 | + '[]'::jsonb AS "Attachments", |
| 44 | + '[]'::jsonb AS "Notes", |
| 45 | + "LastUpdatedAt" AS "TimeSubmitted", |
| 46 | + psi."ArrivalTime", |
| 47 | + psi."DepartureTime", |
| 48 | + psi."Breaks", |
| 49 | + psi."IsCompleted" |
| 50 | + FROM "PollingStationInformation" psi |
| 51 | + INNER JOIN "GetAvailableMonitoringObservers"(@electionRoundId, @ngoId, 'Coalition') AMO on AMO."MonitoringObserverId" = psi."MonitoringObserverId" |
| 52 | + WHERE psi."Id" = @submissionId and psi."ElectionRoundId" = @electionRoundId |
| 53 | + UNION ALL |
| 54 | + SELECT |
| 55 | + fs."Id" AS "SubmissionId", |
| 56 | + f."FormType" AS "FormType", |
| 57 | + f."Code" AS "FormCode", |
| 58 | + fs."PollingStationId", |
| 59 | + fs."MonitoringObserverId", |
| 60 | + fs."Answers", |
| 61 | + f."Id" AS "FormId", |
| 62 | + fs."FollowUpStatus", |
| 63 | + COALESCE((select jsonb_agg(jsonb_build_object('QuestionId', "QuestionId", 'FileName', "FileName", 'MimeType', "MimeType", 'FilePath', "FilePath", 'UploadedFileName', "UploadedFileName", 'TimeSubmitted', "LastUpdatedAt")) |
| 64 | + FROM "Attachments" a |
| 65 | + WHERE |
| 66 | + a."ElectionRoundId" = @electionRoundId |
| 67 | + AND a."FormId" = fs."FormId" |
| 68 | + AND a."MonitoringObserverId" = fs."MonitoringObserverId" |
| 69 | + AND a."IsDeleted" = false AND a."IsCompleted" = true |
| 70 | + AND fs."PollingStationId" = a."PollingStationId"),'[]'::JSONB) AS "Attachments", |
| 71 | + |
| 72 | + COALESCE((select jsonb_agg(jsonb_build_object('QuestionId', "QuestionId", 'Text', "Text", 'TimeSubmitted', "LastUpdatedAt")) |
| 73 | + FROM "Notes" n |
| 74 | + WHERE |
| 75 | + n."ElectionRoundId" = @electionRoundId |
| 76 | + AND n."FormId" = fs."FormId" |
| 77 | + AND n."MonitoringObserverId" = fs."MonitoringObserverId" |
| 78 | + AND fs."PollingStationId" = n."PollingStationId"), '[]'::JSONB) AS "Notes", |
| 79 | + |
| 80 | + "LastUpdatedAt" AS "TimeSubmitted", |
| 81 | + NULL AS "ArrivalTime", |
| 82 | + NULL AS "DepartureTime", |
| 83 | + '[]'::jsonb AS "Breaks", |
| 84 | + fs."IsCompleted" |
| 85 | + FROM "FormSubmissions" fs |
| 86 | + INNER JOIN "GetAvailableMonitoringObservers"(@electionRoundId, @ngoId, 'Coalition') AMO on AMO."MonitoringObserverId" = FS."MonitoringObserverId" |
| 87 | + INNER JOIN "Forms" f ON f."Id" = fs."FormId" |
| 88 | + WHERE fs."Id" = @submissionId and fs."ElectionRoundId" = @electionRoundId) |
| 89 | + SELECT s."SubmissionId", |
| 90 | + s."FormId", |
| 91 | + s."TimeSubmitted", |
| 92 | + s."FormCode", |
| 93 | + s."FormType", |
| 94 | + ps."Id" AS "PollingStationId", |
| 95 | + ps."Level1", |
| 96 | + ps."Level2", |
| 97 | + ps."Level3", |
| 98 | + ps."Level4", |
| 99 | + ps."Level5", |
| 100 | + ps."Number", |
| 101 | + s."MonitoringObserverId", |
| 102 | + AMO."DisplayName" "ObserverName", |
| 103 | + AMO."Email", |
| 104 | + AMO."PhoneNumber", |
| 105 | + AMO."Tags", |
| 106 | + AMO."NgoName", |
| 107 | + AMO."IsOwnObserver", |
| 108 | + s."Attachments", |
| 109 | + s."Notes", |
| 110 | + s."Answers", |
| 111 | + s."FollowUpStatus", |
| 112 | + s."ArrivalTime", |
| 113 | + s."DepartureTime", |
| 114 | + s."Breaks", |
| 115 | + s."IsCompleted" |
| 116 | + FROM submissions s |
| 117 | + INNER JOIN "PollingStations" ps ON ps."Id" = s."PollingStationId" |
| 118 | + INNER JOIN "GetAvailableMonitoringObservers"(@electionRoundId, @ngoId, 'Coalition') AMO on AMO."MonitoringObserverId" = s."MonitoringObserverId" |
| 119 | + """; |
| 120 | + |
| 121 | + var queryArgs = new |
| 122 | + { |
| 123 | + electionRoundId = req.ElectionRoundId, ngoId = req.NgoId, submissionId = req.SubmissionId |
| 124 | + }; |
| 125 | + |
| 126 | + FormSubmissionView submission = null; |
| 127 | + |
| 128 | + using (var dbConnection = await dbConnectionFactory.GetOpenConnectionAsync(ct)) |
| 129 | + { |
| 130 | + submission = await dbConnection.QueryFirstOrDefaultAsync<FormSubmissionView>(sql, queryArgs); |
| 131 | + } |
| 132 | + |
| 133 | + if (submission is null) |
| 134 | + { |
| 135 | + return TypedResults.NotFound(); |
| 136 | + } |
| 137 | + |
| 138 | + submission = submission with |
| 139 | + { |
| 140 | + Attachments = await Task.WhenAll( |
| 141 | + submission.Attachments.Select(async attachment => |
| 142 | + { |
| 143 | + var result = |
| 144 | + await fileStorageService.GetPresignedUrlAsync(attachment.FilePath, attachment.UploadedFileName); |
| 145 | + return result is GetPresignedUrlResult.Ok(var url, _, var urlValidityInSeconds) |
| 146 | + ? attachment with { PresignedUrl = url, UrlValidityInSeconds = urlValidityInSeconds } |
| 147 | + : attachment; |
| 148 | + }) |
| 149 | + ) |
| 150 | + }; |
| 151 | + |
| 152 | + return TypedResults.Ok(submission); |
| 153 | + } |
| 154 | +} |
0 commit comments