public async Task ExecuteAsync(HookType type, IDomainEntityContext <Invoice> context, CancellationToken cancellationToken) { if (type == HookType.BeforeCreate) { // An invoice always has an accompanying PDF document. We can create this by requesting synchronization via a domain event. context.Entity.PdfIsSynced = false; await context.AddMessageAsync(SynchronizeInvoicePdfMessage.Create(_correlationIdProvider.Id, context.Entity.Id), cancellationToken); } else if (type == HookType.BeforeUpdate && context.Pristine.Status == InvoiceStatus.Draft) { var invoiceToPdfModel = await _invoiceToPdfModelMapper.MapAsync(context.Entity, cancellationToken); var checksum = invoiceToPdfModel.GetChecksum(); // Compare calculated checksum against checksum when PDF was last generated if (!string.Equals(checksum, context.Entity.PdfChecksum)) { context.State.TryGet(InvoiceStateKeys.ThrowIfPdfIsNotSynced, out bool throwIfPdfIsNotSynced); if (throwIfPdfIsNotSynced) { // Prevent a possible infinite loop by not allowing synchronization when in the context of // updating the invoice directly after PDF synchronization has occured. In this scenario the // checksums should have been equal. throw new DomainException("Expected invoice PDF to have been synced"); } // Changes made to entity DO affect PDF content. Requesting synchronization. context.Entity.PdfIsSynced = false; await context.AddMessageAsync(SynchronizeInvoicePdfMessage.Create(_correlationIdProvider.Id, context.Entity.Id), cancellationToken); } else { // Changes made to entity DIDNT affect PDF content. Not requesting synchronization. context.Entity.PdfIsSynced = true; } } if (context.Entity.PdfIsSynced && context.IsPropertyDirty(x => x.PdfIsSynced)) { await context.AddEventAsync(InvoicePdfSynchronizedEvent.Create(_correlationIdProvider.Id, context.Entity.Id), cancellationToken); } }
public Task ExecuteAsync(HookType type, IDomainEntityContext <User> context, CancellationToken cancellationToken) { if (context.IsPropertyDirty(x => x.Email)) { context.AddMessageAsync(SyncEmailToAuth0UserMessage.Create(_correlationIdProvider.Id, context.Entity.Id), cancellationToken); } if (context.IsPropertyDirty(x => x.Fullname)) { context.AddMessageAsync(SyncNameToAuth0UserMessage.Create(_correlationIdProvider.Id, context.Entity.Id), cancellationToken); } if (!Enumerable.SequenceEqual( context.Entity.UserRoles.Select(x => x.RoleId), context.Pristine.UserRoles.Select(x => x.RoleId) )) { context.AddMessageAsync(SyncRolesToAuth0UserMessage.Create(_correlationIdProvider.Id, context.Entity.Id), cancellationToken); } return(Task.CompletedTask); }
public Task ExecuteAsync(HookType type, IDomainEntityContext <User> context, CancellationToken cancellationToken) { context.AddMessageAsync(DeleteAuth0UserMessage.Create(_correlationIdProvider.Id, context.Entity.Id), cancellationToken); return(Task.CompletedTask); }