Skip to content

Commit

Permalink
Merge pull request #92 from brafdlog/passSpecificConfigToInternalFunc…
Browse files Browse the repository at this point in the history
…tions

Pass specific config to internal functions Fixes #89
  • Loading branch information
brafdlog authored Sep 7, 2020
2 parents 467f7b5 + f0a8cbe commit b9e108a
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/originalBudgetTrackingApp/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export enum OutputVendorName {

export interface OutputVendor {
name: OutputVendorName;
init?: (config: Config) => Promise<void>;
exportTransactions: (transactionsToCreate: EnrichedTransaction[], startDate: Date, config: Config) => Promise<any>;
init?: (outputVendorsConfig: Config['outputVendors']) => Promise<void>;
exportTransactions: (transactionsToCreate: EnrichedTransaction[], startDate: Date, outputVendorConfig: Config['outputVendors']) => Promise<any>;
}
12 changes: 8 additions & 4 deletions src/originalBudgetTrackingApp/export/exportTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ import { EnrichedTransaction } from '@/originalBudgetTrackingApp/commonTypes';
import _ from 'lodash';
import outputVendors from '@/originalBudgetTrackingApp/export/outputVendors';

export async function createTransactionsInExternalVendors(config: Config, companyIdToTransactions: Record<string, EnrichedTransaction[]>, startDate: Date) {
export async function createTransactionsInExternalVendors(
outputVendorsConfig: Config['outputVendors'],
companyIdToTransactions: Record<string, EnrichedTransaction[]>,
startDate: Date
) {
const executionResult = {};
const allTransactions = _.flatten(Object.values(companyIdToTransactions));

for (let j = 0; j < outputVendors.length; j++) {
const outputVendor = outputVendors[j];
if (config.outputVendors[outputVendor.name]?.active) {
await outputVendor.init?.(config);
if (outputVendorsConfig[outputVendor.name]?.active) {
await outputVendor.init?.(outputVendorsConfig);
console.log(`Start creating transactions in ${outputVendor.name}`);
const vendorResult = await outputVendor.exportTransactions(allTransactions, startDate, config);
const vendorResult = await outputVendor.exportTransactions(allTransactions, startDate, outputVendorsConfig);
console.log(`Finished creating transactions in ${outputVendor.name}`);
executionResult[outputVendor.name] = vendorResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export const googleSheetsOutputVendor: OutputVendor = {
exportTransactions: createTransactionsInGoogleSheets,
};

export async function createTransactionsInGoogleSheets(transactions: EnrichedTransaction[], startDate: Date, config: Config) {
const { spreadsheetId, sheetName, credentialsFilePath } = config.outputVendors.googleSheets!.options;
export async function createTransactionsInGoogleSheets(transactions: EnrichedTransaction[], startDate: Date, outputVendorConfig: Config['outputVendors']) {
const { spreadsheetId, sheetName, credentialsFilePath } = outputVendorConfig.googleSheets!.options;
console.log(`Got ${transactions.length} transactions to create in google sheets`);
const hashesAlreadyExistingInGoogleSheets = await googleSheets.getExistingHashes({ spreadsheetId, sheetName, credentialsFilePath });
const transactionsToCreate = transactions.filter((transaction) => !hashesAlreadyExistingInGoogleSheets.includes(transaction.hash));
Expand Down
13 changes: 5 additions & 8 deletions src/originalBudgetTrackingApp/export/outputVendors/ynab/ynab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import _ from 'lodash';
import * as ynab from 'ynab';
import moment from 'moment/moment';
import { EnrichedTransaction, OutputVendor, OutputVendorName } from '@/originalBudgetTrackingApp/commonTypes';
import { Config, getConfig, YnabConfig } from '../../../configManager/configManager';
import { Config, YnabConfig } from '../../../configManager/configManager';

const INITIAL_YNAB_ACCESS_TOKEN = 'AABB';
const YNAB_DATE_FORMAT = 'YYYY-MM-DD';
Expand All @@ -29,16 +29,13 @@ export const ynabOutputVendor: OutputVendor = {
exportTransactions: createTransactions
};

export async function init(config?: Config) {
export async function init(outputVendorsConfig: Config['outputVendors']) {
if (ynabConfig && ynabAPI) {
console.log('Ynab already initialized, skipping');
return;
}

if (!config) {
config = await getConfig();
}
ynabConfig = config.outputVendors.ynab;
ynabConfig = outputVendorsConfig.ynab;

if (!ynabConfig?.active) {
console.log('Ynab not enabled, skipping');
Expand Down Expand Up @@ -184,9 +181,9 @@ function verifyYnabAccessTokenWasDefined() {
}
}

export async function getYnabAccountDetails(): Promise<YnabAccountDetails> {
export async function getYnabAccountDetails(outputVendorsConfig: Config['outputVendors']): Promise<YnabAccountDetails> {
if (!ynabAccountDetails) {
await init();
await init(outputVendorsConfig);
const { budgets, accounts } = await getBudgetsAndAccountsData();
const categoryNames = await getYnabCategories();
ynabAccountDetails = {
Expand Down
17 changes: 11 additions & 6 deletions src/originalBudgetTrackingApp/import/importTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ import { ScaperScrapingResult, Transaction } from '@/originalBudgetTrackingApp/i
import * as categoryCalculation from '@/originalBudgetTrackingApp/import/categoryCalculationScript';

type AccountToScrapeConfig = configManager.AccountToScrapeConfig;
type ScrapingConfig = Config['scraping'];

const TRANSACTION_STATUS_COMPLETED = 'completed';
const DATE_FORMAT = 'DD/MM/YYYY';

export async function scrapeFinancialAccountsAndFetchTransactions(config: Config, startDate: Date) {
export async function scrapeFinancialAccountsAndFetchTransactions(scrapingConfig: ScrapingConfig, startDate: Date) {
const companyIdToTransactions: Record<string, EnrichedTransaction[]> = {};
const accountsToScrape = config.scraping.accountsToScrape.filter((accountToScrape) => accountToScrape.active !== false);
const accountsToScrape = scrapingConfig.accountsToScrape.filter((accountToScrape) => accountToScrape.active !== false);
for (let i = 0; i < accountsToScrape.length; i++) {
const accountToScrape = accountsToScrape[i];
const companyId = accountToScrape.key;
try {
console.log(`=================== Start fetching transactions for ${accountToScrape.name} ===================`);
const scrapeResult = await fetchTransactions(companyId, accountToScrape.loginFields, startDate, config);
const scrapeResult = await fetchTransactions(companyId, accountToScrape.loginFields, startDate, scrapingConfig);
const transactions = await postProcessTransactions(accountToScrape, scrapeResult);
companyIdToTransactions[companyId] = transactions;
console.log(`=================== Finished fetching transactions for ${accountToScrape.name} ===================`);
Expand All @@ -41,7 +42,7 @@ export async function getFinancialAccountNumbers() {
.toDate();

console.log('Fetching data from financial institutions to determine the account numbers');
const companyIdToTransactions = await scrapeFinancialAccountsAndFetchTransactions(config, startDate);
const companyIdToTransactions = await scrapeFinancialAccountsAndFetchTransactions(config.scraping, startDate);
const companyIdToAccountNumbers: Record<string, string[]> = {};
Object.keys(companyIdToTransactions).forEach((companyId) => {
let accountNumbers = companyIdToTransactions[companyId].map((transaction) => transaction.accountNumber);
Expand All @@ -51,13 +52,17 @@ export async function getFinancialAccountNumbers() {
return companyIdToAccountNumbers;
}

async function fetchTransactions(companyId: AccountToScrapeConfig['key'], credentials: AccountToScrapeConfig['loginFields'], startDate: Date, config: Config) {
async function fetchTransactions(
companyId: AccountToScrapeConfig['key'],
credentials: AccountToScrapeConfig['loginFields'],
startDate: Date, scrapingConfig: Config['scraping']
) {
console.log(`Start scraping ${companyId} from date: ${moment(startDate).format(DATE_FORMAT)}`);
const scrapeResult = await bankScraper.scrape({
companyId,
credentials,
startDate,
showBrowser: config.scraping.showBrowser,
showBrowser: scrapingConfig.showBrowser,
});
if (!scrapeResult.success) {
console.error('Failed scraping ', companyId);
Expand Down
4 changes: 2 additions & 2 deletions src/originalBudgetTrackingApp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export async function scrapeAndUpdateOutputVendors() {
.startOf('day')
.toDate();

const companyIdToTransactions = await scrapeFinancialAccountsAndFetchTransactions(config, startDate);
const companyIdToTransactions = await scrapeFinancialAccountsAndFetchTransactions(config.scraping, startDate);
try {
const executionResult = await createTransactionsInExternalVendors(config, companyIdToTransactions, startDate);
const executionResult = await createTransactionsInExternalVendors(config.outputVendors, companyIdToTransactions, startDate);
const resultToLog = `
Results of job:
${JSON.stringify(executionResult, null, 2)}
Expand Down
4 changes: 3 additions & 1 deletion src/originalBudgetTrackingApp/setupHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { getConfig } from '@/originalBudgetTrackingApp/configManager/configManager';
import { getYnabAccountDetails } from '@/originalBudgetTrackingApp/export/outputVendors/ynab/ynab';
import { getFinancialAccountNumbers } from '@/originalBudgetTrackingApp/import/importTransactions';

export async function printYnabAccountData() {
const ynabAccountData = await getYnabAccountDetails();
const config = await getConfig();
const ynabAccountData = await getYnabAccountDetails(config.outputVendors);
const companyIdToAccountNumbers = await getFinancialAccountNumbers();
console.log();
console.log();
Expand Down

0 comments on commit b9e108a

Please sign in to comment.