From a0a9f4a76839d41daf71ed06179e08c863d06f08 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Sat, 5 Oct 2024 16:08:09 +0200 Subject: [PATCH] fix: Remove empty lines from address formats --- .../GetInvoicePaymentLinkTransformer.ts | 20 ++++++++++++---- .../src/system/models/TenantMetadata.ts | 14 ++++------- .../server/src/utils/address-text-format.ts | 24 ++++++++++--------- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/packages/server/src/services/Sales/Invoices/GetInvoicePaymentLinkTransformer.ts b/packages/server/src/services/Sales/Invoices/GetInvoicePaymentLinkTransformer.ts index 87c4823c0..824c1c5d4 100644 --- a/packages/server/src/services/Sales/Invoices/GetInvoicePaymentLinkTransformer.ts +++ b/packages/server/src/services/Sales/Invoices/GetInvoicePaymentLinkTransformer.ts @@ -104,12 +104,24 @@ export class GetInvoicePaymentLinkMetaTransformer extends SaleInvoiceTransformer ); } - protected formattedCustomerAddress(invoice) { - return contactAddressTextFormat(invoice.customer, `{ADDRESS_1} + get customerAddressFormat() { + return `{ADDRESS_1} {ADDRESS_2} -{CITY}, {STATE} {POSTAL_CODE} +{CITY} {STATE} {POSTAL_CODE} {COUNTRY} -{PHONE}`); +{PHONE}`; + } + + /** + * Retrieves the formatted customer address. + * @param invoice + * @returns {string} + */ + protected formattedCustomerAddress(invoice) { + return contactAddressTextFormat( + invoice.customer, + this.customerAddressFormat + ); } } diff --git a/packages/server/src/system/models/TenantMetadata.ts b/packages/server/src/system/models/TenantMetadata.ts index 0b7ae3ce3..a6256443f 100644 --- a/packages/server/src/system/models/TenantMetadata.ts +++ b/packages/server/src/system/models/TenantMetadata.ts @@ -1,4 +1,7 @@ -import { organizationAddressTextFormat } from '@/utils/address-text-format'; +import { + defaultOrganizationAddressFormat, + organizationAddressTextFormat, +} from '@/utils/address-text-format'; import BaseModel from 'models/Model'; import { getUploadedObjectUri } from '../../services/Attachments/utils'; @@ -67,14 +70,7 @@ export default class TenantMetadata extends BaseModel { * @returns {string} */ public get addressTextFormatted() { - const defaultMessage = `{ORGANIZATION_NAME} - {ADDRESS_1} - {ADDRESS_2} - {CITY}, {STATE} {POSTAL_CODE} - {COUNTRY} - {PHONE} -`; - return organizationAddressTextFormat(defaultMessage, { + return organizationAddressTextFormat(defaultOrganizationAddressFormat, { organizationName: this.name, address1: this.address?.address1, address2: this.address?.address2, diff --git a/packages/server/src/utils/address-text-format.ts b/packages/server/src/utils/address-text-format.ts index d5f85c721..30875ee9d 100644 --- a/packages/server/src/utils/address-text-format.ts +++ b/packages/server/src/utils/address-text-format.ts @@ -11,13 +11,13 @@ interface OrganizationAddressFormatArgs { phone?: string; } -const defaultMessage = ` - {ORGANIZATION_NAME} - {ADDRESS_1} - {ADDRESS_2} - {CITY}, {STATE} {POSTAL_CODE} - {COUNTRY} - {PHONE} +export const defaultOrganizationAddressFormat = ` +{ORGANIZATION_NAME} +{ADDRESS_1} +{ADDRESS_2} +{CITY} {STATE} {POSTAL_CODE} +{COUNTRY} +{PHONE} `; /** * Formats the address text based on the provided message and arguments. @@ -36,7 +36,9 @@ const formatText = (message: string, replacements: Record) => { }, message ); - formattedMessage = formattedMessage.replace(/\n{2,}/g, '\n').trim(); + // Removes any empty lines. + formattedMessage = formattedMessage.replace(/^\s*[\r\n]/gm, ''); + formattedMessage = formattedMessage.replace(/\n{2,}/g, '\n'); formattedMessage = formattedMessage.replace(/\n/g, '
'); formattedMessage = formattedMessage.trim(); @@ -72,17 +74,17 @@ interface ContactAddressTextFormatArgs { phone?: string; } -const contactFormatMessage = `{CONTACT_NAME} +export const defaultContactAddressFormat = `{CONTACT_NAME} {ADDRESS_1} {ADDRESS_2} -{CITY}, {STATE} {POSTAL_CODE} +{CITY} {STATE} {POSTAL_CODE} {COUNTRY} {PHONE} `; export const contactAddressTextFormat = ( contact: IContact, - message: string = contactFormatMessage + message: string = defaultContactAddressFormat ) => { const args = { displayName: contact.displayName,