Skip to content

Commit

Permalink
Improved NewsletterFlow and OrderFlow samples
Browse files Browse the repository at this point in the history
  • Loading branch information
stidsborg committed Apr 28, 2024
1 parent f60914c commit 09e44c1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

namespace Cleipnir.Flows.Sample.Presentation.A_NewsletterSender.Solution;

public class NewsletterFlow : Flow<MailAndRecipients>
public class NewsletterFlow : Flow<MailAndRecipients>, IHaveState<NewsletterFlow.FlowState>
{
public required FlowState State { get; init; }

public override async Task Run(MailAndRecipients mailAndRecipients)
{
var (recipients, subject, content) = mailAndRecipients;
using var client = new SmtpClient();
await client.ConnectAsync("mail.smtpbucket.com", 8025);

var state = Workflow.States.CreateOrGet<State>();

for (var atRecipient = state.AtRecipient; atRecipient < mailAndRecipients.Recipients.Count; atRecipient++)
for (var atRecipient = State.AtRecipient; atRecipient < mailAndRecipients.Recipients.Count; atRecipient++)
{
var recipient = recipients[atRecipient];
var message = new MimeMessage();
Expand All @@ -26,12 +26,12 @@ public override async Task Run(MailAndRecipients mailAndRecipients)
message.Body = new TextPart(TextFormat.Html) { Text = content };
await client.SendAsync(message);

state.AtRecipient = atRecipient;
await state.Save();
State.AtRecipient = atRecipient;
await State.Save();
}
}

private class State : WorkflowState
public class FlowState : WorkflowState
{
public int AtRecipient { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ public record OrderConfirmationEmailSent(string OrderId, Guid CustomerId) : Even
public record ReserveFunds(string OrderId, decimal Amount, Guid TransactionId, Guid CustomerId) : EventsAndCommands;
public record FundsReserved(string OrderId) : EventsAndCommands;
public record ShipProducts(string OrderId, Guid CustomerId, IEnumerable<Guid> ProductIds) : EventsAndCommands;
public record ProductsShipped(string OrderId) : EventsAndCommands;
public record ProductsShipped(string OrderId, string TrackAndTrace) : EventsAndCommands;
public record ProductsShipmentFailed(string OrderId) : EventsAndCommands;

public record SendOrderConfirmationEmail(string OrderId, Guid CustomerId) : EventsAndCommands;
public record SendOrderConfirmationEmail(string OrderId, Guid CustomerId, string TrackAndTrace) : EventsAndCommands;

public record CaptureFunds(string OrderId, Guid CustomerId, Guid TransactionId) : EventsAndCommands;
public record FundsCaptured(string OrderId) : EventsAndCommands;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ private async Task MessageHandler(EventsAndCommands message)
return;

await Task.Delay(1_000);
await _messageBroker.Send(new ProductsShipped(command.OrderId));
await _messageBroker.Send(new ProductsShipped(command.OrderId, TrackAndTrace: Guid.NewGuid().ToString()));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Cleipnir.ResilientFunctions.Reactive.Extensions;
using Cleipnir.ResilientFunctions.Helpers;
using Cleipnir.ResilientFunctions.Reactive.Extensions;
using Serilog;
using ILogger = Serilog.ILogger;

Expand All @@ -18,24 +19,14 @@ public override async Task Run(Order order)
await Messages.FirstOfType<FundsReserved>();

await messageBroker.Send(new ShipProducts(order.OrderId, order.CustomerId, order.ProductIds));

await Messages.FirstOfType<ProductsShipped>();
var trackAndTrace = await Messages.FirstOfType<ProductsShipped>().SelectAsync(msg => msg.TrackAndTrace);

await messageBroker.Send(new CaptureFunds(order.OrderId, order.CustomerId, transactionId));
await Messages.FirstOfType<FundsCaptured>();

await messageBroker.Send(new SendOrderConfirmationEmail(order.OrderId, order.CustomerId));
await messageBroker.Send(new SendOrderConfirmationEmail(order.OrderId, order.CustomerId, trackAndTrace));
await Messages.FirstOfType<OrderConfirmationEmailSent>();

Logger.Information($"Processing of order '{order.OrderId}' completed");
}

/*
var either = await Messages.OfTypes<ProductsShipped, ProductsShipmentFailed>().First();
if (either.HasSecond)
{
await _messageBroker.Send(new CancelFundsReservation(order.OrderId, transactionId));
return;
}
*/
}

0 comments on commit 09e44c1

Please sign in to comment.