private void DoPersistTransaction(PersistTransaction req) { using (var db = new BillingwareDataContext()) { var t = new Transaction { AccountNumber = req.AccountNumber, Amount = req.Amount, TransactionType = req.TransactionType, Reference = req.Reference, BalanceAfter = req.BalanceAfter, BalanceBefore = req.BalanceBefore, ClientId = req.ClientId, ConditionFailReason = req.ConditionFailReason, CreatedAt = req.CreatedAt, Honoured = req.Honoured, Narration = req.Narration, SatisfiedCondition = req.SatisfiedCondition, Ticket = req.Ticket }; //quick fix if (t.TransactionType == TransactionType.Debit) { //if balance = 100, amount=10 => after=90 and before=100 var sumBefore = t.BalanceAfter + t.Amount; if (t.BalanceBefore != sumBefore) { t.BalanceBefore = t.BalanceAfter + (t.Amount); } } else { //if balance = 90, amount=10 => after=100 and before=90 var sumBefore = t.BalanceAfter - t.Amount; if (t.BalanceBefore != sumBefore) { t.BalanceBefore = t.BalanceAfter - (t.Amount); } } db.Transactions.Add(t); db.SaveChanges(); } //increase cache items var typeKey = req.TransactionType == TransactionType.Credit ? "credit" : "debit"; _tranactionsStatsCache.Increase($"count", int.Parse("1")); _tranactionsStatsCache.Increase($"count.{typeKey}", int.Parse("1")); _tranactionsStatsCache.Increase($"count.{typeKey}.{req.AccountNumber}", int.Parse("1")); _tranactionsStatsCache.Increase($"sum.{typeKey}", req.Amount); _tranactionsStatsCache.Increase($"sum.{typeKey}.{req.AccountNumber}", req.Amount); }
private void DoCreditAccountEvent(CreditAccount detail) { using (var db = new BillingwareDataContext()) { var account = db.Accounts.FirstOrDefault(a => a.AccountNumber == detail.Request.AccountNumber); if (account == null) { return; } account.Balance += detail.Request.Amount; db.Entry(account).State = EntityState.Modified; db.SaveChanges(); } }
private void DoCreateAccount(CreateAccount req) { var db = new BillingwareDataContext(); db.Accounts.Add(new Account { AccountNumber = req.AccountNumber, Balance = decimal.Zero, Extra = req.Extra, Alias = req.Alias, CreatedAt = DateTime.Now }); db.SaveChanges(); Sender.Tell(new AccountCreated(new CommonStatusResponse(message: "Successful")), Self); }
private void DoEditAccount(EditAccount req) { var db = new BillingwareDataContext(); var account = db.Accounts.FirstOrDefault(a => a.AccountNumber == req.AccountNumber); if (account == null) { Sender.Tell(new AccountEdited(new CommonStatusResponse(message: "Not found", code: "404", subCode: "404.1")), Self); return; } account.Alias = req.Alias; account.Extra = req.Extra; db.Entry(account).State = EntityState.Modified; db.SaveChanges(); Sender.Tell(new AccountEdited(new CommonStatusResponse(message: "Successful")), Self); }
public void CreateSampleCondition() { //arrange //create condition for the following case: //when the request is "debit" and the account balance <=0, halt the process var whenRequestIsDebit = new Condition { Active = true, ConditionApplicatorType = ConditionApplicatorType.All, ConditionConnector = ConditionConnector.And, Key = ComparatorKey.Custom, KeyExpression = "$.Payload.TransactionType", Type = ComparatorType.EqualTo, Name = "halt_no_balance", Value = "debit", OutcomeId = 1 //same outcome }; var whenAccountHasNoMoney = new Condition { Active = true, ConditionApplicatorType = ConditionApplicatorType.All, ConditionConnector = ConditionConnector.None, Key = ComparatorKey.Balance, Type = ComparatorType.LessThanOrEqualTo, Name = "halt_no_balance", Value = "0", OutcomeId = 1 //same outcome }; //when account has "allowOverdraft=true" in their Extras var whenAccountHasAllowOverdraftFlag = new Condition { Active = true, ConditionApplicatorType = ConditionApplicatorType.One, AppliedToAccountNumbers = "9237452718263673", ConditionConnector = ConditionConnector.None, Key = ComparatorKey.Custom, KeyExpression = "$.Extra.allowOverdraft", Type = ComparatorType.EqualTo, Name = "halt_no_balance", Value = "true", OutcomeId = 1 //same outcome }; var outcome = new ConditionOutcome { Active = true, OutcomeType = OutcomeType.Halt }; //act var db = new BillingwareDataContext(); db.Conditions.Add(whenRequestIsDebit); db.Conditions.Add(whenAccountHasNoMoney); db.Conditions.Add(whenAccountHasAllowOverdraftFlag); db.Outcomes.Add(outcome); db.SaveChanges(); //assert Assert.True(whenRequestIsDebit.Id > 0); }