Skip to content

Commit 18cfdb6

Browse files
authored
refactor: update createPushNotificationContent to use Intl.NumberFormat (#2399)
* refactor: update createPushNotificationContent to use Intl.NumberFormat parsing * fix: use MajorExponent.STANDARD in maximumFractionDigits param
1 parent 519d697 commit 18cfdb6

File tree

5 files changed

+61
-90
lines changed

5 files changed

+61
-90
lines changed

src/config/locales/en.json

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,39 @@
11
{
2-
"currency": {
3-
"BTC": {
4-
"name": "sats",
5-
"symbol": ""
6-
},
7-
"CRC": {
8-
"name": "colones",
9-
"symbol": ""
10-
},
11-
"USD": {
12-
"name": "",
13-
"symbol": "$"
14-
}
15-
},
162
"notification": {
173
"balance": {
18-
"body": "Your balance is {{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
19-
"bodyDisplayCurrency": "Your balance is {{displayCurrencySymbol}}{{displayCurrencyAmount}}{{displayCurrencyName}} ({{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}})",
4+
"body": "Your balance is {{baseCurrencyAmount}}{{baseCurrencyName}}",
5+
"bodyDisplayCurrency": "Your balance is {{displayCurrencyAmount}} ({{baseCurrencyAmount}}{{baseCurrencyName}})",
206
"title": "{{walletCurrency}} Balance"
217
},
228
"transaction": {
239
"intra_ledger_payment": {
24-
"body": "Sent payment of {{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
25-
"bodyDisplayCurrency": "Sent payment of {{displayCurrencySymbol}}{{displayCurrencyAmount}}{{displayCurrencyName}} | {{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
10+
"body": "Sent payment of {{baseCurrencyAmount}}{{baseCurrencyName}}",
11+
"bodyDisplayCurrency": "Sent payment of {{displayCurrencyAmount}} | {{baseCurrencyAmount}}{{baseCurrencyName}}",
2612
"title": "{{walletCurrency}} Transaction"
2713
},
2814
"intra_ledger_receipt": {
29-
"body": "+{{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
30-
"bodyDisplayCurrency": "+{{displayCurrencySymbol}}{{displayCurrencyAmount}}{{displayCurrencyName}} | {{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
15+
"body": "+{{baseCurrencyAmount}}{{baseCurrencyName}}",
16+
"bodyDisplayCurrency": "+{{displayCurrencyAmount}} | {{baseCurrencyAmount}}{{baseCurrencyName}}",
3117
"title": "{{walletCurrency}} Transaction"
3218
},
3319
"onchain_payment": {
34-
"body": "Sent onchain payment of {{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}} confirmed",
35-
"bodyDisplayCurrency": "Sent onchain payment of +{{displayCurrencySymbol}}{{displayCurrencyAmount}}{{displayCurrencyName}} | {{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}} confirmed",
20+
"body": "Sent onchain payment of {{baseCurrencyAmount}}{{baseCurrencyName}} confirmed",
21+
"bodyDisplayCurrency": "Sent onchain payment of +{{displayCurrencyAmount}} | {{baseCurrencyAmount}}{{baseCurrencyName}} confirmed",
3622
"title": "{{walletCurrency}} Transaction"
3723
},
3824
"onchain_receipt": {
39-
"body": "+{{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
40-
"bodyDisplayCurrency": "+{{displayCurrencySymbol}}{{displayCurrencyAmount}}{{displayCurrencyName}} | {{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
25+
"body": "+{{baseCurrencyAmount}}{{baseCurrencyName}}",
26+
"bodyDisplayCurrency": "+{{displayCurrencyAmount}} | {{baseCurrencyAmount}}{{baseCurrencyName}}",
4127
"title": "{{walletCurrency}} Transaction"
4228
},
4329
"onchain_receipt_pending": {
44-
"body": "pending +{{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
45-
"bodyDisplayCurrency": "pending +{{displayCurrencySymbol}}{{displayCurrencyAmount}}{{displayCurrencyName}} | {{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
30+
"body": "pending +{{baseCurrencyAmount}}{{baseCurrencyName}}",
31+
"bodyDisplayCurrency": "pending +{{displayCurrencyAmount}} | {{baseCurrencyAmount}}{{baseCurrencyName}}",
4632
"title": "{{walletCurrency}} Transaction | Pending"
4733
},
4834
"paid-invoice": {
49-
"body": "+{{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
50-
"bodyDisplayCurrency": "+{{displayCurrencySymbol}}{{displayCurrencyAmount}}{{displayCurrencyName}} | {{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
35+
"body": "+{{baseCurrencyAmount}}{{baseCurrencyName}}",
36+
"bodyDisplayCurrency": "+{{displayCurrencyAmount}} | {{baseCurrencyAmount}}{{baseCurrencyName}}",
5137
"title": "{{walletCurrency}} Transaction"
5238
}
5339
}

src/config/locales/es.json

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,39 @@
11
{
2-
"currency": {
3-
"BTC": {
4-
"name": "sats",
5-
"symbol": ""
6-
},
7-
"CRC": {
8-
"name": "colones",
9-
"symbol": ""
10-
},
11-
"USD": {
12-
"name": "",
13-
"symbol": "$"
14-
}
15-
},
162
"notification": {
173
"balance": {
18-
"body": "Su balance es {{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
19-
"bodyDisplayCurrency": "Su balance es {{displayCurrencySymbol}}{{displayCurrencyAmount}}{{displayCurrencyName}} ({{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}})",
4+
"body": "Su balance es {{baseCurrencyAmount}}{{baseCurrencyName}}",
5+
"bodyDisplayCurrency": "Su balance es {{displayCurrencyAmount}} ({{baseCurrencyAmount}}{{baseCurrencyName}})",
206
"title": "Balance {{walletCurrency}}"
217
},
228
"transaction": {
239
"intra_ledger_payment": {
24-
"body": "Pago enviado por {{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
25-
"bodyDisplayCurrency": "Pago enviado por {{displayCurrencySymbol}}{{displayCurrencyAmount}}{{displayCurrencyName}} | {{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
10+
"body": "Pago enviado por {{baseCurrencyAmount}}{{baseCurrencyName}}",
11+
"bodyDisplayCurrency": "Pago enviado por {{displayCurrencyAmount}} | {{baseCurrencyAmount}}{{baseCurrencyName}}",
2612
"title": "Transacción {{walletCurrency}}"
2713
},
2814
"intra_ledger_receipt": {
29-
"body": "+{{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
30-
"bodyDisplayCurrency": "+{{displayCurrencySymbol}}{{displayCurrencyAmount}}{{displayCurrencyName}} | {{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
15+
"body": "+{{baseCurrencyAmount}}{{baseCurrencyName}}",
16+
"bodyDisplayCurrency": "+{{displayCurrencyAmount}} | {{baseCurrencyAmount}}{{baseCurrencyName}}",
3117
"title": "Transacción {{walletCurrency}}"
3218
},
3319
"onchain_payment": {
34-
"body": "Pago onchain por {{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}} confirmado",
35-
"bodyDisplayCurrency": "Pago onchain por +{{displayCurrencySymbol}}{{displayCurrencyAmount}}{{displayCurrencyName}} | {{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}} confirmado",
20+
"body": "Pago onchain por {{baseCurrencyAmount}}{{baseCurrencyName}} confirmado",
21+
"bodyDisplayCurrency": "Pago onchain por +{{displayCurrencyAmount}} | {{baseCurrencyAmount}}{{baseCurrencyName}} confirmado",
3622
"title": "Transacción {{walletCurrency}}"
3723
},
3824
"onchain_receipt": {
39-
"body": "+{{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
40-
"bodyDisplayCurrency": "+{{displayCurrencySymbol}}{{displayCurrencyAmount}}{{displayCurrencyName}} | {{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
25+
"body": "+{{baseCurrencyAmount}}{{baseCurrencyName}}",
26+
"bodyDisplayCurrency": "+{{displayCurrencyAmount}} | {{baseCurrencyAmount}}{{baseCurrencyName}}",
4127
"title": "Transacción {{walletCurrency}}"
4228
},
4329
"onchain_receipt_pending": {
44-
"body": "pendiente +{{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
45-
"bodyDisplayCurrency": "pendiente +{{displayCurrencySymbol}}{{displayCurrencyAmount}}{{displayCurrencyName}} | {{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
30+
"body": "pendiente +{{baseCurrencyAmount}}{{baseCurrencyName}}",
31+
"bodyDisplayCurrency": "pendiente +{{displayCurrencyAmount}} | {{baseCurrencyAmount}}{{baseCurrencyName}}",
4632
"title": "Transacción {{walletCurrency}} | Pendiente por confirmar"
4733
},
4834
"paid-invoice": {
49-
"body": "+{{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
50-
"bodyDisplayCurrency": "+{{displayCurrencySymbol}}{{displayCurrencyAmount}}{{displayCurrencyName}} | {{baseCurrencySymbol}}{{baseCurrencyAmount}}{{baseCurrencyName}}",
35+
"body": "+{{baseCurrencyAmount}}{{baseCurrencyName}}",
36+
"bodyDisplayCurrency": "+{{displayCurrencyAmount}} | {{baseCurrencyAmount}}{{baseCurrencyName}}",
5137
"title": "Transacción {{walletCurrency}}"
5238
}
5339
}

src/config/yaml.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ i18n.configure({
9595
objectNotation: true,
9696
updateFiles: false,
9797
locales: ["en", "es"],
98+
defaultLocale: "en",
99+
retryInDefaultLocale: true,
98100
directory: path.join(__dirname, "locales"),
99101
})
100102

src/services/notifications/create-push-notification-content.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { getI18nInstance } from "@config"
2-
import { getLanguageOrDefault } from "@domain/locale"
2+
3+
import { MajorExponent } from "@domain/fiat"
34
import { WalletCurrency } from "@domain/shared"
5+
import { getLanguageOrDefault } from "@domain/locale"
46

57
const i18n = getI18nInstance()
68

@@ -26,17 +28,19 @@ export const createPushNotificationContent = ({
2628
{ walletCurrency: baseCurrency },
2729
)
2830
const baseCurrencyName = baseCurrency === WalletCurrency.Btc ? "sats" : ""
29-
const baseCurrencySymbol = baseCurrency === WalletCurrency.Usd ? "$" : ""
3031
const displayedBaseAmount =
3132
baseCurrency === WalletCurrency.Usd ? Number(amount.amount) / 100 : amount.amount
3233
const baseCurrencyAmount = displayedBaseAmount.toLocaleString(locale, {
33-
maximumFractionDigits: 2,
34+
minimumFractionDigits: 0,
35+
maximumFractionDigits: MajorExponent.STANDARD,
36+
currency: baseCurrency,
37+
style: baseCurrency === WalletCurrency.Btc ? "decimal" : "currency",
38+
currencyDisplay: "narrowSymbol",
3439
})
3540

3641
let body = i18n.__(
3742
{ phrase: `notification.${notificationType}.body`, locale },
3843
{
39-
baseCurrencySymbol,
4044
baseCurrencyAmount,
4145
baseCurrencyName: baseCurrencyName ? ` ${baseCurrencyName}` : "",
4246
},
@@ -47,24 +51,17 @@ export const createPushNotificationContent = ({
4751
displayAmount.amount > 0 &&
4852
displayAmount.currency !== baseCurrency
4953
) {
50-
const displayCurrencyName = i18n.__({
51-
phrase: `currency.${displayAmount.currency}.name`,
52-
locale,
53-
})
54-
const displayCurrencySymbol = i18n.__({
55-
phrase: `currency.${displayAmount.currency}.symbol`,
56-
locale,
57-
})
5854
const displayCurrencyAmount = displayAmount.amount.toLocaleString(locale, {
59-
maximumFractionDigits: 2,
55+
minimumFractionDigits: 0,
56+
maximumFractionDigits: MajorExponent.STANDARD,
57+
currency: displayAmount.currency,
58+
style: "currency",
59+
currencyDisplay: "narrowSymbol",
6060
})
6161
body = i18n.__(
6262
{ phrase: `notification.${notificationType}.bodyDisplayCurrency`, locale },
6363
{
64-
displayCurrencySymbol,
6564
displayCurrencyAmount,
66-
displayCurrencyName: displayCurrencyName ? ` ${displayCurrencyName}` : "",
67-
baseCurrencySymbol,
6865
baseCurrencyAmount,
6966
baseCurrencyName: baseCurrencyName ? ` ${baseCurrencyName}` : "",
7067
},

test/unit/services/notifications/transactions.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,42 +107,42 @@ export const btcTransactionsWithDisplayCurrency = [
107107
type: NotificationType.IntraLedgerReceipt,
108108
paymentAmount: { amount: 1000n, currency: WalletCurrency.Btc },
109109
displayPaymentAmount: { amount: 500, currency: CRC },
110-
body: "+₡500 colones | 1,000 sats",
110+
body: "+₡500 | 1,000 sats",
111111
title: "BTC Transaction",
112112
},
113113
{
114114
type: NotificationType.IntraLedgerPayment,
115115
paymentAmount: { amount: 1000n, currency: WalletCurrency.Btc },
116116
displayPaymentAmount: { amount: 500, currency: CRC },
117-
body: "Sent payment of ₡500 colones | 1,000 sats",
117+
body: "Sent payment of ₡500 | 1,000 sats",
118118
title: "BTC Transaction",
119119
},
120120
{
121121
type: NotificationType.OnchainReceipt,
122122
paymentAmount: { amount: 1000n, currency: WalletCurrency.Btc },
123123
displayPaymentAmount: { amount: 500, currency: CRC },
124-
body: "+₡500 colones | 1,000 sats",
124+
body: "+₡500 | 1,000 sats",
125125
title: "BTC Transaction",
126126
},
127127
{
128128
type: NotificationType.OnchainReceiptPending,
129129
paymentAmount: { amount: 1000n, currency: WalletCurrency.Btc },
130130
displayPaymentAmount: { amount: 500, currency: CRC },
131-
body: "pending +₡500 colones | 1,000 sats",
131+
body: "pending +₡500 | 1,000 sats",
132132
title: "BTC Transaction | Pending",
133133
},
134134
{
135135
type: NotificationType.OnchainPayment,
136136
paymentAmount: { amount: 1000n, currency: WalletCurrency.Btc },
137137
displayPaymentAmount: { amount: 500, currency: CRC },
138-
body: "Sent onchain payment of +₡500 colones | 1,000 sats confirmed",
138+
body: "Sent onchain payment of +₡500 | 1,000 sats confirmed",
139139
title: "BTC Transaction",
140140
},
141141
{
142142
type: NotificationType.LnInvoicePaid,
143143
paymentAmount: { amount: 1000n, currency: WalletCurrency.Btc },
144144
displayPaymentAmount: { amount: 500, currency: CRC },
145-
body: "+₡500 colones | 1,000 sats",
145+
body: "+₡500 | 1,000 sats",
146146
title: "BTC Transaction",
147147
},
148148
]
@@ -290,7 +290,7 @@ export const usdTransactionsWithDisplayCurrency = [
290290
amount: 3500,
291291
currency: CRC,
292292
},
293-
body: "+₡3,500 colones | $5",
293+
body: "+₡3,500 | $5",
294294
title: "USD Transaction",
295295
},
296296
{
@@ -300,7 +300,7 @@ export const usdTransactionsWithDisplayCurrency = [
300300
amount: 3500,
301301
currency: CRC,
302302
},
303-
body: "Sent payment of ₡3,500 colones | $5",
303+
body: "Sent payment of ₡3,500 | $5",
304304
title: "USD Transaction",
305305
},
306306
{
@@ -310,7 +310,7 @@ export const usdTransactionsWithDisplayCurrency = [
310310
amount: 3500,
311311
currency: CRC,
312312
},
313-
body: "+₡3,500 colones | $5",
313+
body: "+₡3,500 | $5",
314314
title: "USD Transaction",
315315
},
316316
{
@@ -320,7 +320,7 @@ export const usdTransactionsWithDisplayCurrency = [
320320
amount: 3500,
321321
currency: CRC,
322322
},
323-
body: "pending +₡3,500 colones | $5",
323+
body: "pending +₡3,500 | $5",
324324
title: "USD Transaction | Pending",
325325
},
326326
{
@@ -330,7 +330,7 @@ export const usdTransactionsWithDisplayCurrency = [
330330
amount: 3500,
331331
currency: CRC,
332332
},
333-
body: "Sent onchain payment of +₡3,500 colones | $5 confirmed",
333+
body: "Sent onchain payment of +₡3,500 | $5 confirmed",
334334
title: "USD Transaction",
335335
},
336336
{
@@ -340,7 +340,7 @@ export const usdTransactionsWithDisplayCurrency = [
340340
amount: 3500,
341341
currency: CRC,
342342
},
343-
body: "+₡3,500 colones | $5",
343+
body: "+₡3,500 | $5",
344344
title: "USD Transaction",
345345
},
346346
{
@@ -410,7 +410,7 @@ export const usdTransactionsWithDisplayCurrency = [
410410
amount: 3500,
411411
currency: CRC,
412412
},
413-
body: "+₡3,500 colones | $5.07",
413+
body: "+₡3,500 | $5.07",
414414
title: "USD Transaction",
415415
},
416416
{
@@ -420,7 +420,7 @@ export const usdTransactionsWithDisplayCurrency = [
420420
amount: 3500,
421421
currency: CRC,
422422
},
423-
body: "Sent payment of ₡3,500 colones | $5.07",
423+
body: "Sent payment of ₡3,500 | $5.07",
424424
title: "USD Transaction",
425425
},
426426
{
@@ -430,7 +430,7 @@ export const usdTransactionsWithDisplayCurrency = [
430430
amount: 3500,
431431
currency: CRC,
432432
},
433-
body: "+₡3,500 colones | $5.07",
433+
body: "+₡3,500 | $5.07",
434434
title: "USD Transaction",
435435
},
436436
{
@@ -440,7 +440,7 @@ export const usdTransactionsWithDisplayCurrency = [
440440
amount: 3500,
441441
currency: CRC,
442442
},
443-
body: "pending +₡3,500 colones | $5.07",
443+
body: "pending +₡3,500 | $5.07",
444444
title: "USD Transaction | Pending",
445445
},
446446
{
@@ -450,7 +450,7 @@ export const usdTransactionsWithDisplayCurrency = [
450450
amount: 3500,
451451
currency: CRC,
452452
},
453-
body: "Sent onchain payment of +₡3,500 colones | $5.07 confirmed",
453+
body: "Sent onchain payment of +₡3,500 | $5.07 confirmed",
454454
title: "USD Transaction",
455455
},
456456
{
@@ -460,7 +460,7 @@ export const usdTransactionsWithDisplayCurrency = [
460460
amount: 3500,
461461
currency: CRC,
462462
},
463-
body: "+₡3,500 colones | $5.07",
463+
body: "+₡3,500 | $5.07",
464464
title: "USD Transaction",
465465
},
466466
]

0 commit comments

Comments
 (0)