Skip to content

Commit

Permalink
Return number of creaeted txns from exporters
Browse files Browse the repository at this point in the history
  • Loading branch information
brafdlog committed Nov 21, 2022
1 parent 97b3399 commit 1e35bf7
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/backend/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,14 @@ export type ExportTransactionsParams = {
outputVendorsConfig: Config['outputVendors'];
}

export type ExportTransactionsResult = {
exportedTransactionsNum: number
}

export type ExportTransactionsFunction = (
exportTransactionsParams: ExportTransactionsParams,
eventPublisher: any
) => Promise<any>;
) => Promise<ExportTransactionsResult>;

export interface OutputVendor {
name: OutputVendorName;
Expand Down
11 changes: 10 additions & 1 deletion src/backend/eventEmitters/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ export class ExporterEvent extends BudgetTrackingEvent {
}
}

export class ExporterEndEvent extends ExporterEvent {
exportedTransactionsNum: number;

constructor(exporterEventParams: ExporterEventParams & { exportedTransactionsNum: number }) {
super(exporterEventParams);
this.exportedTransactionsNum = exporterEventParams.exportedTransactionsNum;
}
}

export class DownalodChromeEvent extends BudgetTrackingEvent {
percent: number;

Expand All @@ -115,7 +124,7 @@ export type EventDataMap = {
[EventNames.EXPORTER_START]: ExporterEvent
[EventNames.EXPORTER_PROGRESS]: ExporterEvent
[EventNames.EXPORTER_ERROR]: ExporterEvent
[EventNames.EXPORTER_END]: ExporterEvent
[EventNames.EXPORTER_END]: ExporterEndEvent
[EventNames.GENERAL_ERROR]: BudgetTrackingEvent
[EventNames.LOG]: BudgetTrackingEvent
};
Expand Down
10 changes: 6 additions & 4 deletions src/backend/export/exportTransactions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash';
import {
AccountStatus, EventNames, EventPublisher, ExporterEvent
AccountStatus, EventNames, EventPublisher, ExporterEndEvent, ExporterEvent
} from '@/backend/eventEmitters/EventEmitter';
import { EnrichedTransaction, Config } from '@/backend/commonTypes';
import outputVendors from '@/backend/export/outputVendors';
Expand All @@ -25,11 +25,13 @@ export async function createTransactionsInExternalVendors(
await outputVendor.init?.(outputVendorsConfig);
await eventPublisher.emit(EventNames.EXPORTER_START, new ExporterEvent({ message: 'Starting', ...baseEvent }));
try {
const vendorResult = await outputVendor.exportTransactions({
const exportTransactionsResult = await outputVendor.exportTransactions({
transactionsToCreate: allTransactions, startDate, outputVendorsConfig
}, eventPublisher);
await eventPublisher.emit(EventNames.EXPORTER_END, new ExporterEvent({ message: 'Finished', ...baseEvent, status: AccountStatus.DONE }));
executionResult[outputVendor.name] = vendorResult;
await eventPublisher.emit(EventNames.EXPORTER_END, new ExporterEndEvent({
message: 'Finished', ...baseEvent, status: AccountStatus.DONE, exportedTransactionsNum: exportTransactionsResult.exportedTransactionsNum
}));
executionResult[outputVendor.name] = exportTransactionsResult;
} catch (e) {
await eventPublisher.emit(EventNames.EXPORTER_ERROR, new ExporterEvent({
message: e.message, error: e, ...baseEvent
Expand Down
3 changes: 3 additions & 0 deletions src/backend/export/outputVendors/csv/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ const exportTransactions: ExportTransactionsFunction = async ({ transactionsToCr
const mergedTransactions = mergeTransactions(savedTransactions, transactionsToCreate);
const sorted = sortByDate(mergedTransactions);
await writeCsvFile(filePath, serializeTransactions(sorted));
return {
exportedTransactionsNum: sorted.length
};
};

const parseTransactionsFile = async (filename: string) => {
Expand Down
10 changes: 7 additions & 3 deletions src/backend/export/outputVendors/googleSheets/googleSheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ const createTransactionsInGoogleSheets: ExportTransactionsFunction = async (

if (transactionsToCreate.length === 0) {
await emitProgressEvent(eventPublisher, transactions, 'All transactions already exist in google sheets');
return null;
return {
exportedTransactionsNum: 0
};
}

await emitProgressEvent(eventPublisher, transactions, `Creating ${transactionsToCreate.length} transactions in google sheets`);
Expand All @@ -66,10 +68,12 @@ const createTransactionsInGoogleSheets: ExportTransactionsFunction = async (
transaction.status
]);

const spreadsheetAppendResult = await googleSheets.appendToSpreadsheet(
await googleSheets.appendToSpreadsheet(
spreadsheetId, `${DEFAULT_SHEET_NAME}!A:A`, transactionsInSheetsFormat, oAuthClient
);
return spreadsheetAppendResult.data;
return {
exportedTransactionsNum: transactionsToCreate.length
};
};

async function emitProgressEvent(eventPublisher: EventPublisher, allTransactions: EnrichedTransaction[], message: string) {
Expand Down
3 changes: 3 additions & 0 deletions src/backend/export/outputVendors/json/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const exportTransactions: ExportTransactionsFunction = async ({ transactionsToCr
const mergedTransactions = mergeTransactions(savedTransactions, transactionsToCreate);
const sorted = sortByDate(mergedTransactions);
await fs.writeFile(filePath, JSON.stringify(sorted, null, 4));
return {
exportedTransactionsNum: sorted.length
};
};

export default {
Expand Down
11 changes: 8 additions & 3 deletions src/backend/export/outputVendors/ynab/ynab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,19 @@ const createTransactions: ExportTransactionsFunction = async ({ transactionsToCr
transactionsThatDontExistInYnab = transactionsThatDontExistInYnab.filter((transaction) => moment(transaction.date, YNAB_DATE_FORMAT).isBefore(NOW));
if (!transactionsThatDontExistInYnab.length) {
await emitProgressEvent(eventPublisher, transactionsToCreate, 'All transactions already exist in ynab. Doing nothing.');
return null;
return {
exportedTransactionsNum: 0
};
}

await emitProgressEvent(eventPublisher, transactionsToCreate, `Creating ${transactionsThatDontExistInYnab.length} transactions in ynab`);
try {
const transactionCreationResult = await ynabAPI!.transactions.createTransactions(ynabConfig.options.budgetId, {
await ynabAPI!.transactions.createTransactions(ynabConfig.options.budgetId, {
transactions: transactionsThatDontExistInYnab
});
return transactionCreationResult;
return {
exportedTransactionsNum: transactionsThatDontExistInYnab.length
};
} catch (e) {
await eventPublisher.emit(EventNames.EXPORTER_ERROR, new ExporterEvent({
message: e.message, error: e, exporterName: ynabOutputVendor.name, allTransactions: transactionsToCreate
Expand Down

0 comments on commit 1e35bf7

Please sign in to comment.