11import moment from 'moment-timezone'
22import {
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'
1512import { fetchAllPaymentsByUserId } from 'services/transactionService'
16- import { streamToCSV } from 'utils/files'
13+ import { TransactionFileData , formatNumberHeaders , formatPaybuttonTransactionsFileData , isNetworkValid , streamToCSV } from 'utils/files'
1714import { setSession } from 'utils/setSession'
1815import { NextApiResponse } from 'next'
1916import { fetchUserProfileFromId } from 'services/userService'
20- import { Prisma } from '@prisma/client'
2117import { Payment } from 'redis/types'
2218import { 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-
8738const 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
13684export default async ( req : any , res : any ) : Promise < void > => {
13785 try {
0 commit comments