mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 13:50:31 +00:00
feat: rewrite repositories with base entity repository class.
feat: sales and purchases status. feat: sales and purchases auto-increment number. fix: settings find query with extra columns.
This commit is contained in:
@@ -1,42 +1,13 @@
|
||||
import TenantRepository from "./TenantRepository";
|
||||
import { IExpense } from 'interfaces';
|
||||
import moment from "moment";
|
||||
|
||||
import { Expense } from 'models';
|
||||
export default class ExpenseRepository extends TenantRepository {
|
||||
/**
|
||||
* Retrieve the given expense by id.
|
||||
* @param {number} expenseId
|
||||
* @return {Promise<IExpense>}
|
||||
* Constructor method.
|
||||
*/
|
||||
getById(expenseId: number) {
|
||||
const { Expense } = this.models;
|
||||
return this.cache.get(`expense.id.${expenseId}`, () => {
|
||||
return Expense.query().findById(expenseId).withGraphFetched('categories');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a new expense object.
|
||||
* @param {IExpense} expense -
|
||||
*/
|
||||
async create(expenseInput: IExpense): Promise<void> {
|
||||
const { Expense } = this.models;
|
||||
const expense = await Expense.query().insertGraph({ ...expenseInput });
|
||||
this.flushCache();
|
||||
|
||||
return expense;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the given expense details.
|
||||
* @param {number} expenseId
|
||||
* @param {IExpense} expense
|
||||
*/
|
||||
async update(expenseId: number, expense: IExpense) {
|
||||
const { Expense } = this.models;
|
||||
|
||||
await Expense.query().findById(expenseId).patch({ ...expense });
|
||||
this.flushCache();
|
||||
constructor(knex, cache) {
|
||||
super(knex, cache);
|
||||
this.model = Expense;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,38 +15,10 @@ export default class ExpenseRepository extends TenantRepository {
|
||||
* @param {number} expenseId
|
||||
*/
|
||||
async publish(expenseId: number): Promise<void> {
|
||||
const { Expense } = this.models;
|
||||
|
||||
await Expense.query().findById(expenseId).patch({
|
||||
super.update({
|
||||
id: expenseId,
|
||||
publishedAt: moment().toMySqlDateTime(),
|
||||
});
|
||||
this.flushCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given expense.
|
||||
* @param {number} expenseId
|
||||
*/
|
||||
async delete(expenseId: number): Promise<void> {
|
||||
const { Expense, ExpenseCategory } = this.models;
|
||||
|
||||
await ExpenseCategory.query().where('expense_id', expenseId).delete();
|
||||
await Expense.query().where('id', expenseId).delete();
|
||||
|
||||
this.flushCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes expenses in bulk.
|
||||
* @param {number[]} expensesIds
|
||||
*/
|
||||
async bulkDelete(expensesIds: number[]): Promise<void> {
|
||||
const { Expense, ExpenseCategory } = this.models;
|
||||
|
||||
await ExpenseCategory.query().whereIn('expense_id', expensesIds).delete();
|
||||
await Expense.query().whereIn('id', expensesIds).delete();
|
||||
|
||||
this.flushCache();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,18 +26,10 @@ export default class ExpenseRepository extends TenantRepository {
|
||||
* @param {number[]} expensesIds
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
async bulkPublish(expensesIds: number): Promise<void> {
|
||||
const { Expense } = this.models;
|
||||
await Expense.query().whereIn('id', expensesIds).patch({
|
||||
async whereIdInPublish(expensesIds: number): Promise<void> {
|
||||
await this.model.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