Skip to content

Commit 87f07d7

Browse files
authored
Merge pull request #866 from PayButton/feat/preferred-quote-csv
Feat/preferred quote csv
2 parents 41308fe + ff56488 commit 87f07d7

3 files changed

Lines changed: 31 additions & 9 deletions

File tree

constants/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ export const SUPPORTED_QUOTES = [ // avoids hitting the DB every time for data t
144144
'usd' as SupportedQuotesType,
145145
'cad' as SupportedQuotesType
146146
]
147-
147+
export const SUPPORTED_QUOTES_FROM_ID: KeyValueT<SupportedQuotesType> = {
148+
1: 'usd',
149+
2: 'cad'
150+
}
148151
export const HUMAN_READABLE_DATE_FORMAT = 'YYYY-MM-DD'
149152

150153
export const PRICE_API_DATE_FORMAT = 'YYYY-MM-DD'

pages/api/paybutton/download/transactions/[paybuttonId].ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import {
66
PAYBUTTON_TRANSACTIONS_FILE_HEADERS,
77
DECIMALS,
88
SUPPORTED_QUOTES,
9-
DEFAULT_QUOTE_SLUG,
109
SupportedQuotesType,
1110
NetworkTickersType,
1211
NETWORK_TICKERS,
13-
NETWORK_IDS
12+
NETWORK_IDS,
13+
SUPPORTED_QUOTES_FROM_ID
1414
} from 'constants/index'
1515
import { TransactionWithAddressAndPrices, fetchTransactionsByPaybuttonId, getTransactionValueInCurrency } from 'services/transactionService'
1616
import { PaybuttonWithAddresses, fetchPaybuttonById } from 'services/paybuttonService'
@@ -147,14 +147,15 @@ export default async (req: any, res: any): Promise<void> => {
147147
const networkTickerReq = req.query.network as string
148148

149149
const networkTicker = (networkTickerReq !== '' && isNetworkValid(networkTickerReq as NetworkTickersType)) ? networkTickerReq.toUpperCase() as NetworkTickersType : undefined
150-
150+
const quoteId = req.query.currency as number
151+
const quoteSlug = SUPPORTED_QUOTES_FROM_ID[quoteId]
151152
const paybutton = await fetchPaybuttonById(paybuttonId)
152153
if (paybutton.providerUserId !== userId) {
153154
throw new Error(RESPONSE_MESSAGES.RESOURCE_DOES_NOT_BELONG_TO_USER_400.message)
154155
}
155156

156157
res.setHeader('Content-Type', 'text/csv')
157-
await downloadPaybuttonTransactionsFile(res, paybutton, DEFAULT_QUOTE_SLUG, networkTicker)
158+
await downloadPaybuttonTransactionsFile(res, paybutton, quoteSlug, networkTicker)
158159
} catch (error: any) {
159160
switch (error.message) {
160161
case RESPONSE_MESSAGES.PAYBUTTON_ID_NOT_PROVIDED_400.message:

pages/button/[id].tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { KeyValueT, NETWORK_TICKERS_FROM_ID, ResponseMessage, SOCKET_MESSAGES }
1313
import config from 'config'
1414
import io from 'socket.io-client'
1515
import PaybuttonTrigger from 'components/Paybutton/PaybuttonTrigger'
16+
import { UserProfile } from '@prisma/client'
1617

1718
export const getServerSideProps: GetServerSideProps = async (context) => {
1819
supertokensNode.init(SuperTokensConfig.backendConfig())
@@ -39,17 +40,33 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
3940

4041
interface PaybuttonProps {
4142
paybuttonId: string
43+
userId: string
4244
}
4345

4446
export default function Button (props: PaybuttonProps): React.ReactElement {
45-
const [paybutton, setPaybutton] = useState(undefined as PaybuttonWithAddresses | undefined)
47+
const [paybutton, setPaybutton] = useState<PaybuttonWithAddresses | undefined>(undefined)
4648
const [isSyncing, setIsSyncing] = useState<KeyValueT<boolean>>({})
4749
const [tableRefreshCount, setTableRefreshCount] = useState<number>(0)
4850
const [paybuttonNetworks, setPaybuttonNetworks] = useState<number[]>([])
49-
5051
const [selectedCurrency, setSelectedCurrency] = useState<string>('')
52+
const [userProfile, setUserProfile] = useState<UserProfile | null>(null)
53+
5154
const router = useRouter()
5255

56+
useEffect(() => {
57+
void (async () => {
58+
try {
59+
const res = await fetch('/api/user/', {
60+
method: 'GET'
61+
})
62+
const profile = await res.json()
63+
setUserProfile(profile)
64+
} catch (error) {
65+
console.error('Error fetching user profile:', error)
66+
}
67+
})()
68+
}, [])
69+
5370
const updateIsSyncing = (addressStringList: string[]): void => {
5471
const newIsSyncing = { ...isSyncing }
5572
addressStringList.forEach((addressString) => {
@@ -108,10 +125,11 @@ export default function Button (props: PaybuttonProps): React.ReactElement {
108125

109126
const downloadCSV = async (paybutton: { id: string, name: string }, currency: string): Promise<void> => {
110127
try {
111-
let url = `/api/paybutton/download/transactions/${paybutton.id}`
128+
const preferredCurrencyId = userProfile?.preferredCurrencyId ?? ''
129+
let url = `/api/paybutton/download/transactions/${paybutton.id}?currency=${preferredCurrencyId}`
112130
const isCurrencyEmptyOrUndefined = (value: string): boolean => (value === '' || value === undefined)
113131
if (!isCurrencyEmptyOrUndefined(currency)) {
114-
url += `?network=${currency}`
132+
url += `&network=${currency}`
115133
}
116134
const response = await fetch(url)
117135

0 commit comments

Comments
 (0)