Skip to content
Merged
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
17 changes: 13 additions & 4 deletions src/DaemonTests/Composites/multi_stage_projections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ private async Task startAppointments()

var boards = await _compositeSession.Query<Board>().ToListAsync();

var i = 0;
foreach (var patient in patients)
{
var requested = new AppointmentRequested(patient.Id, faker.PickRandom(states),
Expand All @@ -150,14 +151,20 @@ private async Task startAppointments()
var board = boards.FirstOrDefault(x =>
x.StateCodes.Contains(requested.StateCode) && x.SpecialtyCodes.Contains(requested.SpecialtyCode));

if (board != null)
var appointmentId = Guid.NewGuid();
List<object> events = [requested];
if (i % 2 == 0)
{
_compositeSession.Events.StartStream<Appointment>(requested, new AppointmentRouted(board.Id));
events.Add(new AppointmentExternalIdentifierAssigned(appointmentId, Guid.NewGuid()));
}
else
if (board != null)
{
_compositeSession.Events.StartStream<Appointment>(requested);
events.Add(new AppointmentRouted(board.Id));
}

_compositeSession.Events.StartStream<Appointment>(appointmentId, events);

i++;
}

await _compositeSession.SaveChangesAsync();
Expand Down Expand Up @@ -200,6 +207,7 @@ public async Task end_to_end(TenancyStyle tenancyStyle)
// 2nd stage projections
projection.Add<AppointmentDetailsProjection>(2);
projection.Add<BoardSummaryProjection>(2);
projection.Add<AppointmentByExternalIdentifierProjection>(2);
});

#endregion
Expand Down Expand Up @@ -235,6 +243,7 @@ public async Task end_to_end(TenancyStyle tenancyStyle)

// Got details from the 2nd stage projection!
(await _compositeSession.Query<AppointmentDetails>().CountAsync()).ShouldBeGreaterThan(0);
(await _compositeSession.Query<AppointmentByExternalIdentifier>().CountAsync()).ShouldBeGreaterThan(0);

// See the downstream BoardSummary too!
(await _compositeSession.Query<BoardSummary>().CountAsync()).ShouldBeGreaterThan(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using JasperFx.Events;
using JasperFx.Events.Grouping;
using Marten;
using Marten.Events.Projections;
using Marten.Schema;

namespace DaemonTests.TeleHealth;

public class AppointmentByExternalIdentifierProjection : MultiStreamProjection<AppointmentByExternalIdentifier, Guid>
{
public AppointmentByExternalIdentifierProjection()
{
Options.CacheLimitPerTenant = 1000;

Identity<AppointmentExternalIdentifierAssigned>(x => x.ExternalId);
}

public override async Task EnrichEventsAsync(SliceGroup<AppointmentByExternalIdentifier, Guid> group,
IQuerySession querySession, CancellationToken cancellation)
{
await group
.EnrichWith<Appointment>() // should be fetched from cache or from store
.ForEvent<AppointmentExternalIdentifierAssigned>()
.ForEntityId(x => x.AppointmentId)
.EnrichAsync((slice, @event, appointment) =>
{
slice.ReplaceEvent(@event, new EnrichedExternalIdentifierAssigned(@event.Data, appointment));
});
}

private sealed record EnrichedExternalIdentifierAssigned(AppointmentExternalIdentifierAssigned Assigned, Appointment Appointment);

public override AppointmentByExternalIdentifier Evolve(AppointmentByExternalIdentifier snapshot, Guid id, IEvent e) =>
e.Data switch
{
EnrichedExternalIdentifierAssigned enriched => new()
{
ExternalIdentifier = enriched.Assigned.ExternalId,
AppointmentId = enriched.Assigned.AppointmentId,
SpecialtyCode = enriched.Appointment.SpecialtyCode
},
_ => snapshot
};
}

public class AppointmentByExternalIdentifier
{
[Identity]
public required Guid ExternalIdentifier { get; set; }
public required Guid AppointmentId { get; set; }

public required string SpecialtyCode { get; set; }
}
1 change: 0 additions & 1 deletion src/DaemonTests/TeleHealth/AppointmentDetailsProjection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using JasperFx.Events;
Expand Down
1 change: 1 addition & 0 deletions src/DaemonTests/TeleHealth/Appointments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace DaemonTests.TeleHealth;

public record AppointmentRequested(Guid PatientId, string StateCode, string SpecialtyCode);
public record AppointmentRouted(Guid BoardId);
public record AppointmentExternalIdentifierAssigned(Guid AppointmentId, Guid ExternalId);
public record ProviderAssigned( Guid ProviderId);
public record AppointmentStarted;
public record AppointmentCompleted;
Expand Down
Loading