- feat: Optimize tenancy software architecture.

This commit is contained in:
Ahmed Bouhuolia
2020-08-30 22:11:14 +02:00
parent 74321a2886
commit ca251a2d28
53 changed files with 1581 additions and 1055 deletions

View File

@@ -74,7 +74,7 @@ export default class Bill extends mixin(TenantModel, [CachableModel]) {
* @return {Array}
*/
static async getNotFoundBills(billsIds, vendorId) {
const storedBills = await this.tenant().query()
const storedBills = await this.query()
.onBuild((builder) => {
builder.whereIn('id', billsIds);
@@ -94,8 +94,7 @@ export default class Bill extends mixin(TenantModel, [CachableModel]) {
static changePaymentAmount(billId, amount) {
const changeMethod = amount > 0 ? 'increment' : 'decrement';
return this.tenant()
.query()
return this.query()
.where('id', billId)
[changeMethod]('payment_amount', Math.abs(amount));
}

View File

@@ -33,10 +33,9 @@ export default class Customer extends TenantModel {
* @param {Numeric} amount
*/
static async changeBalance(customerId, amount) {
const changeMethod = amount > 0 ? 'increment' : 'decrement';
const changeMethod = (amount > 0) ? 'increment' : 'decrement';
await this.tenant()
.query()
return this.query()
.where('id', customerId)
[changeMethod]('balance', Math.abs(amount));
}
@@ -47,8 +46,7 @@ export default class Customer extends TenantModel {
* @param {Integer} amount
*/
static async incrementBalance(customerId, amount) {
await this.tenant()
.query()
return this.query()
.where('id', customerId)
.increment('balance', amount);
}
@@ -59,9 +57,25 @@ export default class Customer extends TenantModel {
* @param {integer} amount -
*/
static async decrementBalance(customerId, amount) {
await this.tenant()
.query()
await this.query()
.where('id', customerId)
.decrement('balance', amount);
}
static changeDiffBalance(customerId, oldCustomerId, amount, oldAmount) {
const diffAmount = amount - oldAmount;
const asyncOpers = [];
if (customerId != oldCustomerId) {
const oldCustomerOper = this.changeBalance(oldCustomerId, (oldAmount * -1));
const customerOper = this.changeBalance(customerId, amount);
asyncOpers.push(customerOper);
asyncOpers.push(oldCustomerOper);
} else {
const balanceChangeOper = this.changeBalance(customerId, diffAmount);
asyncOpers.push(balanceChangeOper);
}
return Promise.all(asyncOpers);
}
}

View File

@@ -17,13 +17,12 @@ export default class ItemEntry extends TenantModel {
return ['created_at', 'updated_at'];
}
/**
* Relationship mapping.
*/
static get relationMappings() {
return {
static get virtualAttributes() {
return ['amount'];
}
};
static amount() {
return this.calcAmount(this);
}
static calcAmount(itemEntry) {

View File

@@ -119,8 +119,7 @@ export default class SaleInvoice extends mixin(TenantModel, [CachableModel]) {
static async changePaymentAmount(invoiceId, amount) {
const changeMethod = amount > 0 ? 'increment' : 'decrement';
await this.tenant()
.query()
await this.query()
.where('id', invoiceId)
[changeMethod]('payment_amount', Math.abs(amount));
}

View File

@@ -25,8 +25,7 @@ export default class Vendor extends TenantModel {
static async changeBalance(vendorId, amount) {
const changeMethod = amount > 0 ? 'increment' : 'decrement';
return this.tenant()
.query()
return this.query()
.where('id', vendorId)
[changeMethod]('balance', Math.abs(amount));
}