-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Transactions: Use server based export for both sync download on browser and async email export #10049
Transactions: Use server based export for both sync download on browser and async email export #10049
Changes from 11 commits
a2df32c
fb28a83
e000558
f23b237
eebb724
d367565
b782842
68c9291
3b0fc57
565cf8b
899f809
8eebcd6
406bfa6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: update | ||
|
||
Change Transactions sync download to use server export. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import { uniq } from 'lodash'; | |
import { useDispatch } from '@wordpress/data'; | ||
import { useMemo } from '@wordpress/element'; | ||
import { __, _n, sprintf } from '@wordpress/i18n'; | ||
|
||
import { | ||
TableCard, | ||
Search, | ||
|
@@ -19,11 +20,6 @@ import { | |
getQuery, | ||
updateQueryString, | ||
} from '@woocommerce/navigation'; | ||
import { | ||
downloadCSVFile, | ||
generateCSVDataFromTable, | ||
generateCSVFileName, | ||
} from '@woocommerce/csv-export'; | ||
import apiFetch from '@wordpress/api-fetch'; | ||
|
||
/** | ||
|
@@ -96,6 +92,11 @@ interface Column extends TableCardColumn { | |
labelInCsv?: string; | ||
} | ||
|
||
interface TransactionExportResponse { | ||
download_url?: string; | ||
exported_transactions: number; | ||
} | ||
|
||
const getPaymentSourceDetails = ( txn: Transaction ) => { | ||
if ( ! txn.source_identifier ) { | ||
return <Fragment></Fragment>; | ||
|
@@ -600,6 +601,7 @@ export const TransactionsList = ( | |
const downloadable = !! rows.length; | ||
|
||
const endpointExport = async ( language: string ) => { | ||
const downloadType = totalRows > rows.length ? 'async' : 'sync'; | ||
// We destructure page and path to get the right params. | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { page, path, ...params } = getQuery(); | ||
|
@@ -662,7 +664,7 @@ export const TransactionsList = ( | |
window.confirm( confirmMessage ) | ||
) { | ||
try { | ||
await apiFetch( { | ||
const response = await apiFetch< TransactionExportResponse >( { | ||
path: getTransactionsCSV( { | ||
userEmail, | ||
locale, | ||
|
@@ -686,20 +688,29 @@ export const TransactionsList = ( | |
riskLevelIs, | ||
riskLevelIsNot, | ||
depositId, | ||
downloadType, | ||
} ), | ||
method: 'POST', | ||
} ); | ||
|
||
createNotice( | ||
'success', | ||
sprintf( | ||
__( | ||
'Your export will be emailed to %s', | ||
'woocommerce-payments' | ||
), | ||
userEmail | ||
) | ||
); | ||
if ( response?.download_url ) { | ||
const link = document.createElement( 'a' ); | ||
// Add force_download=true to the URL to force the download, which adds the appropriate `Content-Disposition: attachment` header when using production server. | ||
link.href = response.download_url + '?force_download=true'; | ||
link.click(); | ||
} else { | ||
// Show email notification if no direct download URL | ||
createNotice( | ||
'success', | ||
sprintf( | ||
__( | ||
'Your export will be emailed to %s', | ||
'woocommerce-payments' | ||
), | ||
userEmail | ||
) | ||
); | ||
} | ||
} catch { | ||
createNotice( | ||
'error', | ||
|
@@ -718,32 +729,17 @@ export const TransactionsList = ( | |
// We destructure page and path to get the right params. | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { page, path, ...params } = getQuery(); | ||
const downloadType = totalRows > rows.length ? 'endpoint' : 'browser'; | ||
|
||
recordEvent( 'wcpay_transactions_download_csv_click', { | ||
location: props.depositId ? 'deposit_details' : 'transactions', | ||
download_type: downloadType, | ||
exported_transactions: rows.length, | ||
total_transactions: transactionsSummary.count, | ||
} ); | ||
Comment on lines
713
to
717
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💭 Rather than remove, should we expand the event prop |
||
|
||
if ( 'endpoint' === downloadType ) { | ||
if ( ! isDefaultSiteLanguage() && ! isExportModalDismissed() ) { | ||
setCSVExportModalOpen( true ); | ||
} else { | ||
endpointExport( '' ); | ||
} | ||
if ( ! isDefaultSiteLanguage() && ! isExportModalDismissed() ) { | ||
setCSVExportModalOpen( true ); | ||
} else { | ||
const columnsToDisplayInCsv = columnsToDisplay.map( ( column ) => { | ||
if ( column.labelInCsv ) { | ||
return { ...column, label: column.labelInCsv }; | ||
} | ||
return column; | ||
} ); | ||
downloadCSVFile( | ||
generateCSVFileName( title, params ), | ||
generateCSVDataFromTable( columnsToDisplayInCsv, rows ) | ||
); | ||
endpointExport( '' ); | ||
} | ||
|
||
setIsDownloading( false ); | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -432,12 +432,13 @@ public function get_fraud_outcome_transactions_export( $request ) { | |||||
* @param string $user_email The email to search for. | ||||||
* @param string $deposit_id The deposit to filter on. | ||||||
* @param string $locale Site locale. | ||||||
* @param string $download_type The type of download to perform. | ||||||
* | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💭 It may be worth listing the possible values to clarify what this param is for.
Suggested change
|
||||||
* @return array Export summary | ||||||
* | ||||||
* @throws API_Exception - Exception thrown on request failure. | ||||||
*/ | ||||||
public function get_transactions_export( $filters = [], $user_email = '', $deposit_id = null, $locale = null ) { | ||||||
public function get_transactions_export( $filters = [], $user_email = '', $deposit_id = null, $locale = null, $download_type = null ) { | ||||||
// Map Order # terms to the actual charge id to be used in the server. | ||||||
if ( ! empty( $filters['search'] ) ) { | ||||||
$filters['search'] = WC_Payments_Utils::map_search_orders_to_charge_ids( $filters['search'] ); | ||||||
|
@@ -451,6 +452,9 @@ public function get_transactions_export( $filters = [], $user_email = '', $depos | |||||
if ( ! empty( $locale ) ) { | ||||||
$filters['locale'] = $locale; | ||||||
} | ||||||
if ( ! empty( $download_type ) ) { | ||||||
$filters['download_type'] = $download_type; | ||||||
} | ||||||
|
||||||
return $this->request( $filters, self::TRANSACTIONS_API . '/download', self::POST ); | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency,
download_type: query.downloadType
can be placed withinformatQueryFilters
which is conducting the same type of operation on other filter params.