feat(nestjs): migrate to NestJS

This commit is contained in:
Ahmed Bouhuolia
2025-04-07 11:51:24 +02:00
parent f068218a16
commit 55fcc908ef
3779 changed files with 631 additions and 195332 deletions

View File

@@ -0,0 +1,117 @@
import { isEmpty } from 'lodash';
import {
IBIllEventDeletedPayload,
IBillCreatedPayload,
IBillCreatingPayload,
IBillEditedPayload,
} from '@/modules/Bills/Bills.types';
import { ValidateAttachments } from '../ValidateAttachments';
import { OnEvent } from '@nestjs/event-emitter';
import { Injectable } from '@nestjs/common';
import { UnlinkAttachment } from '../UnlinkAttachment';
import { LinkAttachment } from '../LinkAttachment';
import { events } from '@/common/events/events';
@Injectable()
export class AttachmentsOnBills {
/**
* @param {LinkAttachment} linkAttachmentService
* @param {UnlinkAttachment} unlinkAttachmentService
* @param {ValidateAttachments} validateDocuments
*/
constructor(
private readonly linkAttachmentService: LinkAttachment,
private readonly unlinkAttachmentService: UnlinkAttachment,
private readonly validateDocuments: ValidateAttachments,
) {}
/**
* Validates the attachment keys on creating bill.
* @param {ISaleInvoiceCreatingPaylaod}
* @returns {Promise<void>}
*/
@OnEvent(events.bill.onCreating)
async validateAttachmentsOnBillCreate({
billDTO,
}: IBillCreatingPayload): Promise<void> {
if (isEmpty(billDTO.attachments)) {
return;
}
const documentKeys = billDTO?.attachments?.map((a) => a.key);
await this.validateDocuments.validate(documentKeys);
}
/**
* Handles linking the attachments of the created bill.
* @param {ISaleInvoiceCreatedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.bill.onCreated)
async handleAttachmentsOnBillCreated({
bill,
billDTO,
trx,
}: IBillCreatedPayload): Promise<void> {
if (isEmpty(billDTO.attachments)) return;
const keys = billDTO.attachments?.map((attachment) => attachment.key);
await this.linkAttachmentService.bulkLink(keys, 'Bill', bill.id, trx);
}
/**
* Handles unlinking all the unpresented keys of the edited bill.
* @param {IBillEditedPayload}
*/
@OnEvent(events.bill.onEdited)
async handleUnlinkUnpresentedKeysOnBillEdited({
billDTO,
bill,
trx,
}: IBillEditedPayload) {
const keys = billDTO.attachments?.map((attachment) => attachment.key);
await this.unlinkAttachmentService.unlinkUnpresentedKeys(
keys,
'Bill',
bill.id,
trx,
);
}
/**
* Handles linking all the presented keys of the edited bill.
* @param {ISaleInvoiceEditedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.bill.onEdited)
async handleLinkPresentedKeysOnBillEdited({
billDTO,
oldBill,
trx,
}: IBillEditedPayload) {
if (isEmpty(billDTO.attachments)) return;
const keys = billDTO.attachments?.map((attachment) => attachment.key);
await this.linkAttachmentService.bulkLink(keys, 'Bill', oldBill.id, trx);
}
/**
* Unlink all attachments once the bill deleted.
* @param {ISaleInvoiceDeletedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.bill.onDeleting)
async handleUnlinkAttachmentsOnBillDeleted({
oldBill,
trx,
}: IBIllEventDeletedPayload) {
await this.unlinkAttachmentService.unlinkAllModelKeys(
'Bill',
oldBill.id,
trx,
);
}
}

View File

@@ -0,0 +1,129 @@
import { isEmpty } from 'lodash';
import {
ICreditNoteCreatedPayload,
ICreditNoteCreatingPayload,
ICreditNoteDeletingPayload,
ICreditNoteEditedPayload,
} from '@/modules/CreditNotes/types/CreditNotes.types';
import { ValidateAttachments } from '../ValidateAttachments';
import { Injectable } from '@nestjs/common';
import { LinkAttachment } from '../LinkAttachment';
import { UnlinkAttachment } from '../UnlinkAttachment';
import { OnEvent } from '@nestjs/event-emitter';
import { events } from '@/common/events/events';
@Injectable()
export class AttachmentsOnCreditNote {
/**
* @param {LinkAttachment} linkAttachmentService -
* @param {UnlinkAttachment} unlinkAttachmentService -
* @param {ValidateAttachments} validateDocuments -
*/
constructor(
private readonly linkAttachmentService: LinkAttachment,
private readonly unlinkAttachmentService: UnlinkAttachment,
private readonly validateDocuments: ValidateAttachments,
) {}
/**
* Validates the attachment keys on creating credit note.
* @param {ICreditNoteCreatingPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.creditNote.onCreating)
async validateAttachmentsOnCreditNoteCreate({
creditNoteDTO,
}: ICreditNoteCreatingPayload): Promise<void> {
if (isEmpty(creditNoteDTO.attachments)) {
return;
}
const documentKeys = creditNoteDTO?.attachments?.map((a) => a.key);
await this.validateDocuments.validate(documentKeys);
}
/**
* Handles linking the attachments of the created credit note.
* @param {ICreditNoteCreatedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.creditNote.onCreated)
async handleAttachmentsOnCreditNoteCreated({
creditNote,
creditNoteDTO,
trx,
}: ICreditNoteCreatedPayload): Promise<void> {
if (isEmpty(creditNoteDTO.attachments)) return;
const keys = creditNoteDTO.attachments?.map((attachment) => attachment.key);
await this.linkAttachmentService.bulkLink(
keys,
'CreditNote',
creditNote.id,
trx,
);
}
/**
* Handles unlinking all the unpresented keys of the edited credit note.
* @param {ICreditNoteEditedPayload}
*/
@OnEvent(events.creditNote.onEdited)
async handleUnlinkUnpresentedKeysOnCreditNoteEdited({
creditNoteEditDTO,
oldCreditNote,
trx,
}: ICreditNoteEditedPayload) {
const keys = creditNoteEditDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.unlinkAttachmentService.unlinkUnpresentedKeys(
keys,
'CreditNote',
oldCreditNote.id,
trx,
);
}
/**
* Handles linking all the presented keys of the edited credit note.
* @param {ICreditNoteEditedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.creditNote.onEdited)
async handleLinkPresentedKeysOnCreditNoteEdited({
creditNoteEditDTO,
oldCreditNote,
trx,
}: ICreditNoteEditedPayload) {
if (isEmpty(creditNoteEditDTO.attachments)) return;
const keys = creditNoteEditDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.linkAttachmentService.bulkLink(
keys,
'CreditNote',
oldCreditNote.id,
trx,
);
}
/**
* Unlink all attachments once the credit note deleted.
* @param {ICreditNoteDeletingPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.creditNote.onDeleting)
async handleUnlinkAttachmentsOnCreditNoteDeleted({
oldCreditNote,
trx,
}: ICreditNoteDeletingPayload) {
await this.unlinkAttachmentService.unlinkAllModelKeys(
'CreditNote',
oldCreditNote.id,
trx,
);
}
}

View File

@@ -0,0 +1,120 @@
import { isEmpty } from 'lodash';
import { OnEvent } from '@nestjs/event-emitter';
import { Injectable } from '@nestjs/common';
import {
IExpenseCreatedPayload,
IExpenseCreatingPayload,
IExpenseDeletingPayload,
IExpenseEventEditPayload,
} from '@/modules/Expenses/Expenses.types';
import { ValidateAttachments } from '../ValidateAttachments';
import { UnlinkAttachment } from '../UnlinkAttachment';
import { LinkAttachment } from '../LinkAttachment';
import { events } from '@/common/events/events';
@Injectable()
export class AttachmentsOnExpenses {
constructor(
private readonly linkAttachmentService: LinkAttachment,
private readonly unlinkAttachmentService: UnlinkAttachment,
private readonly validateDocuments: ValidateAttachments,
) {}
/**
* Validates the attachment keys on creating expense.
* @param {ISaleInvoiceCreatingPaylaod}
* @returns {Promise<void>}
*/
@OnEvent(events.expenses.onCreating)
async validateAttachmentsOnExpenseCreate({
expenseDTO,
}: IExpenseCreatingPayload): Promise<void> {
if (isEmpty(expenseDTO.attachments)) {
return;
}
const documentKeys = expenseDTO?.attachments?.map((a) => a.key);
await this.validateDocuments.validate(documentKeys);
}
/**
* Handles linking the attachments of the created expense.
* @param {ISaleInvoiceCreatedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.expenses.onCreated)
async handleAttachmentsOnExpenseCreated({
expenseDTO,
expense,
trx,
}: IExpenseCreatedPayload): Promise<void> {
if (isEmpty(expenseDTO.attachments)) return;
const keys = expenseDTO.attachments?.map((attachment) => attachment.key);
await this.linkAttachmentService.bulkLink(
keys,
'Expense',
expense.id,
trx,
);
}
/**
* Handles unlinking all the unpresented keys of the edited expense.
* @param {ISaleInvoiceEditedPayload}
*/
@OnEvent(events.expenses.onEdited)
async handleUnlinkUnpresentedKeysOnExpenseEdited({
expenseDTO,
expense,
trx,
}: IExpenseEventEditPayload) {
const keys = expenseDTO.attachments?.map((attachment) => attachment.key);
await this.unlinkAttachmentService.unlinkUnpresentedKeys(
keys,
'Expense',
expense.id,
trx,
);
}
/**
* Handles linking all the presented keys of the edited expense.
* @param {ISaleInvoiceEditedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.expenses.onEdited)
async handleLinkPresentedKeysOnExpenseEdited({
expenseDTO,
oldExpense,
trx,
}: IExpenseEventEditPayload) {
if (isEmpty(expenseDTO.attachments)) return;
const keys = expenseDTO.attachments?.map((attachment) => attachment.key);
await this.linkAttachmentService.bulkLink(
keys,
'Expense',
oldExpense.id,
trx,
);
}
/**
* Unlink all attachments once the expense deleted.
* @param {ISaleInvoiceDeletedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.expenses.onDeleting)
async handleUnlinkAttachmentsOnExpenseDeleted({
oldExpense,
trx,
}: IExpenseDeletingPayload) {
await this.unlinkAttachmentService.unlinkAllModelKeys(
'Expense',
oldExpense.id,
trx,
);
}
}

View File

@@ -0,0 +1,125 @@
import { isEmpty } from 'lodash';
import {
IManualJournalCreatingPayload,
IManualJournalEventCreatedPayload,
IManualJournalEventDeletedPayload,
IManualJournalEventEditedPayload,
} from '@/modules/ManualJournals/types/ManualJournals.types';
import { ValidateAttachments } from '../ValidateAttachments';
import { OnEvent } from '@nestjs/event-emitter';
import { UnlinkAttachment } from '../UnlinkAttachment';
import { LinkAttachment } from '../LinkAttachment';
import { Injectable } from '@nestjs/common';
import { events } from '@/common/events/events';
@Injectable()
export class AttachmentsOnManualJournals {
constructor(
private readonly linkAttachmentService: LinkAttachment,
private readonly unlinkAttachmentService: UnlinkAttachment,
private readonly validateDocuments: ValidateAttachments,
) {}
/**
* Validates the attachment keys on creating manual journal.
* @param {IManualJournalCreatingPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.manualJournals.onCreating)
async validateAttachmentsOnManualJournalCreate({
manualJournalDTO,
}: IManualJournalCreatingPayload): Promise<void> {
if (isEmpty(manualJournalDTO.attachments)) {
return;
}
const documentKeys = manualJournalDTO?.attachments?.map((a) => a.key);
await this.validateDocuments.validate(documentKeys);
}
/**
* Handles linking the attachments of the created manual journal.
* @param {IManualJournalEventCreatedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.manualJournals.onCreated)
async handleAttachmentsOnManualJournalCreated({
manualJournalDTO,
manualJournal,
trx,
}: IManualJournalEventCreatedPayload): Promise<void> {
if (isEmpty(manualJournalDTO.attachments)) return;
const keys = manualJournalDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.linkAttachmentService.bulkLink(
keys,
'ManualJournal',
manualJournal.id,
trx,
);
}
/**
* Handles unlinking all the unpresented keys of the edited manual journal.
* @param {ISaleInvoiceEditedPayload}
*/
@OnEvent(events.manualJournals.onEdited)
async handleUnlinkUnpresentedKeysOnManualJournalEdited({
manualJournalDTO,
manualJournal,
trx,
}: IManualJournalEventEditedPayload) {
const keys = manualJournalDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.unlinkAttachmentService.unlinkUnpresentedKeys(
keys,
'SaleInvoice',
manualJournal.id,
trx,
);
}
/**
* Handles linking all the presented keys of the edited manual journal.
* @param {ISaleInvoiceEditedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.manualJournals.onEdited)
async handleLinkPresentedKeysOnManualJournalEdited({
manualJournalDTO,
oldManualJournal,
trx,
}: IManualJournalEventEditedPayload) {
if (isEmpty(manualJournalDTO.attachments)) return;
const keys = manualJournalDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.linkAttachmentService.bulkLink(
keys,
'ManualJournal',
oldManualJournal.id,
trx,
);
}
/**
* Unlink all attachments once the manual journal deleted.
* @param {ISaleInvoiceDeletedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.manualJournals.onDeleting)
async handleUnlinkAttachmentsOnManualJournalDeleted({
oldManualJournal,
trx,
}: IManualJournalEventDeletedPayload) {
await this.unlinkAttachmentService.unlinkAllModelKeys(
'SaleInvoice',
oldManualJournal.id,
trx,
);
}
}

View File

@@ -0,0 +1,125 @@
import { isEmpty } from 'lodash';
import {
IBillPaymentCreatingPayload,
IBillPaymentDeletingPayload,
IBillPaymentEventCreatedPayload,
IBillPaymentEventEditedPayload,
} from '@/modules/BillPayments/types/BillPayments.types';
import { ValidateAttachments } from '../ValidateAttachments';
import { Injectable } from '@nestjs/common';
import { LinkAttachment } from '../LinkAttachment';
import { UnlinkAttachment } from '../UnlinkAttachment';
import { events } from '@/common/events/events';
import { OnEvent } from '@nestjs/event-emitter';
@Injectable()
export class AttachmentsOnBillPayments {
constructor(
private readonly linkAttachmentService: LinkAttachment,
private readonly unlinkAttachmentService: UnlinkAttachment,
private readonly validateDocuments: ValidateAttachments,
) {}
/**
* Validates the attachment keys on creating bill payment.
* @param {IBillPaymentCreatingPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.billPayment.onCreating)
async validateAttachmentsOnBillPaymentCreate({
billPaymentDTO,
}: IBillPaymentCreatingPayload): Promise<void> {
if (isEmpty(billPaymentDTO.attachments)) {
return;
}
const documentKeys = billPaymentDTO?.attachments?.map((a) => a.key);
await this.validateDocuments.validate(documentKeys);
}
/**
* Handles linking the attachments of the created bill payment.
* @param {IBillPaymentEventCreatedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.billPayment.onCreated)
async handleAttachmentsOnBillPaymentCreated({
billPaymentDTO,
billPayment,
trx,
}: IBillPaymentEventCreatedPayload): Promise<void> {
if (isEmpty(billPaymentDTO.attachments)) return;
const keys = billPaymentDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.linkAttachmentService.bulkLink(
keys,
'BillPayment',
billPayment.id,
trx,
);
}
/**
* Handles unlinking all the unpresented keys of the edited bill payment.
* @param {IBillPaymentEventEditedPayload}
*/
@OnEvent(events.billPayment.onEdited)
async handleUnlinkUnpresentedKeysOnBillPaymentEdited({
billPaymentDTO,
oldBillPayment,
trx,
}: IBillPaymentEventEditedPayload) {
const keys = billPaymentDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.unlinkAttachmentService.unlinkUnpresentedKeys(
keys,
'BillPayment',
oldBillPayment.id,
trx,
);
}
/**
* Handles linking all the presented keys of the edited bill payment.
* @param {IBillPaymentEventEditedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.billPayment.onEdited)
async handleLinkPresentedKeysOnBillPaymentEdited({
billPaymentDTO,
oldBillPayment,
trx,
}: IBillPaymentEventEditedPayload) {
if (isEmpty(billPaymentDTO.attachments)) return;
const keys = billPaymentDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.linkAttachmentService.bulkLink(
keys,
'BillPayment',
oldBillPayment.id,
trx,
);
}
/**
* Unlink all attachments once the bill payment deleted.
* @param {IBillPaymentDeletingPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.billPayment.onDeleting)
async handleUnlinkAttachmentsOnBillPaymentDeleted({
oldBillPayment,
trx,
}: IBillPaymentDeletingPayload) {
await this.unlinkAttachmentService.unlinkAllModelKeys(
'BillPayment',
oldBillPayment.id,
trx,
);
}
}

View File

@@ -0,0 +1,125 @@
import { isEmpty } from 'lodash';
import {
IPaymentReceivedCreatedPayload,
IPaymentReceivedCreatingPayload,
IPaymentReceivedDeletingPayload,
IPaymentReceivedEditedPayload,
} from '@/modules/PaymentReceived/types/PaymentReceived.types';
import { ValidateAttachments } from '../ValidateAttachments';
import { OnEvent } from '@nestjs/event-emitter';
import { Injectable } from '@nestjs/common';
import { UnlinkAttachment } from '../UnlinkAttachment';
import { LinkAttachment } from '../LinkAttachment';
import { events } from '@/common/events/events';
@Injectable()
export class AttachmentsOnPaymentsReceived {
constructor(
private readonly linkAttachmentService: LinkAttachment,
private readonly unlinkAttachmentService: UnlinkAttachment,
private readonly validateDocuments: ValidateAttachments,
) {}
/**
* Validates the attachment keys on creating payment.
* @param {IPaymentReceivedCreatingPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.paymentReceive.onCreating)
async validateAttachmentsOnPaymentCreate({
paymentReceiveDTO,
}: IPaymentReceivedCreatingPayload): Promise<void> {
if (isEmpty(paymentReceiveDTO.attachments)) {
return;
}
const documentKeys = paymentReceiveDTO?.attachments?.map((a) => a.key);
await this.validateDocuments.validate(documentKeys);
}
/**
* Handles linking the attachments of the created payment.
* @param {IPaymentReceivedCreatedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.paymentReceive.onCreated)
async handleAttachmentsOnPaymentCreated({
paymentReceiveDTO,
paymentReceive,
trx,
}: IPaymentReceivedCreatedPayload): Promise<void> {
if (isEmpty(paymentReceiveDTO.attachments)) return;
const keys = paymentReceiveDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.linkAttachmentService.bulkLink(
keys,
'PaymentReceive',
paymentReceive.id,
trx,
);
}
/**
* Handles unlinking all the unpresented keys of the edited payment.
* @param {IPaymentReceivedEditedPayload}
*/
@OnEvent(events.paymentReceive.onEdited)
private async handleUnlinkUnpresentedKeysOnPaymentEdited({
paymentReceiveDTO,
oldPaymentReceive,
trx,
}: IPaymentReceivedEditedPayload) {
const keys = paymentReceiveDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.unlinkAttachmentService.unlinkUnpresentedKeys(
keys,
'PaymentReceive',
oldPaymentReceive.id,
trx,
);
}
/**
* Handles linking all the presented keys of the edited payment.
* @param {IPaymentReceivedEditedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.paymentReceive.onEdited)
async handleLinkPresentedKeysOnPaymentEdited({
paymentReceiveDTO,
oldPaymentReceive,
trx,
}: IPaymentReceivedEditedPayload) {
if (isEmpty(paymentReceiveDTO.attachments)) return;
const keys = paymentReceiveDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.linkAttachmentService.bulkLink(
keys,
'PaymentReceive',
oldPaymentReceive.id,
trx,
);
}
/**
* Unlink all attachments once the payment deleted.
* @param {ISaleInvoiceDeletedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.paymentReceive.onDeleting)
async handleUnlinkAttachmentsOnPaymentDelete({
oldPaymentReceive,
trx,
}: IPaymentReceivedDeletingPayload) {
await this.unlinkAttachmentService.unlinkAllModelKeys(
'PaymentReceive',
oldPaymentReceive.id,
trx,
);
}
}

View File

@@ -0,0 +1,123 @@
import { isEmpty } from 'lodash';
import {
ISaleEstimateCreatedPayload,
ISaleEstimateCreatingPayload,
ISaleEstimateDeletingPayload,
ISaleEstimateEditedPayload,
} from '@/modules/SaleEstimates/types/SaleEstimates.types';
import { ValidateAttachments } from '../ValidateAttachments';
import { Injectable } from '@nestjs/common';
import { UnlinkAttachment } from '../UnlinkAttachment';
import { LinkAttachment } from '../LinkAttachment';
import { OnEvent } from '@nestjs/event-emitter';
import { events } from '@/common/events/events';
@Injectable()
export class AttachmentsOnSaleEstimates {
constructor(
private readonly linkAttachmentService: LinkAttachment,
private readonly unlinkAttachmentService: UnlinkAttachment,
private readonly validateDocuments: ValidateAttachments,
) {}
/**
* Validates the attachment keys on creating sale estimate.
* @param {ISaleEstimateCreatingPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.saleEstimate.onCreating)
async validateAttachmentsOnSaleEstimateCreated({
estimateDTO,
}: ISaleEstimateCreatingPayload): Promise<void> {
if (isEmpty(estimateDTO.attachments)) {
return;
}
const documentKeys = estimateDTO?.attachments?.map((a) => a.key);
await this.validateDocuments.validate(documentKeys);
}
/**
* Handles linking the attachments of the created sale estimate.
* @param {ISaleEstimateCreatedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.saleEstimate.onCreated)
async handleAttachmentsOnSaleEstimateCreated({
saleEstimateDTO,
saleEstimate,
trx,
}: ISaleEstimateCreatedPayload): Promise<void> {
if (isEmpty(saleEstimateDTO.attachments)) return;
const keys = saleEstimateDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.linkAttachmentService.bulkLink(
keys,
'SaleEstimate',
saleEstimate.id,
trx,
);
}
/**
* Handles unlinking all the unpresented keys of the edited sale estimate.
* @param {ISaleEstimateEditedPayload}
*/
@OnEvent(events.saleEstimate.onEdited)
async handleUnlinkUnpresentedKeysOnSaleEstimateEdited({
estimateDTO,
oldSaleEstimate,
trx,
}: ISaleEstimateEditedPayload) {
const keys = estimateDTO.attachments?.map((attachment) => attachment.key);
await this.unlinkAttachmentService.unlinkUnpresentedKeys(
keys,
'SaleEstimate',
oldSaleEstimate.id,
trx,
);
}
/**
* Handles linking all the presented keys of the edited sale estimate.
* @param {ISaleEstimateEditedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.saleEstimate.onEdited)
async handleLinkPresentedKeysOnSaleEstimateEdited({
estimateDTO,
oldSaleEstimate,
trx,
}: ISaleEstimateEditedPayload) {
if (isEmpty(estimateDTO.attachments)) return;
const keys = estimateDTO.attachments?.map((attachment) => attachment.key);
await this.linkAttachmentService.bulkLink(
keys,
'SaleEstimate',
oldSaleEstimate.id,
trx,
);
}
/**
* Unlink all attachments once the estimate deleted.
* @param {ISaleEstimateDeletingPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.saleEstimate.onDeleting)
async handleUnlinkAttachmentsOnSaleEstimateDelete({
oldSaleEstimate,
trx,
}: ISaleEstimateDeletingPayload) {
await this.unlinkAttachmentService.unlinkAllModelKeys(
'SaleEstimate',
oldSaleEstimate.id,
trx,
);
}
}

View File

@@ -0,0 +1,127 @@
import { Injectable } from '@nestjs/common';
import { isEmpty } from 'lodash';
import { OnEvent } from '@nestjs/event-emitter';
import {
ISaleInvoiceCreatedPayload,
ISaleInvoiceCreatingPaylaod,
ISaleInvoiceDeletingPayload,
ISaleInvoiceEditedPayload,
} from '@/modules/SaleInvoices/SaleInvoice.types';
import { ValidateAttachments } from '../ValidateAttachments';
import { LinkAttachment } from '../LinkAttachment';
import { UnlinkAttachment } from '../UnlinkAttachment';
import { events } from '@/common/events/events';
@Injectable()
export class AttachmentsOnSaleInvoiceCreated {
constructor(
private readonly linkAttachmentService: LinkAttachment,
private readonly unlinkAttachmentService: UnlinkAttachment,
private readonly validateDocuments: ValidateAttachments,
) {}
/**
* Validates the attachment keys on creating sale invoice.
* @param {ISaleInvoiceCreatingPaylaod}
* @returns {Promise<void>}
*/
@OnEvent(events.saleInvoice.onCreating)
async validateAttachmentsOnSaleInvoiceCreate({
saleInvoiceDTO,
}: ISaleInvoiceCreatingPaylaod): Promise<void> {
if (isEmpty(saleInvoiceDTO.attachments)) {
return;
}
const documentKeys = saleInvoiceDTO?.attachments?.map((a) => a.key);
await this.validateDocuments.validate(documentKeys);
}
/**
* Handles linking the attachments of the created sale invoice.
* @param {ISaleInvoiceCreatedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.saleInvoice.onCreated)
async handleAttachmentsOnSaleInvoiceCreated({
saleInvoiceDTO,
saleInvoice,
trx,
}: ISaleInvoiceCreatedPayload): Promise<void> {
if (isEmpty(saleInvoiceDTO.attachments)) return;
const keys = saleInvoiceDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.linkAttachmentService.bulkLink(
keys,
'SaleInvoice',
saleInvoice.id,
trx,
);
}
/**
* Handles unlinking all the unpresented keys of the edited sale invoice.
* @param {ISaleInvoiceEditedPayload}
*/
@OnEvent(events.saleInvoice.onEdited)
async handleUnlinkUnpresentedKeysOnInvoiceEdited({
saleInvoiceDTO,
saleInvoice,
trx,
}: ISaleInvoiceEditedPayload) {
// if (isEmpty(saleInvoiceDTO.attachments)) return;
const keys = saleInvoiceDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.unlinkAttachmentService.unlinkUnpresentedKeys(
keys,
'SaleInvoice',
saleInvoice.id,
trx,
);
}
/**
* Handles linking all the presented keys of the edited sale invoice.
* @param {ISaleInvoiceEditedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.saleInvoice.onEdited)
async handleLinkPresentedKeysOnInvoiceEdited({
saleInvoiceDTO,
oldSaleInvoice,
trx,
}: ISaleInvoiceEditedPayload) {
if (isEmpty(saleInvoiceDTO.attachments)) return;
const keys = saleInvoiceDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.linkAttachmentService.bulkLink(
keys,
'SaleInvoice',
oldSaleInvoice.id,
trx,
);
}
/**
* Unlink all attachments once the invoice deleted.
* @param {ISaleInvoiceDeletedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.saleInvoice.onDeleting)
async handleUnlinkAttachmentsOnInvoiceDeleted({
oldSaleInvoice,
trx,
}: ISaleInvoiceDeletingPayload) {
await this.unlinkAttachmentService.unlinkAllModelKeys(
'SaleInvoice',
oldSaleInvoice.id,
trx,
);
}
}

View File

@@ -0,0 +1,125 @@
import { isEmpty } from 'lodash';
import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import {
ISaleReceiptCreatedPayload,
ISaleReceiptCreatingPayload,
ISaleReceiptDeletingPayload,
ISaleReceiptEditedPayload,
} from '../../SaleReceipts/types/SaleReceipts.types';
import { ValidateAttachments } from '../ValidateAttachments';
import { LinkAttachment } from '../LinkAttachment';
import { UnlinkAttachment } from '../UnlinkAttachment';
import { events } from '@/common/events/events';
@Injectable()
export class AttachmentsOnSaleReceipt {
constructor(
private readonly linkAttachmentService: LinkAttachment,
private readonly unlinkAttachmentService: UnlinkAttachment,
private readonly validateDocuments: ValidateAttachments,
) {}
/**
* Validates the attachment keys on creating sale receipt.
* @param {ISaleReceiptCreatingPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.saleReceipt.onCreating)
async validateAttachmentsOnSaleInvoiceCreate({
saleReceiptDTO,
}: ISaleReceiptCreatingPayload): Promise<void> {
if (isEmpty(saleReceiptDTO.attachments)) {
return;
}
const documentKeys = saleReceiptDTO?.attachments?.map((a) => a.key);
await this.validateDocuments.validate(documentKeys);
}
/**
* Handles linking the attachments of the created sale receipt.
* @param {ISaleReceiptCreatedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.saleReceipt.onCreated)
async handleAttachmentsOnSaleInvoiceCreated({
saleReceiptDTO,
saleReceipt,
trx,
}: ISaleReceiptCreatedPayload): Promise<void> {
if (isEmpty(saleReceiptDTO.attachments)) return;
const keys = saleReceiptDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.linkAttachmentService.bulkLink(
keys,
'SaleReceipt',
saleReceipt.id,
trx,
);
}
/**
* Handles unlinking all the unpresented keys of the edited sale receipt.
* @param {ISaleReceiptEditedPayload}
*/
@OnEvent(events.saleReceipt.onEdited)
async handleUnlinkUnpresentedKeysOnInvoiceEdited({
saleReceiptDTO,
saleReceipt,
trx,
}: ISaleReceiptEditedPayload) {
const keys = saleReceiptDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.unlinkAttachmentService.unlinkUnpresentedKeys(
keys,
'SaleReceipt',
saleReceipt.id,
trx,
);
}
/**
* Handles linking all the presented keys of the edited sale receipt.
* @param {ISaleReceiptEditedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.saleReceipt.onEdited)
async handleLinkPresentedKeysOnInvoiceEdited({
saleReceiptDTO,
oldSaleReceipt,
trx,
}: ISaleReceiptEditedPayload) {
if (isEmpty(saleReceiptDTO.attachments)) return;
const keys = saleReceiptDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.linkAttachmentService.bulkLink(
keys,
'SaleReceipt',
oldSaleReceipt.id,
trx,
);
}
/**
* Unlink all attachments once the receipt deleted.
* @param {ISaleReceiptDeletingPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.saleReceipt.onDeleting)
async handleUnlinkAttachmentsOnReceiptDeleted({
oldSaleReceipt,
trx,
}: ISaleReceiptDeletingPayload) {
await this.unlinkAttachmentService.unlinkAllModelKeys(
'SaleReceipt',
oldSaleReceipt.id,
trx,
);
}
}

View File

@@ -0,0 +1,125 @@
import { isEmpty } from 'lodash';
import {
IVendorCreditCreatedPayload,
IVendorCreditCreatingPayload,
IVendorCreditDeletingPayload,
IVendorCreditEditedPayload,
} from '../../VendorCredit/types/VendorCredit.types';
import { ValidateAttachments } from '../ValidateAttachments';
import { OnEvent } from '@nestjs/event-emitter';
import { Injectable } from '@nestjs/common';
import { UnlinkAttachment } from '../UnlinkAttachment';
import { LinkAttachment } from '../LinkAttachment';
import { events } from '@/common/events/events';
@Injectable()
export class AttachmentsOnVendorCredits {
constructor(
private readonly linkAttachmentService: LinkAttachment,
private readonly unlinkAttachmentService: UnlinkAttachment,
private readonly validateDocuments: ValidateAttachments,
) {}
/**
* Validates the attachment keys on creating vendor credit.
* @param {IVendorCreditCreatingPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.vendorCredit.onCreating)
async validateAttachmentsOnVendorCreditCreate({
vendorCreditCreateDTO,
}: IVendorCreditCreatingPayload): Promise<void> {
if (isEmpty(vendorCreditCreateDTO.attachments)) {
return;
}
const documentKeys = vendorCreditCreateDTO?.attachments?.map((a) => a.key);
await this.validateDocuments.validate(documentKeys);
}
/**
* Handles linking the attachments of the created vendor credit.
* @param {IVendorCreditCreatedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.vendorCredit.onCreated)
async handleAttachmentsOnVendorCreditCreated({
vendorCreditCreateDTO,
vendorCredit,
trx,
}: IVendorCreditCreatedPayload): Promise<void> {
if (isEmpty(vendorCreditCreateDTO.attachments)) return;
const keys = vendorCreditCreateDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.linkAttachmentService.bulkLink(
keys,
'VendorCredit',
vendorCredit.id,
trx,
);
}
/**
* Handles unlinking all the unpresented keys of the edited vendor credit.
* @param {IVendorCreditEditedPayload}
*/
@OnEvent(events.vendorCredit.onEdited)
async handleUnlinkUnpresentedKeysOnVendorCreditEdited({
vendorCreditDTO,
oldVendorCredit,
trx,
}: IVendorCreditEditedPayload) {
const keys = vendorCreditDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.unlinkAttachmentService.unlinkUnpresentedKeys(
keys,
'VendorCredit',
oldVendorCredit.id,
trx,
);
}
/**
* Handles linking all the presented keys of the edited vendor credit.
* @param {IVendorCreditEditedPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.vendorCredit.onEdited)
async handleLinkPresentedKeysOnVendorCreditEdited({
vendorCreditDTO,
oldVendorCredit,
trx,
}: IVendorCreditEditedPayload) {
if (isEmpty(vendorCreditDTO.attachments)) return;
const keys = vendorCreditDTO.attachments?.map(
(attachment) => attachment.key,
);
await this.linkAttachmentService.bulkLink(
keys,
'VendorCredit',
oldVendorCredit.id,
trx,
);
}
/**
* Unlink all attachments once the vendor credit deleted.
* @param {IVendorCreditDeletingPayload}
* @returns {Promise<void>}
*/
@OnEvent(events.vendorCredit.onDeleting)
async handleUnlinkAttachmentsOnVendorCreditDeleted({
oldVendorCredit,
trx,
}: IVendorCreditDeletingPayload) {
await this.unlinkAttachmentService.unlinkAllModelKeys(
'VendorCredit',
oldVendorCredit.id,
trx,
);
}
}