refactor: split the services to multiple service classes (#202)

This commit is contained in:
Ahmed Bouhuolia
2023-08-10 20:29:39 +02:00
committed by GitHub
parent ffef627dc3
commit 26c6ca9e36
150 changed files with 7188 additions and 5007 deletions

View File

@@ -1,20 +1,16 @@
import { Service, Inject } from 'typedi';
import events from '@/subscribers/events';
import TenancyService from '@/services/Tenancy/TenancyService';
import BillsService from '@/services/Purchases/Bills';
import {
IBillCreatedPayload,
IBillEditedPayload,
IBIllEventDeletedPayload,
} from '@/interfaces';
import { BillInventoryTransactions } from '@/services/Purchases/Bills/BillInventoryTransactions';
@Service()
export default class BillWriteInventoryTransactionsSubscriber {
@Inject()
tenancy: TenancyService;
@Inject()
billsService: BillsService;
private billsInventory: BillInventoryTransactions;
/**
* Attaches events with handles.
@@ -42,7 +38,7 @@ export default class BillWriteInventoryTransactionsSubscriber {
billId,
trx,
}: IBillCreatedPayload) => {
await this.billsService.recordInventoryTransactions(
await this.billsInventory.recordInventoryTransactions(
tenantId,
billId,
false,
@@ -58,7 +54,7 @@ export default class BillWriteInventoryTransactionsSubscriber {
billId,
trx,
}: IBillEditedPayload) => {
await this.billsService.recordInventoryTransactions(
await this.billsInventory.recordInventoryTransactions(
tenantId,
billId,
true,
@@ -74,6 +70,10 @@ export default class BillWriteInventoryTransactionsSubscriber {
billId,
trx,
}: IBIllEventDeletedPayload) => {
await this.billsService.revertInventoryTransactions(tenantId, billId, trx);
await this.billsInventory.revertInventoryTransactions(
tenantId,
billId,
trx
);
};
}

View File

@@ -1,9 +1,6 @@
import { Inject, Service } from 'typedi';
import { map, head } from 'lodash';
import events from '@/subscribers/events';
import TenancyService from '@/services/Tenancy/TenancyService';
import SaleInvoicesCost from '@/services/Sales/SalesInvoicesCost';
import InventoryItemsQuantitySync from '@/services/Inventory/InventoryItemsQuantitySync';
import InventoryService from '@/services/Inventory/Inventory';
import {
@@ -12,23 +9,21 @@ import {
IInventoryTransactionsDeletedPayload,
} from '@/interfaces';
import { runAfterTransaction } from '@/services/UnitOfWork/TransactionsHooks';
import { SaleInvoicesCost } from '@/services/Sales/Invoices/SalesInvoicesCost';
@Service()
export default class InventorySubscriber {
@Inject()
saleInvoicesCost: SaleInvoicesCost;
private saleInvoicesCost: SaleInvoicesCost;
@Inject()
tenancy: TenancyService;
private itemsQuantitySync: InventoryItemsQuantitySync;
@Inject()
itemsQuantitySync: InventoryItemsQuantitySync;
@Inject()
inventoryService: InventoryService;
private inventoryService: InventoryService;
@Inject('agenda')
agenda: any;
private agenda: any;
/**
* Attaches events with handlers.

View File

@@ -1,7 +1,6 @@
import { Inject, Service } from 'typedi';
import events from '@/subscribers/events';
import BillPaymentsService from '@/services/Purchases/BillPayments/BillPayments';
import TenancyService from '@/services/Tenancy/TenancyService';
import { BillPaymentBillSync } from '@/services/Purchases/BillPayments/BillPaymentBillSync';
import {
IBillPaymentEventCreatedPayload,
IBillPaymentEventDeletedPayload,
@@ -11,16 +10,13 @@ import {
@Service()
export default class PaymentSyncBillBalance {
@Inject()
tenancy: TenancyService;
@Inject()
billPaymentsService: BillPaymentsService;
private billPaymentsService: BillPaymentBillSync;
/**
*
* @param bus
*/
attach(bus) {
public attach(bus) {
bus.subscribe(
events.billPayment.onCreated,
this.handleBillsIncrementPaymentAmount
@@ -34,6 +30,7 @@ export default class PaymentSyncBillBalance {
this.handleBillDecrementPaymentAmount
);
}
/**
* Handle bill payment amount increment/decrement once bill payment created or edited.
*/

View File

@@ -1,13 +1,13 @@
import { Service, Inject } from 'typedi';
import events from '@/subscribers/events';
import { EventSubscriber } from '@/lib/EventPublisher/EventPublisher';
import PaymentReceiveService from '@/services/Sales/PaymentReceives/PaymentsReceives';
import { PaymentReceiveIncrement } from '@/services/Sales/PaymentReceives/PaymentReceiveIncrement';
import { IPaymentReceiveCreatedPayload } from '@/interfaces';
@Service()
export default class PaymentReceiveAutoSerialSubscriber extends EventSubscriber {
@Inject()
paymentReceivesService: PaymentReceiveService;
private paymentIncrement: PaymentReceiveIncrement;
/**
* Attaches the events with handles.
@@ -29,8 +29,6 @@ export default class PaymentReceiveAutoSerialSubscriber extends EventSubscriber
paymentReceiveId,
trx,
}: IPaymentReceiveCreatedPayload) => {
await this.paymentReceivesService.incrementNextPaymentReceiveNumber(
tenantId
);
await this.paymentIncrement.incrementNextPaymentReceiveNumber(tenantId);
};
}

View File

@@ -1,6 +1,6 @@
import { Inject, Service } from 'typedi';
import events from '@/subscribers/events';
import PaymentReceiveService from '@/services/Sales/PaymentReceives/PaymentsReceives';
import { PaymentReceiveInvoiceSync } from '@/services/Sales/PaymentReceives/PaymentReceiveInvoiceSync';
import {
IPaymentReceiveCreatedPayload,
IPaymentReceiveDeletedPayload,
@@ -8,15 +8,15 @@ import {
} from '@/interfaces';
@Service()
export default class PaymentReceiveSyncInvoices {
export default class PaymentReceiveSyncInvoicesSubscriber {
@Inject()
paymentReceivesService: PaymentReceiveService;
private paymentSyncInvoice: PaymentReceiveInvoiceSync;
/**
* Attaches the events to handles.
* @param bus
*/
attach(bus) {
public attach(bus) {
bus.subscribe(
events.paymentReceive.onCreated,
this.handleInvoiceIncrementPaymentOnceCreated
@@ -41,7 +41,7 @@ export default class PaymentReceiveSyncInvoices {
paymentReceive,
trx,
}: IPaymentReceiveCreatedPayload) => {
await this.paymentReceivesService.saveChangeInvoicePaymentAmount(
await this.paymentSyncInvoice.saveChangeInvoicePaymentAmount(
tenantId,
paymentReceive.entries,
null,
@@ -59,7 +59,7 @@ export default class PaymentReceiveSyncInvoices {
oldPaymentReceive,
trx,
}: IPaymentReceiveEditedPayload) => {
await this.paymentReceivesService.saveChangeInvoicePaymentAmount(
await this.paymentSyncInvoice.saveChangeInvoicePaymentAmount(
tenantId,
paymentReceive.entries,
oldPaymentReceive?.entries || null,
@@ -76,7 +76,7 @@ export default class PaymentReceiveSyncInvoices {
oldPaymentReceive,
trx,
}: IPaymentReceiveDeletedPayload) => {
await this.paymentReceivesService.saveChangeInvoicePaymentAmount(
await this.paymentSyncInvoice.saveChangeInvoicePaymentAmount(
tenantId,
oldPaymentReceive.entries.map((entry) => ({
...entry,

View File

@@ -1,13 +1,13 @@
import { Service, Inject } from 'typedi';
import events from '@/subscribers/events';
import PaymentReceiveNotifyBySms from '@/services/Sales/PaymentReceives/PaymentReceiveSmsNotify';
import { PaymentReceiveNotifyBySms } from '@/services/Sales/PaymentReceives/PaymentReceiveSmsNotify';
import { IPaymentReceiveCreatedPayload } from '@/interfaces';
import { runAfterTransaction } from '@/services/UnitOfWork/TransactionsHooks';
@Service()
export default class SendSmsNotificationPaymentReceive {
@Inject()
paymentReceiveSmsNotify: PaymentReceiveNotifyBySms;
private paymentReceiveSmsNotify: PaymentReceiveNotifyBySms;
/**
* Attach events.

View File

@@ -1,13 +1,13 @@
import { Inject, Service } from 'typedi';
import events from '@/subscribers/events';
import SaleEstimateNotifyBySms from '@/services/Sales/Estimates/SaleEstimateSmsNotify';
import { SaleEstimateNotifyBySms } from '@/services/Sales/Estimates/SaleEstimateSmsNotify';
import { ISaleEstimateCreatedPayload } from '@/interfaces';
import { runAfterTransaction } from '@/services/UnitOfWork/TransactionsHooks';
@Service()
export default class SaleEstimateSmsNotificationSubscriber {
@Inject()
saleEstimateNotifyBySms: SaleEstimateNotifyBySms;
private saleEstimateNotifyBySms: SaleEstimateNotifyBySms;
/**
* Attaches events to handles.events.saleEstimate.onCreated

View File

@@ -1,13 +1,13 @@
import { Inject, Service } from 'typedi';
import { EventSubscriber } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events';
import SaleInvoicesService from '@/services/Sales/SalesInvoices';
import { SaleInvoiceIncrement } from '@/services/Sales/Invoices/SaleInvoiceIncrement';
import { ISaleInvoiceCreatedPayload } from '@/interfaces';
@Service()
export default class SaleInvoiceAutoIncrementSubscriber extends EventSubscriber {
@Inject()
saleInvoicesService: SaleInvoicesService;
private saleInvoicesService: SaleInvoiceIncrement;
/**
* Constructor method.

View File

@@ -1,13 +1,13 @@
import { Inject, Service } from 'typedi';
import { EventSubscriber } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events';
import SaleEstimateService from '@/services/Sales/SalesEstimate';
import { ConvertSaleEstimate } from '@/services/Sales/Estimates/ConvetSaleEstimate';
import { ISaleInvoiceCreatedPayload } from '@/interfaces';
import events from '@/subscribers/events';
@Service()
export default class SaleInvoiceConvertFromEstimateSubscriber extends EventSubscriber {
@Inject()
saleEstimatesService: SaleEstimateService;
private convertEstimateToInvoiceService: ConvertSaleEstimate;
/**
* Constructor method.
@@ -30,7 +30,7 @@ export default class SaleInvoiceConvertFromEstimateSubscriber extends EventSubsc
trx,
}: ISaleInvoiceCreatedPayload) => {
if (saleInvoiceDTO.fromEstimateId) {
await this.saleEstimatesService.convertEstimateToInvoice(
await this.convertEstimateToInvoiceService.convertEstimateToInvoice(
tenantId,
saleInvoiceDTO.fromEstimateId,
saleInvoiceId,

View File

@@ -1,13 +1,13 @@
import { Inject, Service } from 'typedi';
import events from '@/subscribers/events';
import SaleInvoiceNotifyBySms from '@/services/Sales/SaleInvoiceNotifyBySms';
import { SaleInvoiceNotifyBySms } from '@/services/Sales/Invoices/SaleInvoiceNotifyBySms';
import { ISaleInvoiceCreatedPayload } from '@/interfaces';
import { runAfterTransaction } from '@/services/UnitOfWork/TransactionsHooks';
@Service()
export default class SendSmsNotificationToCustomer {
@Inject()
saleInvoiceNotifyBySms: SaleInvoiceNotifyBySms;
private saleInvoiceNotifyBySms: SaleInvoiceNotifyBySms;
/**
* Attaches events with handlers.

View File

@@ -1,20 +1,16 @@
import { Service, Inject } from 'typedi';
import events from '@/subscribers/events';
import TenancyService from '@/services/Tenancy/TenancyService';
import SaleInvoicesService from '@/services/Sales/SalesInvoices';
import {
ISaleInvoiceCreatedPayload,
ISaleInvoiceDeletedPayload,
ISaleInvoiceEditedPayload,
} from '@/interfaces';
import { InvoiceInventoryTransactions } from '@/services/Sales/Invoices/InvoiceInventoryTransactions';
@Service()
export default class WriteInventoryTransactions {
@Inject()
tenancy: TenancyService;
@Inject()
saleInvoicesService: SaleInvoicesService;
private saleInvoiceInventory: InvoiceInventoryTransactions;
/**
* Attaches events with handles
@@ -43,7 +39,7 @@ export default class WriteInventoryTransactions {
saleInvoice,
trx,
}: ISaleInvoiceCreatedPayload) => {
await this.saleInvoicesService.recordInventoryTranscactions(
await this.saleInvoiceInventory.recordInventoryTranscactions(
tenantId,
saleInvoice,
false,
@@ -53,14 +49,14 @@ export default class WriteInventoryTransactions {
/**
* Rewriting the inventory transactions once the sale invoice be edited.
* @param {ISaleInvoiceEditPayload} payload -
* @param {ISaleInvoiceEditPayload} payload -
*/
private handleRewritingInventoryTransactions = async ({
tenantId,
saleInvoice,
trx,
}: ISaleInvoiceEditedPayload) => {
await this.saleInvoicesService.recordInventoryTranscactions(
await this.saleInvoiceInventory.recordInventoryTranscactions(
tenantId,
saleInvoice,
true,
@@ -70,7 +66,7 @@ export default class WriteInventoryTransactions {
/**
* Handles deleting the inventory transactions once the invoice deleted.
* @param {ISaleInvoiceDeletedPayload} payload -
* @param {ISaleInvoiceDeletedPayload} payload -
*/
private handleDeletingInventoryTransactions = async ({
tenantId,
@@ -78,7 +74,7 @@ export default class WriteInventoryTransactions {
oldSaleInvoice,
trx,
}: ISaleInvoiceDeletedPayload) => {
await this.saleInvoicesService.revertInventoryTransactions(
await this.saleInvoiceInventory.revertInventoryTransactions(
tenantId,
saleInvoiceId,
trx

View File

@@ -1,12 +1,12 @@
import { Service, Inject } from 'typedi';
import events from '@/subscribers/events';
import SalesReceiptService from '@/services/Sales/SalesReceipts';
import { SaleReceiptIncrement } from '@/services/Sales/Receipts/SaleReceiptIncrement';
import { ISaleReceiptCreatedPayload } from '@/interfaces';
@Service()
export default class SaleReceiptAutoSerialSubscriber {
@Inject()
saleReceiptsService: SalesReceiptService;
private saleReceiptsService: SaleReceiptIncrement;
/**
*

View File

@@ -1,13 +1,13 @@
import { Inject, Service } from 'typedi';
import events from '@/subscribers/events';
import SaleReceiptNotifyBySms from '@/services/Sales/SaleReceiptNotifyBySms';
import { SaleReceiptNotifyBySms } from '@/services/Sales/Receipts/SaleReceiptNotifyBySms';
import { ISaleReceiptCreatedPayload } from '@/interfaces';
import { runAfterTransaction } from '@/services/UnitOfWork/TransactionsHooks';
@Service()
export default class SendSmsNotificationSaleReceipt {
@Inject()
saleReceiptNotifyBySms: SaleReceiptNotifyBySms;
private saleReceiptNotifyBySms: SaleReceiptNotifyBySms;
/**
* Attaches events with handlers.

View File

@@ -1,26 +1,22 @@
import { Inject } from 'typedi';
import { EventSubscriber } from 'event-dispatch';
import events from '@/subscribers/events';
import TenancyService from '@/services/Tenancy/TenancyService';
import SalesReceiptService from '@/services/Sales/SalesReceipts';
import {
ISaleReceiptCreatedPayload,
ISaleReceiptEditedPayload,
ISaleReceiptEventDeletedPayload,
} from '@/interfaces';
import { SaleReceiptInventoryTransactions } from '@/services/Sales/Receipts/SaleReceiptInventoryTransactions';
@EventSubscriber()
export default class SaleReceiptInventoryTransactionsSubscriber {
@Inject()
tenancy: TenancyService;
@Inject()
saleReceiptsService: SalesReceiptService;
private saleReceiptInventory: SaleReceiptInventoryTransactions;
/**
* Subscribe events to handles.
*/
attach(bus) {
public attach(bus) {
bus.subscribe(
events.saleReceipt.onCreated,
this.handleWritingInventoryTransactions
@@ -44,7 +40,7 @@ export default class SaleReceiptInventoryTransactionsSubscriber {
saleReceipt,
trx,
}: ISaleReceiptCreatedPayload) => {
await this.saleReceiptsService.recordInventoryTransactions(
await this.saleReceiptInventory.recordInventoryTransactions(
tenantId,
saleReceipt,
false,
@@ -61,7 +57,7 @@ export default class SaleReceiptInventoryTransactionsSubscriber {
saleReceipt,
trx,
}: ISaleReceiptEditedPayload) => {
await this.saleReceiptsService.recordInventoryTransactions(
await this.saleReceiptInventory.recordInventoryTransactions(
tenantId,
saleReceipt,
true,
@@ -78,7 +74,7 @@ export default class SaleReceiptInventoryTransactionsSubscriber {
saleReceiptId,
trx,
}: ISaleReceiptEventDeletedPayload) => {
await this.saleReceiptsService.revertInventoryTransactions(
await this.saleReceiptInventory.revertInventoryTransactions(
tenantId,
saleReceiptId,
trx

View File

@@ -1,21 +1,17 @@
import { Service, Inject } from 'typedi';
import events from '@/subscribers/events';
import TenancyService from '@/services/Tenancy/TenancyService';
import SalesReceiptService from '@/services/Sales/SalesReceipts';
import {
ISaleReceiptCreatedPayload,
ISaleReceiptEditedPayload,
ISaleReceiptEventDeletedPayload,
} from '@/interfaces';
import { SaleReceiptGLEntries } from '@/services/Sales/SaleReceiptGLEntries';
import { SaleReceiptGLEntries } from '@/services/Sales/Receipts/SaleReceiptGLEntries';
@Service()
export default class SaleReceiptWriteGLEntriesSubscriber {
@Inject()
tenancy: TenancyService;
@Inject()
saleReceiptGLEntries: SaleReceiptGLEntries;
private saleReceiptGLEntries: SaleReceiptGLEntries;
/**
* Attaches events with handlers.