Ejemplo n.º 1
0
        private AccountState ApplyEvent(TaxAppliedDuringBilling occurred)
        {
            var trans = new FinancialTransaction(new Tax {
                Amount = (double)occurred.TaxAmountApplied
            },
                                                 (double)occurred.TaxAmountApplied);

            Obligations[occurred.ObligationNumber]?.PostTransaction(trans);
            var newState = new AccountState(
                AccountNumber,
                CurrentBalance + occurred.TaxAmountApplied,
                AccountStatus,
                Obligations,
                SimulatedFields,
                AuditLog.Add(new StateLog(
                                 "TaxAppliedDuringBilling",
                                 occurred.Message + " Balance After: " + (CurrentBalance + occurred.TaxAmountApplied).ToString("C"),
                                 occurred.UniqueGuid(),
                                 occurred.OccurredOn())),
                OpeningBalance,
                Inventroy,
                UserName,
                LastPaymentAmount,
                LastPaymentDate);

            return(newState);
        }
Ejemplo n.º 2
0
        private AccountState ApplyEvent(ObligationAssessedConcept occurred)
        {
            if (!Obligations.ContainsKey(occurred.ObligationNumber))
            {
                Console.WriteLine(
                    $"Account {AccountNumber} does not contain obligation {occurred.ObligationNumber}. No Action Taken.");
                throw new Exception(
                          $"Account {AccountNumber} does not contain obligation {occurred.ObligationNumber}. No Action Taken.");
            }

            var trans = new FinancialTransaction(occurred.FinancialBucket, occurred.FinancialBucket.Amount);

            Obligations[occurred.ObligationNumber].PostTransaction(trans);
            var newBalance = CurrentBalance + decimal.Parse(occurred.FinancialBucket.Amount.ToString());
            var newState   = new AccountState(
                AccountNumber,
                newBalance,
                AccountStatus,
                Obligations,
                SimulatedFields,
                AuditLog.Add(
                    new StateLog(
                        "ObligationAssessedConcept",
                        occurred.Message + " Balance After: " + ((decimal)newBalance).ToString("C"),
                        occurred.UniqueGuid(),
                        occurred.OccurredOn())
                    ),
                OpeningBalance,
                Inventroy,
                UserName,
                LastPaymentAmount,
                LastPaymentDate);

            return(newState);
        }
Ejemplo n.º 3
0
 public AccountState Event(SomeOneSaidHiToMe occurred)
 {
     return(new AccountState(this.AccountNumber, this.CurrentBalance,
                             this.AccountStatus, this.Obligations,
                             LoadSimulation().ToImmutableDictionary(),
                             AuditLog.Add(new StateLog("SomeOneSaidHiToMe", occurred.UniqueGuid(), occurred.OccurredOn()))));
 }
Ejemplo n.º 4
0
        private AccountState ApplyEvent(AccountCurrentBalanceUpdated occurred)
        {
            var adjustmentsObligation = new MaintenanceFee("AccountAdjustments", 0, ObligationStatus.Boarding);

            if (Obligations.ContainsKey("AccountAdjustments"))
            {
                adjustmentsObligation = Obligations["AccountAdjustments"];
            }

            var trans = new FinancialTransaction(new Adjustment(occurred.CurrentBalance), occurred.CurrentBalance);

            adjustmentsObligation.PostTransaction(trans);


            return(new AccountState(
                       AccountNumber,
                       decimal.Parse(occurred.CurrentBalance.ToString()),
                       AccountStatus,
                       Obligations.Add(adjustmentsObligation.ObligationNumber, adjustmentsObligation),
                       SimulatedFields,
                       AuditLog.Add(new StateLog("AccountCurrentBalanceUpdated", "Boarding Adjustment", occurred.UniqueGuid(),
                                                 occurred.OccurredOn())),
                       OpeningBalance,
                       Inventroy,
                       UserName,
                       LastPaymentAmount,
                       LastPaymentDate));
        }
Ejemplo n.º 5
0
 public AccountState Event(AccountCancelled occurred)
 {
     return(new AccountState(this.AccountNumber, this.CurrentBalance,
                             occurred.AccountStatus, this.Obligations,
                             SimulatedFields,
                             AuditLog.Add(new StateLog("AccountCancelled", occurred.UniqueGuid(), occurred.OccurredOn()))));
 }
Ejemplo n.º 6
0
 public AccountState Event(SuperSimpleSuperCoolEventFoundByRules occurred)
 {
     return(new AccountState(this.AccountNumber, this.CurrentBalance,
                             this.AccountStatus, this.Obligations,
                             LoadSumulation(SimulatedFields, "1", "My state has been updated, see..."),
                             AuditLog.Add(new StateLog("SuperSimpleSuperCoolEventFoundByRules", occurred.UniqueGuid(), occurred.OccurredOn()))));
 }
Ejemplo n.º 7
0
        private AccountState ApplyEvent(UacAppliedAfterBilling occurred)
        {
            var trans = new FinancialTransaction(new Tax {
                Amount = occurred.UacAmountApplied
            },
                                                 occurred.UacAmountApplied);

            Obligations[occurred.ObligationNumber]?.PostTransaction(trans);
            var newBal   = CurrentBalance + decimal.Parse((-1 * occurred.UacAmountApplied).ToString());
            var newState = new AccountState(
                AccountNumber,
                newBal,
                AccountStatus,
                Obligations,
                SimulatedFields,
                AuditLog.Add(new StateLog(
                                 "UacAppliedAfterBilling",
                                 $"{occurred.Message} Balance After: {newBal:C}",
                                 occurred.UniqueGuid(),
                                 occurred.OccurredOn())),
                OpeningBalance,
                Inventroy,
                UserName,
                LastPaymentAmount,
                LastPaymentDate
                );


            return(newState);
        }
Ejemplo n.º 8
0
        private Task OnAfterSaveChanges(List <AuditEntry> auditEntries)
        {
            if (auditEntries == null || auditEntries.Count == 0)
            {
                return(Task.CompletedTask);
            }

            foreach (var auditEntry in auditEntries)
            {
                // Get the final value of the temporary properties
                foreach (var prop in auditEntry.TemporaryProperties)
                {
                    if (prop.Metadata.IsPrimaryKey())
                    {
                        auditEntry.KeyValues[prop.Metadata.Name] = prop.CurrentValue;
                    }
                    else
                    {
                        auditEntry.NewValues[prop.Metadata.Name] = prop.CurrentValue;
                    }
                }

                // Save the Audit entry
                AuditLog.Add(auditEntry.ToAudit());
            }

            return(SaveChangesAsync());
        }
Ejemplo n.º 9
0
        public async Task <int> SaveChangesWithAuditAsync(string adminUserId = null)
        {
            Guid transactionId = Guid.NewGuid();
            var  changed       = ChangeTracker.Entries()
                                 .Where(e => e.State == EntityState.Modified);

            Guid?adminGuid = string.IsNullOrEmpty(adminUserId) ? default(Guid?) : new Guid(adminUserId);

            foreach (DbEntityEntry entry in changed)
            {
                Type entityType = ObjectContext.GetObjectType(entry.Entity.GetType());
                if (!typeof(IAuditable).IsAssignableFrom(entityType))
                {
                    break;
                }

                string userId = entry.OriginalValues["Id"].ToString();
                Guid   _;
                if (!Guid.TryParse(userId, out _))
                {
                    userId = entry.OriginalValues["UserId"].ToString();
                }

                PropertyInfo[] fullProperties      = entityType.GetProperties();
                var            interfaceProperties = entityType.GetProperties().Where(p => p.IsDefined(typeof(AuditableAttribute), false));

                if (interfaceProperties.Any())
                {
                    foreach (var prop in interfaceProperties)
                    {
                        var originalValue = entry.OriginalValues[prop.Name];
                        var newValue      = entry.CurrentValues[prop.Name];

                        var customAttributes = prop.GetCustomAttribute <AuditableAttribute>();

                        if (customAttributes.LogAlways || !Equals(originalValue, newValue))
                        {
                            string auditLog = customAttributes.Obfuscate
                                ? $"{prop.Name} was updated"
                                : $"{prop.Name} was updated from {originalValue} to {newValue}";

                            AuditLog.Add(new Entity.ActivityLog
                            {
                                DateCreated = DateTime.UtcNow,
                                TxId        = transactionId,
                                Entity      = entityType.Name,
                                Property    = prop.Name,
                                OldValue    = customAttributes.Obfuscate ? null : originalValue.ToString(),
                                NewValue    = customAttributes.Obfuscate ? null : newValue.ToString(),
                                UserId      = new Guid(userId),
                                AdminUserId = adminGuid
                            });
                        }
                    }
                }
            }

            return(await base.SaveChangesAsync());
        }
Ejemplo n.º 10
0
 public AccountState Event(ObligationAddedToAccount occurred)
 {
     return(new AccountState(this.AccountNumber, this.CurrentBalance,
                             this.AccountStatus,
                             this.Obligations.Add(occurred.Obligation.ObligationNumber, occurred.Obligation),
                             SimulatedFields,
                             AuditLog.Add(new StateLog("ObligationAddedToAccount", occurred.UniqueGuid(), occurred.OccurredOn()))));
 }
Ejemplo n.º 11
0
        public AccountState Event(ObligationSettledConcept occurred)
        {
            var trans = new FinancialTransaction(occurred.FinancialConcept, occurred.Amount);

            this.Obligations[occurred.ObligationNumber]?.PostTransaction(trans);
            return(new AccountState(this.AccountNumber, this.CurrentBalance,
                                    this.AccountStatus, this.Obligations,
                                    SimulatedFields,
                                    AuditLog.Add(new StateLog("ObligationSettledConcept", occurred.UniqueGuid(), occurred.OccurredOn()))));
        }
Ejemplo n.º 12
0
 private AccountState ApplyEvent(AccountCancelled occurred)
 {
     return(new AccountState(AccountNumber,
                             CurrentBalance,
                             occurred.AccountStatus, Obligations,
                             SimulatedFields,
                             AuditLog.Add(new StateLog("AccountCancelled", occurred.Message, occurred.UniqueGuid(),
                                                       occurred.OccurredOn()))
                             , OpeningBalance,
                             Inventroy,
                             UserName,
                             LastPaymentAmount,
                             LastPaymentDate));
 }
Ejemplo n.º 13
0
 private AccountState ApplyEvent(SomeOneSaidHiToMe occurred)
 {
     return(new AccountState(
                AccountNumber,
                CurrentBalance,
                AccountStatus,
                Obligations,
                LoadSimulation().ToImmutableDictionary(),
                AuditLog.Add(new StateLog("SomeOneSaidHiToMe", occurred.Message, occurred.UniqueGuid(),
                                          occurred.OccurredOn())),
                OpeningBalance,
                Inventroy,
                UserName,
                LastPaymentAmount,
                LastPaymentDate));
 }
Ejemplo n.º 14
0
 private AccountState ApplyEvent(ObligationAddedToAccount occurred)
 {
     return(new AccountState(
                AccountNumber,
                CurrentBalance,
                AccountStatus,
                Obligations.Add(occurred.MaintenanceFee.ObligationNumber, occurred.MaintenanceFee),
                SimulatedFields,
                AuditLog.Add(new StateLog("ObligationAddedToAccount", occurred.Message, occurred.UniqueGuid(),
                                          occurred.OccurredOn()))
                , OpeningBalance,
                Inventroy,
                UserName,
                LastPaymentAmount,
                LastPaymentDate));
 }
Ejemplo n.º 15
0
 private AccountState ApplyEvent(SuperSimpleSuperCoolDomainEventFoundByRules occurred)
 {
     return(new AccountState(
                AccountNumber,
                CurrentBalance,
                AccountStatus, Obligations,
                LoadSumulation(SimulatedFields, "1", "My state has been updated, see..."),
                AuditLog.Add(new StateLog("SuperSimpleSuperCoolEventFoundByRules", occurred.Message,
                                          occurred.UniqueGuid(),
                                          occurred.OccurredOn())),
                OpeningBalance,
                Inventroy,
                UserName,
                LastPaymentAmount,
                LastPaymentDate));
 }
Ejemplo n.º 16
0
 private AccountState ApplyEvent(AccountBusinessRuleValidationSuccess occurred)
 {
     return(new AccountState(
                AccountNumber,
                CurrentBalance,
                AccountStatus,
                Obligations,
                SimulatedFields,
                AuditLog.Add(
                    new StateLog(
                        "AccountBusinessRuleValidationSuccess",
                        $"{occurred.Message}",
                        occurred.UniqueGuid(),
                        occurred.OccurredOn()
                        )
                    ),
                OpeningBalance,
                Inventroy,
                UserName,
                LastPaymentAmount,
                LastPaymentDate
                ));
 }
Ejemplo n.º 17
0
        private AccountState ApplyEvent(AccountCreated occurred)
        {
            var newState = new AccountState(
                occurred.AccountNumber,
                accountStatus: AccountStatus.Boarded,
                obligations: ImmutableDictionary.Create <string, MaintenanceFee>(),
                simulation: LoadSimulation(),
                log: AuditLog.Add(
                    new StateLog("AccountCreated",
                                 occurred.Message + " Balance After: " + ((decimal)occurred.OpeningBalance).ToString("C"),
                                 occurred.UniqueGuid(),
                                 occurred.OccurredOn()
                                 )
                    ),
                openingBalance: occurred.OpeningBalance,
                currentBalance: 0, //best to affect currBal explicitly with an event
                inventory: occurred.Inventory,
                userName: occurred.UserName,
                lastPaymentAmount: occurred.LastPaymentAmount,
                lastPaymentDate: occurred.LastPaymentDate
                );

            return(newState);
        }
Ejemplo n.º 18
0
        private AccountState ApplyEvent(PaymentAppliedToObligation occurred)
        {
            var trans = new FinancialTransaction(occurred.FinancialBucket, occurred.Amount);

            Obligations[occurred.ObligationNumber].PostTransaction(trans);
            decimal newBal = CurrentBalance - decimal.Parse(occurred.Amount.ToString());

            return(new AccountState(
                       AccountNumber,
                       newBal,
                       AccountStatus,
                       Obligations,
                       SimulatedFields,
                       AuditLog.Add(new StateLog(
                                        "PaymentAppliedToObligation",
                                        $"{occurred.Message} Balance After: {newBal :C}",
                                        occurred.UniqueGuid(),
                                        occurred.OccurredOn()))
                       , OpeningBalance
                       , Inventroy
                       , UserName
                       , lastPaymentAmount: occurred.Amount
                       , lastPaymentDate: DateTime.Today));
        }
Ejemplo n.º 19
0
        private List <AuditEntry> OnBeforeSaveChanges()
        {
            ChangeTracker.DetectChanges();
            var auditEntries = new List <AuditEntry>();

            foreach (var entry in ChangeTracker.Entries())
            {
                if (entry.Entity is AuditLog || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged)
                {
                    continue;
                }

                var auditEntry = new AuditEntry(entry)
                {
                    TableName = entry.Metadata.Relational().TableName
                };
                auditEntries.Add(auditEntry);

                foreach (var property in entry.Properties)
                {
                    if (property.IsTemporary)
                    {
                        // value will be generated by the database, get the value after saving
                        auditEntry.TemporaryProperties.Add(property);
                        continue;
                    }

                    string propertyName = property.Metadata.Name;
                    if (property.Metadata.IsPrimaryKey())
                    {
                        auditEntry.KeyValues[propertyName] = property.CurrentValue;
                        continue;
                    }

                    switch (entry.State)
                    {
                    case EntityState.Added:
                        auditEntry.NewValues[propertyName] = property.CurrentValue;
                        break;

                    case EntityState.Deleted:
                        auditEntry.OldValues[propertyName] = property.OriginalValue;
                        break;

                    case EntityState.Modified:
                        if (property.IsModified)
                        {
                            auditEntry.OldValues[propertyName] = property.OriginalValue;
                            auditEntry.NewValues[propertyName] = property.CurrentValue;
                        }
                        break;
                    }
                }
            }
            // Save audit entities that have all the modifications
            foreach (var auditEntry in auditEntries.Where(_ => !_.HasTemporaryProperties))
            {
                AuditLog.Add(auditEntry.ToAudit());
            }

            // keep a list of entries where the value of some properties are unknown at this step
            return(auditEntries.Where(_ => _.HasTemporaryProperties).ToList());
        }
Ejemplo n.º 20
0
 public AccountState Event(AccountCreated occurred)
 {
     return(new AccountState(occurred.AccountNumber,
                             LoadSimulation(),
                             AuditLog.Add(new StateLog("AccountCreated", occurred.UniqueGuid(), occurred.OccurredOn()))));
 }