Esempio n. 1
0
        public async Task <IActionResult> EditCustodianAccount(string storeId, string accountId,
                                                               EditCustodianAccountViewModel vm)
        {
            // The locale is not important yet, but keeping it here so we can find it easily when localization becomes a thing.
            var locale = "en-US";

            var custodianAccount = await _custodianAccountRepository.FindById(storeId, accountId);

            if (custodianAccount == null)
            {
                return(NotFound());
            }

            var custodian = _custodianRegistry.GetCustodianByCode(custodianAccount.CustodianCode);

            if (custodian == null)
            {
                // TODO The custodian account is broken. The custodian is no longer available. Maybe delete the custodian account?
                return(NotFound());
            }
            var configForm = await custodian.GetConfigForm(custodianAccount.GetBlob(), locale);

            var newData = new JObject();

            foreach (var pair in Request.Form)
            {
                if ("CustodianAccount.Name".Equals(pair.Key))
                {
                    custodianAccount.Name = pair.Value;
                }
                else
                {
                    // TODO support posted array notation, like a field called "WithdrawToAddressNamePerPaymentMethod[BTC-OnChain]". The data should be nested in the JSON.
                    newData.Add(pair.Key, pair.Value.ToString());
                }
            }

            var newConfigData = RemoveUnusedFieldsFromConfig(custodianAccount.GetBlob(), newData, configForm);
            var newConfigForm = await custodian.GetConfigForm(newConfigData, locale);

            if (newConfigForm.IsValid())
            {
                custodianAccount.SetBlob(newConfigData);
                custodianAccount = await _custodianAccountRepository.CreateOrUpdate(custodianAccount);

                return(RedirectToAction(nameof(ViewCustodianAccount),
                                        new { storeId = custodianAccount.StoreId, accountId = custodianAccount.Id }));
            }

            // Form not valid: The user must fix the errors before we can save
            vm.CustodianAccount = custodianAccount;
            vm.ConfigForm       = newConfigForm;
            return(View(vm));
        }
Esempio n. 2
0
        public async Task <IActionResult> CreateCustodianAccount(string storeId, CreateCustodianAccountRequest request, CancellationToken cancellationToken)
        {
            request ??= new CreateCustodianAccountRequest();
            var custodian = GetCustodianByCode(request.CustodianCode);

            // Use the name provided or if none provided use the name of the custodian.
            string name = string.IsNullOrEmpty(request.Name) ? custodian.Name : request.Name;

            var custodianAccount = new CustodianAccountData()
            {
                CustodianCode = custodian.Code, Name = name, StoreId = storeId,
            };

            custodianAccount.SetBlob(request.Config);

            await _custodianAccountRepository.CreateOrUpdate(custodianAccount);

            return(Ok(await ToModel(custodianAccount, false, cancellationToken)));
        }