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:
Ahmed Bouhuolia
2020-12-13 19:50:59 +02:00
parent e9e4ddaee0
commit 188e411f02
78 changed files with 1634 additions and 869 deletions

View File

@@ -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`);
}
}