Skip to content

Commit

Permalink
chore: using dollars insted of cents
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth Tiwari authored and Siddharth Tiwari committed Jan 3, 2024
1 parent 03c70c5 commit 48b1db0
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 34 deletions.
9 changes: 6 additions & 3 deletions apps/dashboard/app/batch-payments/server-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { getServerSession } from "next-auth"

import { authOptions } from "../api/auth/[...nextauth]/route"

import { dollarsToCents } from "../utils"

import { CSVRecord } from "./utils"

import { getWalletDetailsByUsername } from "@/services/graphql/queries/get-user-wallet-id"
import { intraLedgerUsdPaymentSend } from "@/services/graphql/mutations/intra-ledger-payment-send/usd"
import { intraLedgerBtcPaymentSend } from "@/services/graphql/mutations/intra-ledger-payment-send/btc"
import { WalletCurrency } from "@/services/graphql/generated"

export type ProcessedRecords = {
username: string
Expand All @@ -21,7 +24,7 @@ export type ProcessedRecords = {
}
export const processRecords = async (
records: CSVRecord[],
walletType: string,
walletType: WalletCurrency,
): Promise<{
error: boolean
message: string
Expand Down Expand Up @@ -49,7 +52,7 @@ export const processRecords = async (
}
}

const amount = walletType === "USD" ? record.cents : record.sats
const amount = walletType === WalletCurrency.Usd ? record.dollars : record.sats
processedRecords.push({
username: record.username,
recipient_wallet_id: getDefaultWalletID?.data.accountDefaultWallet.id,
Expand Down Expand Up @@ -91,7 +94,7 @@ export const processPaymentsServerAction = async (
if (walletDetails.walletCurrency === "USD") {
response = await intraLedgerUsdPaymentSend({
token,
amount: record.amount,
amount: dollarsToCents(record.amount),
memo: record.memo,
recipientWalletId: record.recipient_wallet_id,
walletId: walletDetails.id,
Expand Down
40 changes: 30 additions & 10 deletions apps/dashboard/app/batch-payments/utils.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { parse } from "csv-parse/sync"

import { centsToDollars } from "../utils"

import { WalletCurrency } from "@/services/graphql/generated"

const CSV_HEADER_USERNAME = "username"
const CSV_HEADER_CENTS = "cents"
const CSV_HEADER_DOLLARS = "dollars"
const CSV_HEADER_SATS = "sats"
const CSV_HEADER_MEMO = "memo"

export type CSVRecord = {
username: string
cents?: string
dollars?: string
sats?: string
memo?: string
}

function validateCSV(fileContent: string):
export function validateCSV(fileContent: string):
| {
records: CSVRecord[]
walletType: WalletCurrency
walletCurrency: WalletCurrency
totalAmount: number
}
| Error {
Expand All @@ -35,11 +37,11 @@ function validateCSV(fileContent: string):

if (
headers.includes(CSV_HEADER_USERNAME) &&
(headers.includes(CSV_HEADER_CENTS) || headers.includes(CSV_HEADER_SATS)) &&
(headers.includes(CSV_HEADER_DOLLARS) || headers.includes(CSV_HEADER_SATS)) &&
headers.length === 3 &&
headers.includes(CSV_HEADER_MEMO)
) {
const walletType = headers.includes(CSV_HEADER_CENTS)
const walletCurrency = headers.includes(CSV_HEADER_DOLLARS)
? WalletCurrency.Usd
: WalletCurrency.Btc

Expand All @@ -49,22 +51,40 @@ function validateCSV(fileContent: string):
return new Error("Record with missing username field found.")
}

const amount = walletType === WalletCurrency.Usd ? record.cents : record.sats
const amount = walletCurrency === WalletCurrency.Usd ? record.dollars : record.sats
if (!amount || Number(amount) <= 0) {
return new Error("Record with invalid amount (negative or zero) found.")
return new Error("Record with invalid amount")
}

totalAmount += Number(amount)
}

return {
records: records as CSVRecord[],
walletType,
walletCurrency,
totalAmount,
}
}

return new Error("Incorrect CSV Format")
}

export { validateCSV }
export const displayWalletBalanceBatchPayments = ({
amount,
walletCurrency,
}: {
amount: number
walletCurrency: WalletCurrency
}) => {
return walletCurrency === WalletCurrency.Usd
? `$${centsToDollars(amount)} USD`
: `${amount} sats`
}

export const displayCurrencyBatchPayments = ({
walletCurrency,
}: {
walletCurrency: WalletCurrency
}) => {
return walletCurrency === WalletCurrency.Usd ? "USD" : "sats"
}
8 changes: 8 additions & 0 deletions apps/dashboard/app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ import { WalletCurrency } from "@/services/graphql/generated"
export const getCurrencyFromWalletType = (walletType: WalletCurrency) => {
return walletType === WalletCurrency.Usd ? "cents" : "sats"
}

export const dollarsToCents = (amount: number) => {
return amount * 100
}

export const centsToDollars = (amount: number) => {
return amount / 100
}
41 changes: 24 additions & 17 deletions apps/dashboard/components/batch-payments/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,25 @@ import Details from "../details-card/derails"

import BatchPaymentsList from "./list"

import { validateCSV } from "@/app/batch-payments/utils"
import {
displayCurrencyBatchPayments,
displayWalletBalanceBatchPayments,
validateCSV,
} from "@/app/batch-payments/utils"

import {
ProcessedRecords,
processPaymentsServerAction,
processRecords,
} from "@/app/batch-payments/server-actions"
import { getCurrencyFromWalletType } from "@/app/utils"
import { WalletCurrency } from "@/services/graphql/generated"
import { centsToDollars } from "@/app/utils"

type paymentDetails = {
totalAmount: number
walletType: WalletCurrency
walletDetails?: {
balance: number
walletCurrency: string
walletCurrency: WalletCurrency
id: string
}
}
Expand Down Expand Up @@ -80,12 +83,12 @@ export default function BatchPayments() {
}

const walletDetails = userData?.defaultAccount.wallets.find((wallet) => {
return wallet.walletCurrency === validationResult.walletType
return wallet.walletCurrency === validationResult.walletCurrency
})

if (
!walletDetails?.balance ||
walletDetails?.balance < validationResult.totalAmount
centsToDollars(walletDetails?.balance) < validationResult.totalAmount
) {
setModalDetails({
open: true,
Expand All @@ -98,7 +101,7 @@ export default function BatchPayments() {

const processedRecords = await processRecords(
validationResult.records,
validationResult.walletType,
validationResult.walletCurrency,
)

if (processedRecords.error || !processedRecords.responsePayload) {
Expand All @@ -114,11 +117,9 @@ export default function BatchPayments() {
setCsvData(processedRecords.responsePayload)
setPaymentDetails({
totalAmount: validationResult.totalAmount,
walletType: validationResult.walletType,
walletDetails,
})
}

reader.readAsText(file)
}

Expand Down Expand Up @@ -214,17 +215,23 @@ export default function BatchPayments() {
<DetailsCard>
<Details
label="Total Amount"
value={`${String(paymentDetails.totalAmount)} ${getCurrencyFromWalletType(
paymentDetails.walletType,
)}`}
value={`${String(
paymentDetails.totalAmount,
)} ${displayCurrencyBatchPayments({
walletCurrency: paymentDetails.walletDetails.walletCurrency,
})}`}
/>
<Details
label="Wallet Type"
value={paymentDetails.walletDetails.walletCurrency}
/>
<Details label="Wallet Type" value={paymentDetails.walletType} />
<Details label="Wallet Id" value={paymentDetails.walletDetails.id} />
<Details
label="Wallet Balance"
value={`${
paymentDetails.walletDetails.balance
} ${getCurrencyFromWalletType(paymentDetails.walletType)}`}
value={displayWalletBalanceBatchPayments({
amount: paymentDetails.walletDetails.balance,
walletCurrency: paymentDetails.walletDetails.walletCurrency,
})}
/>
<Button onClick={processPayments} loading={processPaymentLoading}>
Confirm Payment
Expand All @@ -238,7 +245,7 @@ export default function BatchPayments() {
</Box>
<BatchPaymentsList
processedList={csvData}
walletType={paymentDetails.walletType}
walletCurrency={paymentDetails.walletDetails.walletCurrency}
></BatchPaymentsList>
</>
) : (
Expand Down
11 changes: 7 additions & 4 deletions apps/dashboard/components/batch-payments/list.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import React from "react"
import Table from "@mui/joy/Table"

import { getCurrencyFromWalletType } from "@/app/utils"
import { ProcessedRecords } from "@/app/batch-payments/server-actions"
import { WalletCurrency } from "@/services/graphql/generated"
import { displayCurrencyBatchPayments } from "@/app/batch-payments/utils"

type BatchPaymentListProps = {
processedList: ProcessedRecords[]
walletType: WalletCurrency
walletCurrency: WalletCurrency
}

const BatchPaymentsList: React.FC<BatchPaymentListProps> = ({
processedList,
walletType,
walletCurrency,
}) => {
const renderTable = (transactions: ProcessedRecords[]) => (
<>
Expand All @@ -36,7 +36,10 @@ const BatchPaymentsList: React.FC<BatchPaymentListProps> = ({
<td>{record.username}</td>
<td>{record.recipient_wallet_id}</td>
<td>
{record.amount} {getCurrencyFromWalletType(walletType)}
{record.amount}{" "}
{displayCurrencyBatchPayments({
walletCurrency,
})}
</td>
<td>{record.memo}</td>
<td>
Expand Down

0 comments on commit 48b1db0

Please sign in to comment.