Commit a5d6f48b authored by Benjamin Bouvier's avatar Benjamin Bouvier
Browse files

[server] Simplify model imports by reexporting them all from models/index.ts;

parent 33d5df25
Pipeline #206132 passed with stages
in 14 minutes and 56 seconds
import { makeLogger } from './helpers';
import { setupOrm } from './models';
import Users from './models/users';
import { setupOrm, Users } from './models';
let log = makeLogger('cli');
......
import Settings from '../models/settings';
import { Settings } from '../models';
export async function getManifest(req, res) {
const iconsDirectory = 'favicon/';
......
import Accesses from '../../models/accesses';
import { Accesses } from '../../models';
import accountManager from '../../lib/accounts-manager';
import { fullPoll } from '../../lib/poller';
......
import Accesses from '../../models/accesses';
import Accounts from '../../models/accounts';
import Settings from '../../models/settings';
import { Accesses, Accounts, Settings } from '../../models';
import { makeLogger, KError, asyncErr } from '../../helpers';
import { checkAllowedFields } from '../../shared/validators';
import accountManager from '../../lib/accounts-manager';
import { isDemoEnabled } from './settings';
import { makeLogger, KError, asyncErr } from '../../helpers';
import { checkAllowedFields } from '../../shared/validators';
let log = makeLogger('controllers/accounts');
// Prefills the @account field with a queried bank account.
......
import Accounts from '../../models/accounts';
import Alerts from '../../models/alerts';
import { Accounts, Alerts } from '../../models';
import { asyncErr, KError } from '../../helpers';
import { checkAlert } from '../../shared/validators';
......
import * as crypto from 'crypto';
import Accesses from '../../models/accesses';
import Accounts from '../../models/accounts';
import Alerts from '../../models/alerts';
import Budgets from '../../models/budgets';
import Categories from '../../models/categories';
import Settings from '../../models/settings';
import Transactions from '../../models/transactions';
import {
Accesses,
Accounts,
Alerts,
Budgets,
Categories,
Settings,
Transactions
} from '../../models';
import { ConfigGhostSettings } from '../../lib/ghost-settings';
import { validatePassword } from '../../shared/helpers';
import DefaultSettings from '../../shared/default-settings';
......
import Budgets from '../../models/budgets';
import Categories from '../../models/categories';
import { Budgets, Categories } from '../../models';
import { KError, asyncErr } from '../../helpers';
import { checkBudget } from '../../shared/validators';
......
import Budgets from '../../models/budgets';
import Categories from '../../models/categories';
import Transactions from '../../models/transactions';
import { Budgets, Categories, Transactions } from '../../models';
import { makeLogger, KError, asyncErr } from '../../helpers';
import { checkExactFields, checkAllowedFields } from '../../shared/validators';
......
import Accesses from '../../models/accesses';
import Budgets from '../../models/budgets';
import Categories from '../../models/categories';
import Settings from '../../models/settings';
import { Accesses, Budgets, Categories, Settings } from '../../models';
import { asyncErr, KError, translate as $t } from '../../helpers';
import { isDemoForced, isDemoEnabled } from './settings';
......
import fs from 'fs';
import Accesses from '../../models/accesses';
import Accounts from '../../models/accounts';
import { Accesses, Accounts } from '../../models';
import { promisify, asyncErr } from '../../helpers';
import { obfuscateKeywords, obfuscatePasswords } from './helpers';
const readFile = promisify(fs.readFile);
......
import moment from 'moment';
import Categories from '../../models/categories';
import Transaction from '../../models/transactions';
import { Categories, Transactions } from '../../models';
import { isKnownTransactionTypeName } from '../../lib/transaction-types';
import { KError, asyncErr, UNKNOWN_OPERATION_TYPE } from '../../helpers';
async function preload(varName, req, res, next, operationID) {
let { id: userId } = req.user;
try {
let operation = await Transaction.find(userId, operationID);
let operation = await Transactions.find(userId, operationID);
if (!operation) {
throw new KError('bank operation not found', 404);
}
......@@ -81,7 +79,7 @@ export async function update(req, res) {
}
}
await Transaction.update(userId, req.preloaded.operation.id, opUpdate);
await Transactions.update(userId, req.preloaded.operation.id, opUpdate);
res.status(200).end();
} catch (err) {
return asyncErr(res, err, 'when updating attributes of operation');
......@@ -98,9 +96,9 @@ export async function merge(req, res) {
// Transfer various fields upon deletion
let newFields = op.mergeWith(otherOp);
op = await Transaction.update(userId, op.id, newFields);
op = await Transactions.update(userId, op.id, newFields);
await Transaction.destroy(userId, otherOp.id);
await Transactions.destroy(userId, otherOp.id);
res.status(200).json(op);
} catch (err) {
return asyncErr(res, err, 'when merging two operations');
......@@ -112,7 +110,7 @@ export async function create(req, res) {
try {
let { id: userId } = req.user;
let operation = req.body;
if (!Transaction.isOperation(operation)) {
if (!Transactions.isOperation(operation)) {
throw new KError('Not an operation', 400);
}
......@@ -129,7 +127,7 @@ export async function create(req, res) {
operation.debitDate = operation.date;
operation.createdByUser = true;
let op = await Transaction.create(userId, operation);
let op = await Transactions.create(userId, operation);
res.status(201).json(op);
} catch (err) {
return asyncErr(res, err, 'when creating operation for a bank account');
......@@ -141,7 +139,7 @@ export async function destroy(req, res) {
try {
let { id: userId } = req.user;
let op = req.preloaded.operation;
await Transaction.destroy(userId, op.id);
await Transactions.destroy(userId, op.id);
res.status(204).end();
} catch (err) {
return asyncErr(res, err, 'when deleting operation');
......
import Settings from '../../models/settings';
import { Settings } from '../../models';
import * as weboob from '../../lib/sources/weboob';
import getEmailer from '../../lib/emailer';
......
import { makeLogger, setupTranslator } from './helpers';
import { initModels } from './models';
import Settings from './models/settings';
import { initModels, Settings } from './models';
import Poller from './lib/poller';
let log = makeLogger('init');
......
import moment from 'moment';
import Accesses from '../models/accesses';
import Accounts from '../models/accounts';
import Settings from '../models/settings';
import Transactions from '../models/transactions';
import { Accesses, Accounts, Settings, Transactions } from '../models';
import { accountTypeIdToName } from './account-types';
import { transactionTypeIdToName } from './transaction-types';
......
import Notifications from './notifications';
import getEmailer from './emailer';
import { makeLogger, translate as $t, displayLabel } from '../helpers';
import Accounts from '../models/accounts';
import Alerts from '../models/alerts';
import { Accounts, Alerts } from '../models';
import { makeLogger, translate as $t, displayLabel } from '../helpers';
import Notifications from './notifications';
import getEmailer from './emailer';
let log = makeLogger('alert-manager');
......
......@@ -2,7 +2,7 @@ import nodemailer from 'nodemailer';
import { assert, makeLogger, translate as $t, isEmailEnabled } from '../helpers';
import Settings from '../models/settings';
import { Settings } from '../models';
let log = makeLogger('emailer');
......
import moment from 'moment';
import Accesses from '../models/accesses';
import Settings from '../models/settings';
import { Accesses, Settings } from '../models';
import accountManager from './accounts-manager';
import Cron from './cron';
......
import moment from 'moment';
import {
makeLogger,
KError,
......@@ -8,15 +10,9 @@ import {
displayLabel
} from '../helpers';
import { Accesses, Accounts, Alerts, Transactions } from '../models';
import getEmailer from './emailer';
import Accesses from '../models/accesses';
import Accounts from '../models/accounts';
import Alerts from '../models/alerts';
import Transactions from '../models/transactions';
import moment from 'moment';
let log = makeLogger('report-manager');
// Minimum duration between two reports: let T be any time, in the worst case,
......
......@@ -3,18 +3,29 @@ import { createConnection } from 'typeorm';
import { assert, makeLogger } from '../helpers';
import AccessFields from './access-fields';
import Accesses from './accesses';
import Accounts from './accounts';
import Alerts from './alerts';
import Budgets from './budgets';
import Categories from './categories';
import Settings from './settings';
import Transactions from './transactions';
import User from './users';
let log = makeLogger('models/index');
import AccessFields from './entities/access-fields';
import Accesses from './entities/accesses';
import Accounts from './entities/accounts';
import Alerts from './entities/alerts';
import Budgets from './entities/budgets';
import Categories from './entities/categories';
import Settings from './entities/settings';
import Transactions from './entities/transactions';
import Users from './entities/users';
export {
AccessFields,
Accesses,
Accounts,
Alerts,
Budgets,
Categories,
Settings,
Transactions,
Users
};
const log = makeLogger('models/index');
function makeOrmConfig() {
let ormConfig = null;
......@@ -65,7 +76,7 @@ function makeOrmConfig() {
}
export async function setupOrm() {
let ormConfig = Object.assign(makeOrmConfig(), {
const ormConfig = Object.assign(makeOrmConfig(), {
// Automatically run migrations.
migrationsRun: true,
......@@ -90,7 +101,7 @@ export async function initModels(appOptions) {
if (process.kresus.providedUserId !== null) {
userId = process.kresus.providedUserId;
// Check that the user actually exists already.
let user = await User.find(userId);
const user = await Users.find(userId);
if (!user) {
throw new Error(
`The user with provided ID ${userId} doesn't exist. Did you run "kresus create:user" first?`
......@@ -98,11 +109,11 @@ export async function initModels(appOptions) {
}
} else {
// Create default user.
let user = await User.find();
let user = await Users.find();
if (!user) {
let { login } = process.kresus.user;
const { login } = process.kresus.user;
assert(login, 'There should be a default login set!');
user = await User.create({ login });
user = await Users.create({ login });
log.info('Creating default user...');
}
userId = user.id;
......@@ -111,15 +122,20 @@ export async function initModels(appOptions) {
log.info(`User has id ${userId}`);
// Try to migrate the older Pouchdb database, if it's not been done yet.
let didMigrate = await Settings.findOrCreateDefaultBooleanValue(userId, 'migrated-from-cozydb');
const didMigrate = await Settings.findOrCreateDefaultBooleanValue(
userId,
'migrated-from-cozydb'
);
log.info(`Checking if the migration from CozyDB is required... ${didMigrate ? 'no' : 'yes'}`);
if (!didMigrate) {
let all = require('../controllers/v1/all');
let exportCozyDb = require('kresus-export-cozydb');
let options = Object.assign({}, appOptions);
// eslint-disable-next-line import/no-cycle, @typescript-eslint/no-var-requires
const all = require('../controllers/v1/all');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const exportCozyDb = require('kresus-export-cozydb');
const options = Object.assign({}, appOptions);
log.info('Migrating from CozyDB...');
try {
let world = await exportCozyDb.run(options);
const world = await exportCozyDb.run(options);
await all.importData(userId, world);
log.info('Migrating from CozyDB done!');
......
......@@ -4,8 +4,7 @@
import should from 'should';
import AccessFields from '../../server/models/access-fields';
import Accesses from '../../server/models/accesses';
import { AccessFields, Accesses } from '../../server/models';
describe('Accesses model API', () => {
let accessWithoutFields = {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment