Skip to content

Commit

Permalink
Refactor and better organization server functions (#23)
Browse files Browse the repository at this point in the history
* refactor and better organization server functions

* 1.1.3
  • Loading branch information
melledijkstra authored Nov 14, 2023
1 parent 5c9ba5e commit d75ee02
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 229 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gas-fire",
"version": "1.1.1",
"version": "1.1.3",
"author": "Melle Dijkstra",
"description": "Google App Script utilities to automate the FIRE google sheet",
"license": "MIT",
Expand Down
File renamed without changes.
160 changes: 81 additions & 79 deletions src/server/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildColumn } from './table-utils';
import { TableUtils, buildColumn } from './table-utils';
import { Transformers } from './transformers';
import type { Strategy, StrategyOption, Table } from './types';
import { n26Cols, raboCols, openbankCols } from './types';
Expand All @@ -12,93 +12,95 @@ export const AUTO_FILL_COLUMNS = [
];

const defaultAfterImport = [
(table: Table) => Utils.autoFillColumns(table, AUTO_FILL_COLUMNS),
(table: Table) => TableUtils.autoFillColumns(table, AUTO_FILL_COLUMNS),
];

type RootConfig = {
[key in StrategyOption]: Strategy;
};

const n26Config: Strategy = {
beforeImport: [
TableUtils.deleteLastRow,
TableUtils.deleteFirstRow,
TableUtils.sortByDate(n26Cols.Date),
],
columnImportRules: {
ref: null,
iban: (data) => new Array(data.length).fill(Utils.getBankIban('N26')),
date: buildColumn(n26Cols.Date, (val) => new Date(val)),
amount: buildColumn(n26Cols.Amount, parseFloat),
category: buildColumn(n26Cols.Payee, Transformers.transformCategory),
contra_account: buildColumn(n26Cols.Payee, String),
label: buildColumn(n26Cols.TransactionType, String),
import_date: (data) => new Array(data.length).fill(new Date()),
description: buildColumn(n26Cols.PaymentReference, String),
contra_iban: buildColumn(n26Cols.AccountNumber, String),
currency: buildColumn(n26Cols.ForeignCurrencyType, String),
},
afterImport: defaultAfterImport,
};

const rabobankConfig: Strategy = {
beforeImport: [
TableUtils.deleteLastRow,
TableUtils.deleteFirstRow,
TableUtils.sortByDate(raboCols.Datum),
],
columnImportRules: {
ref: buildColumn(raboCols.Volgnr, parseInt),
iban: buildColumn(raboCols.Iban, String),
date: buildColumn(raboCols.Datum, (val) => new Date(val)),
amount: buildColumn(raboCols.Bedrag, Transformers.transformMoney),
category: null,
contra_account: buildColumn(raboCols.NaamTegenpartij, String),
import_date: (data) => new Array(data.length).fill(new Date()),
contra_iban: buildColumn(raboCols.Tegenrekening, String),
currency: buildColumn(raboCols.Munt, String),
description: buildColumn(raboCols.Omschrijving1, String),
label: buildColumn(raboCols.Omschrijving2, String),
},
afterImport: defaultAfterImport,
};

const openbankConfig: Strategy = {
beforeImport: [
TableUtils.deleteFirstRow,
TableUtils.deleteLastRow,
// open bank has some empty columns when importing
(table) => TableUtils.deleteColumns(table, [0, 2, 4, 6, 8]),
],
columnImportRules: {
ref: null,
iban: (data) => new Array(data.length).fill(Utils.getBankIban('OPENBANK')),
date: buildColumn(openbankCols.Fecha, (val) => {
let [day, month, year] = val.split('/');
let yearNum = +year;
if (year && year.length === 2) {
// if year is of length 2 it means it only provides the year since 2000
// to fix we add 2000
yearNum = +year + 2000;
}
return new Date(+yearNum, +month - 1, +day);
}),
amount: buildColumn(openbankCols.Importe, Transformers.transformMoney),
category: null,
contra_account: null,
label: null,
description: buildColumn(openbankCols.Concepto, String),
import_date: (data) => new Array(data.length).fill(new Date()),
contra_iban: null,
currency: null,
},
afterImport: defaultAfterImport,
};

export class Config {
static getConfig(): RootConfig {
return {
n26: {
beforeImport: [
Utils.deleteLastRow,
Utils.deleteFirstRow,
Utils.sortByDate(n26Cols.Date),
],
columnImportRules: {
ref: null,
iban: (data) => new Array(data.length).fill(Utils.getBankIban('N26')),
date: buildColumn(n26Cols.Date, (val) => new Date(val)),
amount: buildColumn(n26Cols.Amount, parseFloat),
category: buildColumn(n26Cols.Payee, Transformers.transformCategory),
contra_account: buildColumn(n26Cols.Payee, String),
label: buildColumn(n26Cols.TransactionType, String),
import_date: (data) => new Array(data.length).fill(new Date()),
description: buildColumn(n26Cols.PaymentReference, String),
contra_iban: buildColumn(n26Cols.AccountNumber, String),
currency: buildColumn(n26Cols.ForeignCurrencyType, String),
},
afterImport: defaultAfterImport,
},
rabobank: {
beforeImport: [
Utils.deleteLastRow,
Utils.deleteFirstRow,
Utils.sortByDate(raboCols.Datum),
],
columnImportRules: {
ref: buildColumn(raboCols.Volgnr, parseInt),
iban: buildColumn(raboCols.Iban, String),
date: buildColumn(raboCols.Datum, (val) => new Date(val)),
amount: buildColumn(raboCols.Bedrag, Transformers.transformMoney),
category: null,
contra_account: buildColumn(raboCols.NaamTegenpartij, String),
import_date: (data) => new Array(data.length).fill(new Date()),
contra_iban: buildColumn(raboCols.Tegenrekening, String),
currency: buildColumn(raboCols.Munt, String),
description: buildColumn(raboCols.Omschrijving1, String),
label: buildColumn(raboCols.Omschrijving2, String),
},
afterImport: defaultAfterImport,
},
openbank: {
beforeImport: [
Utils.deleteFirstRow,
Utils.deleteLastRow,
// open bank has some empty columns when importing
(table) => Utils.deleteColumns(table, [0, 2, 4, 6, 8]),
],
columnImportRules: {
ref: null,
iban: (data) =>
new Array(data.length).fill(Utils.getBankIban('OPENBANK')),
date: buildColumn(openbankCols.Fecha, (val) => {
let [day, month, year] = val.split('/');
let yearNum = +year;
if (year && year.length === 2) {
// if year is of length 2 it means it only provides the year since 2000
// to fix we add 2000
yearNum = +year + 2000;
}
return new Date(+yearNum, +month - 1, +day);
}),
amount: buildColumn(
openbankCols.Importe,
Transformers.transformMoney
),
category: null,
contra_account: null,
label: null,
description: buildColumn(openbankCols.Concepto, String),
import_date: (data) => new Array(data.length).fill(new Date()),
contra_iban: null,
currency: null,
},
afterImport: defaultAfterImport,
},
n26: n26Config,
rabobank: rabobankConfig,
openbank: openbankConfig,
};
}
}
4 changes: 2 additions & 2 deletions src/server/exposed_functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @param xpath simplified xpath as string
* @customfunction
*/
function IMPORTJSON(url: string, xpath: string) {
export function IMPORTJSON(url: string, xpath: string) {
try {
// /rates/EUR
var res = UrlFetchApp.fetch(url);
Expand Down Expand Up @@ -90,7 +90,7 @@ function IMPORTJSON(url: string, xpath: string) {
* @return {string} The hashed input value.
* @customfunction
*/
function MD5(input: string): string {
export function MD5(input: string): string {
var txtHash = '';
var rawHash = Utilities.computeDigest(
Utilities.DigestAlgorithm.MD5,
Expand Down
24 changes: 24 additions & 0 deletions src/server/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getSheetById } from './utils';

export const fireColumns = [
'ref',
'iban',
'date',
'amount',
'balance',
'contra_account',
'description',
'satisfaction',
'icon',
'category',
'label',
'hours',
'contra_iban',
'disabled',
'currency',
];

export const SOURCE_SHEET_ID = 1093484485;
export const FireSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
export const sheets = FireSpreadsheet.getSheets();
export const sourceSheet = getSheetById(SOURCE_SHEET_ID);
11 changes: 6 additions & 5 deletions src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Functions to setup the initial UI
export { onOpen, fileUploadDialog, openAboutDialog } from './ui';

export {
generatePreview,
getStrategyOptions,
processCSV,
} from './remote-calls';
// Remote procedure calls made by the client UI executed on the server
export { getStrategyOptions, processCSV } from './remote-calls';

// Custom functions that can be used within the Spreadsheet UI
export { IMPORTJSON, MD5 } from './exposed_functions';
21 changes: 7 additions & 14 deletions src/server/remote-calls.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { sourceSheet } from './globals';
import { Config } from './config';
import { buildNewTableData } from './table-utils';
import { ServerResponse, Strategy, StrategyOption, Table } from './types';
import { Utils, sourceSheet } from './utils';
import { TableUtils, processTableWithImportRules } from './table-utils';
import { ServerResponse, StrategyOption, Table } from './types';

export function processCSV(
input: Table,
inputTable: Table,
importStrategy: StrategyOption
): ServerResponse {
const strategies = Config.getConfig();
Expand All @@ -19,12 +19,12 @@ export function processCSV(

if (beforeImport) {
for (const rule of beforeImport) {
input = rule(input);
inputTable = rule(inputTable);
}
}

let output = buildNewTableData(input, columnImportRules);
Utils.importData(output);
let output = processTableWithImportRules(inputTable, columnImportRules);
TableUtils.importData(output);

if (afterImport) {
for (const rule of afterImport) {
Expand All @@ -45,10 +45,3 @@ export function processCSV(
export function getStrategyOptions(): typeof StrategyOption {
return StrategyOption;
}

export function generatePreview(
data: Table,
strategy: StrategyOption
): { result: Table; newBalance: number } {
return { result: data, newBalance: 1240.56 };
}
Loading

0 comments on commit d75ee02

Please sign in to comment.