fix: database migrations FK relations.

fix: database columns indexing.
This commit is contained in:
Ahmed Bouhuolia
2020-10-03 12:08:11 +02:00
parent 1250eccc0d
commit 0114ed9f8b
86 changed files with 788 additions and 801 deletions

View File

@@ -1,5 +1,6 @@
import TenantRepository from 'repositories/TenantRepository';
import { IAccount } from 'interfaces';
import { Account } from 'models';
export default class AccountRepository extends TenantRepository {
models: any;
@@ -57,7 +58,7 @@ export default class AccountRepository extends TenantRepository {
/**
* Retrieve the account by the given id.
* @param {number} id - Account id.
* @param {number} id - Account id.
* @return {IAccount}
*/
getById(id: number): IAccount {
@@ -67,4 +68,63 @@ export default class AccountRepository extends TenantRepository {
});
}
/**
* Retrieve accounts by the given ids.
* @param {number[]} ids -
* @return {IAccount[]}
*/
findByIds(accountsIds: number[]) {
const { Account } = this.models;
return Account.query().whereIn('id', accountsIds);
}
/**
* Activate the given account.
* @param {number} accountId -
* @return {void}
*/
async activate(accountId: number): Promise<void> {
const { Account } = this.models;
await Account.query().findById(accountId).patch({ active: 1 })
this.flushCache();
}
/**
* Inserts a new accounts to the storage.
* @param {IAccount} account
*/
async insert(account: IAccount): Promise<void> {
const { Account } = this.models;
await Account.query().insertAndFetch({ ...account });
this.flushCache();
}
/**
* Updates account of the given account.
* @param {number} accountId - Account id.
* @param {IAccount} account
* @return {void}
*/
async edit(accountId: number, account: IAccount): Promise<void> {
const { Account } = this.models;
await Account.query().findById(accountId).patch({ ...account });
this.flushCache();
}
/**
* Deletes the given account by id.
* @param {number} accountId - Account id.
*/
async deleteById(accountId: number): Promise<void> {
const { Account } = this.models;
await Account.query().deleteById(accountId);
this.flushCache();
}
/**
* Flush repository cache.
*/
flushCache(): void {
this.cache.delStartWith('accounts');
}
}

View File

@@ -76,4 +76,11 @@ export default class AccountTypeRepository extends TenantRepository {
return AccountType.query().where('root_type', rootType);
});
}
/**
* Flush repository cache.
*/
flushCache() {
this.cache.delStartWith('accountType');
}
}

View File

@@ -1,4 +1,6 @@
import TenantRepository from 'repositories/TenantRepository';
import { IContact } from 'interfaces';
import Contact from 'models/Contact';
export default class ContactRepository extends TenantRepository {
cache: any;
@@ -17,21 +19,70 @@ export default class ContactRepository extends TenantRepository {
this.cache = this.tenancy.cache(tenantId);
}
findById(contactId: number) {
/**
* Retrieve the given contact model.
* @param {number} contactId
*/
findById(contactId: number): IContact {
const { Contact } = this.models;
return this.cache.get(`contact.id.${contactId}`, () => {
return this.cache.get(`contacts.id.${contactId}`, () => {
return Contact.query().findById(contactId);
})
}
findByIds(contactIds: number[]) {
/**
* Retrieve the given contacts model.
* @param {number[]} contactIds - Contacts ids.
*/
findByIds(contactIds: number[]): IContact[] {
const { Contact } = this.models;
return this.cache.get(`contact.ids.${contactIds.join(',')}`, () => {
return this.cache.get(`contacts.ids.${contactIds.join(',')}`, () => {
return Contact.query().whereIn('id', contactIds);
});
}
insert(contact) {
/**
* Inserts a new contact model.
* @param contact
*/
async insert(contact) {
await Contact.query().insert({ ...contact })
this.flushCache();
}
/**
* Updates the contact details.
* @param {number} contactId - Contact id.
* @param {IContact} contact - Contact input.
*/
async update(contactId: number, contact: IContact) {
await Contact.query().findById(contactId).patch({ ...contact });
this.flushCache();
}
/**
* Deletes contact of the given id.
* @param {number} contactId -
* @return {Promise<void>}
*/
async deleteById(contactId: number): Promise<void> {
await Contact.query().where('id', contactId).delete();
this.flushCache();
}
/**
* Deletes contacts in bulk.
* @param {number[]} contactsIds
*/
async bulkDelete(contactsIds: number[]) {
await Contact.query().whereIn('id', contactsIds);
this.flushCache();
}
/**
* Flush contact repository cache.
*/
flushCache() {
this.cache.delStartWith(`contacts`);
}
}

View File

@@ -1,26 +0,0 @@
import { Customer } from 'models';
export default class CustomerRepository {
static changeDiffBalance(customerId, oldCustomerId, amount, oldAmount) {
const diffAmount = amount - oldAmount;
const asyncOpers = [];
if (customerId != oldCustomerId) {
const oldCustomerOper = Customer.changeBalance(
oldCustomerId,
(oldAmount * -1)
);
const customerOper = Customer.changeBalance(
customerId,
amount,
);
asyncOpers.push(customerOper);
asyncOpers.push(oldCustomerOper);
} else {
const balanceChangeOper = Customer.changeBalance(customerId, diffAmount);
asyncOpers.push(balanceChangeOper);
}
return Promise.all(asyncOpers);
}
}

View File

@@ -17,7 +17,7 @@ export default class CustomerRepository extends TenantRepository {
/**
* Retrieve customer details of the given id.
* @param {number} customerId -
* @param {number} customerId - Customer id.
*/
getById(customerId: number) {
const { Contact } = this.models;

View File

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

View File

@@ -1,3 +1,4 @@
import { IVendor } from "interfaces";
import TenantRepository from "./TenantRepository";
@@ -18,7 +19,7 @@ export default class VendorRepository extends TenantRepository {
/**
* Retrieve the bill that associated to the given vendor id.
* @param {number} vendorId
* @param {number} vendorId - Vendor id.
*/
getBills(vendorId: number) {
const { Bill } = this.models;
@@ -29,16 +30,17 @@ export default class VendorRepository extends TenantRepository {
}
/**
*
* Retrieve all the given vendors.
* @param {numner[]} vendorsIds
* @return {IVendor}
*/
vendors(vendorsIds: number[]) {
vendors(vendorsIds: number[]): IVendor[] {
const { Contact } = this.models;
return Contact.query().modifier('vendor').whereIn('id', vendorsIds);
}
/**
*
* Retrieve vendors with associated bills.
* @param {number[]} vendorIds
*/
vendorsWithBills(vendorIds: number[]) {