public Task SetAsync(string clientId, string legalEntityId, string assetId, ClientSwiftCredentials item)
 {
     _storage.AddOrUpdate(GetKey(clientId, legalEntityId, assetId), item, (key, oldValue) => item);
     return(Task.CompletedTask);
 }
        public async Task <ClientSwiftCredentials> GetSwiftCredentialsAsync(string clientId, string legalEntityId, string assetId)
        {
            var clientSwiftCredentials = await _clientSwiftCredentialsCache.GetAsync(clientId, legalEntityId, assetId);

            if (clientSwiftCredentials != null)
            {
                return(clientSwiftCredentials);
            }

            var asset = await _assetsServiceWithCache.TryGetAssetAsync(assetId);

            if (asset == null)
            {
                await _log.WriteWarningAsync(nameof(ClientService), nameof(GetSwiftCredentialsAsync),
                                             new { clientId, legalEntityId, assetId }.ToJson(), "Asset not found.");

                return(null);
            }

            var swiftCredentialsList = await _swiftCredentialsRepository.FindAsync(legalEntityId, assetId);

            if (swiftCredentialsList.Count > 1)
            {
                await _log.WriteWarningAsync(nameof(ClientService), nameof(GetSwiftCredentialsAsync),
                                             new { legalEntityId, assetId }.ToJson(), "Ambiguous swift credentials.");

                return(null);
            }

            var swiftCredentials = swiftCredentialsList.FirstOrDefault();

            if (swiftCredentials == null)
            {
                await _log.WriteWarningAsync(nameof(ClientService), nameof(GetSwiftCredentialsAsync),
                                             new { clientId, legalEntityId, assetId }.ToJson(), "Swift credentials not found.");

                return(null);
            }

            var personalData = await _personalDataService.GetAsync(clientId);

            if (personalData == null)
            {
                await _log.WriteWarningAsync(nameof(ClientService), nameof(GetSwiftCredentialsAsync),
                                             new { clientId, legalEntityId, assetId }.ToJson(), "Personal data not found.");

                return(null);
            }

            var assetTitle = string.IsNullOrEmpty(asset.DisplayId) ? asset.Id : asset.DisplayId;

            var purposeOfPayment = string.Format(swiftCredentials.PurposeOfPaymentFormat, assetTitle,
                                                 personalData.Email?.Replace("@", "."));

            clientSwiftCredentials = new ClientSwiftCredentials
            {
                Bic                  = swiftCredentials.Bic,
                AccountName          = swiftCredentials.AccountName,
                AccountNumber        = swiftCredentials.AccountNumber,
                PurposeOfPayment     = purposeOfPayment,
                BankAddress          = swiftCredentials.BankAddress,
                CompanyAddress       = swiftCredentials.CompanyAddress,
                CorrespondentAccount = swiftCredentials.CorrespondentAccount
            };

            await _clientSwiftCredentialsCache.SetAsync(clientId, legalEntityId, assetId, clientSwiftCredentials);

            return(clientSwiftCredentials);
        }