Skip to content

A local-first personal finance app

:fire: remove unused code (plaid, sync-full) (#116)

Matiss Janis Aboltins44038fbdced8
diff --git a/app-plaid.js b/app-plaid.js
deleted file mode 100644
index 3e53338ead..0000000000
--- a/app-plaid.js
+++ /dev/null
@@ -1,397 +0,0 @@
-// This app is unused right now. Maybe you could use it as a starting
-// point for Plaid integration!
-
-const express = require('express');
-const uuid = require('uuid');
-const fetch = require('node-fetch');
-const plaid = require('plaid');
-const { middleware: connectDb } = require('./db');
-const { handleError } = require('./util/handle-error');
-const { validateSubscribedUser } = require('./util/validate-user');
-const config = require('./load-config');
-
-const app = express();
-
-let plaidClient;
-function init() {
- plaidClient = new plaid.Client({
- clientID: config.plaid.client_id,
- secret: config.plaid.secret,
- env: config.plaid.env,
- options: { version: '2019-05-29' }
- });
-}
-
-async function validateToken(req, res) {
- let { token } = req.body;
- let rows = await req.runQuery(
- 'SELECT * FROM webTokens WHERE token_id = $1',
- [token],
- true
- );
- if (rows.length === 0) {
- res.send(JSON.stringify({ status: 'error', reason: 'not-found' }));
- return null;
- }
-
- // Tokens are only valid for 10 minutes
- let validTime = 1000 * 60 * 10;
- let row = rows[0];
- let timeCreated = JSON.parse(row.time_created);
-
- if (Date.now() - timeCreated >= validTime) {
- res.send(JSON.stringify({ status: 'error', reason: 'expired' }));
- return null;
- }
-
- return row;
-}
-
-app.post(
- '/create-web-token',
- connectDb,
- handleError(async (req, res) => {
- let user = await validateSubscribedUser(req, res);
- if (!user) {
- return;
- }
-
- let token = uuid.v4();
- await req.runQuery('DELETE FROM webTokens WHERE user_id = $1', [user.id]);
- await req.runQuery(
- 'INSERT INTO webTokens (user_id, token_id, time_created) VALUES ($1, $2, $3)',
- [user.id, token, Date.now()]
- );
- res.send(
- JSON.stringify({
- status: 'ok',
- data: token
- })
- );
- })
-);
-
-app.post(
- '/validate-web-token',
- connectDb,
- handleError(async (req, res) => {
- let token = await validateToken(req, res);
- if (!token) {
- return;
- }
-
- res.send(JSON.stringify({ status: 'ok' }));
- })
-);
-
-app.post(
- '/put-web-token-contents',
- connectDb,
- handleError(async (req, res) => {
- let token = await validateToken(req, res);
- if (!token) {
- return;
- }
-
- let { data } = req.body;
-
- await req.runQuery(
- 'UPDATE webTokens SET contents = $1 WHERE token_id = $2',
- [JSON.stringify(data), token.token_id]
- );
- res.send(
- JSON.stringify({
- status: 'ok',
- data: null
- })
- );
- })
-);
-
-app.post(
- '/get-web-token-contents',
- connectDb,
- handleError(async (req, res) => {
- let user = await validateSubscribedUser(req, res);
- if (!user) {
- return;
- }
-
- let token = await validateToken(req, res);
- if (!token) {
- return;
- }
-
- let rows = await req.runQuery(
- 'SELECT * FROM webTokens WHERE user_id = $1 AND token_id = $2',
- [user.id, token.token_id],
- true
- );
-
- if (rows.length === 0) {
- res.send(
- JSON.stringify({
- status: 'error',
- reason: 'not-found'
- })
- );
- }
-
- res.send(
- JSON.stringify({
- status: 'ok',
- data: JSON.parse(rows[0].contents)
- })
- );
- })
-);
-
-app.post(
- '/make_link_token',
- connectDb,
- handleError(async (req, res) => {
- let token = await validateToken(req, res);
- if (!token) {
- return;
- }
-
- let result = await plaidClient.createLinkToken({
- user: {
- client_user_id: token.user_id
- },
- client_name: 'Actual',
- products: ['transactions'],
- country_codes: ['US'],
- language: 'en'
- });
- res.send(JSON.stringify({ status: 'ok', data: result.link_token }));
- })
-);
-
-app.post(
- '/handoff_public_token',
- connectDb,
- handleError(async (req, res) => {
- let user = await validateSubscribedUser(req, res);
- if (!user) {
- return;
- }
- let { item_id, public_token } = req.body;
-
- let url = config.plaid.env + '/item/public_token/exchange';
- let resData = await fetch(url, {
- method: 'POST',
- body: JSON.stringify({
- client_id: config.plaid.client_id,
- secret: config.plaid.secret,
- public_token: public_token
- }),
- headers: {
- 'Content-Type': 'application/json',
- 'User-Agent': 'Actual Budget'
- }
- }).then((res) => res.json());
-
- await req.runQuery(
- 'INSERT INTO access_tokens (item_id, user_id, access_token) VALUES ($1, $2, $3)',
- [item_id, user.id, resData.access_token]
- );
-
- res.send(JSON.stringify({ status: 'ok' }));
- })
-);
-
-app.post(
- '/remove-access-token',
- connectDb,
- handleError(async (req, res) => {
- let user = await validateSubscribedUser(req, res);
- if (!user) {
- return;
- }
- let { item_id } = req.body;
-
- const rows = await req.runQuery(
- 'SELECT * FROM access_tokens WHERE user_id = $1 AND item_id = $2',
- [user.id, item_id],
- true
- );
- if (rows.length === 0) {
- throw new Error('access token not found');
- }
- const { access_token } = rows[0];
-
- const url = config.plaid.env + '/item/remove';
- const resData = await fetch(url, {
- method: 'POST',
- body: JSON.stringify({
- client_id: config.plaid.client_id,
- secret: config.plaid.secret,
- access_token: access_token
- }),
- headers: {
- 'Content-Type': 'application/json',
- 'User-Agent': 'Actual Budget'
- }
- }).then((res) => res.json());
-
- if (resData.removed !== true) {
- console.log('[Error] Item not removed: ' + access_token.slice(0, 3));
- }
-
- await req.runQuery(
- 'UPDATE access_tokens SET deleted = TRUE WHERE access_token = $1',
- [access_token]
- );
-
- res.send(
- JSON.stringify({
- status: 'ok',
- data: resData
- })
- );
- })
-);
-
-app.post(
- '/accounts',
- connectDb,
- handleError(async (req, res) => {
- let user = await validateSubscribedUser(req, res);
- if (!user) {
- return;
- }
- const { item_id } = req.body;
-
- const rows = await req.runQuery(
- 'SELECT * FROM access_tokens WHERE user_id = $1 AND item_id = $2',
- [user.id, item_id],
- true
- );
-
- if (rows.length === 0) {
- throw new Error('access token not found');
- }
- const { access_token } = rows[0];
-
- const url = config.plaid.env + '/accounts/get';
- const resData = await fetch(url, {
- method: 'POST',
- body: JSON.stringify({
- client_id: config.plaid.client_id,
- secret: config.plaid.secret,
- access_token: access_token
- }),
- headers: {
- 'Content-Type': 'application/json',
- 'User-Agent': 'Actual Budget'
- }
- }).then((res) => res.json());
-
- res.send(
- JSON.stringify({
- status: 'ok',
- data: resData
- })
- );
- })
-);
-
-app.post(
- '/transactions',
- connectDb,
- handleError(async (req, res) => {
- let user = await validateSubscribedUser(req, res);
- if (!user) {
- return;
- }
- let { item_id, start_date, end_date, account_id, count, offset } = req.body;
-
- let resData;
-
- const rows = await req.runQuery(
- 'SELECT * FROM access_tokens WHERE user_id = $1 AND item_id = $2 AND deleted = FALSE',
- [user.id, item_id],
- true
- );
-
- if (rows.length === 0) {
- res.status(400);
- res.send('access-token-not-found');
- return;
- }
-
- const { access_token } = rows[0];
-
- const url = config.plaid.env + '/transactions/get';
- resData = await fetch(url, {
- method: 'POST',
- body: JSON.stringify({
- client_id: config.plaid.client_id,
- secret: config.plaid.secret,
- access_token: access_token,
- start_date: start_date,
- end_date: end_date,
- options: {
- account_ids: [account_id],
- count: count,
- offset: offset
- }
- }),
- headers: {
- 'Content-Type': 'application/json',
- 'User-Agent': 'Actual Budget'
- }
- }).then((res) => res.json());
-
- res.send(
- JSON.stringify({
- status: 'ok',
- data: resData
- })
- );
- })
-);
-
-app.post(
- '/make-public-token',
- connectDb,
- handleError(async (req, res) => {
- let user = await validateSubscribedUser(req, res);
- if (!user) {
- return;
- }
- let { item_id } = req.body;
-
- const rows = await req.runQuery(
- 'SELECT * FROM access_tokens WHERE user_id = $1 AND item_id = $2',
- [user.id, item_id],
- true
- );
-
- if (rows.length === 0) {
- throw new Error('access token not found');
- }
- const { access_token } = rows[0];
-
- let result = await plaidClient.createLinkToken({
- user: {
- client_user_id: user.id
- },
- client_name: 'Actual',
- country_codes: ['US'],
- language: 'en',
- access_token: access_token
- });
-
- res.send(
- JSON.stringify({
- status: 'ok',
- data: result
- })
- );
- })
-);
-
-module.exports.handlers = app;
-module.exports.init = init;
diff --git a/package.json b/package.json
index 89f05de442..35e16742b8 100644
--- a/package.json
+++ b/package.json
@@ -22,7 +22,6 @@
"express": "4.17",
"express-actuator": "^1.8.1",
"express-response-size": "^0.0.3",
- "node-fetch": "^2.6.7",
"uuid": "^3.3.2"
},
"devDependencies": {
diff --git a/sync-full.js b/sync-full.js
deleted file mode 100644
index 6c8be3544f..0000000000
--- a/sync-full.js
+++ /dev/null
@@ -1,53 +0,0 @@
-let { sequential } = require('./util/async');
-
-let actual = require('@actual-app/api');
-let SyncPb = actual.internal.SyncProtoBuf;
-
-// This method must be sequential (TODO: document why, because Actual
-// is global etc)
-const sync = sequential(async function syncAPI(messages, since, fileId) {
- let prefs = await actual.internal.send('load-prefs');
- if (prefs == null || prefs.id !== fileId) {
- if (prefs != null) {
- await actual.internal.send('close-budget');
- }
-
- await actual.internal.send('load-budget', { id: fileId });
- }
-
- messages = messages.map((envPb) => {
- let timestamp = envPb.getTimestamp();
- let msg = SyncPb.Message.deserializeBinary(envPb.getContent());
- return {
- timestamp: timestamp,
- dataset: msg.getDataset(),
- row: msg.getRow(),
- column: msg.getColumn(),
- value: msg.getValue()
- };
- });
-
- const newMessages = await actual.internal.syncAndReceiveMessages(
- messages,
- since
- );
-
- return {
- trie: actual.internal.timestamp.getClock().merkle,
- newMessages: newMessages.map((msg) => {
- const envelopePb = new SyncPb.MessageEnvelope();
-
- const messagePb = new SyncPb.Message();
- messagePb.setDataset(msg.dataset);
- messagePb.setRow(msg.row);
- messagePb.setColumn(msg.column);
- messagePb.setValue(msg.value);
- envelopePb.setTimestamp(msg.timestamp);
-
- envelopePb.setContent(messagePb.serializeBinary());
- return envelopePb;
- })
- };
-});
-
-module.exports = { sync };
diff --git a/util/handle-error.js b/util/handle-error.js
deleted file mode 100644
index 6955150506..0000000000
--- a/util/handle-error.js
+++ /dev/null
@@ -1,11 +0,0 @@
-function handleError(func) {
- return (req, res) => {
- func(req, res).catch((err) => {
- console.log('Error', req.originalUrl, err);
- res.status(500);
- res.send({ status: 'error', reason: 'internal-error' });
- });
- };
-}
-
-module.exports = { handleError };
diff --git a/yarn.lock b/yarn.lock
index 6acfc4da12..d3a8ab7028 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1532,7 +1532,6 @@ __metadata:
express-actuator: ^1.8.1
express-response-size: ^0.0.3
jest: ^29.3.1
- node-fetch: ^2.6.7
prettier: ^2.6.2
supertest: ^6.3.1
typescript: ^4.6.4
@@ -4764,20 +4763,6 @@ __metadata:
languageName: node
linkType: hard
-"node-fetch@npm:^2.6.7":
- version: 2.6.9
- resolution: "node-fetch@npm:2.6.9"
- dependencies:
- whatwg-url: ^5.0.0
- peerDependencies:
- encoding: ^0.1.0
- peerDependenciesMeta:
- encoding:
- optional: true
- checksum: acb04f9ce7224965b2b59e71b33c639794d8991efd73855b0b250921382b38331ffc9d61bce502571f6cc6e11a8905ca9b1b6d4aeb586ab093e2756a1fd190d0
- languageName: node
- linkType: hard
-
"node-gyp@npm:latest":
version: 9.3.1
resolution: "node-gyp@npm:9.3.1"