Esempio n. 1
0
        public async Task <IActionResult> CreateCustodianAccount(string storeId, CreateCustodianAccountViewModel vm)
        {
            var store = GetCurrentStore();

            vm.StoreId = store.Id;
            vm.SetCustodianRegistry(_custodianRegistry);

            var custodian = _custodianRegistry.GetCustodianByCode(vm.SelectedCustodian);

            if (custodian == null)
            {
                ModelState.AddModelError(nameof(vm.SelectedCustodian), "Invalid Custodian");
                return(View(vm));
            }
            if (string.IsNullOrEmpty(vm.Name))
            {
                vm.Name = custodian.Name;
            }

            var custodianAccountData = new CustodianAccountData {
                CustodianCode = vm.SelectedCustodian, StoreId = vm.StoreId, Name = custodian.Name
            };


            var configData = new JObject();

            foreach (var pair in Request.Form)
            {
                configData.Add(pair.Key, pair.Value.ToString());
            }

            var configForm = await custodian.GetConfigForm(configData, "en-US");

            if (configForm.IsValid())
            {
                // configForm.removeUnusedKeys();
                custodianAccountData.SetBlob(configData);
                custodianAccountData = await _custodianAccountRepository.CreateOrUpdate(custodianAccountData);

                TempData[WellKnownTempData.SuccessMessage] = "Custodian account successfully created";
                CreatedCustodianAccountId = custodianAccountData.Id;

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

            // Ask for more data
            vm.ConfigForm = configForm;
            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)));
        }
Esempio n. 3
0
        private async Task <CustodianAccountDataClient> ToModel(CustodianAccountData custodianAccount, bool includeAsset, CancellationToken cancellationToken)
        {
            var custodian = GetCustodianByCode(custodianAccount.CustodianCode);
            var r         = includeAsset ? new CustodianAccountResponse() : new CustodianAccountDataClient();

            r.Id            = custodianAccount.Id;
            r.CustodianCode = custodian.Code;
            r.Name          = custodianAccount.Name;
            r.StoreId       = custodianAccount.StoreId;
            if (await CanSeeCustodianAccountConfig())
            {
                // Only show the "config" field if the user can create or manage the Custodian Account, because config contains sensitive information (API key, etc).
                r.Config = custodianAccount.GetBlob();
            }
            if (includeAsset)
            {
                var balances = await GetCustodianByCode(r.CustodianCode).GetAssetBalancesAsync(r.Config, cancellationToken);

                ((CustodianAccountResponse)r).AssetBalances = balances;
            }
            return(r);
        }