public async Task UpdateAsync(Pex2AplosMappingModel model, CancellationToken cancellationToken) { var entity = _storageMappingService.Map(model); entity.PartitionKey = PartitionKey; entity.RowKey = model.PEXBusinessAcctId.ToString(); entity.ETag = "*"; var operation = TableOperation.Merge(entity); await Table.ExecuteAsync(operation, cancellationToken); }
private async Task <string> RenewExternalToken(Pex2AplosMappingModel mapping, CancellationToken cancellationToken) { if (mapping.GetLastRenewedDateUtc() < DateTime.UtcNow.AddMonths(-6)) { _log.LogWarning($"External API token is older than 6 months and could not be renewed for business: {mapping.PEXBusinessAcctId}"); return(mapping.PEXExternalAPIToken); } if (mapping.GetLastRenewedDateUtc() < DateTime.UtcNow.AddMonths(-5).AddDays(-1)) //Wait an extra day in case of timing issues (server time differences, DST complications, etc.) { _log.LogInformation($"Renewing external API token for business: {mapping.PEXBusinessAcctId}"); var response = await _pexApiClient.RenewExternalToken(mapping.PEXExternalAPIToken, cancellationToken); return(response.Token); } return(mapping.PEXExternalAPIToken); }
public async Task CreateAsync(Pex2AplosMappingModel model, CancellationToken cancellationToken) { var entity = _storageMappingService.Map(model); entity.PartitionKey = PartitionKey; entity.RowKey = model.PEXBusinessAcctId.ToString(); var operation = TableOperation.Insert(entity); try { await Table.ExecuteAsync(operation, cancellationToken); } catch (Exception ex) { _logger.LogError(ex, ex.Message); throw; } }
public async Task Run( [QueueTrigger("pex-aplos-mapping", Connection = "StorageConnectionString")] Pex2AplosMappingModel mapping, ILogger log, CancellationToken cancellationToken) { log.LogInformation($"Beginning Azure Function {nameof(SyncProcessor)} for {nameof(mapping.PEXBusinessAcctId)} {mapping.PEXBusinessAcctId}."); try { await _aplosIntegrationService.Sync(mapping, log, cancellationToken); } catch (Exception ex) { log.LogError(ex, $"Error running Azure Function {nameof(SyncProcessor)} for {nameof(mapping.PEXBusinessAcctId)} {mapping.PEXBusinessAcctId}."); } log.LogInformation($"Completed Azure Function {nameof(SyncProcessor)} for {nameof(mapping.PEXBusinessAcctId)} {mapping.PEXBusinessAcctId}."); }
public async Task <ActionResult> CreateAplosToken(string sessionId, [FromBody] AplosTokenRequestModel model, CancellationToken cancellationToken) { if (!Guid.TryParse(sessionId, out var sessionGuid)) { return(BadRequest()); } PexOAuthSessionModel session = await _pexOAuthSessionStorage.GetBySessionGuidAsync(sessionGuid, cancellationToken); if (session == null) { return(Unauthorized()); } Pex2AplosMappingModel mapping = await _pex2AplosMappingStorage.GetByBusinessAcctIdAsync(session.PEXBusinessAcctId, cancellationToken); if (!string.IsNullOrWhiteSpace(model.AplosClientId)) { mapping.AplosClientId = model.AplosClientId; } if (!string.IsNullOrWhiteSpace(model.AplosPrivateKey)) { mapping.AplosPrivateKey = model.AplosPrivateKey; } bool result = await _aplosIntegrationService.ValidateAplosApiCredentials(mapping, cancellationToken); if (!result) { return(BadRequest()); } await _pex2AplosMappingStorage.UpdateAsync(mapping, cancellationToken); return(Ok()); }
public Pex2AplosMappingEntity Map(Pex2AplosMappingModel model) { Pex2AplosMappingEntity result = default; if (model != null) { string encryptedAplosClientId = default; if (!string.IsNullOrWhiteSpace(model.AplosClientId)) { encryptedAplosClientId = _dataProtector.Protect(model.AplosClientId); } string encryptedAplosPrivateKey = default; if (!string.IsNullOrWhiteSpace(model.AplosPrivateKey)) { encryptedAplosPrivateKey = _dataProtector.Protect(model.AplosPrivateKey); } string encryptedAplosAccessToken = default; if (!string.IsNullOrWhiteSpace(model.AplosAccessToken)) { encryptedAplosAccessToken = _dataProtector.Protect(model.AplosAccessToken); } result = new Pex2AplosMappingEntity { CreatedUtc = model.CreatedUtc, PEXBusinessAcctId = model.PEXBusinessAcctId, LastSyncUtc = model.LastSyncUtc, LastRenewedUtc = model.LastRenewedUtc, PEXExternalAPIToken = model.PEXExternalAPIToken, SyncTags = model.SyncTags, SyncTransactions = model.SyncTransactions, SyncTransfers = model.SyncTransfers, SyncPexFees = model.SyncPexFees, SyncApprovedOnly = model.SyncApprovedOnly, EarliestTransactionDateToSync = model.EarliestTransactionDateToSync, AplosAccountId = model.AplosAccountId, AplosPartnerVerified = model.AplosPartnerVerified, AplosClientId = encryptedAplosClientId, AplosPrivateKey = encryptedAplosPrivateKey, AplosAuthenticationMode = (int)model.AplosAuthenticationMode, AplosAccessToken = encryptedAplosAccessToken, AplosAccessTokenExpiresAt = model.AplosAccessTokenExpiresAt, AplosRegisterAccountNumber = model.AplosRegisterAccountNumber.ToString(), SyncTransactionsCreateContact = model.SyncTransactionsCreateContact, DefaultAplosContactId = model.DefaultAplosContactId, DefaultAplosFundId = model.DefaultAplosFundId, DefaultAplosTransactionAccountNumber = model.DefaultAplosTransactionAccountNumber.ToString(), TransfersAplosContactId = model.TransfersAplosContactId, TransfersAplosFundId = model.TransfersAplosFundId, TransfersAplosTransactionAccountNumber = model.TransfersAplosTransactionAccountNumber.ToString(), PexFeesAplosContactId = model.PexFeesAplosContactId, PexFeesAplosFundId = model.PexFeesAplosFundId, PexFeesAplosTransactionAccountNumber = model.PexFeesAplosTransactionAccountNumber.ToString(), PexFundsTagId = model.PexFundsTagId, SyncFundsToPex = model.SyncFundsToPex, ExpenseAccountMappings = JsonConvert.SerializeObject(model.ExpenseAccountMappings), TagMappings = JsonConvert.SerializeObject(model.TagMappings), }; } return(result); }
public Pex2AplosMappingModel Map(Pex2AplosMappingEntity model) { Pex2AplosMappingModel result = default; if (model != null) { string decryptedAplosClientId = default; if (!string.IsNullOrWhiteSpace(model.AplosClientId)) { decryptedAplosClientId = _dataProtector.Unprotect(model.AplosClientId); } string decryptedAplosPrivateKey = default; if (!string.IsNullOrWhiteSpace(model.AplosPrivateKey)) { decryptedAplosPrivateKey = _dataProtector.Unprotect(model.AplosPrivateKey); } string decryptedAplosAccessToken = default; if (!string.IsNullOrWhiteSpace(model.AplosAccessToken)) { decryptedAplosAccessToken = _dataProtector.Unprotect(model.AplosAccessToken); } decimal.TryParse(model.AplosRegisterAccountNumber, out var aplosRegisterAccountNumber); decimal.TryParse(model.DefaultAplosTransactionAccountNumber, out var defaultAplosTransactionAccountNumber); decimal.TryParse(model.TransfersAplosTransactionAccountNumber, out var transfersAplosTransactionAccountNumber); decimal.TryParse(model.PexFeesAplosTransactionAccountNumber, out var pexFeesAplosTransactionAccountNumber); result = new Pex2AplosMappingModel { CreatedUtc = model.CreatedUtc, PEXBusinessAcctId = model.PEXBusinessAcctId, LastSyncUtc = model.LastSyncUtc, LastRenewedUtc = model.LastRenewedUtc, PEXExternalAPIToken = model.PEXExternalAPIToken, SyncTags = model.SyncTags, SyncTransactions = model.SyncTransactions, SyncTransfers = model.SyncTransfers, SyncPexFees = model.SyncPexFees, SyncApprovedOnly = model.SyncApprovedOnly, EarliestTransactionDateToSync = model.EarliestTransactionDateToSync, AplosAccountId = model.AplosAccountId, AplosPartnerVerified = model.AplosPartnerVerified, AplosClientId = decryptedAplosClientId, AplosPrivateKey = decryptedAplosPrivateKey, AplosAuthenticationMode = (AplosAuthenticationMode)model.AplosAuthenticationMode, AplosAccessToken = decryptedAplosAccessToken, AplosAccessTokenExpiresAt = model.AplosAccessTokenExpiresAt, AplosRegisterAccountNumber = aplosRegisterAccountNumber, SyncTransactionsCreateContact = model.SyncTransactionsCreateContact, DefaultAplosContactId = model.DefaultAplosContactId, DefaultAplosFundId = model.DefaultAplosFundId, DefaultAplosTransactionAccountNumber = defaultAplosTransactionAccountNumber, TransfersAplosContactId = model.TransfersAplosContactId, TransfersAplosFundId = model.TransfersAplosFundId, TransfersAplosTransactionAccountNumber = transfersAplosTransactionAccountNumber, PexFeesAplosContactId = model.PexFeesAplosContactId, PexFeesAplosFundId = model.PexFeesAplosFundId, PexFeesAplosTransactionAccountNumber = pexFeesAplosTransactionAccountNumber, PexFundsTagId = model.PexFundsTagId, SyncFundsToPex = model.SyncFundsToPex, ExpenseAccountMappings = model.ExpenseAccountMappings == null ? null : JsonConvert.DeserializeObject <ExpenseAccountMappingModel[]>(model.ExpenseAccountMappings), TagMappings = model.TagMappings == null ? null : JsonConvert.DeserializeObject <TagMappingModel[]>(model.TagMappings), }; } return(result); }
public async Task EnqueueMapping(Pex2AplosMappingModel mapping, CancellationToken cancellationToken) { var message = new CloudQueueMessage(JsonConvert.SerializeObject(mapping)); await Queue.AddMessageAsync(message, cancellationToken); }