Skip to content

Commit f754178

Browse files
committed
refactor: code clean up
1 parent c5858df commit f754178

3 files changed

Lines changed: 55 additions & 108 deletions

File tree

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

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import moment from 'moment-timezone'
22
import {
3-
PRICE_API_DATE_FORMAT,
43
RESPONSE_MESSAGES,
54
DEFAULT_PAYBUTTON_CSV_FILE_DELIMITER,
65
PAYBUTTON_TRANSACTIONS_FILE_HEADERS,
7-
DECIMALS,
8-
SUPPORTED_QUOTES,
96
SupportedQuotesType,
107
NetworkTickersType,
118
NETWORK_TICKERS,
@@ -14,38 +11,11 @@ import {
1411
} from 'constants/index'
1512
import { TransactionWithAddressAndPrices, fetchTransactionsByPaybuttonId, getTransactionValueInCurrency } from 'services/transactionService'
1613
import { PaybuttonWithAddresses, fetchPaybuttonById } from 'services/paybuttonService'
17-
import { streamToCSV } from 'utils/files'
14+
import { TransactionFileData, formatNumberHeaders, formatPaybuttonTransactionsFileData, isNetworkValid, streamToCSV } from 'utils/files'
1815
import { setSession } from 'utils/setSession'
1916
import { NextApiResponse } from 'next'
2017
import { getNetworkIdFromSlug } from 'services/networkService'
2118
import { fetchUserProfileFromId } from 'services/userService'
22-
import { Prisma } from '@prisma/client'
23-
24-
export interface TransactionFileData {
25-
amount: Prisma.Decimal
26-
date: moment.Moment
27-
value: number
28-
rate: number
29-
transactionId: string
30-
currency: string
31-
address: string
32-
}
33-
34-
export interface FormattedTransactionFileData {
35-
amount: string
36-
date: string
37-
value: string
38-
rate: string
39-
transactionId: string
40-
address: string
41-
}
42-
export function isCurrencyValid (currency: SupportedQuotesType): boolean {
43-
return SUPPORTED_QUOTES.includes(currency)
44-
}
45-
46-
function isNetworkValid (slug: NetworkTickersType): boolean {
47-
return Object.values(NETWORK_TICKERS).includes(slug)
48-
}
4919

5020
const getPaybuttonTransactionsFileData = (transaction: TransactionWithAddressAndPrices, currency: SupportedQuotesType, timezone: string): TransactionFileData => {
5121
const { amount, hash, address, timestamp } = transaction
@@ -64,27 +34,6 @@ const getPaybuttonTransactionsFileData = (transaction: TransactionWithAddressAnd
6434
}
6535
}
6636

67-
const formatPaybuttonTransactionsFileData = (data: TransactionFileData): FormattedTransactionFileData => {
68-
const {
69-
amount,
70-
date,
71-
value,
72-
rate,
73-
currency
74-
} = data
75-
return {
76-
...data,
77-
amount: amount.toFixed(DECIMALS[currency]),
78-
date: date.format(PRICE_API_DATE_FORMAT),
79-
value: value.toFixed(2),
80-
rate: rate.toFixed(14)
81-
}
82-
}
83-
84-
const formatNumberHeaders = (headers: string[], currency: string): string[] => {
85-
return headers.map(h => h === PAYBUTTON_TRANSACTIONS_FILE_HEADERS.value ? h + ` (${currency.toUpperCase()})` : h)
86-
}
87-
8837
const sortTransactionsByNetworkId = async (transactions: TransactionWithAddressAndPrices[]): Promise<TransactionWithAddressAndPrices[]> => {
8938
const groupedByNetworkIdTransactions = transactions.reduce<Record<number, TransactionWithAddressAndPrices[]>>((acc, transaction) => {
9039
const networkId = transaction.address.networkId

pages/api/payments/download/index.ts

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,23 @@
11
import moment from 'moment-timezone'
22
import {
3-
PRICE_API_DATE_FORMAT,
43
RESPONSE_MESSAGES,
54
DEFAULT_PAYBUTTON_CSV_FILE_DELIMITER,
6-
SUPPORTED_QUOTES,
75
SupportedQuotesType,
86
SUPPORTED_QUOTES_FROM_ID,
97
PAYBUTTON_TRANSACTIONS_FILE_HEADERS,
10-
DECIMALS,
118
NETWORK_TICKERS,
129
NetworkTickersType,
1310
NETWORK_IDS
1411
} from 'constants/index'
1512
import { fetchAllPaymentsByUserId } from 'services/transactionService'
16-
import { streamToCSV } from 'utils/files'
13+
import { TransactionFileData, formatNumberHeaders, formatPaybuttonTransactionsFileData, isNetworkValid, streamToCSV } from 'utils/files'
1714
import { setSession } from 'utils/setSession'
1815
import { NextApiResponse } from 'next'
1916
import { fetchUserProfileFromId } from 'services/userService'
20-
import { Prisma } from '@prisma/client'
2117
import { Payment } from 'redis/types'
2218
import { getNetworkIdFromSlug } from 'services/networkService'
2319

24-
export interface PaymentFileData {
25-
amount: Prisma.Decimal
26-
date: moment.Moment
27-
value: number
28-
rate: number
29-
transactionId: string
30-
currency: string
31-
address?: string
32-
}
33-
34-
export interface FormattedPaymentFileData {
35-
amount: string
36-
date: string
37-
value: string
38-
rate: string
39-
transactionId: string
40-
address?: string
41-
}
42-
43-
export function isCurrencyValid (currency: SupportedQuotesType): boolean {
44-
return SUPPORTED_QUOTES.includes(currency)
45-
}
46-
47-
const getPaymentsFileData = (payment: Payment, currency: SupportedQuotesType, timezone: string): PaymentFileData => {
20+
const getPaymentsFileData = (payment: Payment, currency: SupportedQuotesType, timezone: string): TransactionFileData => {
4821
const { values, hash, timestamp, address } = payment
4922
const amount = values.amount
5023
const value = Number(values.values[currency])
@@ -62,28 +35,6 @@ const getPaymentsFileData = (payment: Payment, currency: SupportedQuotesType, ti
6235
}
6336
}
6437

65-
const formatPaymentsFileData = (data: PaymentFileData): FormattedPaymentFileData => {
66-
const {
67-
amount,
68-
date,
69-
value,
70-
rate,
71-
currency
72-
} = data
73-
74-
return {
75-
...data,
76-
amount: amount.toFixed(DECIMALS[currency]),
77-
date: date.format(PRICE_API_DATE_FORMAT),
78-
value: value.toFixed(2),
79-
rate: rate.toFixed(14)
80-
}
81-
}
82-
83-
const formatNumberHeaders = (headers: string[], currency: string): string[] => {
84-
return headers.map(h => h === PAYBUTTON_TRANSACTIONS_FILE_HEADERS.value ? h + ` (${currency.toUpperCase()})` : h)
85-
}
86-
8738
const sortPaymentsByNetworkId = (payments: Payment[]): Payment[] => {
8839
const groupedByNetworkIdPayments = payments.reduce<Record<number, Payment[]>>((acc, payment) => {
8940
const networkId = payment.networkId
@@ -116,7 +67,7 @@ const downloadPaymentsFileByUserId = async (
11667
const sortedPayments = await sortPaymentsByNetworkId(payments)
11768
const mappedPaymentsData = sortedPayments.map(payment => {
11869
const data = getPaymentsFileData(payment, currency, timezone)
119-
return formatPaymentsFileData(data)
70+
return formatPaybuttonTransactionsFileData(data)
12071
})
12172
const headers = Object.keys(PAYBUTTON_TRANSACTIONS_FILE_HEADERS)
12273
const humanReadableHeaders = formatNumberHeaders(Object.values(PAYBUTTON_TRANSACTIONS_FILE_HEADERS), currency)
@@ -129,9 +80,6 @@ const downloadPaymentsFileByUserId = async (
12980
humanReadableHeaders
13081
)
13182
}
132-
function isNetworkValid (slug: NetworkTickersType): boolean {
133-
return Object.values(NETWORK_TICKERS).includes(slug)
134-
}
13583

13684
export default async (req: any, res: any): Promise<void> => {
13785
try {

utils/files.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,57 @@
1-
import { MAX_RECORDS_PER_FILE, RESPONSE_MESSAGES } from '../constants/index'
1+
import { Prisma } from '@prisma/client'
2+
import { DECIMALS, MAX_RECORDS_PER_FILE, NETWORK_TICKERS, NetworkTickersType, PAYBUTTON_TRANSACTIONS_FILE_HEADERS, PRICE_API_DATE_FORMAT, RESPONSE_MESSAGES, SUPPORTED_QUOTES, SupportedQuotesType } from '../constants/index'
23
import { NextApiResponse } from 'next'
34
import { Transform } from 'stream'
45

6+
export interface TransactionFileData {
7+
amount: Prisma.Decimal
8+
date: moment.Moment
9+
value: number
10+
rate: number
11+
transactionId: string
12+
currency: string
13+
address?: string
14+
}
15+
16+
export interface FormattedTransactionFileData {
17+
amount: string
18+
date: string
19+
value: string
20+
rate: string
21+
transactionId: string
22+
address?: string
23+
}
24+
25+
export function isCurrencyValid (currency: SupportedQuotesType): boolean {
26+
return SUPPORTED_QUOTES.includes(currency)
27+
}
28+
29+
export function isNetworkValid (slug: NetworkTickersType): boolean {
30+
return Object.values(NETWORK_TICKERS).includes(slug)
31+
}
32+
33+
export const formatNumberHeaders = (headers: string[], currency: string): string[] => {
34+
return headers.map(h => h === PAYBUTTON_TRANSACTIONS_FILE_HEADERS.value ? h + ` (${currency.toUpperCase()})` : h)
35+
}
36+
37+
export const formatPaybuttonTransactionsFileData = (data: TransactionFileData): FormattedTransactionFileData => {
38+
const {
39+
amount,
40+
date,
41+
value,
42+
rate,
43+
currency
44+
} = data
45+
46+
return {
47+
...data,
48+
amount: amount.toFixed(DECIMALS[currency]),
49+
date: date.format(PRICE_API_DATE_FORMAT),
50+
value: value.toFixed(2),
51+
rate: rate.toFixed(14)
52+
}
53+
}
54+
555
export function valuesToCsvLine (values: string[], delimiter: string): string {
656
return values.join(delimiter) + '\n'
757
}

0 commit comments

Comments
 (0)