Skip to content

Commit

Permalink
Re-enable linting on the server (#4363)
Browse files Browse the repository at this point in the history
* getAccountDb

* uuidv4

* === / !==

* default exports

* re-enable linting for the server

* fix jest

* note
  • Loading branch information
matt-fidd authored Feb 12, 2025
1 parent 524707c commit f1a4c88
Show file tree
Hide file tree
Showing 41 changed files with 118 additions and 90 deletions.
19 changes: 18 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ export default [
'packages/loot-core/**/node_modules/*',
'packages/loot-core/**/lib-dist/*',
'packages/loot-core/**/proto/*',
'packages/sync-server',
'.yarn/*',
'.github/*',
],
Expand Down Expand Up @@ -818,6 +817,7 @@ export default [
'**/*.test.ts',
'**/*.test.jsx',
'**/*.test.tsx',
'**/*.spec.js',
],

rules: {
Expand All @@ -835,4 +835,21 @@ export default [
'@typescript-eslint/consistent-type-definitions': ['warn', 'type'],
},
},
{
files: ['packages/sync-server/**/*'],
// TODO: fix the issues in these files
rules: {
'import/extensions': 'off',
'rulesdir/typography': 'off',
},
},
{
files: ['packages/sync-server/src/app-gocardless/banks/*.js'],
rules: {
'import/no-anonymous-default-export': 'off',
'import/no-default-export': 'off',
// can be re-enabled after https://github.com/actualbudget/actual/pull/4253
'@typescript-eslint/no-unused-vars': 'off',
},
},
];
4 changes: 2 additions & 2 deletions packages/sync-server/app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import runMigrations from './src/migrations.js';
import { run as runMigrations } from './src/migrations.js';

runMigrations()
.then(() => {
//import the app here becasue initial migrations need to be run first - they are dependencies of the app.js
import('./src/app.js').then(app => app.default()); // run the app
import('./src/app.js').then(app => app.run()); // run the app
})
.catch(err => {
console.log('Error starting app:', err);
Expand Down
5 changes: 3 additions & 2 deletions packages/sync-server/jest.global-setup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import getAccountDb from './src/account-db.js';
import runMigrations from './src/migrations.js';
import { getAccountDb } from './src/account-db.js';
import { run as runMigrations } from './src/migrations.js';

const GENERIC_ADMIN_ID = 'genericAdmin';
const GENERIC_USER_ID = 'genericUser';
Expand Down Expand Up @@ -60,6 +60,7 @@ const setSessionUser = (userId, token = 'valid-token') => {
}
};

// eslint-disable-next-line import/no-default-export
export default async function setup() {
const NEVER_EXPIRES = -1; // or consider using a far future timestamp

Expand Down
3 changes: 2 additions & 1 deletion packages/sync-server/jest.global-teardown.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import runMigrations from './src/migrations.js';
import { run as runMigrations } from './src/migrations.js';

// eslint-disable-next-line import/no-default-export
export default async function teardown() {
await runMigrations('down');
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fs from 'node:fs/promises';

import config from '../src/load-config.js';
import { config } from '../src/load-config.js';

async function ensureExists(path) {
try {
await fs.mkdir(path);
} catch (err) {
if (err.code == 'EEXIST') {
if (err.code === 'EEXIST') {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getAccountDb from '../src/account-db.js';
import { getAccountDb } from '../src/account-db.js';

export const up = async function () {
await getAccountDb().exec(`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getAccountDb from '../src/account-db.js';
import { getAccountDb } from '../src/account-db.js';

export const up = async function () {
await getAccountDb().exec(`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getAccountDb from '../src/account-db.js';
import { getAccountDb } from '../src/account-db.js';

export const up = async function () {
await getAccountDb().exec(
Expand Down
2 changes: 1 addition & 1 deletion packages/sync-server/migrations/1718889148000-openid.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getAccountDb from '../src/account-db.js';
import { getAccountDb } from '../src/account-db.js';

export const up = async function () {
await getAccountDb().exec(
Expand Down
14 changes: 7 additions & 7 deletions packages/sync-server/migrations/1719409568000-multiuser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as uuid from 'uuid';
import { v4 as uuidv4 } from 'uuid';

import getAccountDb from '../src/account-db.js';
import { getAccountDb } from '../src/account-db.js';

export const up = async function () {
const accountDb = getAccountDb();
Expand All @@ -10,7 +10,7 @@ export const up = async function () {
`
CREATE TABLE users
(id TEXT PRIMARY KEY,
user_name TEXT,
user_name TEXT,
display_name TEXT,
role TEXT,
enabled INTEGER NOT NULL DEFAULT 1,
Expand All @@ -22,11 +22,11 @@ export const up = async function () {
PRIMARY KEY (user_id, file_id),
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (file_id) REFERENCES files(id)
);
);
ALTER TABLE files
ADD COLUMN owner TEXT;
ALTER TABLE sessions
ADD COLUMN expires_at INTEGER;
Expand All @@ -38,7 +38,7 @@ export const up = async function () {
`,
);

const userId = uuid.v4();
const userId = uuidv4();
accountDb.mutate(
'INSERT INTO users (id, user_name, display_name, enabled, owner, role) VALUES (?, ?, ?, 1, 1, ?)',
[userId, '', '', 'ADMIN'],
Expand Down Expand Up @@ -66,7 +66,7 @@ export const down = async function () {
SELECT token FROM sessions;
DROP TABLE sessions;
ALTER TABLE sessions_backup RENAME TO sessions;
CREATE TABLE files_backup (
Expand Down
12 changes: 6 additions & 6 deletions packages/sync-server/src/account-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import * as bcrypt from 'bcrypt';

import { bootstrapOpenId } from './accounts/openid.js';
import { bootstrapPassword, loginWithPassword } from './accounts/password.js';
import openDatabase from './db.js';
import config from './load-config.js';
import { openDatabase } from './db.js';
import { config } from './load-config.js';

let _accountDb;

export default function getAccountDb() {
export function getAccountDb() {
if (_accountDb === undefined) {
const dbPath = join(config.serverFiles, 'account.sqlite');
_accountDb = openDatabase(dbPath);
Expand Down Expand Up @@ -179,10 +179,10 @@ export async function disableOpenID(loginSettings) {
accountDb.transaction(() => {
accountDb.mutate('DELETE FROM sessions');
accountDb.mutate(
`DELETE FROM user_access
`DELETE FROM user_access
WHERE user_access.user_id IN (
SELECT users.id
FROM users
SELECT users.id
FROM users
WHERE users.user_name <> ?
);`,
[''],
Expand Down
10 changes: 5 additions & 5 deletions packages/sync-server/src/accounts/openid.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { generators, Issuer } from 'openid-client';
import * as uuid from 'uuid';
import { v4 as uuidv4 } from 'uuid';

import getAccountDb, { clearExpiredSessions } from '../account-db.js';
import finalConfig from '../load-config.js';
import { clearExpiredSessions, getAccountDb } from '../account-db.js';
import { config as finalConfig } from '../load-config.js';
import {
getUserByUsername,
transferAllFilesFromUser,
Expand Down Expand Up @@ -207,7 +207,7 @@ export async function loginWithOpenIdFinalize(body) {
[''],
);
if (countUsersWithUserName === 0) {
userId = uuid.v4();
userId = uuidv4();
// Check if user was created by another transaction
const existingUser = accountDb.first(
'SELECT id FROM users WHERE user_name = ?',
Expand Down Expand Up @@ -261,7 +261,7 @@ export async function loginWithOpenIdFinalize(body) {
}
}

const token = uuid.v4();
const token = uuidv4();

let expiration;
if (finalConfig.token_expiration === 'openid-provider') {
Expand Down
19 changes: 9 additions & 10 deletions packages/sync-server/src/accounts/password.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as bcrypt from 'bcrypt';
import * as uuid from 'uuid';
import { v4 as uuidv4 } from 'uuid';

import getAccountDb, { clearExpiredSessions } from '../account-db.js';
import finalConfig from '../load-config.js';
import { clearExpiredSessions, getAccountDb } from '../account-db.js';
import { config } from '../load-config.js';
import { TOKEN_EXPIRATION_NEVER } from '../util/validate-user.js';

function isValidPassword(password) {
Expand Down Expand Up @@ -58,14 +58,14 @@ export function loginWithPassword(password) {
['password'],
);

const token = sessionRow ? sessionRow.token : uuid.v4();
const token = sessionRow ? sessionRow.token : uuidv4();

const { totalOfUsers } = accountDb.first(
'SELECT count(*) as totalOfUsers FROM users',
);
let userId = null;
if (totalOfUsers === 0) {
userId = uuid.v4();
userId = uuidv4();
accountDb.mutate(
'INSERT INTO users (id, user_name, display_name, enabled, owner, role) VALUES (?, ?, ?, 1, 1, ?)',
[userId, '', '', 'ADMIN'],
Expand All @@ -85,12 +85,11 @@ export function loginWithPassword(password) {

let expiration = TOKEN_EXPIRATION_NEVER;
if (
finalConfig.token_expiration != 'never' &&
finalConfig.token_expiration != 'openid-provider' &&
typeof finalConfig.token_expiration === 'number'
config.token_expiration !== 'never' &&
config.token_expiration !== 'openid-provider' &&
typeof config.token_expiration === 'number'
) {
expiration =
Math.floor(Date.now() / 1000) + finalConfig.token_expiration * 60;
expiration = Math.floor(Date.now() / 1000) + config.token_expiration * 60;
}

if (!sessionRow) {
Expand Down
4 changes: 2 additions & 2 deletions packages/sync-server/src/app-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
errorMiddleware,
requestLoggerMiddleware,
} from './util/middlewares.js';
import validateSession, { validateAuthHeader } from './util/validate-user.js';
import { validateAuthHeader, validateSession } from './util/validate-user.js';

const app = express();
app.use(express.json());
Expand Down Expand Up @@ -66,7 +66,7 @@ app.post('/login', async (req, res) => {
const obfuscated =
'*'.repeat(headerVal.length) || 'No password provided.';
console.debug('HEADER VALUE: ' + obfuscated);
if (headerVal == '') {
if (headerVal === '') {
res.send({ status: 'error', reason: 'invalid-header' });
return;
} else {
Expand Down
6 changes: 3 additions & 3 deletions packages/sync-server/src/app-admin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express';
import * as uuid from 'uuid';
import { v4 as uuidv4 } from 'uuid';

import { isAdmin } from './account-db.js';
import * as UserService from './services/user-service.js';
Expand All @@ -8,7 +8,7 @@ import {
requestLoggerMiddleware,
validateSessionMiddleware,
} from './util/middlewares.js';
import validateSession from './util/validate-user.js';
import { validateSession } from './util/validate-user.js';

const app = express();
app.use(express.json());
Expand Down Expand Up @@ -78,7 +78,7 @@ app.post('/users', validateSessionMiddleware, async (req, res) => {
return;
}

const userId = uuid.v4();
const userId = uuidv4();
UserService.insertUser(
userId,
userName,
Expand Down
2 changes: 1 addition & 1 deletion packages/sync-server/src/app-admin.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import request from 'supertest';
import { v4 as uuidv4 } from 'uuid';

import getAccountDb from './account-db.js';
import { getAccountDb } from './account-db.js';
import { handlers as app } from './app-admin.js';

const ADMIN_ROLE = 'ADMIN';
Expand Down
7 changes: 5 additions & 2 deletions packages/sync-server/src/app-gocardless/bank-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ async function loadBanks() {

export const banks = await loadBanks();

export default institutionId =>
banks.find(b => b.institutionIds.includes(institutionId)) || IntegrationBank;
export function BankFactory(institutionId) {
return (
banks.find(b => b.institutionIds.includes(institutionId)) || IntegrationBank
);
}

export const BANKS_WITH_LIMITED_HISTORY = [
'BANCA_AIDEXA_AIDXITMM',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default {
const diff =
+new Date(b.valueDate || b.bookingDate) -
+new Date(a.valueDate || a.bookingDate);
if (diff != 0) return diff;
if (diff !== 0) return diff;
return parseInt(b.transactionId) - parseInt(a.transactionId);
}),

Expand Down
12 changes: 6 additions & 6 deletions packages/sync-server/src/app-gocardless/banks/hype_hyeeit22.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default {
/** Online card payments - identified by "crd" transaction code
* always start with PAGAMENTO PRESSO + <payee name>
*/
if (transaction.proprietaryBankTransactionCode == 'crd') {
if (transaction.proprietaryBankTransactionCode === 'crd') {
// remove PAGAMENTO PRESSO and set payee name
transaction.debtorName =
transaction.remittanceInformationUnstructured?.slice(
Expand All @@ -25,15 +25,15 @@ export default {
* HAI (INVIATO/RICEVUTO) UN BONIFICO (A/DA) {payee_name} - {payment_info} (bon)
*/
if (
transaction.proprietaryBankTransactionCode == 'p2p' ||
transaction.proprietaryBankTransactionCode == 'bon'
transaction.proprietaryBankTransactionCode === 'p2p' ||
transaction.proprietaryBankTransactionCode === 'bon'
) {
// keep only {payment_info} portion of remittance info
// NOTE: if {payee_name} contains dashes (unlikely / impossible?), this probably gets bugged!
const infoIdx =
transaction.remittanceInformationUnstructured.indexOf(' - ') + 3;
transaction.remittanceInformationUnstructured =
infoIdx == -1
infoIdx === -1
? transaction.remittanceInformationUnstructured
: transaction.remittanceInformationUnstructured.slice(infoIdx).trim();
}
Expand All @@ -44,15 +44,15 @@ export default {
* so it groups them in 4bytes bundles
* the code below assumes this is always the case
*/
if (transaction.proprietaryBankTransactionCode == 'p2p') {
if (transaction.proprietaryBankTransactionCode === 'p2p') {
let str = transaction.remittanceInformationUnstructured;
let idx = str.indexOf('\\U');
let start_idx = idx;
let codepoints = [];
while (idx !== -1) {
codepoints.push(parseInt(str.slice(idx + 2, idx + 6), 16));
const next_idx = str.indexOf('\\U', idx + 6);
if (next_idx == idx + 6) {
if (next_idx === idx + 6) {
idx = next_idx;
continue;
}
Expand Down
Loading

0 comments on commit f1a4c88

Please sign in to comment.