mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 12:20:31 +00:00
fix: database migrations FK relations.
fix: database columns indexing.
This commit is contained in:
@@ -7,6 +7,10 @@ export default class ExpenseRepository extends TenantRepository {
|
||||
repositories: any;
|
||||
cache: any;
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
* @param {number} tenantId
|
||||
*/
|
||||
constructor(tenantId: number) {
|
||||
super(tenantId);
|
||||
|
||||
@@ -14,38 +18,97 @@ export default class ExpenseRepository extends TenantRepository {
|
||||
this.cache = this.tenancy.cache(tenantId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the given expense by id.
|
||||
* @param {number} expenseId
|
||||
* @return {Promise<IExpense>}
|
||||
*/
|
||||
getById(expenseId: number) {
|
||||
const { Expense } = this.models;
|
||||
return this.cache.get(`expense.id.${expenseId}`, () => {
|
||||
return Expense.query().findById(expenseId);
|
||||
})
|
||||
}
|
||||
|
||||
create(expense: IExpense) {
|
||||
const { Expense } = this.models;
|
||||
return Expense.query().insert({ ...expense });
|
||||
}
|
||||
|
||||
update(expenseId: number, expense: IExpense) {
|
||||
const { Expense } = this.models;
|
||||
return Expense.query().patchAndFetchById(expenseId, { ...expense });
|
||||
}
|
||||
|
||||
publish(expenseId: number) {
|
||||
const { Expense } = this.models;
|
||||
|
||||
return Expense.query().findById(expenseId).patch({
|
||||
publishedAt: moment().toMySqlDateTime(),
|
||||
return Expense.query().findById(expenseId).withGraphFetched('categories');
|
||||
});
|
||||
}
|
||||
|
||||
delete(expenseId: number) {
|
||||
/**
|
||||
* Inserts a new expense object.
|
||||
* @param {IExpense} expense -
|
||||
*/
|
||||
async create(expense: IExpense): Promise<void> {
|
||||
const { Expense } = this.models;
|
||||
return Expense.query().findById(expenseId).delete();
|
||||
await Expense.query().insert({ ...expense });
|
||||
|
||||
this.flushCache();
|
||||
}
|
||||
|
||||
bulkDelete(expensesIds: number[]) {
|
||||
/**
|
||||
* Updates the given expense details.
|
||||
* @param {number} expenseId
|
||||
* @param {IExpense} expense
|
||||
*/
|
||||
async update(expenseId: number, expense: IExpense) {
|
||||
const { Expense } = this.models;
|
||||
return Expense.query().whereIn('id', expensesIds).delete();
|
||||
|
||||
await Expense.query().findById(expenseId).patch({ ...expense });
|
||||
this.flushCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Publish the given expense.
|
||||
* @param {number} expenseId
|
||||
*/
|
||||
async publish(expenseId: number): Promise<void> {
|
||||
const { Expense } = this.models;
|
||||
|
||||
await Expense.query().findById(expenseId).patch({
|
||||
publishedAt: moment().toMySqlDateTime(),
|
||||
});
|
||||
this.flushCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given expense.
|
||||
* @param {number} expenseId
|
||||
*/
|
||||
async delete(expenseId: number): Promise<void> {
|
||||
const { Expense } = this.models;
|
||||
|
||||
await Expense.query().where('id', expenseId).delete();
|
||||
await Expense.query().where('expense_id', expenseId).delete();
|
||||
|
||||
this.flushCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes expenses in bulk.
|
||||
* @param {number[]} expensesIds
|
||||
*/
|
||||
async bulkDelete(expensesIds: number[]): Promise<void> {
|
||||
const { Expense } = this.models;
|
||||
|
||||
await Expense.query().whereIn('expense_id', expensesIds).delete();
|
||||
await Expense.query().whereIn('id', expensesIds).delete();
|
||||
|
||||
this.flushCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Publishes the given expenses in bulk.
|
||||
* @param {number[]} expensesIds
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
async bulkPublish(expensesIds: number): Promise<void> {
|
||||
const { Expense } = this.models;
|
||||
await Expense.query().whereIn('id', expensesIds).patch({
|
||||
publishedAt: moment().toMySqlDateTime(),
|
||||
});
|
||||
this.flushCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Flushes repository cache.
|
||||
*/
|
||||
flushCache() {
|
||||
this.cache.delStartWith(`expense`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user