Beispiel #1
0
        public async Task ExecuteAsync(HookType type, IDomainEntityContext <Invoice> context, CancellationToken cancellationToken)
        {
            var isStatusPropertyDirty = context.EditMode == EditMode.Create ||
                                        context.EditMode == EditMode.Update && context.IsPropertyDirty(x => x.Status);

            if (isStatusPropertyDirty)
            {
                switch (context.Entity.Status)
                {
                case InvoiceStatus.Sent:
                    await context.AddEventAsync(InvoiceSentEvent.Create(_correlationIdProvider.Id, context.Entity.Id), cancellationToken);

                    break;

                case InvoiceStatus.Paid:
                    await context.AddEventAsync(InvoicePaidEvent.Create(_correlationIdProvider.Id, context.Entity.Id), cancellationToken);

                    break;

                case InvoiceStatus.Cancelled:
                    await context.AddEventAsync(InvoiceCancelledEvent.Create(_correlationIdProvider.Id, context.Entity.Id), cancellationToken);

                    break;
                }
            }
        }
        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);
        }
Beispiel #3
0
        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 async Task <IEnumerable <ValidationMessage> > ValidateAsync(IDomainEntityContext <Role> context, CancellationToken cancellationToken = default)
        {
            if (context.EditMode == EditMode.Delete)
            {
                return(ValidationResult.Ok());
            }

            if (context.IsPropertyDirty(x => x.Name))
            {
                var alreadyExists = await _roleQuery.AsQueryable()
                                    .Where(x => x.Id != context.Entity.Id)
                                    .Where(x => x.Name == context.Entity.Name)
                                    .AnyAsync(cancellationToken);

                if (alreadyExists)
                {
                    return(ValidationResult.Invalid($"A role with name '{context.Entity.Name}' already exists."));
                }
            }

            return(ValidationResult.Ok());
        }