Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions api/src/Feature.Forms/FetchAll/Endpoint.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Feature.Forms.Models;
using Microsoft.EntityFrameworkCore;
using Vote.Monitor.Core.Helpers;
using Vote.Monitor.Domain;
using Vote.Monitor.Domain.Entities.FormAggregate;
using Vote.Monitor.Domain.Entities.FormBase;
Expand Down Expand Up @@ -80,7 +79,9 @@ public override async Task<Results<Ok<NgoFormsResponseModel>, NotFound>> Execute
return TypedResults.Ok(new NgoFormsResponseModel
{
ElectionRoundId = monitoringNgo.ElectionRoundId,
Version = DeterministicGuid.Create(resultForms.Select(x => x.Id)).ToString(),
// The same key as forms:version, so it changes when a form's content changes, not only the set of forms.
Version = await ObserverFormsVersion.GetAsync(context, req.ElectionRoundId, monitoringNgo.MonitoringNgoId,
monitoringNgo.FormsVersion, ct),
Forms = resultForms.OrderBy(x => x.DisplayOrder).ThenBy(x => x.Code).ToList()
});
}
Expand Down
8 changes: 5 additions & 3 deletions api/src/Feature.Forms/GetFormsVersion/Endpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override void Configure()
Summary(s =>
{
s.Summary = "Gets current version of forms for users monitoring ngo";
s.Description = "Cache key changes every time any form changes";
s.Description = "Cache key changes every time any form the observer receives changes, coalition forms included";
});
}

Expand All @@ -28,7 +28,7 @@ public override async Task<Results<Ok<FormVersionResponseModel>, NotFound>> Exec
.Where(x => x.ElectionRoundId == req.ElectionRoundId)
.Where(x => x.Status == MonitoringObserverStatus.Active)
.Where(x => x.MonitoringNgo.ElectionRoundId == req.ElectionRoundId)
.Select(x => new { x.MonitoringNgo.FormsVersion, x.MonitoringNgo.ElectionRoundId })
.Select(x => new { x.MonitoringNgoId, x.MonitoringNgo.FormsVersion, x.MonitoringNgo.ElectionRoundId })
.FirstOrDefaultAsync(ct);

if (monitoringNgo is null)
Expand All @@ -38,7 +38,9 @@ public override async Task<Results<Ok<FormVersionResponseModel>, NotFound>> Exec

return TypedResults.Ok(new FormVersionResponseModel
{
ElectionRoundId = monitoringNgo.ElectionRoundId, CacheKey = monitoringNgo.FormsVersion.ToString()
ElectionRoundId = monitoringNgo.ElectionRoundId,
CacheKey = await ObserverFormsVersion.GetAsync(context, req.ElectionRoundId, monitoringNgo.MonitoringNgoId,
monitoringNgo.FormsVersion, ct)
});
}
}
43 changes: 43 additions & 0 deletions api/src/Feature.Forms/ObserverFormsVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.EntityFrameworkCore;
using Vote.Monitor.Core.Helpers;
using Vote.Monitor.Domain;
using Vote.Monitor.Domain.Entities.FormBase;

namespace Feature.Forms;

/// <summary>
/// The cache key of the forms an observer's monitoring NGO hands out (<c>forms:version</c> and <c>forms:fetchAll</c>).
/// An NGO's own <c>FormsVersion</c> changes whenever one of its forms changes, but in a coalition its observers also
/// receive forms owned by other NGOs (<see cref="Vote.Monitor.Domain.Entities.CoalitionAggregate.CoalitionFormAccess"/>):
/// the key therefore also follows those owners' versions and which forms are shared, so editing a published shared
/// form or granting access to one gives every receiving NGO a new key.
/// </summary>
public static class ObserverFormsVersion
{
public static async Task<string> GetAsync(VoteMonitorContext context, Guid electionRoundId, Guid monitoringNgoId,
Guid ownFormsVersion, CancellationToken ct)
{
var shared = await context.CoalitionFormAccess
.Where(x => x.MonitoringNgoId == monitoringNgoId && x.Coalition.ElectionRoundId == electionRoundId)
.Where(x => x.Form.FormType != FormType.CitizenReporting)
.Select(x => new { x.FormId, x.Form.MonitoringNgo.FormsVersion })
.AsNoTracking()
.ToListAsync(ct);

// Outside a coalition the key stays the NGO's own version, as before.
if (shared.Count == 0)
{
return ownFormsVersion.ToString();
}

var parts = shared
.Select(x => x.FormsVersion)
.Distinct()
.Order()
.Select(version => $"owner:{version}")
.Concat(shared.Select(x => x.FormId).Distinct().Order().Select(id => $"form:{id}"))
.Prepend($"own:{ownFormsVersion}");

return DeterministicGuid.Create(string.Join('|', parts)).ToString();
}
}
Loading