Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Invoice mail receipt preview #723

Merged
merged 18 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: send invoice receipt preview
  • Loading branch information
abouolia committed Oct 31, 2024
commit dbbaa387bdfcbd8f5651e468b88e25089f83610b
8 changes: 4 additions & 4 deletions packages/server/src/api/controllers/Sales/SalesInvoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export default class SaleInvoicesController extends BaseController {
'/:id/mail',
[
...this.specificSaleInvoiceValidation,

body('subject').isString().optional({ nullable: true }),
body('message').isString().optional({ nullable: true }),

Expand All @@ -201,7 +201,7 @@ export default class SaleInvoicesController extends BaseController {
this.handleServiceErrors
);
router.get(
'/:id/mail',
'/:id/mail/state',
[...this.specificSaleInvoiceValidation],
this.validationResult,
asyncMiddleware(this.getSaleInvoiceMail.bind(this)),
Expand Down Expand Up @@ -789,7 +789,7 @@ export default class SaleInvoicesController extends BaseController {
}

/**
* Retrieves the default mail options of the given sale invoice.
* Retrieves the mail state of the given sale invoice.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
Expand All @@ -803,7 +803,7 @@ export default class SaleInvoicesController extends BaseController {
const { id: invoiceId } = req.params;

try {
const data = await this.saleInvoiceApplication.getSaleInvoiceMail(
const data = await this.saleInvoiceApplication.getSaleInvoiceMailState(
tenantId,
invoiceId
);
Expand Down
24 changes: 24 additions & 0 deletions packages/server/src/interfaces/SaleInvoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,30 @@ export interface SaleInvoiceMailOptions extends CommonMailOptions {
formatArgs?: Record<string, any>;
}

export interface SaleInvoiceMailState extends SaleInvoiceMailOptions {
invoiceNo: string;

invoiceDate: string;
invoiceDateFormatted: string;

dueDate: string;
dueDateFormatted: string;

total: number;
totalFormatted: string;

subtotal: number;
subtotalFormatted: number;

companyName: string;
companyLogoUri: string;

customerName: string;

// # Invoice entries
entries?: Array<{ label: string; total: string; quantity: string | number }>;
}

export interface SendInvoiceMailDTO extends CommonMailOptionsDTO {
attachInvoice?: boolean;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { SaleInvoiceMailOptions, SaleInvoiceMailState } from '@/interfaces';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { Inject } from 'typedi';
import { SendSaleInvoiceMailCommon } from './SendInvoiceInvoiceMailCommon';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
import { GetSaleInvoiceMailStateTransformer } from './GetSaleInvoiceMailStateTransformer';

export class GetSaleInvoiceMailState {
@Inject()
private tenancy: HasTenancyService;

@Inject()
private invoiceMail: SendSaleInvoiceMailCommon;

@Inject()
private transformer: TransformerInjectable;

/**
* Retrieves the invoice mail state of the given sale invoice.
* Invoice mail state includes the mail options, branding attributes and the invoice details.
*
* @param {number} tenantId
* @param {number} saleInvoiceId
* @returns {Promise<SaleInvoiceMailState>}
*/
async getInvoiceMailState(
tenantId: number,
saleInvoiceId: number
): Promise<SaleInvoiceMailState> {
const { SaleInvoice } = this.tenancy.models(tenantId);

const saleInvoice = await SaleInvoice.query()
.findById(saleInvoiceId)
.withGraphFetched('customer')
.withGraphFetched('entries.item')
.withGraphFetched('pdfTemplate')
.throwIfNotFound();

const mailOptions = await this.invoiceMail.getInvoiceMailOptions(
tenantId,
saleInvoiceId
);
// Transforms the sale invoice mail state.
const transformed = await this.transformer.transform(
tenantId,
saleInvoice,
new GetSaleInvoiceMailStateTransformer(),
{
mailOptions,
}
);
return transformed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { Transformer } from '@/lib/Transformer/Transformer';
import { SaleInvoiceTransformer } from './SaleInvoiceTransformer';
import { ItemEntryTransformer } from './ItemEntryTransformer';

export class GetSaleInvoiceMailStateTransformer extends SaleInvoiceTransformer {
/**
* Exclude these attributes from user object.
* @returns {Array}
*/
public excludeAttributes = (): string[] => {
return ['*'];
};

public includeAttributes = (): string[] => {
return [
'invoiceDate',
'invoiceDateFormatted',

'dueDate',
'dueDateFormatted',

'dueAmount',
'dueAmountFormatted',

'total',
'totalFormatted',

'subtotal',
'subtotalFormatted',

'invoiceNo',

'entries',

'companyName',
'companyLogoUri',

'primaryColor',
];
};

protected companyName = () => {
return this.context.organization.name;
};

protected companyLogoUri = (invoice) => {
return invoice.pdfTemplate?.attributes?.companyLogoUri;
};

protected primaryColor = (invoice) => {
return invoice.pdfTemplate?.attributes?.primaryColor;
};

/**
*
* @param invoice
* @returns
*/
protected entries = (invoice) => {
return this.item(
invoice.entries,
new GetSaleInvoiceMailStateEntryTransformer(),
{
currencyCode: invoice.currencyCode,
}
);
};

/**
* Merges the mail options with the invoice object.
*/
public transform = (object: any) => {
return {
...this.options.mailOptions,
...object,
};
};
}

class GetSaleInvoiceMailStateEntryTransformer extends ItemEntryTransformer {
/**
* Exclude these attributes from user object.
* @returns {Array}
*/
public excludeAttributes = (): string[] => {
return ['*'];
};

public name = (entry) => {
return entry.item.name;
};

public includeAttributes = (): string[] => {
return [
'name',
'quantity',
'quantityFormatted',
'rate',
'rateFormatted',
'total',
'totalFormatted',
];
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ISystemUser,
ITenantUser,
InvoiceNotificationType,
SaleInvoiceMailState,
SendInvoiceMailDTO,
} from '@/interfaces';
import { Inject, Service } from 'typedi';
Expand All @@ -29,6 +30,8 @@ import { SendInvoiceMailReminder } from './SendSaleInvoiceMailReminder';
import { SendSaleInvoiceMail } from './SendSaleInvoiceMail';
import { GetSaleInvoiceMailReminder } from './GetSaleInvoiceMailReminder';
import { GetSaleInvoiceState } from './GetSaleInvoiceState';
import { GetSaleInvoiceBrandTemplate } from './GetSaleInvoiceBrandTemplate';
import { GetSaleInvoiceMailState } from './GetSaleInvoiceMailState';

@Service()
export class SaleInvoiceApplication {
Expand Down Expand Up @@ -72,7 +75,7 @@ export class SaleInvoiceApplication {
private sendSaleInvoiceMailService: SendSaleInvoiceMail;

@Inject()
private getSaleInvoiceReminderService: GetSaleInvoiceMailReminder;
private getSaleInvoiceMailStateService: GetSaleInvoiceMailState;

@Inject()
private getSaleInvoiceStateService: GetSaleInvoiceState;
Expand Down Expand Up @@ -361,10 +364,10 @@ export class SaleInvoiceApplication {
* Retrieves the default mail options of the given sale invoice.
* @param {number} tenantId
* @param {number} saleInvoiceid
* @returns {Promise<SendInvoiceMailDTO>}
* @returns {Promise<SaleInvoiceMailState>}
*/
public getSaleInvoiceMail(tenantId: number, saleInvoiceid: number) {
return this.sendSaleInvoiceMailService.getMailOption(
public getSaleInvoiceMailState(tenantId: number, saleInvoiceid: number) {
return this.getSaleInvoiceMailStateService.getInvoiceMailState(
tenantId,
saleInvoiceid
);
Expand Down
32 changes: 2 additions & 30 deletions packages/server/src/services/Sales/Invoices/SendSaleInvoiceMail.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { Inject, Service } from 'typedi';
import Mail from '@/lib/Mail';
import {
ISaleInvoiceMailSend,
SaleInvoiceMailOptions,
SendInvoiceMailDTO,
} from '@/interfaces';
import { ISaleInvoiceMailSend, SendInvoiceMailDTO } from '@/interfaces';
import { SaleInvoicePdf } from './SaleInvoicePdf';
import { SendSaleInvoiceMailCommon } from './SendInvoiceInvoiceMailCommon';
import {
DEFAULT_INVOICE_MAIL_CONTENT,
DEFAULT_INVOICE_MAIL_SUBJECT,
} from './constants';
import {
parseMailOptions,
validateRequiredMailOptions,
Expand Down Expand Up @@ -58,26 +50,6 @@ export class SendSaleInvoiceMail {
} as ISaleInvoiceMailSend);
}

/**
* Retrieves the mail options of the given sale invoice.
* @param {number} tenantId
* @param {number} saleInvoiceId
* @returns {Promise<SaleInvoiceMailOptions>}
*/
public async getMailOption(
tenantId: number,
saleInvoiceId: number,
defaultSubject: string = DEFAULT_INVOICE_MAIL_SUBJECT,
defaultMessage: string = DEFAULT_INVOICE_MAIL_CONTENT
): Promise<SaleInvoiceMailOptions> {
return this.invoiceMail.getInvoiceMailOptions(
tenantId,
saleInvoiceId,
defaultSubject,
defaultMessage
);
}

/**
* Triggers the mail invoice.
* @param {number} tenantId
Expand All @@ -90,7 +62,7 @@ export class SendSaleInvoiceMail {
saleInvoiceId: number,
messageOptions: SendInvoiceMailDTO
) {
const defaultMessageOptions = await this.getMailOption(
const defaultMessageOptions = await this.invoiceMail.getInvoiceMailOptions(
tenantId,
saleInvoiceId
);
Expand Down
2 changes: 0 additions & 2 deletions packages/webapp/src/components/DialogsContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import ProjectBillableEntriesFormDialog from '@/containers/Projects/containers/P
import TaxRateFormDialog from '@/containers/TaxRates/dialogs/TaxRateFormDialog/TaxRateFormDialog';
import { DialogsName } from '@/constants/dialogs';
import InvoiceExchangeRateChangeDialog from '@/containers/Sales/Invoices/InvoiceForm/Dialogs/InvoiceExchangeRateChangeDialog';
import InvoiceMailDialog from '@/containers/Sales/Invoices/InvoiceMailDialog/InvoiceMailDialog';
import EstimateMailDialog from '@/containers/Sales/Estimates/EstimateMailDialog/EstimateMailDialog';
import ReceiptMailDialog from '@/containers/Sales/Receipts/ReceiptMailDialog/ReceiptMailDialog';
import PaymentMailDialog from '@/containers/Sales/PaymentsReceived/PaymentMailDialog/PaymentMailDialog';
Expand Down Expand Up @@ -144,7 +143,6 @@ export default function DialogsContainer() {
<InvoiceExchangeRateChangeDialog
dialogName={DialogsName.InvoiceExchangeRateChangeNotice}
/>
<InvoiceMailDialog dialogName={DialogsName.InvoiceMail} />
<EstimateMailDialog dialogName={DialogsName.EstimateMail} />
<ReceiptMailDialog dialogName={DialogsName.ReceiptMail} />
<PaymentMailDialog dialogName={DialogsName.PaymentMail} />
Expand Down

This file was deleted.

Loading
Loading