Skip to content

Commit 9d46073

Browse files
authored
Merge pull request #912 from PayButton/fix/timezone-csv
fix: add timezone to csv
2 parents 3d3c9ce + 0f659ce commit 9d46073

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import moment from 'moment'
1+
import moment from 'moment-timezone'
22
import {
33
PRICE_API_DATE_FORMAT,
44
RESPONSE_MESSAGES,
@@ -47,11 +47,10 @@ function isNetworkValid (slug: NetworkTickersType): boolean {
4747
return Object.values(NETWORK_TICKERS).includes(slug)
4848
}
4949

50-
const getPaybuttonTransactionsFileData = (transaction: TransactionWithAddressAndPrices, currency: SupportedQuotesType): TransactionFileData => {
50+
const getPaybuttonTransactionsFileData = (transaction: TransactionWithAddressAndPrices, currency: SupportedQuotesType, timezone: string): TransactionFileData => {
5151
const { amount, hash, address, timestamp } = transaction
5252
const value = getTransactionValueInCurrency(transaction, currency)
53-
const date = moment(timestamp * 1000)
54-
53+
const date = moment.tz(timestamp * 1000, timezone)
5554
const rate = value / amount.toNumber()
5655

5756
return {
@@ -106,6 +105,7 @@ const downloadPaybuttonTransactionsFile = async (
106105
res: NextApiResponse,
107106
paybutton: PaybuttonWithAddresses,
108107
currency: SupportedQuotesType,
108+
timezone: string,
109109
networkTicker?: NetworkTickersType): Promise<void> => {
110110
let networkIdArray = Object.values(NETWORK_IDS)
111111
if (networkTicker !== undefined) {
@@ -117,7 +117,7 @@ const downloadPaybuttonTransactionsFile = async (
117117
const sortedTransactions = await sortTransactionsByNetworkId(transactions)
118118

119119
const mappedTransactionsData = sortedTransactions.map(tx => {
120-
const data = getPaybuttonTransactionsFileData(tx, currency)
120+
const data = getPaybuttonTransactionsFileData(tx, currency, timezone)
121121
return formatPaybuttonTransactionsFileData(data)
122122
})
123123
const headers = Object.keys(PAYBUTTON_TRANSACTIONS_FILE_HEADERS)
@@ -160,9 +160,12 @@ export default async (req: any, res: any): Promise<void> => {
160160
if (paybutton.providerUserId !== userId) {
161161
throw new Error(RESPONSE_MESSAGES.RESOURCE_DOES_NOT_BELONG_TO_USER_400.message)
162162
}
163+
const userReqTimezone = req.headers.timezone as string
164+
const userPreferredTimezone = user?.preferredTimezone
165+
const timezone = userPreferredTimezone !== '' ? userPreferredTimezone : userReqTimezone
163166

164167
res.setHeader('Content-Type', 'text/csv')
165-
await downloadPaybuttonTransactionsFile(res, paybutton, quoteSlug, networkTicker)
168+
await downloadPaybuttonTransactionsFile(res, paybutton, quoteSlug, timezone, networkTicker)
166169
} catch (error: any) {
167170
switch (error.message) {
168171
case RESPONSE_MESSAGES.PAYBUTTON_ID_NOT_PROVIDED_400.message:

pages/button/[id].tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ export default function Button (props: PaybuttonProps): React.ReactElement {
124124
if (!isCurrencyEmptyOrUndefined(currency)) {
125125
url += `&network=${currency}`
126126
}
127-
const response = await fetch(url)
127+
const response = await fetch(url, {
128+
headers: {
129+
Timezone: moment.tz.guess()
130+
}
131+
})
128132

129133
if (!response.ok) {
130134
throw new Error('Failed to download CSV')

0 commit comments

Comments
 (0)