From dc1311340f4f38cfc79ecc5b1e6ff8309bb10b7f Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 27 Sep 2018 15:15:42 +0200 Subject: [PATCH 1/5] add documents property to order --- .../wiaas/includes/class-wiaas-order.php | 41 +++++++++++++++++++ frontend/src/helpers/OrderHelper.js | 1 + 2 files changed, 42 insertions(+) diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-order.php b/backend/app/plugins/wiaas/includes/class-wiaas-order.php index 006fa24..e103884 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-order.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-order.php @@ -118,6 +118,8 @@ class Wiaas_Order { $data = self::_append_order_process($data, $order, $request); $data = self::_append_order_comments($data, $order, $request); + + $data = self::_append_documents($data, $order, $request); } $response->set_data($data); @@ -351,6 +353,45 @@ class Wiaas_Order { } } + return $data; + } + + /** Append downloadable documents from order products if single order is requested + * @param $data + * @param $order + * @param $request + */ + private static function _append_documents($data, $order, $request) { + if (isset($request['id'])) { + $data['documents'] = array(); + + //This requires _append_packages to be called before this method + + foreach($data['line_items'] as $item){ + $product = wc_get_product($item['product_id']); + + $product_documents = array(); + foreach ( $product->get_downloads() as $file_id => $file ) { + $product_documents[] = array( + 'id' => $file_id, + 'name' => $file->get_name(), + 'extension' => $file->get_file_extension() + ); + } + + if (count($product_documents)){ + $data['documents'][] = array( + 'package' => array( + 'id' => $product->get_id(), + 'name' => $product->get_name() + ), + 'documents' => $product_documents + ); + } + } + + } + return $data; } } diff --git a/frontend/src/helpers/OrderHelper.js b/frontend/src/helpers/OrderHelper.js index 10b604a..9db29e9 100644 --- a/frontend/src/helpers/OrderHelper.js +++ b/frontend/src/helpers/OrderHelper.js @@ -37,6 +37,7 @@ export const fromWCOrder = (WCOrder) => { email: WCOrder.billing.email, address: formatAddress(WCOrder.billing) }, + documents: WCOrder.documents, packages: WCOrder['line_items'].map(packageLine => { return { id: packageLine['product_id'], From 174517a161f404ab057b90c69d3bf048bc79dc16 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 27 Sep 2018 15:21:43 +0200 Subject: [PATCH 2/5] stop fetching data on view change --- frontend/src/containers/orders/ProcessContainer.jsx | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/frontend/src/containers/orders/ProcessContainer.jsx b/frontend/src/containers/orders/ProcessContainer.jsx index 4c04de0..7ae0345 100644 --- a/frontend/src/containers/orders/ProcessContainer.jsx +++ b/frontend/src/containers/orders/ProcessContainer.jsx @@ -140,9 +140,6 @@ class ProcessContainer extends Component { } onViewChange(activeView){ - if(activeView === 'documents'){ - this.props.dispatch(fetchOrderInfo(this.props.idOrder)); - } this.setState({activeView}); } @@ -232,7 +229,7 @@ class ProcessContainer extends Component { { this.state.activeView === 'documents' && - + } } From 2cd4a77a342dc5b0c205ff3f04ac6f097729cb78 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 27 Sep 2018 15:22:04 +0200 Subject: [PATCH 3/5] rename props --- .../orders/components/OrderDocuments.jsx | 8 ++---- .../orders/components/OrderDocumentsGroup.jsx | 28 +++++++++---------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/frontend/src/containers/orders/components/OrderDocuments.jsx b/frontend/src/containers/orders/components/OrderDocuments.jsx index 1b42f47..3e49821 100644 --- a/frontend/src/containers/orders/components/OrderDocuments.jsx +++ b/frontend/src/containers/orders/components/OrderDocuments.jsx @@ -5,16 +5,14 @@ import {orderTexts} from '../../../constants/ordersConstants'; class OrderDocuments extends Component { render() { - const {orderInfo} = this.props; + const {orderDocumentsBundle} = this.props; return (
{ - orderInfo.packages.map(orderPackage => ) - } - { - orderInfo.orderDocuments && + orderDocumentsBundle.map(bundle => ) } +
); } diff --git a/frontend/src/containers/orders/components/OrderDocumentsGroup.jsx b/frontend/src/containers/orders/components/OrderDocumentsGroup.jsx index cd11fb0..6f6ed24 100644 --- a/frontend/src/containers/orders/components/OrderDocumentsGroup.jsx +++ b/frontend/src/containers/orders/components/OrderDocumentsGroup.jsx @@ -40,39 +40,39 @@ class OrderDocumentsGroup extends Component { } getDocumentText(document) { - const name = document.documentName + '.' + document.extension; + const name = document.name + '.' + document.extension; return name.length > 9 ? name.substring(0, 10) + '...' : name; } - downloadDocument(document){ - const fileUrl = `${API_SERVER}/utils/api/downloadFile?idDocument=${document.idDocument}&fileName=${document.documentName}.${document.extension}&fileType=${document.documentTypeName}` - const fileName = document.documentName + '.' + document.extension; + downloadDocument(document, packageID){ + const fileUrl = `${API_SERVER}/wp-json/wiaas/download-package-file?document_id=${document.id}&package_id=${packageID}` + const fileName = document.name + '.' + document.extension; fileHandler.download(fileUrl, fileName); } render() { - const {documentsGroup} = this.props; + const {documents, packageName, packageID} = this.props; return (
{ - documentsGroup.documents && - documentsGroup.documents.length > 0 && - + documents && + documents.length > 0 && + { - documentsGroup.documents.map(document => -
{this.downloadDocument(document)}} className="document-link-big"> + documents.map(document => +
{this.downloadDocument(document, packageID)}} className="document-link-big">
{this.getDocumentText(document)} this.toggle(document.idDocument)}> - {document.documentName} ({document.extension}) + isOpen={this.state[document.id]} + target={'document-' + document.id} + toggle={()=>this.toggle(document.id)}> + {document.name} ({document.extension})
) From 925896c469a68a54836a9256189ca97a856f84bd Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 27 Sep 2018 15:22:48 +0200 Subject: [PATCH 4/5] don't show documents on other tabs --- .../orders/components/packages/PackageInfo.jsx | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/frontend/src/containers/orders/components/packages/PackageInfo.jsx b/frontend/src/containers/orders/components/packages/PackageInfo.jsx index 66d086e..a79bac8 100644 --- a/frontend/src/containers/orders/components/packages/PackageInfo.jsx +++ b/frontend/src/containers/orders/components/packages/PackageInfo.jsx @@ -22,12 +22,6 @@ class PackageInfo extends Component { }); } - downloadDocument(document){ - const fileUrl = `${API_SERVER}/utils/api/downloadFile?idDocument=${document.idDocument}&fileName=${document.documentName}.${document.extension}&fileType=${document.documentTypeName}` - const fileName = document.documentName + '.' + document.extension; - fileHandler.download(fileUrl, fileName); - } - render() { const {orderPackage, currency, onViewChange} = this.props; const shouldShowPriceInfo = orderPackage.recurringPrice > 0 || orderPackage.servicePrice > 0; @@ -122,17 +116,6 @@ class PackageInfo extends Component { } } - { - orderPackage.documents && - -
{orderTexts.labels.DOCUMENTS}:
- { - orderPackage.documents.length > 0 ? -
onViewChange('documents')}>{orderTexts.buttons.SEE_DOCUMENTS}
: - {orderTexts.labels.NO_DOCUMENTS} - } - - } ); From ea9a47fd39f9c1b86373507f3319c3def1ed3677 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Sun, 30 Sep 2018 21:21:14 +0200 Subject: [PATCH 5/5] create unit test --- .../tests/unit-tests/test-wiaas-order.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-order.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-order.php index b330dfb..6d78633 100644 --- a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-order.php +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-order.php @@ -198,4 +198,28 @@ class Wiaas_Order_Test extends Wiaas_Unit_Test_Case { $this->assertArrayHasKey('email', $transformed_order_response['commercial_lead']); $this->assertArrayHasKey('phone', $transformed_order_response['commercial_lead']); } + + /** + * @covers Wiaas_Order::transform_rest_order() + */ + function test_order_rest_response_has_empty_documents_when_package_has_no_documents() { + $order_response = array( + 'customer_id' => $this->customer_id, + 'status' => 'processing', + 'line_items' => array() + ); + $request = array( 'id' => $this->order_id); + + $order_rest_response = new WP_REST_Response($order_response); + + $order_rest_response = Wiaas_Order::transform_rest_order( + $order_rest_response, + wc_get_order($this->order_id), + $request); + + $transformed_order_response = $order_rest_response->get_data(); + + $this->assertNotNull($transformed_order_response['documents']); + $this->assertTrue(is_array($transformed_order_response['documents'])); + } } \ No newline at end of file