mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 22:00:31 +00:00
feat(server): change estimate and receipts status once delivering mail
This commit is contained in:
@@ -7,9 +7,15 @@ import {
|
||||
DEFAULT_RECEIPT_MAIL_CONTENT,
|
||||
DEFAULT_RECEIPT_MAIL_SUBJECT,
|
||||
} from './constants';
|
||||
import { SaleReceiptMailOpts, SaleReceiptMailOptsDTO } from '@/interfaces';
|
||||
import {
|
||||
ISaleReceiptMailPresend,
|
||||
SaleReceiptMailOpts,
|
||||
SaleReceiptMailOptsDTO,
|
||||
} from '@/interfaces';
|
||||
import { ContactMailNotification } from '@/services/MailNotification/ContactMailNotification';
|
||||
import { parseAndValidateMailOptions } from '@/services/MailNotification/utils';
|
||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
||||
import events from '@/subscribers/events';
|
||||
|
||||
@Service()
|
||||
export class SaleReceiptMailNotification {
|
||||
@@ -25,6 +31,9 @@ export class SaleReceiptMailNotification {
|
||||
@Inject()
|
||||
private contactMailNotification: ContactMailNotification;
|
||||
|
||||
@Inject()
|
||||
private eventPublisher: EventPublisher;
|
||||
|
||||
@Inject('agenda')
|
||||
private agenda: any;
|
||||
|
||||
@@ -37,14 +46,21 @@ export class SaleReceiptMailNotification {
|
||||
public async triggerMail(
|
||||
tenantId: number,
|
||||
saleReceiptId: number,
|
||||
messageOpts: SaleReceiptMailOptsDTO
|
||||
messageOptions: SaleReceiptMailOptsDTO
|
||||
) {
|
||||
const payload = {
|
||||
tenantId,
|
||||
saleReceiptId,
|
||||
messageOpts,
|
||||
messageOpts: messageOptions,
|
||||
};
|
||||
await this.agenda.now('sale-receipt-mail-send', payload);
|
||||
|
||||
// Triggers the event `onSaleReceiptPreMailSend`.
|
||||
await this.eventPublisher.emitAsync(events.saleReceipt.onPreMailSend, {
|
||||
tenantId,
|
||||
saleReceiptId,
|
||||
messageOptions,
|
||||
} as ISaleReceiptMailPresend);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,7 +6,7 @@ import { SaleReceiptCostGLEntries } from '../SaleReceiptCostGLEntries';
|
||||
@Service()
|
||||
export class SaleReceiptCostGLEntriesSubscriber {
|
||||
@Inject()
|
||||
saleReceiptCostEntries: SaleReceiptCostGLEntries;
|
||||
private saleReceiptCostEntries: SaleReceiptCostGLEntries;
|
||||
|
||||
/**
|
||||
* Attaches events.
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { ISaleReceiptMailPresend } from '@/interfaces';
|
||||
import events from '@/subscribers/events';
|
||||
import { CloseSaleReceipt } from '../CloseSaleReceipt';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { ServiceError } from '@/exceptions';
|
||||
import { ERRORS } from '../constants';
|
||||
|
||||
@Service()
|
||||
export class SaleReceiptMarkClosedOnMailSentSubcriber {
|
||||
@Inject()
|
||||
private closeReceiptService: CloseSaleReceipt;
|
||||
|
||||
/**
|
||||
* Attaches events.
|
||||
*/
|
||||
public attach(bus) {
|
||||
bus.subscribe(events.saleReceipt.onPreMailSend, this.markReceiptClosed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the sale receipt closed on submitting mail.
|
||||
* @param {ISaleReceiptMailPresend}
|
||||
*/
|
||||
private markReceiptClosed = async ({
|
||||
tenantId,
|
||||
saleReceiptId,
|
||||
messageOptions,
|
||||
}: ISaleReceiptMailPresend) => {
|
||||
try {
|
||||
await this.closeReceiptService.closeSaleReceipt(tenantId, saleReceiptId);
|
||||
} catch (error) {
|
||||
if (
|
||||
error instanceof ServiceError &&
|
||||
error.errorType === ERRORS.SALE_RECEIPT_IS_ALREADY_CLOSED
|
||||
) {
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user