From 45cf2dc2796336aa7bd76435cdabfa515b19169c Mon Sep 17 00:00:00 2001 From: Joao Ramos Date: Thu, 14 Dec 2023 17:20:36 +0000 Subject: [PATCH] Added a number asset to limit to a specific number of decimal cases if needed --- src/pages/api/users/update.ts | 4 ++-- src/pages/payment-record.tsx | 7 ++++--- src/utils/number.ts | 13 +++++++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 src/utils/number.ts diff --git a/src/pages/api/users/update.ts b/src/pages/api/users/update.ts index 315f84a1..7b6ea5f3 100644 --- a/src/pages/api/users/update.ts +++ b/src/pages/api/users/update.ts @@ -11,7 +11,7 @@ import {errorMessages} from "@/constants/errors"; import moment from "moment"; import ShortUniqueId from "short-unique-id"; import {Payment} from "@/interfaces/paypal"; - +import { toFixedNumber } from "@/utils/number"; const db = getFirestore(app); const auth = getAuth(app); @@ -32,7 +32,7 @@ const managePaymentRecords = async (user: User, userId: string | undefined): Pro corporate: userId, agent: user.corporateInformation.referralAgent, agentCommission: user.corporateInformation.payment!.commission, - agentValue: (user.corporateInformation.payment!.commission / 100) * user.corporateInformation.payment!.value, + agentValue: toFixedNumber((user.corporateInformation.payment!.commission / 100) * user.corporateInformation.payment!.value, 2), currency: user.corporateInformation.payment!.currency, value: user.corporateInformation.payment!.value, isPaid: false, diff --git a/src/pages/payment-record.tsx b/src/pages/payment-record.tsx index 908f3ca0..24c99d6f 100644 --- a/src/pages/payment-record.tsx +++ b/src/pages/payment-record.tsx @@ -25,6 +25,7 @@ import Input from "@/components/Low/Input"; import ReactDatePicker from "react-datepicker"; import moment from "moment"; import PaymentAssetManager from "@/components/PaymentAssetManager"; +import { toFixedNumber } from "@/utils/number"; export const getServerSideProps = withIronSessionSsr(({req, res}) => { const user = req.session.user; @@ -89,7 +90,7 @@ const PaymentCreator = ({onClose, reload}: {onClose: () => void; reload: () => v corporate: corporate?.id, agent: referralAgent?.id, agentCommission: commission, - agentValue: (commission / 100) * price, + agentValue: toFixedNumber((commission / 100) * price, 2), currency, value: price, isPaid: false, @@ -431,7 +432,7 @@ export default function PaymentRecord() { header: "Amount", cell: (info) => ( - {info.getValue().toFixed(2)} {CURRENCIES.find((x) => x.currency === info.row.original.currency)?.label} + {toFixedNumber(info.getValue(), 2)} {CURRENCIES.find((x) => x.currency === info.row.original.currency)?.label} ), }), @@ -453,7 +454,7 @@ export default function PaymentRecord() { header: "Commission Value", cell: (info) => ( - {info.getValue().toFixed(2)} {CURRENCIES.find((x) => x.currency === info.row.original.currency)?.label} + {toFixedNumber(info.getValue(), 2)} {CURRENCIES.find((x) => x.currency === info.row.original.currency)?.label} ), }), diff --git a/src/utils/number.ts b/src/utils/number.ts new file mode 100644 index 00000000..47f536b7 --- /dev/null +++ b/src/utils/number.ts @@ -0,0 +1,13 @@ +export function isDecimal(num: number) { + return num % 1 !== 0; + } + +export function toFixedNumber(num: number, decimals: number = 2) { + // Rounds to 2 decimal places + if(isDecimal(num)) { + const multiplier = Math.pow(10, decimals); + return Math.round(num * multiplier) / multiplier; + } + + return num; +}