changed currency displaying on notif based on locale
MODIFY
src/async-task/tasks/BillTransactionAsyncTask.ts
src/async-task/tasks/BillTransactionAsyncTask.ts
@@ -43,6 +43,7 @@ export default class BillTransactionAsyncTask extends AbstractAsyncTask {
}
}
} catch (error) {
+ // TODO: add root cause ? logging have been added to send notification
await Logging.logActionExceptionMessage(tenant.id, ServerAction.BILLING_TRANSACTION, error);
}
}

MODIFY
src/integration/billing/BillingIntegration.ts
src/integration/billing/BillingIntegration.ts
@@ -7,6 +7,7 @@ import { BillingSettings } from '../../types/Setting';
import BillingStorage from '../../storage/mongodb/BillingStorage';
import Constants from '../../utils/Constants';
import { DataResult } from '../../types/DataResult';
+import Decimal from 'decimal.js';
import Logging from '../../utils/Logging';
import NotificationHandler from '../../notification/NotificationHandler';
import { Request } from 'express';
@@ -238,11 +239,16 @@ export default abstract class BillingIntegration {
return actionsDone;
}
- public async sendInvoiceNotification(billingInvoice: BillingInvoice): Promise<void> {
+ public async sendInvoiceNotification(billingInvoice: BillingInvoice): Promise<boolean> {
+ try {
// Do not send notifications for invoices that are not yet finalized!
if (billingInvoice.status === BillingInvoiceStatus.OPEN || billingInvoice.status === BillingInvoiceStatus.PAID) {
// Send link to the user using our notification framework (link to the front-end + download)
const tenant = await TenantStorage.getTenant(this.tenant.id);
+ // Stripe saves amount in cents
+ const decimInvoiceAmount = new Decimal(billingInvoice.amount).div(100);
+ // Format amunt with currency symbol depending on locale
+ const invoiceAmount = new Intl.NumberFormat(Utils.convertLocaleForCurrency(billingInvoice.user.locale), { style: 'currency', currency: billingInvoice.currency.toUpperCase() }).format(decimInvoiceAmount.toNumber());
// Send async notification
await NotificationHandler.sendBillingNewInvoiceNotification(
this.tenant.id,
@@ -255,12 +261,24 @@ export default abstract class BillingIntegration {
invoiceDownloadUrl: Utils.buildEvseBillingDownloadInvoicesURL(tenant.subdomain, billingInvoice.id),
// Empty url allows to decide wether to display "pay" button in the email
payInvoiceUrl: billingInvoice.status === BillingInvoiceStatus.OPEN ? billingInvoice.payInvoiceUrl : '',
- // Stripe saves amount in cents
- invoiceAmount: Utils.createDecimal(billingInvoice.amount).div(100),
+ invoiceAmount: invoiceAmount,
invoiceNumber: billingInvoice.number,
invoiceStatus: billingInvoice.status,
}
);
+ // Needed only for testing
+ return true;
+ }
+ } catch (error) {
+ await Logging.logError({
+ tenantID: this.tenant.id,
+ source: Constants.CENTRAL_SERVER,
+ action: ServerAction.BILLING_TRANSACTION,
+ module: MODULE_NAME, method: 'sendInvoiceNotification',
+ message: `Failed to send notification for invoice '${billingInvoice.id}'`,
+ detailedMessages: { error: error.message, stack: error.stack }
+ });
+ return false;
}
}

MODIFY
src/types/UserNotifications.ts
src/types/UserNotifications.ts
@@ -278,7 +278,7 @@ export interface BillingNewInvoiceNotification extends BaseNotification {
invoiceDownloadUrl: string;
payInvoiceUrl: string;
invoiceNumber: string;
- invoiceAmount: Decimal;
+ invoiceAmount: string;
invoiceStatus: string;
}

MODIFY
src/utils/Utils.ts
src/utils/Utils.ts
@@ -353,6 +353,10 @@ export default class Utils {
return Constants.DEFAULT_LOCALE;
}
+ public static convertLocaleForCurrency(locale: string): string {
+ return locale.replace('_', '-');
+ }
+
public static getConnectorLimitSourceString(limitSource: ConnectorCurrentLimitSource): string {
switch (limitSource) {
case ConnectorCurrentLimitSource.CHARGING_PROFILE:

changed currency displaying on notif based on locale