Example #1
0
        public async Task <LockableResult <AccountDetailsOutDto> > Create(AccountNewDto accountIn)
        {
            if (accountIn.AuthorizedLimit != null && accountIn.AuthorizedLimit.Currency != accountIn.InitialBalance.Currency)
            {
                return(new BadRequestLockableResult <AccountDetailsOutDto>(BadRequestOutDto.WrongCurrencyType));
            }
            var rand                = new Random();
            var dateTimeNow         = DateTimeOffset.UtcNow;
            var accountDetailEntity = new AccountDetailEntity()
            {
                ResourceId      = Guid.NewGuid(),
                Name            = accountIn.Name,
                Product         = accountIn.Product,
                CashAccountType = accountIn.CashAccountType,
                Status          = accountIn.Status,
                Usage           = accountIn.Usage,
                Lock            = null,
                Pan             = string.Join("", "000123456789".Select(c => rand.Next(0, 10))),
                Msisdn          = accountIn.Msisdn,
                Currency        = accountIn.InitialBalance.Currency,
                RoutingNumbers  = new AccountReferenceEntity.RoutingNumbersEntity()
                {
                    Ach = AabRoutingNumbers.GetRandomRoutingNumber()
                },
                Balances = new BalanceEntity[]
                {
                    new BalanceEntity()
                    {
                        BalanceType              = BalanceTypeEnum.closingBooked,
                        BalanceAmount            = accountIn.InitialBalance.ToEntity(),
                        CreditLimitIncluded      = false,
                        LastChangeDateTime       = dateTimeNow,
                        LastCommittedTransaction = null
                    },
                },
                AuthorizedLimit = accountIn.AuthorizedLimit?.ToEntity()
            };

            if (accountIn.AuthorizedLimit != null)
            {
                accountDetailEntity.Balances = accountDetailEntity.Balances.Concat(new BalanceEntity[]
                {
                    new BalanceEntity()
                    {
                        BalanceType              = BalanceTypeEnum.authorised,
                        BalanceAmount            = accountIn.InitialBalance.ToEntity().Add(accountIn.AuthorizedLimit.ToEntity()),
                        CreditLimitIncluded      = true,
                        LastChangeDateTime       = dateTimeNow,
                        LastCommittedTransaction = null
                    },
                });
            }

            accountDetailEntity.LastModifiedDate = dateTimeNow;
            accountDetailEntity.CreatedDate      = dateTimeNow;

            await _accounts.InsertOneAsync(accountDetailEntity);

            return(new SuccessfulLockableResult <AccountDetailsOutDto>(AccountDetailsOutDto.EntityToOutDto(accountDetailEntity)));
        }
Example #2
0
        public async Task <AccountDetailsOutDto> Get(Guid id)
        {
            var accountDetailEntity = (await _accounts.FindAsync(a => a.ResourceId == id))
                                      .FirstOrDefault();

            return(accountDetailEntity != null
                ? AccountDetailsOutDto.EntityToOutDto(accountDetailEntity)
                : null);
        }
Example #3
0
        public async Task <LockableResult <AccountDetailsOutDto> > Update(Guid resourceId,
                                                                          AccountUpdateDto accountUpdateDto)
        {
            var foundAccountResult = await GetEntity(resourceId);

            AccountDetailEntity foundAccount;

            switch (foundAccountResult)
            {
            case FailedTypedResult <AccountDetailEntity> failedTypedResult:
                return(new FailedLockableResult <AccountDetailsOutDto>(failedTypedResult.Error));

            case NotFoundTypedResult <AccountDetailEntity> _:
                return(new NotFoundLockableResult <AccountDetailsOutDto>());

            case SuccessfulTypedResult <AccountDetailEntity> successfulTypedResult:
                foundAccount = successfulTypedResult.Value;
                break;

            default:
                return(new FailedLockableResult <AccountDetailsOutDto>(
                           new ArgumentOutOfRangeException(nameof(foundAccountResult))));
            }

            if (foundAccount.Lock == null)
            {
                return(new NotLockedLockableResult <AccountDetailsOutDto>());
            }

            if (foundAccount.Lock.Secret != accountUpdateDto.LockSecret)
            {
                return(new LockedLockableResult <AccountDetailsOutDto>());
            }


            var updates = new List <UpdateDefinition <AccountDetailEntity> >();

            if (!string.IsNullOrWhiteSpace(accountUpdateDto.Name))
            {
                updates.Add(Builders <AccountDetailEntity> .Update.Set(ade => ade.Name, accountUpdateDto.Name));
            }

            if (!string.IsNullOrWhiteSpace(accountUpdateDto.Msisdn))
            {
                updates.Add(Builders <AccountDetailEntity> .Update.Set(ade => ade.Msisdn, accountUpdateDto.Msisdn));
            }

            if (accountUpdateDto.Status != null)
            {
                updates.Add(Builders <AccountDetailEntity> .Update.Set(ade => ade.Status, accountUpdateDto.Status));
            }

            if (!updates.Any())
            {
                return(new SuccessfulLockableResult <AccountDetailsOutDto>(await this.Get(resourceId)));
            }

            // Set Last Modified Date
            updates.Add(Builders <AccountDetailEntity> .Update.Set(ade => ade.LastModifiedDate, DateTimeOffset.UtcNow));

            var updatedAccount = await _accounts.FindOneAndUpdateAsync <AccountDetailEntity>(
                f => f.ResourceId == resourceId && f.Lock != null && f.Lock.Secret == accountUpdateDto.LockSecret,
                Builders <AccountDetailEntity> .Update.Combine(updates),
                GetEntityAfterUpdateOption <AccountDetailEntity>());

            if (updatedAccount != null)
            {
                return(new SuccessfulLockableResult <AccountDetailsOutDto>(
                           AccountDetailsOutDto.EntityToOutDto(updatedAccount)));
            }
            else
            {
                return(new FailedLockableResult <AccountDetailsOutDto>(
                           new Exception("Lock status changed unexpectedly. No update.")));
            }
        }