Skip to content

Commit

Permalink
fix: [IOBP-908] Timeline amount format in cents (#6242)
Browse files Browse the repository at this point in the history
## Short description
This PR fixes the timeline amount format considering the amount as cents

## List of changes proposed in this pull request
- Used the `formatNumberCentsToAmount` function inside the
`formatAbsNumberAmountCentsOrDefault` to format the amount from cents
into correct string.

## How to test
- The related unit tests should pass correctly
- Checkout the dev-server with this PR:
pagopa/io-dev-api-server#416
- After that enable IDPay feature flag from the profile settings
- With the dev-server started you should be able to see into
"Portafoglio" section two mocked initiatives

---------

Co-authored-by: Mario Perrotta <mario.perrotta@pagopa.it>
  • Loading branch information
Hantex9 and hevelius authored Oct 4, 2024
1 parent f6956c3 commit d191cbc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
11 changes: 7 additions & 4 deletions ts/features/idpay/common/utils/strings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as O from "fp-ts/lib/Option";
import { pipe } from "fp-ts/lib/function";
import { formatNumberAmount } from "../../../../utils/stringBuilder";
import {
formatNumberAmount,
formatNumberCentsToAmount
} from "../../../../utils/stringBuilder";
import { format } from "../../../../utils/dates";

export const formatNumberCurrency = (amount: number) =>
Expand Down Expand Up @@ -37,20 +40,20 @@ export const formatNumberCurrencyCentsOrDefault = (
* This function takes a number and converts it into its absolute value, then formats it as a string
* using the 'formatNumberAmount' function. If the input is undefined, it returns the specified default value.
*
* @param {number | undefined} amount - The number to be formatted, or undefined if not available.
* @param {number | undefined} amount - The number amount in cents to be formatted, or undefined if not available.
* @param {string} [defaultValue='-'] - The default value to be returned when the input 'amount' is undefined.
*
* @returns {string} - The formatted absolute number as a string or the default value.
*/
export const formatAbsNumberAmountOrDefault = (
export const formatAbsNumberAmountCentsOrDefault = (
amount: number | undefined,
defaultValue: string = "-"
) =>
pipe(
amount,
O.fromNullable,
O.map(Math.abs),
O.map(formatNumberAmount),
O.map(formatNumberCentsToAmount),
O.getOrElse(() => defaultValue)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import I18n from "../../../../i18n";
import { hoursAndMinutesToAccessibilityReadableFormat } from "../../../../utils/accessibility";
import { localeDateFormat } from "../../../../utils/locale";
import { getBadgeTextByTransactionStatus } from "../../../payments/common/utils";
import { formatAbsNumberAmountOrDefault } from "../../common/utils/strings";
import { formatAbsNumberAmountCentsOrDefault } from "../../common/utils/strings";

export type TimelineOperationListItemProps = WithTestID<
| {
Expand Down Expand Up @@ -155,7 +155,9 @@ const getTransactionOperationProps = (

const getAccruedString = () => {
const signString = isReversal ? "" : "-";
const accruedString = `${formatAbsNumberAmountOrDefault(accruedCents)} €`;
const accruedString = `${formatAbsNumberAmountCentsOrDefault(
accruedCents
)} €`;

return `${signString}${accruedString}`;
};
Expand Down Expand Up @@ -305,7 +307,7 @@ const getRefundOperationProps = (
subtitle,
paymentLogoIcon,
transactionStatus: "success",
transactionAmount: `${formatAbsNumberAmountOrDefault(amountCents)} €`
transactionAmount: `${formatAbsNumberAmountCentsOrDefault(amountCents)} €`
};
};

Expand Down Expand Up @@ -371,7 +373,7 @@ export const getOperationSubtitleWithAmount = (
withMinusSign: boolean = false
): string => {
const signString = withMinusSign ? "-" : "";
const amountString = `${formatAbsNumberAmountOrDefault(amount)} €`;
const amountString = `${formatAbsNumberAmountCentsOrDefault(amount)} €`;

return `${getOperationSubtitle(
operationDate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe("Test TimelineOperationListItem", () => {
it("should render the component correctly", () => {
const T_DATE = new Date(2023, 4, 5, 16, 31);
const T_SUBTITLE = getOperationSubtitle(T_DATE);
const T_AMOUNT = 4578.74;
const T_AMOUNT = 457874;
const T_AMOUNT_STRING = "4.578,74 €";

const mockOnPress = jest.fn();
Expand Down Expand Up @@ -137,7 +137,7 @@ describe("Test TimelineOperationListItem", () => {
it("should render the component correctly", () => {
const T_DATE = new Date(2023, 4, 5, 16, 31);
const T_SUBTITLE = getOperationSubtitle(T_DATE);
const T_AMOUNT = 4578.74;
const T_AMOUNT = 457874;
const T_AMOUNT_STRING = "4.578,74 €";

const mockOnPress = jest.fn();
Expand Down Expand Up @@ -420,7 +420,7 @@ describe("Test TimelineOperationListItem", () => {
it("should render the component correctly", () => {
const T_DATE = new Date(2023, 4, 5, 16, 31);
const T_AMOUNT = 3456.1;
const T_ACCRUED = 234.56;
const T_ACCRUED = 23456;
const T_ACCRUED_STRING = "-234,56 €";
const T_SUBTITLE = getOperationSubtitleWithAmount(T_DATE, T_AMOUNT);

Expand Down Expand Up @@ -450,7 +450,7 @@ describe("Test TimelineOperationListItem", () => {
it("should render the component correctly with business name", () => {
const T_DATE = new Date(2023, 4, 5, 16, 31);
const T_AMOUNT = 3456.1;
const T_ACCRUED = 234.56;
const T_ACCRUED = 23456;
const T_ACCRUED_STRING = "-234,56 €";
const T_SUBTITLE = getOperationSubtitleWithAmount(T_DATE, T_AMOUNT);
const T_BUSINESS_NAME = "Business name";
Expand Down Expand Up @@ -483,7 +483,7 @@ describe("Test TimelineOperationListItem", () => {
it("should render the component correctly if transaction is cancelled", () => {
const T_DATE = new Date(2023, 4, 5, 16, 31);
const T_AMOUNT = 3456.1;
const T_ACCRUED = 234.56;
const T_ACCRUED = 23456;
const T_ACCRUED_STRING = "-234,56 €";
const T_SUBTITLE = getOperationSubtitleWithAmount(
T_DATE,
Expand Down Expand Up @@ -521,7 +521,7 @@ describe("Test TimelineOperationListItem", () => {
it("should render the component correctly", () => {
const T_DATE = new Date(2023, 4, 5, 16, 31);
const T_AMOUNT = 3456.1;
const T_ACCRUED = 234.56;
const T_ACCRUED = 23456;
const T_ACCRUED_STRING = "-234,56 €";
const T_SUBTITLE = getOperationSubtitleWithAmount(
T_DATE,
Expand Down Expand Up @@ -558,7 +558,7 @@ describe("Test TimelineOperationListItem", () => {
it("should render the component correctly with business name", () => {
const T_DATE = new Date(2023, 4, 5, 16, 31);
const T_AMOUNT = 3456.1;
const T_ACCRUED = 234.56;
const T_ACCRUED = 23456;
const T_ACCRUED_STRING = "-234,56 €";
const T_SUBTITLE = getOperationSubtitleWithAmount(
T_DATE,
Expand Down Expand Up @@ -598,7 +598,7 @@ describe("Test TimelineOperationListItem", () => {
it("should render the component correctly if transaction is cancelled", () => {
const T_DATE = new Date(2023, 4, 5, 16, 31);
const T_AMOUNT = 3456.1;
const T_ACCRUED = 234.56;
const T_ACCRUED = 23456;
const T_ACCRUED_STRING = "-234,56 €";
const T_SUBTITLE = getOperationSubtitleWithAmount(
T_DATE,
Expand Down Expand Up @@ -649,7 +649,7 @@ describe("Test getOperationSubtitle", () => {
describe("Test getOperationSubtitleWithAmount", () => {
it("should return the correct date string with positive number", () => {
const T_DATE = new Date(2023, 4, 5, 16, 31);
const T_AMOUNT = 4135.67;
const T_AMOUNT = 413567;
const T_RESULT = "05 mag 2023, 16:31 · 4.135,67 €";

const result = getOperationSubtitleWithAmount(T_DATE, T_AMOUNT);
Expand All @@ -659,7 +659,7 @@ describe("Test getOperationSubtitleWithAmount", () => {

it("should return the correct date string with negative number", () => {
const T_DATE = new Date(2023, 4, 5, 16, 31);
const T_AMOUNT = 4135.67;
const T_AMOUNT = 413567;
const T_RESULT = "05 mag 2023, 16:31 · -4.135,67 €";

const result = getOperationSubtitleWithAmount(T_DATE, T_AMOUNT, true);
Expand Down

0 comments on commit d191cbc

Please sign in to comment.