Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions backend/ormconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,7 @@ const { join } = require('path')
const config: ConnectionOptions = {
type: 'sqlite',
database: process.env.BUDGE_DATABASE || './budge.sqlite',

// type: 'postgres',
// port: parseInt(process.env.BUDGE_DB_PORT) || 5432,

// type: 'mariadb',
// port: parseInt(process.env.BUDGE_DB_PORT) || 3306,

// host: process.env.BUDGE_DB_HOST || '192.168.1.91',
// username: process.env.BUDGE_DB_USERNAME || 'budge',
// password: process.env.BUDGE_DB_PASSWORD || '3uY8xTX6j8oTVqJ_',
// database: process.env.BUDGE_DATABASE || 'budge',
synchronize: true,
synchronize: false,
logging: false,
entities: [join(__dirname, 'src/entities/**', '*.{ts,js}')],
migrations: [join(__dirname, 'src/migrations/**', '*.{ts,js}')],
Expand Down
7 changes: 2 additions & 5 deletions backend/src/controllers/BudgetsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export class BudgetsController extends Controller {
{
id: 'abc123',
name: 'My Budget',
toBeBudgeted: 0,
accounts: [
{
id: 'def123',
Expand All @@ -46,7 +45,6 @@ export class BudgetsController extends Controller {
{
id: 'abc456',
name: 'Another Budget',
toBeBudgeted: 0,
accounts: [],
created: new Date('2011-10-05T14:48:00.000Z'),
updated: new Date('2011-10-05T14:48:00.000Z'),
Expand Down Expand Up @@ -76,7 +74,6 @@ export class BudgetsController extends Controller {
data: {
id: 'abc123',
name: 'My Budget',
toBeBudgeted: 0,
accounts: [],
created: new Date('2011-10-05T14:48:00.000Z'),
updated: new Date('2011-10-05T14:48:00.000Z'),
Expand Down Expand Up @@ -111,7 +108,6 @@ export class BudgetsController extends Controller {
data: {
id: 'abc123',
name: 'My Budget',
toBeBudgeted: 0,
accounts: [],
created: new Date('2011-10-05T14:48:00.000Z'),
updated: new Date('2011-10-05T14:48:00.000Z'),
Expand Down Expand Up @@ -153,7 +149,6 @@ export class BudgetsController extends Controller {
data: {
id: 'abc123',
name: 'My Budget',
toBeBudgeted: 0,
accounts: [],
created: new Date('2011-10-05T14:48:00.000Z'),
updated: new Date('2011-10-05T14:48:00.000Z'),
Expand Down Expand Up @@ -201,6 +196,7 @@ export class BudgetsController extends Controller {
income: 0,
activity: 0,
budgeted: 0,
available: 0,
underfunded: 0,
created: new Date('2011-10-05T14:48:00.000Z'),
updated: new Date('2011-10-05T14:48:00.000Z'),
Expand Down Expand Up @@ -248,6 +244,7 @@ export class BudgetsController extends Controller {
income: 0,
activity: 0,
budgeted: 0,
available: 0,
underfunded: 0,
categories: [
{
Expand Down
5 changes: 5 additions & 0 deletions backend/src/controllers/UsersController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export class UsersController extends Controller {
},
})
public async createUser(@Body() requestBody: UserCreateRequest): Promise<UserResponse | ErrorResponse> {
if (process.env.REGISTRATION_DISABLED?.match(/true|1/i)) {
this.setStatus(400)
return { message: 'Registration is disabled' }
}

const { email } = requestBody

const emailCheck: User = await getRepository(User).findOne({ email })
Expand Down
2 changes: 1 addition & 1 deletion backend/src/entities/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class Account {
/**
* Belongs to a budget
*/
@ManyToOne(() => Budget, budget => budget.accounts)
@ManyToOne(() => Budget, budget => budget.accounts, { onDelete: 'CASCADE' })
budget: Promise<Budget>

/**
Expand Down
17 changes: 8 additions & 9 deletions backend/src/entities/Budget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CategoryGroup } from './CategoryGroup'
import { Category } from './Category'
import { BudgetMonth } from './BudgetMonth'
import { Transaction } from './Transaction'
import { Payee } from './Payee'

@Entity('budgets')
export class Budget {
Expand All @@ -18,12 +19,6 @@ export class Budget {
@Column({ type: 'varchar' })
name: string

@Column({
type: 'int',
default: 0,
})
toBeBudgeted: number = 0

@CreateDateColumn()
created: Date

Expand All @@ -33,7 +28,7 @@ export class Budget {
/**
* Belongs to a user
*/
@ManyToOne(() => User, user => user.budgets)
@ManyToOne(() => User, user => user.budgets, { onDelete: 'CASCADE' })
user: User

/**
Expand Down Expand Up @@ -66,6 +61,12 @@ export class Budget {
@OneToMany(() => Transaction, transaction => transaction.budget)
transactions: Promise<Transaction[]>

/**
* Has many budget transactions
*/
@OneToMany(() => Payee, payee => payee.budget)
payees: Promise<Payee[]>

public update(partial: DeepPartial<Budget>): Budget {
Object.assign(this, partial)
return this
Expand All @@ -76,15 +77,13 @@ export class Budget {
id: this.id,
userId: this.userId,
name: this.name,
toBeBudgeted: this.toBeBudgeted || 0,
}
}

public async toResponseModel(): Promise<BudgetModel> {
return {
id: this.id,
name: this.name,
toBeBudgeted: this.toBeBudgeted,
accounts: await Promise.all((await this.accounts).map(account => account.toResponseModel())),
created: this.created,
updated: this.updated,
Expand Down
64 changes: 61 additions & 3 deletions backend/src/entities/BudgetMonth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,53 @@
import { BudgetMonthModel } from '../models/BudgetMonth'
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, ManyToOne, Index, OneToMany } from 'typeorm'
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
ManyToOne,
Index,
OneToMany,
AfterLoad,
} from 'typeorm'
import { Budget } from './Budget'
import { CategoryMonth } from './CategoryMonth'

export type BudgetMonthOriginalValues = {
income: number
budgeted: number
activity: number
available: number
underfunded: number
}

export class BudgetMonthCache {
static cache: { [key: string]: BudgetMonthOriginalValues } = {}

public static get(id: string): BudgetMonthOriginalValues | null {
if (BudgetMonthCache.cache[id]) {
return BudgetMonthCache.cache[id]
}

return {
income: 0,
budgeted: 0,
activity: 0,
available: 0,
underfunded: 0,
}
}

public static set(budgetMonth: BudgetMonth) {
BudgetMonthCache.cache[budgetMonth.id] = {
income: budgetMonth.income,
budgeted: budgetMonth.budgeted,
activity: budgetMonth.activity,
available: budgetMonth.available,
underfunded: budgetMonth.underfunded,
}
}
}

@Entity('budget_months')
export class BudgetMonth {
@PrimaryGeneratedColumn('uuid')
Expand Down Expand Up @@ -34,6 +79,12 @@ export class BudgetMonth {
})
activity: number = 0

@Column({
type: 'int',
default: 0,
})
available: number = 0

@Column({
type: 'int',
default: 0,
Expand All @@ -49,15 +100,20 @@ export class BudgetMonth {
/**
* Belongs to a budget
*/
@ManyToOne(() => Budget, budget => budget.months)
@ManyToOne(() => Budget, budget => budget.months, { onDelete: 'CASCADE' })
budget: Promise<Budget>

/**
* Has man category months
* Has many category months
*/
@OneToMany(() => CategoryMonth, categoryMonth => categoryMonth.budgetMonth)
categories: Promise<CategoryMonth[]>

@AfterLoad()
private storeOriginalValues(): void {
BudgetMonthCache.set(this)
}

public getUpdatePayload() {
return {
id: this.id,
Expand All @@ -66,6 +122,7 @@ export class BudgetMonth {
income: this.income,
budgeted: this.budgeted,
activity: this.activity,
available: this.available,
underfunded: this.underfunded,
}
}
Expand All @@ -78,6 +135,7 @@ export class BudgetMonth {
income: this.income,
budgeted: this.budgeted,
activity: this.activity,
available: this.available,
underfunded: this.underfunded,
created: this.created,
updated: this.updated,
Expand Down
2 changes: 1 addition & 1 deletion backend/src/entities/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class Category {
/**
* Belongs to a category group
*/
@ManyToOne(() => CategoryGroup, categoryGroup => categoryGroup.categories)
@ManyToOne(() => CategoryGroup, categoryGroup => categoryGroup.categories, { onDelete: 'CASCADE' })
categoryGroup: CategoryGroup

/**
Expand Down
2 changes: 1 addition & 1 deletion backend/src/entities/CategoryGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class CategoryGroup {
/**
* Belongs to a budget
*/
@ManyToOne(() => Budget, budget => budget.categoryGroups)
@ManyToOne(() => Budget, budget => budget.categoryGroups, { onDelete: 'CASCADE' })
budget: Budget

/**
Expand Down
2 changes: 1 addition & 1 deletion backend/src/entities/CategoryMonth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class CategoryMonth {
/**
* Belongs to a category
*/
@ManyToOne(() => Category, category => category.categoryMonths)
@ManyToOne(() => Category, category => category.categoryMonths, { onDelete: 'CASCADE' })
category: Promise<Category>

/**
Expand Down
17 changes: 15 additions & 2 deletions backend/src/entities/Payee.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { Entity, OneToOne, JoinColumn, PrimaryGeneratedColumn, Column, CreateDateColumn, OneToMany } from 'typeorm'
import {
Entity,
OneToOne,
JoinColumn,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
OneToMany,
ManyToOne,
} from 'typeorm'
import { Account } from './Account'
import { PayeeModel } from '../models/Payee'
import { Transaction } from './Transaction'
import { Budget } from './Budget'

@Entity('payees')
export class Payee {
Expand All @@ -26,7 +36,10 @@ export class Payee {
@CreateDateColumn()
updated: Date

@OneToOne(() => Account, account => account.transferPayee)
@ManyToOne(() => Budget, budget => budget.payees, { onDelete: 'CASCADE' })
budget: Promise<Budget>

@OneToOne(() => Account, account => account.transferPayee, { onDelete: 'CASCADE' })
@JoinColumn()
transferAccount: Promise<Account>

Expand Down
2 changes: 1 addition & 1 deletion backend/src/entities/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class Transaction {
/**
* Belongs to an account
*/
@ManyToOne(() => Account, account => account.transactions)
@ManyToOne(() => Account, account => account.transactions, { onDelete: 'CASCADE' })
account: Promise<Account>

/**
Expand Down
Loading