From f2d3efc5265aa90729f7dbca1a33c8c98886be6f Mon Sep 17 00:00:00 2001 From: Nedim Uka Date: Thu, 30 Aug 2018 13:25:22 +0200 Subject: [PATCH 01/41] Added templates product --- .../js/wiaas-template-admin-selection.js | 51 ++++ .../js/wiaas-template-admin-write-panels.js | 247 ++++++++++++++++++ .../class-wiaas-admin-template-selection.php | 247 ++++++++++++++++++ .../class-wiaas-template-admin-ajax.php | 82 ++++++ .../class-wiaas-template-products.php | 233 +++++++++++++++++ .../class-wiaas-wc-product-template.php | 46 ++++ .../views/html-wiaas-template-product.php | 36 +++ .../views/html-wiaas-template-selection.php | 13 + .../wiaas/includes/class-wiaas-admin.php | 6 +- 9 files changed, 960 insertions(+), 1 deletion(-) create mode 100644 backend/app/plugins/wiaas/assets/js/wiaas-template-admin-selection.js create mode 100644 backend/app/plugins/wiaas/assets/js/wiaas-template-admin-write-panels.js create mode 100644 backend/app/plugins/wiaas/includes/admin/template/class-wiaas-admin-template-selection.php create mode 100644 backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-admin-ajax.php create mode 100644 backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-products.php create mode 100644 backend/app/plugins/wiaas/includes/admin/template/class-wiaas-wc-product-template.php create mode 100644 backend/app/plugins/wiaas/includes/admin/template/views/html-wiaas-template-product.php create mode 100644 backend/app/plugins/wiaas/includes/admin/template/views/html-wiaas-template-selection.php diff --git a/backend/app/plugins/wiaas/assets/js/wiaas-template-admin-selection.js b/backend/app/plugins/wiaas/assets/js/wiaas-template-admin-selection.js new file mode 100644 index 0000000..1ad4aac --- /dev/null +++ b/backend/app/plugins/wiaas/assets/js/wiaas-template-admin-selection.js @@ -0,0 +1,51 @@ +jQuery(document).ready(function ($) { + var $selectTemplate = $('#_select_template'); + var $container = $('#wiaas_selected_template_items_container'); + + $selectTemplate + + .on('change', function () { + var selectedTemplate = $selectTemplate.val(); + + var data = { + action: 'wiaas_select_template', + selected_template: selectedTemplate + }; + + + setTimeout(function () { + + $.post(woocommerce_admin_meta_boxes.ajax_url, data, function (response) { + + if ('' !== response.markup) { + + $container.append(response.markup); + + } else { + $container.children("div").remove(); + } + + if (response.message !== '') { + window.alert(response.message); + } + + + }); + + }, 250); + }); + + var $product_type = $('#product-type'); + var $template_product_meta_box = $('#template_product_meta_box'); + $product_type.on('change', function () { + var selectedProductType = $product_type.val(); + + if (selectedProductType === 'bundle') { + $template_product_meta_box.show(); + } else { + $template_product_meta_box.hide(); + } + }); + + +}); \ No newline at end of file diff --git a/backend/app/plugins/wiaas/assets/js/wiaas-template-admin-write-panels.js b/backend/app/plugins/wiaas/assets/js/wiaas-template-admin-write-panels.js new file mode 100644 index 0000000..8054a6e --- /dev/null +++ b/backend/app/plugins/wiaas/assets/js/wiaas-template-admin-write-panels.js @@ -0,0 +1,247 @@ +jQuery(function ($) { + + var $edit_in_cart = $('.bundle_edit_in_cart'), + $group_mode_select = $('select#_wc_pb_group_mode'), + $bundled_products_panel = $('#bundled_product_data'), + $bundled_products_toolbar = $bundled_products_panel.find('.toolbar'), + $template_products_container_hardware = $('.wiaas-template-items_hardware'), + $template_products_container_software = $('.wiaas-template-items_software'), + $template_products_container_services = $('.wiaas-template-items_services'), + $template_products_container_isntallation = $('.wiaas-template-items_installation'), + + $template_products = $('.wc-bundled-item', + $template_products_container_services, + $template_products_container_software, + $template_products_container_hardware, + $template_products_container_isntallation), + + $template_search_hardware = $('#wiaastemplate_products_hardware'), + $template_search_software = $('#wiaastemplate_products_software'), + $template_search_services = $('#wiaastemplate_products_services'), + $template_search_installation = $('#wiaastemplate_products_installation'), + bundled_product_objects = {}, + bundled_products_add_count = $template_products.length, + block_params = { + message: null, + overlayCSS: { + background: '#fff', + opacity: 0.6 + } + }; + + $.fn.wc_bundles_select2 = function () { + $(document.body).trigger('wc-enhanced-select-init'); + }; + + // Bundle type move stock msg up. + $('.bundle_stock_msg').appendTo('._manage_stock_field .description'); + + // Simple type options are valid for bundles. + $('.show_if_simple:not(.hide_if_bundle)').addClass('show_if_bundle'); + + $('body').on('woocommerce-product-type-change', function (event, select_val) { + + if ('bundle' === select_val) { + + $('.show_if_external').hide(); + $('.show_if_bundle').show(); + + $('input#_manage_stock').change(); + + $('#_nyp').change(); + } + + }); + + $group_mode_select.change(function () { + if ($.inArray($group_mode_select.val(), wc_bundles_admin_params.group_modes_with_parent) === -1) { + $edit_in_cart.hide(); + } else { + $edit_in_cart.show(); + } + }); + + // Downloadable support. + $('input#_downloadable').change(function () { + $('select#product-type').change(); + }); + + // Trigger product type change. + $('select#product-type').change(); + + // Trigger group mode change. + $group_mode_select.change(); + + + init_event_handlers(); + + function init_event_handlers() { + + + $template_search_hardware + + .on('change', function () { + addSearchedProduct('hardware', $template_search_hardware, $template_products_container_hardware) + }); + + $template_search_software + + .on('change', function () { + addSearchedProduct('software', $template_search_software, $template_products_container_software) + }); + + + $template_search_installation + + .on('change', function () { + addSearchedProduct('services', $template_search_installation, $template_products_container_isntallation) + }); + + + $template_search_services + + .on('change', function () { + addSearchedProduct('installation', $template_search_services, $template_products_container_services) + }); + + + function addSearchedProduct(options, search, container) { + var bundled_product_ids = search.val(), + bundled_product_id = bundled_product_ids && bundled_product_ids.length > 0 ? bundled_product_ids.shift() : false; + + if (!bundled_product_id) { + return false; + } + + search.val([]).change(); + + $bundled_products_panel.block(block_params); + + bundled_products_add_count++; + + var data = { + action: 'wiaas_add_template_product', + post_id: woocommerce_admin_meta_boxes.post_id, + id: bundled_products_add_count, + product_id: bundled_product_id, + security: wc_bundles_admin_params.add_bundled_product_nonce, + options: options, + }; + + setTimeout(function () { + + $.post(woocommerce_admin_meta_boxes.ajax_url, data, function (response) { + + if ('' !== response.markup) { + + container.append(response.markup); + + var $added = $('.wiaas-template-item', container).last(), + added_id = 'bundled_item_' + bundled_products_add_count; + + $added.data('bundled_item_id', added_id); + $added.wc_bundles_select2(); + + $bundled_products_panel.trigger('wc-bundles-added-bundled-product'); + + } else if (response.message !== '') { + window.alert(response.message); + } + + // Open and close to resolve "sticky" modal issue. + if ('yes' === wc_bundles_admin_params.is_wc_version_gte_3_2) { + search.selectWoo('open'); + search.selectWoo('close'); + } else { + search.select2('open'); + search.select2('close'); + } + + $bundled_products_panel.unblock(); + + }); + + }, 250); + + return false; + } + + $template_products_container_isntallation + + // Remove Item. + .on('click', 'a.remove_row', function (e) { + + var $el = $(this).closest('.wc-bundled-item'), + el_id = $el.data('bundled_item_id'); + + $el.find('*').off(); + $el.remove(); + + delete bundled_product_objects[el_id]; + + $bundled_products_panel.triggerHandler('wc-bundled-products-changed'); + + e.preventDefault(); + + }); + + $template_products_container_hardware + + // Remove Item. + .on('click', 'a.remove_row', function (e) { + + var $el = $(this).closest('.wc-bundled-item'), + el_id = $el.data('bundled_item_id'); + + $el.find('*').off(); + $el.remove(); + + delete bundled_product_objects[el_id]; + + $bundled_products_panel.triggerHandler('wc-bundled-products-changed'); + + e.preventDefault(); + + }); + + $template_products_container_software + + // Remove Item. + .on('click', 'a.remove_row', function (e) { + + var $el = $(this).closest('.wc-bundled-item'), + el_id = $el.data('bundled_item_id'); + + $el.find('*').off(); + $el.remove(); + + delete bundled_product_objects[el_id]; + + $bundled_products_panel.triggerHandler('wc-bundled-products-changed'); + + e.preventDefault(); + + }); + + $template_products_container_services + + // Remove Item. + .on('click', 'a.remove_row', function (e) { + + var $el = $(this).closest('.wc-bundled-item'), + el_id = $el.data('bundled_item_id'); + + $el.find('*').off(); + $el.remove(); + + delete bundled_product_objects[el_id]; + + $bundled_products_panel.triggerHandler('wc-bundled-products-changed'); + + e.preventDefault(); + + }) + + } +}); + diff --git a/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-admin-template-selection.php b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-admin-template-selection.php new file mode 100644 index 0000000..828768e --- /dev/null +++ b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-admin-template-selection.php @@ -0,0 +1,247 @@ +(optional)', 'cmb'), + 'Wiaas_Admin_Template_Selection::add_custom_content_meta_box', + 'product', + 'normal', + 'high' + ); + } + + + public static function add_custom_content_meta_box($post) { + + $value = get_post_meta($post->ID, '_select_template', true); + + if (empty($value)) { + $value = ''; + } + + $products = wc_get_products(array( + 'type' => 'wiaastemplate' + )); + $product_ids[''] = __('Select a template', 'wiaas'); + foreach ($products as $prod) { + $product_ids[$prod->id] = $prod->name; + } + woocommerce_wp_select(array( + 'id' => '_select_template', + 'options' => $product_ids, + 'value' => $value + )); + + ?> +
$template_products_hardware, + 'service' => $template_products_service, + 'installation' => $template_products_installation, + 'software' => $template_products_software + ); + + } + + public static function render_template_products($template_products) { + + if (!empty($template_products)) { + + foreach ($template_products as $item) { + + $product_id = $item['product_id']; + $title = $item['product_title']; + $quantity = $item['quantity']; + + include('views/html-wiaas-template-selection.php'); + + } + } + } + + public static function get_product_categories($products) { + $category_ids = array(); + + if (empty($products)) { + return $category_ids; + } + + foreach ($products as $product) { + + $product_id = $product['product_id']; + + $post_terms = wp_get_object_terms($product_id, 'product_cat', array('fields' => 'id=>name')); + + if (!empty($post_terms) && !is_wp_error($post_terms)) { + + $cat = ''; + + foreach ($post_terms as $term) { + if ($term != "Uncategorized") { + $cat = $term; + } + } + + $product_with_cat = array( + 'quantity' => $product['quantity'], + 'category_term' => $cat + ); + + $category_ids[$product_id] = $product_with_cat; + + + } + } + return $category_ids; + } + + public static function get_bundled_product_categories($bundled_items) { + + $category_ids = array(); + foreach ($bundled_items as $item_id => $item) { + $item_data = $item->get_data(); + $post_terms = wp_get_object_terms($item->get_product_id(), 'product_cat', array('fields' => 'id=>name')); + + if (!empty($post_terms) && !is_wp_error($post_terms)) { + + $cat = ''; + + foreach ($post_terms as $term) { + if ($term != "Uncategorized") { + $cat = $term; + } + } + + $category_ids[$cat] = $item_data['quantity_max']; + + } + } + + return $category_ids; + } + + + function save_custom_content_meta_box($post_id) { + $selected_template = $_POST['_select_template']; + if (!empty($selected_template)) + update_post_meta($post_id, '_select_template', esc_attr($selected_template)); + else { + update_post_meta($post_id, '_select_template', ''); + } + + } + + public static function validate_bundle($product) { + + + if ($product->get_type() == 'bundle') { + $selected_template = $_POST['_select_template']; + $bundled_items = $product->get_bundled_items('view'); + $products_form_template = self::show_template_products($selected_template); + + $missing_categories = array(); + $insufficient_product_quantities = array(); + + + $categories_form_template = self::get_categories_from_templates($products_form_template); + $categories_form_bundle = self::get_bundled_product_categories($bundled_items); + $bundle_category_keys = array_keys($categories_form_bundle); + + + if (!empty($categories_form_template)) { + + foreach ($categories_form_template as $category) { + + if (!in_array($category['category_term'], $bundle_category_keys)) { + + array_push($missing_categories, $category['category_term']); + + } else { + + $product_from_bundle_quantity = $categories_form_bundle[$category['category_term']]; + + if ($category['quantity'] != $product_from_bundle_quantity) { + array_push($insufficient_product_quantities, $category['category_term']); + } + } + } + } + + $categories_meesage = implode(',', $missing_categories); + $quantity_message = implode(',', $insufficient_product_quantities); + + + if (!empty($missing_categories)) { + WC_PB_Meta_Box_Product_Data::add_admin_error(__(' WIAAS This product bundle does not correspond to selected template Categories missing: ' . $categories_meesage, 'woocommerce-product-bundles')); + } + + if (!empty($insufficient_product_quantities)) { + WC_PB_Meta_Box_Product_Data::add_admin_error(__(' WIAAS This product bundle does not correspond to selected template Categories with different quantities: ' . $quantity_message, 'woocommerce-product-bundles')); + } + } + + } + + public + static function get_category_ids_form_bundle($product) { + $products_form_budnle = $product->get_bundled_data_items(); + $product_ids = array(); + + foreach ($products_form_budnle as $product) { + array_push($product_ids, $product->get_id()); + } + + return self::get_product_categories($product_ids); + + } +} + +Wiaas_Admin_Template_Selection::init(); diff --git a/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-admin-ajax.php b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-admin-ajax.php new file mode 100644 index 0000000..51f4b98 --- /dev/null +++ b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-admin-ajax.php @@ -0,0 +1,82 @@ + '', + 'message' => '' + ); + + $template_products = Wiaas_Admin_Template_Selection::show_template_products($selected_template_id); + + if (!empty($template_products)) { + ob_start(); + Wiaas_Admin_Template_Selection::render_template_products($template_products['hardware']); + Wiaas_Admin_Template_Selection::render_template_products($template_products['service']); + Wiaas_Admin_Template_Selection::render_template_products($template_products['installation']); + Wiaas_Admin_Template_Selection::render_template_products($template_products['software']); + + $response['markup'] = ob_get_clean(); + } + + wp_send_json($response); + } + + + /** + * Handles adding products to template via ajax. + */ + public static function ajax_add_wiaas_template_product() { + +// check_ajax_referer( 'wc_bundles_add_bundled_product', 'security' ); + + $loop = intval($_POST['id']); + $product_id = intval($_POST['product_id']); + $options = $_POST['options']; + $product = wc_get_product($product_id); + $title = $product->get_title(); + + + $response = array( + 'markup' => '', + 'message' => '' + ); + + if ($product) { + + ob_start(); + include('views/html-wiaas-template-product.php'); + $response['markup'] = ob_get_clean(); + } else { + $response['message'] = __('The selected product is invalid.', 'woocommerce-product-bundles'); + } + + wp_send_json($response); + } +} + +Wiaas_Template_Admin_Ajax::init(); diff --git a/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-products.php b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-products.php new file mode 100644 index 0000000..18e7ff6 --- /dev/null +++ b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-products.php @@ -0,0 +1,233 @@ + + __('Hardware', 'wiaastemplate_hardware'), + 'target' => 'wiaastemplate_options_hardware', + 'class' => array('show_if_wiaastemplate', 'show_if_wiaastemplate'), + ); + + $tabs['wiaastemplate_installation'] = array( + 'label' => __('Installation', 'wiaastemplate_isntallation'), + 'target' => 'wiaastemplate_options_installation', + 'class' => array('show_if_wiaastemplate', 'show_if_wiaastemplate'), + ); + + $tabs['wiaastemplate_services'] = array( + 'label' => __('Services', 'wiaastemplate_services'), + 'target' => 'wiaastemplate_options_services', + 'class' => array('show_if_wiaastemplate', 'show_if_wiaastemplate'), + ); + + $tabs['wiaastemplate_software'] = array( + 'label' => __('Software', 'wiaastemplate_software'), + 'target' => 'wiaastemplate_options_software', + 'class' => array('show_if_wiaastemplate', 'show_if_wiaastemplate'), + ); + + return $tabs; + + } + + public static function wiaastemplate_product_tab_content_software() { + + self::wiaastemplate_product_tab_content('hardware'); + self::wiaastemplate_product_tab_content('installation'); + self::wiaastemplate_product_tab_content('services'); + self::wiaastemplate_product_tab_content('software'); + } + + + /** + * Contents of the template options product tab. + */ + public static function wiaastemplate_product_tab_content($options) { + + global $post; + $template_items = get_post_meta($post->ID, '_template_items_' . $options, true); + + ?> +
' class='panel woocommerce_options_panel'> +
+ + + + +
$product_id, + 'product_title' => $product_title, + 'item_id' => $item_id, + 'quantity' => $quantity + + ); + + $processed_template_data[$product_id] = $item_data; + + } + + update_post_meta($post_id, '_template_items_' . $option, $processed_template_data); + } + + public static function menu_sort_order($a, $b) { + if (isset($a['menu_order']) && isset($b['menu_order'])) { + return $a['menu_order'] - $b['menu_order']; + } else { + return isset($a['menu_order']) ? 1 : -1; + } + } + + + /** + * Hide Attributes data panel. + */ + function hide_attributes_data_panel($tabs) { + + $tabs['advanced']['class'][] = 'hide_if_simple_wiaastemplate hide_if_wiaastemplate'; + $tabs['shipping']['class'][] = 'hide_if_simple_wiaastemplate hide_if_wiaastemplate'; + $tabs['linked_product']['class'][] = 'hide_if_simple_wiaastemplate hide_if_wiaastemplate'; + $tabs['attribute']['class'][] = 'hide_if_simple_wiaastemplate hide_if_wiaastemplate'; + + return $tabs; + + } +} + +Wiaas_template::init(); + diff --git a/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-wc-product-template.php b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-wc-product-template.php new file mode 100644 index 0000000..5080c6d --- /dev/null +++ b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-wc-product-template.php @@ -0,0 +1,46 @@ + +
+

+ + +

+
+ + +
'_product_quantity', + 'label' => __('Quantity', 'woocommerce'), + 'desc_tip' => 'true', + 'description' => __('Choose the quantity of product', 'woocommerce'), + 'type' => 'text', + 'name' => 'template_data_'.$options .'['. $loop.'][quantity]', + 'value' => $quantity + )); + + ?>
diff --git a/backend/app/plugins/wiaas/includes/admin/template/views/html-wiaas-template-selection.php b/backend/app/plugins/wiaas/includes/admin/template/views/html-wiaas-template-selection.php new file mode 100644 index 0000000..dbf9438 --- /dev/null +++ b/backend/app/plugins/wiaas/includes/admin/template/views/html-wiaas-template-selection.php @@ -0,0 +1,13 @@ + +
+

+ + +

+
Date: Wed, 19 Sep 2018 22:33:59 +0200 Subject: [PATCH 02/41] Repalced virtual product with template category --- .../js/wiaas-template-admin-write-panels.js | 84 +++++++++++++++++ .../class-wiaas-admin-template-selection.php | 91 +++++++++--------- .../class-wiaas-template-admin-ajax.php | 42 ++++++--- .../class-wiaas-template-products.php | 29 +++--- .../wiaas/includes/class-wiass-templates.php | 11 +++ .../class-wiaas-template-category-object.php | 23 +++++ .../class-wiaas-template-category.php | 93 +++++++++++++++++++ backend/app/plugins/wiaas/wiaas.php | 2 + 8 files changed, 297 insertions(+), 78 deletions(-) create mode 100644 backend/app/plugins/wiaas/includes/class-wiass-templates.php create mode 100644 backend/app/plugins/wiaas/includes/templates/class-wiaas-template-category-object.php create mode 100644 backend/app/plugins/wiaas/includes/templates/class-wiaas-template-category.php diff --git a/backend/app/plugins/wiaas/assets/js/wiaas-template-admin-write-panels.js b/backend/app/plugins/wiaas/assets/js/wiaas-template-admin-write-panels.js index 8054a6e..0baeabe 100644 --- a/backend/app/plugins/wiaas/assets/js/wiaas-template-admin-write-panels.js +++ b/backend/app/plugins/wiaas/assets/js/wiaas-template-admin-write-panels.js @@ -109,6 +109,8 @@ jQuery(function ($) { var bundled_product_ids = search.val(), bundled_product_id = bundled_product_ids && bundled_product_ids.length > 0 ? bundled_product_ids.shift() : false; + var template_category_title =search.text(); + if (!bundled_product_id) { return false; } @@ -123,6 +125,7 @@ jQuery(function ($) { action: 'wiaas_add_template_product', post_id: woocommerce_admin_meta_boxes.post_id, id: bundled_products_add_count, + title: template_category_title, product_id: bundled_product_id, security: wc_bundles_admin_params.add_bundled_product_nonce, options: options, @@ -243,5 +246,86 @@ jQuery(function ($) { }) } + + // Ajax product search box + $( ':input.wiaas-term-search' ).filter( ':not(.enhanced)' ).each( function() { + var select2_args = { + allowClear: $( this ).data( 'allow_clear' ) ? true : false, + placeholder: $( this ).data( 'placeholder' ), + minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : '3', + escapeMarkup: function( m ) { + return m; + }, + ajax: { + url: wc_enhanced_select_params.ajax_url, + dataType: 'json', + delay: 250, + data: function( params ) { + return { + term: params.term, + action: 'wiaas_template_category_search', + security: wc_enhanced_select_params.search_products_nonce, + exclude: $( this ).data( 'exclude' ), + include: $( this ).data( 'include' ), + limit: $( this ).data( 'limit' ) + }; + }, + processResults: function( data ) { + var terms = []; + if ( data ) { + $.each( data, function( id, text ) { + terms.push( { id: id, text: text } ); + }); + } + return { + results: terms + }; + }, + cache: true + } + }; + + // select2_args = $.extend( select2_args, getEnhancedSelectFormatString() ); + + $( this ).selectWoo( select2_args ).addClass( 'enhanced' ); + + if ( $( this ).data( 'sortable' ) ) { + var $select = $(this); + var $list = $( this ).next( '.select2-container' ).find( 'ul.select2-selection__rendered' ); + + $list.sortable({ + placeholder : 'ui-state-highlight select2-selection__choice', + forcePlaceholderSize: true, + items : 'li:not(.select2-search__field)', + tolerance : 'pointer', + stop: function() { + $( $list.find( '.select2-selection__choice' ).get().reverse() ).each( function() { + var id = $( this ).data( 'data' ).id; + var option = $select.find( 'option[value="' + id + '"]' )[0]; + $select.prepend( option ); + } ); + } + }); + // Keep multiselects ordered alphabetically if they are not sortable. + } else if ( $( this ).prop( 'multiple' ) ) { + $( this ).on( 'change', function(){ + var $children = $( this ).children(); + $children.sort(function(a, b){ + var atext = a.text.toLowerCase(); + var btext = b.text.toLowerCase(); + + if ( atext > btext ) { + return 1; + } + if ( atext < btext ) { + return -1; + } + return 0; + }); + $( this ).html( $children ); + }); + } + }); + }); diff --git a/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-admin-template-selection.php b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-admin-template-selection.php index 828768e..e1012a6 100644 --- a/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-admin-template-selection.php +++ b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-admin-template-selection.php @@ -47,6 +47,7 @@ class Wiaas_Admin_Template_Selection { ?>
'id=>name')); - - if (!empty($post_terms) && !is_wp_error($post_terms)) { - - $cat = ''; - - foreach ($post_terms as $term) { - if ($term != "Uncategorized") { - $cat = $term; - } - } - - $product_with_cat = array( - 'quantity' => $product['quantity'], - 'category_term' => $cat - ); - - $category_ids[$product_id] = $product_with_cat; + if (!empty($meta)) { + $category_template_id = $meta['template_category_id']; + + $template_category_object = new Wiaas_Template_Category_Object( + $category_template_id, + $meta['template_category_title'], + $meta['quantity']); + + $category_objects[$category_template_id] = $template_category_object; } + } - return $category_ids; + return empty($category_objects) ? null : $category_objects; } public static function get_bundled_product_categories($bundled_items) { - $category_ids = array(); + $template_category_objects = array(); foreach ($bundled_items as $item_id => $item) { $item_data = $item->get_data(); - $post_terms = wp_get_object_terms($item->get_product_id(), 'product_cat', array('fields' => 'id=>name')); + $post_terms = wp_get_object_terms($item->get_product_id(), 'template_category', array('fields' => 'id=>name')); if (!empty($post_terms) && !is_wp_error($post_terms)) { $cat = ''; + $category_template_id = ''; - foreach ($post_terms as $term) { - if ($term != "Uncategorized") { - $cat = $term; - } + foreach ($post_terms as $id => $term) { + $cat = $term; + $category_template_id = $id; } - $category_ids[$cat] = $item_data['quantity_max']; + $template_category_object = new Wiaas_Template_Category_Object( + $category_template_id, + $cat, + $item_data['quantity_max']); + $template_category_objects[$category_template_id] = $template_category_object; } } - return $category_ids; + return $template_category_objects; } @@ -199,28 +195,30 @@ class Wiaas_Admin_Template_Selection { if (!empty($categories_form_template)) { foreach ($categories_form_template as $category) { + $template_cat_id = $category->template_category_id; - if (!in_array($category['category_term'], $bundle_category_keys)) { - array_push($missing_categories, $category['category_term']); + if (!in_array($template_cat_id, $bundle_category_keys)) { + + array_push($missing_categories, $category->name); } else { - $product_from_bundle_quantity = $categories_form_bundle[$category['category_term']]; + $product_from_bundle_quantity = $categories_form_bundle[$template_cat_id]->quantity; + $quantity_from_template = $category->quantity; - if ($category['quantity'] != $product_from_bundle_quantity) { - array_push($insufficient_product_quantities, $category['category_term']); + if ((int)($quantity_from_template) !== $product_from_bundle_quantity) { + array_push($insufficient_product_quantities, $category->name); } } } } - $categories_meesage = implode(',', $missing_categories); + $categories_message = implode(',', $missing_categories); $quantity_message = implode(',', $insufficient_product_quantities); - if (!empty($missing_categories)) { - WC_PB_Meta_Box_Product_Data::add_admin_error(__(' WIAAS This product bundle does not correspond to selected template Categories missing: ' . $categories_meesage, 'woocommerce-product-bundles')); + WC_PB_Meta_Box_Product_Data::add_admin_error(__(' WIAAS This product bundle does not correspond to selected template Categories missing: ' . $categories_message, 'woocommerce-product-bundles')); } if (!empty($insufficient_product_quantities)) { @@ -230,8 +228,7 @@ class Wiaas_Admin_Template_Selection { } - public - static function get_category_ids_form_bundle($product) { + public static function get_category_ids_form_bundle($product) { $products_form_budnle = $product->get_bundled_data_items(); $product_ids = array(); diff --git a/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-admin-ajax.php b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-admin-ajax.php index 51f4b98..d38d03e 100644 --- a/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-admin-ajax.php +++ b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-admin-ajax.php @@ -12,8 +12,9 @@ class Wiaas_Template_Admin_Ajax { */ public static function init() { - // Ajax add bundled product. + // Ajax template category add_action('wp_ajax_wiaas_add_template_product', array(__CLASS__, 'ajax_add_wiaas_template_product')); + add_action('wp_ajax_wiaas_template_category_search', array(__CLASS__, 'ajax_wiaas_template_category_search')); // Ajax add bundled product. add_action('wp_ajax_wiaas_select_template', array(__CLASS__, 'ajax_handle_template_selection')); @@ -22,7 +23,6 @@ class Wiaas_Template_Admin_Ajax { } - public static function ajax_handle_template_selection() { $selected_template_id = intval($_POST['selected_template']); @@ -48,31 +48,47 @@ class Wiaas_Template_Admin_Ajax { /** - * Handles adding products to template via ajax. + * Handles adding template categories to template via ajax. */ public static function ajax_add_wiaas_template_product() { -// check_ajax_referer( 'wc_bundles_add_bundled_product', 'security' ); + check_ajax_referer('wc_bundles_add_bundled_product', 'security'); $loop = intval($_POST['id']); $product_id = intval($_POST['product_id']); + $title = $_POST['title']; $options = $_POST['options']; - $product = wc_get_product($product_id); - $title = $product->get_title(); - $response = array( 'markup' => '', 'message' => '' ); - if ($product) { + ob_start(); + include('views/html-wiaas-template-product.php'); + $response['markup'] = ob_get_clean(); + + wp_send_json($response); + } + + /** + * + */ + public static function ajax_wiaas_template_category_search() { + + $term = ($_POST['term']); + $response = array(); + + $terms = get_terms(array( + 'taxonomy' => 'template_category', + 'name__like' => $term + )); + + foreach ($terms as $t) { + error_log($t->name); + error_log($t->term_id); + $response[$t->term_id] = $t->name; - ob_start(); - include('views/html-wiaas-template-product.php'); - $response['markup'] = ob_get_clean(); - } else { - $response['message'] = __('The selected product is invalid.', 'woocommerce-product-bundles'); } wp_send_json($response); diff --git a/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-products.php b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-products.php index 18e7ff6..ddb5e54 100644 --- a/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-products.php +++ b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-products.php @@ -9,7 +9,7 @@ class Wiaas_template { add_filter('product_type_selector', array(__CLASS__, 'add_template_product_type')); add_action('admin_footer', array(__CLASS__, 'templates_custom_js')); add_filter('woocommerce_product_data_tabs', array(__CLASS__, 'custom_product_tabs')); - add_action('woocommerce_product_data_panels', array(__CLASS__, 'wiaastemplate_product_tab_content_software')); + add_action('woocommerce_product_data_panels', array(__CLASS__, 'wiaastemplate_product_tab_content_all')); add_action('woocommerce_admin_process_product_object', array(__CLASS__, 'save_wiaastemplate')); add_filter('woocommerce_product_data_tabs', array(__CLASS__, 'hide_attributes_data_panel')); add_action('woocommerce_process_product_meta_wiaastemplate', array(__CLASS__, 'save_wiaastemplate')); @@ -26,11 +26,11 @@ class Wiaas_template { wp_register_script('wiaas-template-admin-selection', $plugin_url . '/assets/js/wiaas-template-admin-selection.js', array('jquery', 'jquery-ui-datepicker', 'wc-admin-meta-boxes')); - wp_enqueue_script( 'wiaas-template-admin-write-panels', $plugin_url . 'assets/js/wiaas-template-admin-write-panels.js', - array( 'jquery', 'jquery-ui-datepicker', 'wc-admin-meta-boxes' )); + wp_enqueue_script('wiaas-template-admin-write-panels', $plugin_url . 'assets/js/wiaas-template-admin-write-panels.js', + array('jquery', 'jquery-ui-datepicker', 'wc-admin-meta-boxes')); - wp_enqueue_script( 'wiaas-template-admin-selection', $plugin_url . '/assets/js/wiaas-template-admin-selection.js', - array( 'jquery', 'jquery-ui-datepicker', 'wc-admin-meta-boxes' )); + wp_enqueue_script('wiaas-template-admin-selection', $plugin_url . '/assets/js/wiaas-template-admin-selection.js', + array('jquery', 'jquery-ui-datepicker', 'wc-admin-meta-boxes')); } @@ -100,7 +100,7 @@ class Wiaas_template { } - public static function wiaastemplate_product_tab_content_software() { + public static function wiaastemplate_product_tab_content_all() { self::wiaastemplate_product_tab_content('hardware'); self::wiaastemplate_product_tab_content('installation'); @@ -128,8 +128,8 @@ class Wiaas_template { foreach ($template_items as $item) { - $product_id = $item['product_id']; - $title = $item['product_title']; + $product_id = $item['template_category_id']; + $title = $item['template_category_title']; $quantity = $item['quantity']; include('views/html-wiaas-template-product.php'); @@ -139,7 +139,7 @@ class Wiaas_template { } ?>
- + + + + + + + + + + + + +
{profileTexts.labels.BILLING_ADDRESSES}
{profileTexts.labels.ADDRESS_CITY}* Date: Wed, 12 Sep 2018 22:22:25 +0200 Subject: [PATCH 20/41] add first name, last name and email fields to profile address on backend --- .../app/plugins/wiaas/includes/class-wiaas-user.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-user.php b/backend/app/plugins/wiaas/includes/class-wiaas-user.php index c06c63d..189a797 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-user.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-user.php @@ -50,7 +50,10 @@ class Wiaas_User { 'idCountrySelected' => $received_address->idCountrySelected, 'city' => $received_address->city, 'detailedAddress' => $received_address->detailedAddress, - 'zipCode' => $received_address->zipCode + 'zipCode' => $received_address->zipCode, + 'firstName' => $received_address->firstName, + 'lastName' => $received_address->lastName, + 'invoiceMail' => $received_address->invoiceMail ); foreach($profile_addresses as $key => $address){ if ($address['id'] === $received_address->id){ @@ -66,7 +69,10 @@ class Wiaas_User { 'idCountrySelected' => $received_address->idCountrySelected, 'city' => $received_address->city, 'detailedAddress' => $received_address->detailedAddress, - 'zipCode' => $received_address->zipCode + 'zipCode' => $received_address->zipCode, + 'firstName' => $received_address->firstName, + 'lastName' => $received_address->lastName, + 'invoiceMail' => $received_address->invoiceMail ); array_push($profile_addresses, $new_delivery_address); From 90402d3e1df870c6b55a5c27bcdd51fd4be97052 Mon Sep 17 00:00:00 2001 From: GotPPay Date: Wed, 12 Sep 2018 22:33:17 +0200 Subject: [PATCH 21/41] change property name to comply with frontend --- backend/app/plugins/wiaas/includes/class-wiaas-user.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-user.php b/backend/app/plugins/wiaas/includes/class-wiaas-user.php index 189a797..fe6efd0 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-user.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-user.php @@ -53,7 +53,7 @@ class Wiaas_User { 'zipCode' => $received_address->zipCode, 'firstName' => $received_address->firstName, 'lastName' => $received_address->lastName, - 'invoiceMail' => $received_address->invoiceMail + 'deliveryMail' => $received_address->deliveryMail ); foreach($profile_addresses as $key => $address){ if ($address['id'] === $received_address->id){ @@ -72,7 +72,7 @@ class Wiaas_User { 'zipCode' => $received_address->zipCode, 'firstName' => $received_address->firstName, 'lastName' => $received_address->lastName, - 'invoiceMail' => $received_address->invoiceMail + 'deliveryMail' => $received_address->deliveryMail ); array_push($profile_addresses, $new_delivery_address); From c322c8ac78f2f4632156863909b0ed211743e861 Mon Sep 17 00:00:00 2001 From: GotPPay Date: Wed, 12 Sep 2018 22:39:18 +0200 Subject: [PATCH 22/41] display name, last name and email above address for profile addresses --- .../cart/components/SelectDeliveryAddress.jsx | 11 ++++++++++- .../profileSettings/components/ProfileAddress.jsx | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/frontend/src/containers/cart/components/SelectDeliveryAddress.jsx b/frontend/src/containers/cart/components/SelectDeliveryAddress.jsx index 9fddad5..2dea908 100644 --- a/frontend/src/containers/cart/components/SelectDeliveryAddress.jsx +++ b/frontend/src/containers/cart/components/SelectDeliveryAddress.jsx @@ -21,7 +21,16 @@ class SelectDeliveryAddress extends Component {
- {profileAddress.countryName}, {profileAddress.city}, {profileAddress.detailedAddress}, {profileAddress.zipCode} +
+ {profileAddress.firstName} {profileAddress.lastName} + { + profileAddress.deliveryMail && + ( {profileAddress.deliveryMail} ) + } +
+
+ {profileAddress.countryName}, {profileAddress.city}, {profileAddress.detailedAddress}, {profileAddress.zipCode} +
- {profileAddress.countryName}, {profileAddress.city}, {profileAddress.detailedAddress}, {profileAddress.zipCode} +
+ {profileAddress.firstName} {profileAddress.lastName} + { + profileAddress.deliveryMail && + ( {profileAddress.deliveryMail} ) + } +
+
+ {profileAddress.countryName}, {profileAddress.city}, {profileAddress.detailedAddress}, {profileAddress.zipCode} +
Date: Wed, 12 Sep 2018 22:46:11 +0200 Subject: [PATCH 23/41] turn off autocomplete also for billing address input/edit --- .../profileSettings/components/AddEditBillingAddress.jsx | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/frontend/src/containers/profileSettings/components/AddEditBillingAddress.jsx b/frontend/src/containers/profileSettings/components/AddEditBillingAddress.jsx index c889af9..3748e78 100644 --- a/frontend/src/containers/profileSettings/components/AddEditBillingAddress.jsx +++ b/frontend/src/containers/profileSettings/components/AddEditBillingAddress.jsx @@ -42,6 +42,7 @@ class AddEditBillingAddress extends Component { {profileTexts.labels.LAST_NAME} {profileTexts.labels.INVOICE_MAIL} @@ -89,6 +93,7 @@ class AddEditBillingAddress extends Component { {profileTexts.labels.ADDRESS_DETAILS}* {profileTexts.labels.ADDRESS_ZIP}* Date: Thu, 13 Sep 2018 05:50:10 +0200 Subject: [PATCH 24/41] use new list of countries and adapt backend and frontend to it --- .../api/class-wiaas-rest-user-api.php | 4 +- .../wiaas/includes/class-wiaas-countries.php | 21 ++++++++++ .../wiaas/includes/class-wiaas-user.php | 10 ++--- .../user/class-wiaas-user-profile.php | 40 ------------------- 4 files changed, 28 insertions(+), 47 deletions(-) delete mode 100644 backend/app/plugins/wiaas/includes/user/class-wiaas-user-profile.php diff --git a/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-user-api.php b/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-user-api.php index 0ebfb24..1615ad5 100644 --- a/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-user-api.php +++ b/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-user-api.php @@ -9,7 +9,7 @@ class Wiass_REST_User_API { private static $namespace = 'wiaas'; public function __construct() { - include_once dirname( __FILE__ ) . '/user/class-wiaas-user-profile.php'; + include_once dirname( __FILE__ ) . '/../class-wiaas-countries.php'; } @@ -43,7 +43,7 @@ class Wiass_REST_User_API { } public static function get_countries(){ - $countries = Wiaas_User_Profile::get_list_of_countries(); + $countries = Wiaas_Countries::get_list_of_countries(); return new WP_REST_Response($countries); } diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-countries.php b/backend/app/plugins/wiaas/includes/class-wiaas-countries.php index 375a013..f88e161 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-countries.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-countries.php @@ -17,18 +17,21 @@ class Wiaas_Countries { */ private static $available_countries = array( 'Sweden' => array( + 'id' => 1, 'name' => 'Sweden', 'code' => 'se', 'vat' => 9 , 'currency' => 'SEK' ), 'Denmark' => array( + 'id' => 2, 'name' => 'Denmark', 'code' => 'dk', 'vat' => 9 , 'currency' => 'DKK' ), 'Finland' => array( + 'id' => 3, 'name' => 'Finland', 'code' => 'fi', 'vat' => 9 , @@ -41,6 +44,24 @@ class Wiaas_Countries { add_action('woocommerce_after_register_taxonomy', array(__CLASS__, 'register_product_countries_taxonomy')); } + public static function get_list_of_countries(){ + $result = []; + foreach(self::$available_countries as $country){ + array_push($result, array( + 'idCountry' => $country['id'], + 'countryName' => $country['name'] + )); + } + return $result; + } + + public static function get_country_name_by_id($id){ + foreach(self::$available_countries as $country){ + if ($country['id'] == $id) return $country['name']; + } + return ''; + } + /** * Registers product taxonomy for avaiable countries */ diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-user.php b/backend/app/plugins/wiaas/includes/class-wiaas-user.php index fe6efd0..69441de 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-user.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-user.php @@ -8,7 +8,7 @@ defined( 'ABSPATH' ) || exit; class Wiaas_User { public static function init() { - include_once dirname( __FILE__ ) . '/user/class-wiaas-user-profile.php'; + include_once dirname( __FILE__ ) . '/class-wiaas-countries.php'; add_action('init', array(__CLASS__, 'load_user_organization')); add_action('plugins_loaded', array(__CLASS__, 'remove_default_user_groups'), 30); @@ -45,7 +45,7 @@ class Wiaas_User { if ($received_address->id){ $updated = array( 'id' => $received_address->id, - 'countryName' => Wiaas_User_Profile::get_country_name_by_id($received_address->idCountrySelected), + 'countryName' => Wiaas_Countries::get_country_name_by_id($received_address->idCountrySelected), 'deliveryMail' => $received_address->deliveryMail, 'idCountrySelected' => $received_address->idCountrySelected, 'city' => $received_address->city, @@ -64,7 +64,7 @@ class Wiaas_User { }else{ $new_delivery_address = array( 'id' => time(), - 'countryName' => Wiaas_User_Profile::get_country_name_by_id($received_address->idCountrySelected), + 'countryName' => Wiaas_Countries::get_country_name_by_id($received_address->idCountrySelected), 'deliveryMail' => $received_address->deliveryMail, 'idCountrySelected' => $received_address->idCountrySelected, 'city' => $received_address->city, @@ -89,7 +89,7 @@ class Wiaas_User { if ($received_address->id){ $updated = array( 'id' => $received_address->id, - 'countryName' => Wiaas_User_Profile::get_country_name_by_id($received_address->idCountrySelected), + 'countryName' => Wiaas_Countries::get_country_name_by_id($received_address->idCountrySelected), 'deliveryMail' => $received_address->deliveryMail, 'idCountrySelected' => $received_address->idCountrySelected, 'city' => $received_address->city, @@ -108,7 +108,7 @@ class Wiaas_User { }else{ $new_billing_address = array( 'id' => time(), - 'countryName' => Wiaas_User_Profile::get_country_name_by_id($received_address->idCountrySelected), + 'countryName' => Wiaas_Countries::get_country_name_by_id($received_address->idCountrySelected), 'deliveryMail' => $received_address->deliveryMail, 'idCountrySelected' => $received_address->idCountrySelected, 'city' => $received_address->city, diff --git a/backend/app/plugins/wiaas/includes/user/class-wiaas-user-profile.php b/backend/app/plugins/wiaas/includes/user/class-wiaas-user-profile.php deleted file mode 100644 index a13ede6..0000000 --- a/backend/app/plugins/wiaas/includes/user/class-wiaas-user-profile.php +++ /dev/null @@ -1,40 +0,0 @@ - '1', - 'countryName' => 'Sweden' - ), - array( - 'idCountry' => '2', - 'countryName' => 'Finland' - ), - array( - 'idCountry' => '3', - 'countryName' => 'Denmark' - ) - ]; - - public static function get_list_of_countries(){ - return self::LIST_OF_COUNTRIES; - } - - public static function check_country_id($id){ - foreach (self::LIST_OF_COUNTRIES as $country){ - if ($country['idCountry'] === $id) return true; - } - return false; - } - - public static function get_country_name_by_id($id){ - foreach(self::LIST_OF_COUNTRIES as $country){ - if ($country['idCountry'] === $id) return $country['countryName']; - } - return ''; - } -} \ No newline at end of file From 82e4579e88f026fd01911bd8f7a05f2bb38784f6 Mon Sep 17 00:00:00 2001 From: GotPPay Date: Thu, 13 Sep 2018 06:30:09 +0200 Subject: [PATCH 25/41] simple backend address validation --- .../wiaas/includes/class-wiaas-user.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-user.php b/backend/app/plugins/wiaas/includes/class-wiaas-user.php index 69441de..c64a436 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-user.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-user.php @@ -40,6 +40,9 @@ class Wiaas_User { if (isset($request['deliveryAddress'])){ $received_address = json_decode($request['deliveryAddress']); + if (!self::validate_address($received_address)){ + return false; + } $profile_addresses = get_user_meta($customer->ID, 'profile_addresses', true) ?: []; if ($received_address->id){ @@ -84,6 +87,9 @@ class Wiaas_User { if (isset($request['billingAddress'])){ $received_address = json_decode($request['billingAddress']); + if (!self::validate_address($received_address)){ + return false; + } $billing_addresses = get_user_meta($customer->ID, 'billing_addresses', true) ?: []; if ($received_address->id){ @@ -221,6 +227,25 @@ class Wiaas_User { } + /** + * Check if address is valid + * + * @param $address + * + * @return mixed + */ + private static function validate_address($address){ + if (empty($address->city)){ + return false; + } + + if (empty($address->detailedAddress)){ + return false; + } + + return is_numeric($address->zipCode); + } + } From 32379f7f47b39ed75570df4332e6fa64fd168c3e Mon Sep 17 00:00:00 2001 From: GotPPay Date: Thu, 13 Sep 2018 06:30:21 +0200 Subject: [PATCH 26/41] simple frontend address validation --- .../BillingAddressesContainer.jsx | 26 ++++++++++++++++++- .../ProfileAddressesContainer.jsx | 26 ++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/frontend/src/containers/profileSettings/BillingAddressesContainer.jsx b/frontend/src/containers/profileSettings/BillingAddressesContainer.jsx index df0ebf0..ed7081b 100644 --- a/frontend/src/containers/profileSettings/BillingAddressesContainer.jsx +++ b/frontend/src/containers/profileSettings/BillingAddressesContainer.jsx @@ -9,6 +9,7 @@ import {profileTexts} from '../../constants/profileSettingsConstants'; import {setDialogContent, setDialogOpenFlag} from '../../actions/dialog/dialogActions'; import {saveBillingAddress, removeBillingAddress} from '../../actions/profileSettings/addressActions'; import './style/AddressesContainer.css'; +import { updateMessages } from '../../actions/notification/notificationActions'; class BillingAddressesContainer extends Component { constructor(props) { @@ -21,7 +22,30 @@ class BillingAddressesContainer extends Component { } saveBillingAddress(address){ - this.props.dispatch(saveBillingAddress(this.props.idUser, address)); + const messages = []; + if (!address.zipCode){ + messages.push({ + 'code':'error', + 'message':'ADD_ZIP' + }); + } + if (!address.detailedAddress){ + messages.push({ + 'code':'error', + 'message':'ADD_ADDRESS' + }); + } + if (!address.city){ + messages.push({ + 'code':'error', + 'message':'ADD_CITY' + }); + } + if (messages.length > 0){ + this.props.dispatch(updateMessages(messages, profileTexts.messages)); + }else{ + this.props.dispatch(saveBillingAddress(this.props.idUser, address)); + } } onAddressChange(address){ diff --git a/frontend/src/containers/profileSettings/ProfileAddressesContainer.jsx b/frontend/src/containers/profileSettings/ProfileAddressesContainer.jsx index b636cd9..079441d 100644 --- a/frontend/src/containers/profileSettings/ProfileAddressesContainer.jsx +++ b/frontend/src/containers/profileSettings/ProfileAddressesContainer.jsx @@ -9,6 +9,7 @@ import {profileTexts} from '../../constants/profileSettingsConstants'; import {setDialogContent, setDialogOpenFlag} from '../../actions/dialog/dialogActions'; import {saveProfileAddress, removeProfileAddress} from '../../actions/profileSettings/addressActions'; import './style/AddressesContainer.css'; +import { updateMessages } from '../../actions/notification/notificationActions'; class ProfileAddressesContainer extends Component { constructor(props) { @@ -21,7 +22,30 @@ class ProfileAddressesContainer extends Component { } saveProfileAddress(profileAddress){ - this.props.dispatch(saveProfileAddress(this.props.idUser, profileAddress)); + const messages = []; + if (!profileAddress.zipCode){ + messages.push({ + 'code':'error', + 'message':'ADD_ZIP' + }); + } + if (!profileAddress.detailedAddress){ + messages.push({ + 'code':'error', + 'message':'ADD_ADDRESS' + }); + } + if (!profileAddress.city){ + messages.push({ + 'code':'error', + 'message':'ADD_CITY' + }); + } + if (messages.length > 0){ + this.props.dispatch(updateMessages(messages, profileTexts.messages)); + }else{ + this.props.dispatch(saveProfileAddress(this.props.idUser, profileAddress)); + } } onAddressChange(address){ From d36398f2ab993a69cfea3b57fbf341116463af7b Mon Sep 17 00:00:00 2001 From: GotPPay Date: Thu, 13 Sep 2018 06:34:01 +0200 Subject: [PATCH 27/41] remove comment --- backend/app/plugins/wiaas/includes/class-wiaas-user.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-user.php b/backend/app/plugins/wiaas/includes/class-wiaas-user.php index c64a436..b5043d5 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-user.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-user.php @@ -82,7 +82,6 @@ class Wiaas_User { } update_user_meta( $customer->ID, 'profile_addresses', $profile_addresses); - //delete_user_meta ($customer->ID, 'profile_addresses'); } if (isset($request['billingAddress'])){ @@ -129,7 +128,6 @@ class Wiaas_User { } update_user_meta( $customer->ID, 'billing_addresses', $billing_addresses); - //delete_user_meta ($customer->ID, 'profile_addresses'); } if (isset($request['removeDeliveryAddress'])){ From af2da3eed075f0c9a984adc6d75904f4e3174da1 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Wed, 19 Sep 2018 16:07:22 +0200 Subject: [PATCH 28/41] add customer class and endpoint --- .../api/class-wiaas-rest-customer.php | 175 ++++++++++++++ .../wiaas/includes/class-wiaas-api.php | 6 +- .../wiaas/includes/class-wiaas-user.php | 182 +-------------- .../includes/user/class-wiaas-customer.php | 217 ++++++++++++++++++ 4 files changed, 401 insertions(+), 179 deletions(-) create mode 100644 backend/app/plugins/wiaas/includes/api/class-wiaas-rest-customer.php create mode 100644 backend/app/plugins/wiaas/includes/user/class-wiaas-customer.php diff --git a/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-customer.php b/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-customer.php new file mode 100644 index 0000000..af8d977 --- /dev/null +++ b/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-customer.php @@ -0,0 +1,175 @@ +\d+)/profile-addresses', array( + 'methods' => 'PUT', + 'callback' => array(__CLASS__, 'update_customer_profile_addresses'), + 'permission_callback' => 'is_user_logged_in' + ) ); + + register_rest_route( self::$namespace, 'customer/(?P\d+)/profile-addresses', array( + 'methods' => 'POST', + 'callback' => array(__CLASS__, 'delete_customer_profile_address'), + 'permission_callback' => 'is_user_logged_in' + ) ); + + register_rest_route( self::$namespace, 'customer/(?P\d+)/billing-addresses', array( + 'methods' => 'PUT', + 'callback' => array(__CLASS__, 'update_customer_billing_addresses'), + 'permission_callback' => 'is_user_logged_in' + ) ); + + register_rest_route( self::$namespace, 'customer/(?P\d+)/billing-addresses', array( + 'methods' => 'POST', + 'callback' => array(__CLASS__, 'delete_customer_billing_addresses'), + 'permission_callback' => 'is_user_logged_in' + ) ); + + register_rest_route( self::$namespace, 'customer/(?P\d+)/personal-info', array( + 'methods' => 'PUT', + 'callback' => array(__CLASS__, 'update_customer_personal_info'), + 'permission_callback' => 'is_user_logged_in' + ) ); + + register_rest_route( self::$namespace, 'customer/(?P\d+)/company-info', array( + 'methods' => 'PUT', + 'callback' => array(__CLASS__, 'update_customer_company_info'), + 'permission_callback' => 'is_user_logged_in' + ) ); + + } + + + + public static function update_customer_profile_addresses(WP_REST_Request $request){ + $customer_id = $request['id']; + $params = $request->get_body_params(); + $new_address = json_decode($params['profile_address']); + + if (!Wiaas_Customer::update_customer_profile_addresses($customer_id, $new_address)){ + return self::generate_wiaas_response('PROFILE_ADDRESS_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id)); + } + + return self::generate_wiaas_response('PROFILE_ADDRESS_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id)); + } + + public static function delete_customer_profile_address(WP_REST_Request $request){ + $customer_id = $request['id']; + $params = $request->get_body_params(); + $address_id = $params['address_id']; + + if (!Wiaas_Customer::delete_customer_profile_address($customer_id, $address_id)){ + return self::generate_wiaas_response('ADDRESS_ERROR', 'error', Wiaas_Customer::get_customer_info($customer_id)); + } + + return self::generate_wiaas_response('ADDRESS_REMOVED', 'success', Wiaas_Customer::get_customer_info($customer_id)); + } + + public static function update_customer_billing_addresses(WP_REST_Request $request){ + $customer_id = $request['id']; + $params = $request->get_body_params(); + $new_address = json_decode($params['billing_address']); + + if (!Wiaas_Customer::update_customer_billing_addresses($customer_id, $new_address)){ + return self::generate_wiaas_response('BILLING_ADDRESS_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id)); + } + + return self::generate_wiaas_response('BILLING_ADDRESS_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id)); + + } + + public static function delete_customer_billing_addresses(WP_REST_Request $request){ + $customer_id = $request['id']; + $params = $request->get_body_params(); + $address_id = json_decode($params['address_id']); + + if (!Wiaas_Customer::delete_customer_billing_address($customer_id, $address_id)){ + return self::generate_wiaas_response('ADDRESS_ERROR', 'error', Wiaas_Customer::get_customer_info($customer_id)); + } + + return self::generate_wiaas_response('ADDRESS_REMOVED', 'success', Wiaas_Customer::get_customer_info($customer_id)); + } + + public static function update_customer_personal_info(WP_REST_Request $request){ + $customer_id = $request['id']; + $params = $request->get_body_params(); + + $first_name = $params['first_name']; + $last_name = $params['last_name']; + $phone = $params['phone']; + $name = $first_name . ' ' . $last_name; + + if (!is_string($name) || strlen($name) < 1){ + return self::generate_wiaas_response('ADD_NAME', 'error', Wiaas_Customer::get_customer_info($customer_id)); + } + + if (!is_string($phone) || strlen($phone) < 1){ + return self::generate_wiaas_response('ADD_PHONE_NUMBER', 'error', Wiaas_Customer::get_customer_info($customer_id)); + } + + if (!Wiaas_Customer::update_customer_profile_info($customer_id, $first_name, $last_name, $phone)){ + return self::generate_wiaas_response('PROFILE_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id)); + } + + return self::generate_wiaas_response('PROFILE_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id)); + } + + public static function update_customer_company_info(WP_REST_Request $request){ + $customer_id = $request['id']; + $params = $request->get_body_params(); + + $company_name = $params['company_name']; + $vat_code = $params['vat_code']; + + + if (!is_string($company_name) || strlen($company_name) < 1){ + return self::generate_wiaas_response('ADD_COPMANY_NAME', 'error', Wiaas_Customer::get_customer_info($customer_id)); + } + + if (!is_string($vat_code) || strlen($vat_code) < 1){ + return self::generate_wiaas_response('ADD_VAT', 'error', Wiaas_Customer::get_customer_info($customer_id)); + } + + if (!Wiaas_Customer::update_customer_company_info($customer_id, $company_name, $vat_code)){ + return self::generate_wiaas_response('COMPANY_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id)); + } + + return self::generate_wiaas_response('COMPANY_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id)); + + + } + + + + + private static function generate_wiaas_response($message, $code, $data = NULL){ + $response = array( + 'messages' => [ + array( + 'code' => $code, + 'message' => $message + ) + ], + 'data' => $data + ); + + return new WP_REST_Response($response); + } + + +} \ No newline at end of file diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-api.php b/backend/app/plugins/wiaas/includes/class-wiaas-api.php index 5e3089f..574a7bc 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-api.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-api.php @@ -38,6 +38,9 @@ class Wiaas_API { #User controller include_once dirname( __FILE__ ) . '/api/class-wiaas-rest-user-api.php'; + #Customer controller + include_once dirname( __FILE__ ) . '/api/class-wiaas-rest-customer.php'; + } public static function register_rest_routes() { @@ -45,7 +48,8 @@ class Wiaas_API { 'Wiass_REST_Delivery_Process_API', 'Wiaas_Cart_API', 'Wiaas_Document_API', - 'Wiass_REST_User_API' + 'Wiass_REST_User_API', + 'Wiaas_REST_Customer_API' ); foreach ( $controllers as $controller ) { diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-user.php b/backend/app/plugins/wiaas/includes/class-wiaas-user.php index b5043d5..f61fcb1 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-user.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-user.php @@ -9,158 +9,15 @@ class Wiaas_User { public static function init() { include_once dirname( __FILE__ ) . '/class-wiaas-countries.php'; + include_once dirname( __FILE__ ) . '/user/class-wiaas-customer.php'; add_action('init', array(__CLASS__, 'load_user_organization')); add_action('plugins_loaded', array(__CLASS__, 'remove_default_user_groups'), 30); - add_action('woocommerce_rest_insert_customer', array(__CLASS__, 'add_meta_data_to_customer'), 10, 3); - + add_filter('woocommerce_rest_prepare_customer', array(__CLASS__, 'transform_rest_customer'), 10, 3); add_filter('jwt_auth_token_before_dispatch', array(__CLASS__, 'transform_jwt_token_response'), 10, 2); } - /** - * Fires after a customer is created or updated via the REST API. - * - * @param WP_User $customer Data used to create the customer. - * @param WP_REST_Request $request Request object. - * @param boolean $creating True when creating customer, false when updating customer. - */ - public static function add_meta_data_to_customer($customer, $request, $creating){ - if (isset($request['phone'])){ - update_user_meta( $customer->ID, 'phone', $request['phone'] ); - } - - if (isset($request['vatCode'])){ - update_user_meta( $customer->ID, 'vat_code', $request['vatCode']); - } - - if (isset($request['companyName'])){ - update_user_meta( $customer->ID, 'company_name', $request['companyName']); - } - - if (isset($request['deliveryAddress'])){ - $received_address = json_decode($request['deliveryAddress']); - if (!self::validate_address($received_address)){ - return false; - } - $profile_addresses = get_user_meta($customer->ID, 'profile_addresses', true) ?: []; - - if ($received_address->id){ - $updated = array( - 'id' => $received_address->id, - 'countryName' => Wiaas_Countries::get_country_name_by_id($received_address->idCountrySelected), - 'deliveryMail' => $received_address->deliveryMail, - 'idCountrySelected' => $received_address->idCountrySelected, - 'city' => $received_address->city, - 'detailedAddress' => $received_address->detailedAddress, - 'zipCode' => $received_address->zipCode, - 'firstName' => $received_address->firstName, - 'lastName' => $received_address->lastName, - 'deliveryMail' => $received_address->deliveryMail - ); - foreach($profile_addresses as $key => $address){ - if ($address['id'] === $received_address->id){ - $profile_addresses[$key] = $updated; - break; - } - } - }else{ - $new_delivery_address = array( - 'id' => time(), - 'countryName' => Wiaas_Countries::get_country_name_by_id($received_address->idCountrySelected), - 'deliveryMail' => $received_address->deliveryMail, - 'idCountrySelected' => $received_address->idCountrySelected, - 'city' => $received_address->city, - 'detailedAddress' => $received_address->detailedAddress, - 'zipCode' => $received_address->zipCode, - 'firstName' => $received_address->firstName, - 'lastName' => $received_address->lastName, - 'deliveryMail' => $received_address->deliveryMail - ); - - array_push($profile_addresses, $new_delivery_address); - } - - update_user_meta( $customer->ID, 'profile_addresses', $profile_addresses); - } - - if (isset($request['billingAddress'])){ - $received_address = json_decode($request['billingAddress']); - if (!self::validate_address($received_address)){ - return false; - } - $billing_addresses = get_user_meta($customer->ID, 'billing_addresses', true) ?: []; - - if ($received_address->id){ - $updated = array( - 'id' => $received_address->id, - 'countryName' => Wiaas_Countries::get_country_name_by_id($received_address->idCountrySelected), - 'deliveryMail' => $received_address->deliveryMail, - 'idCountrySelected' => $received_address->idCountrySelected, - 'city' => $received_address->city, - 'detailedAddress' => $received_address->detailedAddress, - 'zipCode' => $received_address->zipCode, - 'firstName' => $received_address->firstName, - 'lastName' => $received_address->lastName, - 'invoiceMail' => $received_address->invoiceMail - ); - foreach($billing_addresses as $key => $address){ - if ($address['id'] === $received_address->id){ - $billing_addresses[$key] = $updated; - break; - } - } - }else{ - $new_billing_address = array( - 'id' => time(), - 'countryName' => Wiaas_Countries::get_country_name_by_id($received_address->idCountrySelected), - 'deliveryMail' => $received_address->deliveryMail, - 'idCountrySelected' => $received_address->idCountrySelected, - 'city' => $received_address->city, - 'detailedAddress' => $received_address->detailedAddress, - 'zipCode' => $received_address->zipCode, - 'firstName' => $received_address->firstName, - 'lastName' => $received_address->lastName, - 'invoiceMail' => $received_address->invoiceMail - ); - - array_push($billing_addresses, $new_billing_address); - } - - update_user_meta( $customer->ID, 'billing_addresses', $billing_addresses); - } - - if (isset($request['removeDeliveryAddress'])){ - $addressID = $request['removeDeliveryAddress']; - $profile_addresses = get_user_meta($customer->ID, 'profile_addresses', true) ?: []; - $counter = 0; - foreach($profile_addresses as $key => $address){ - if ($address['id'] == $addressID){ - array_splice($profile_addresses, $counter, 1); - break; - } - $counter++; - } - update_user_meta( $customer->ID, 'profile_addresses', $profile_addresses); - } - - if (isset($request['removeBillingAddress'])){ - $addressID = $request['removeBillingAddress']; - $billing_addresses = get_user_meta($customer->ID, 'billing_addresses', true) ?: []; - $counter = 0; - foreach($billing_addresses as $key => $address){ - if ($address['id'] == $addressID){ - array_splice($billing_addresses, $counter, 1); - break; - } - $counter++; - } - - update_user_meta( $customer->ID, 'billing_addresses', $billing_addresses); - } - - } - public static function load_user_organization() { if (class_exists('WP_User_Taxonomy')) { require_once dirname( __FILE__ ) . '/user/class-wiaas-user-organization.php'; @@ -186,21 +43,9 @@ class Wiaas_User { public static function transform_rest_customer($response, $order, $request) { $data = $response->get_data(); $user_id = $data['id']; - $result = array( - 'id' => $user_id, - 'idCompany' => 0, - 'isCompanyAdmin' => 1, - 'mail' => $data['email'], - 'name' => $data['first_name'] . ' ' . $data['last_name'], - 'phone' => get_user_meta($user_id, 'phone', true), - 'companyName' => get_user_meta($user_id, 'company_name', true), - 'vatCode' => get_user_meta($user_id, 'vat_code', true), - 'billingAddresses' => get_user_meta($user_id, 'billing_addresses', true) ?: [], - 'profileAddresses' => get_user_meta($user_id, 'profile_addresses', true) ?: [], - 'userType' => $data['role'] - ); + $customer_info = Wiaas_Customer::get_customer_info($user_id); - return new WP_REST_Response($result); + return new WP_REST_Response($customer_info); } /** @@ -225,25 +70,6 @@ class Wiaas_User { } - /** - * Check if address is valid - * - * @param $address - * - * @return mixed - */ - private static function validate_address($address){ - if (empty($address->city)){ - return false; - } - - if (empty($address->detailedAddress)){ - return false; - } - - return is_numeric($address->zipCode); - } - } diff --git a/backend/app/plugins/wiaas/includes/user/class-wiaas-customer.php b/backend/app/plugins/wiaas/includes/user/class-wiaas-customer.php new file mode 100644 index 0000000..5e78abf --- /dev/null +++ b/backend/app/plugins/wiaas/includes/user/class-wiaas-customer.php @@ -0,0 +1,217 @@ + $customer_id, + 'company_id' => self::get_customer_company_id($customer_id), + 'is_company_admin' => self::get_customer_company_admin_status($customer_id), + 'mail' => $user->user_email, + 'name' => $user->first_name . ' ' . $user->last_name, + 'phone' => self::get_customer_phone_number($customer_id), + 'company_name' => self::get_customer_company_name($customer_id), + 'vat_code' => self::get_customer_vat_code($customer_id), + 'billing_addresses' => self::get_customer_billing_addresses($customer_id), + 'profile_addresses' => self::get_customer_profile_addresses($customer_id), + 'user_type' => $user->roles + ); + + return $result; + } + + public static function get_customer_profile_addresses($customer_id){ + return get_user_meta($customer_id, 'profile_addresses', true) ?: []; + } + + public static function get_customer_billing_addresses($customer_id){ + return get_user_meta($customer_id, 'billing_addresses', true) ?: []; + } + + public static function get_customer_vat_code($customer_id){ + return get_user_meta($customer_id, 'vat_code', true) ?: ''; + } + + public static function get_customer_company_name($customer_id){ + return get_user_meta($customer_id, 'company_name', true) ?: ''; + } + + public static function get_customer_company_id($customer_id){ + return 0; //TODO: don't hardocde this + } + + public static function get_customer_company_admin_status($customer_id){ + return 1; //TODO: don't hardcode this + } + + public static function get_customer_phone_number($customer_id){ + return get_user_meta($customer_id, 'phone', true) ?: ''; + } + + public static function update_customer_profile_info($customer_id, $first_name, $last_name, $phone){ + $user = array( + 'ID' => $customer_id, + 'first_name' => $first_name, + 'last_name' => $last_name + ); + + if (is_wp_error(wp_update_user($user))){ + return false; + } + update_user_meta( $customer_id, 'phone', $phone); + + return true; + } + + public static function update_customer_company_info($customer_id, $company_name, $vat_code){ + $result = update_user_meta( $customer_id, 'company_name', $company_name); + return $result || update_user_meta( $customer_id, 'vat_code', $vat_code ); + } + + public static function update_customer_billing_addresses($customer_id, $new_address){ + if (!self::validate_address($new_address)){ + return false; + } + $billing_addresses = self::get_customer_billing_addresses($customer_id); + + if ($new_address->id){ + $updated = array( + 'id' => $new_address->id, + 'countryName' => Wiaas_Countries::get_country_name_by_id($new_address->idCountrySelected), + 'deliveryMail' => $new_address->deliveryMail, + 'idCountrySelected' => $new_address->idCountrySelected, + 'city' => $new_address->city, + 'detailedAddress' => $new_address->detailedAddress, + 'zipCode' => $new_address->zipCode, + 'firstName' => $new_address->firstName, + 'lastName' => $new_address->lastName, + 'invoiceMail' => $new_address->invoiceMail + ); + foreach($billing_addresses as $key => $address){ + if ($address['id'] === $new_address->id){ + $billing_addresses[$key] = $updated; + break; + } + } + }else{ + $new_billing_address = array( + 'id' => time(), + 'countryName' => Wiaas_Countries::get_country_name_by_id($new_address->idCountrySelected), + 'deliveryMail' => $new_address->deliveryMail, + 'idCountrySelected' => $new_address->idCountrySelected, + 'city' => $new_address->city, + 'detailedAddress' => $new_address->detailedAddress, + 'zipCode' => $new_address->zipCode, + 'firstName' => $new_address->firstName, + 'lastName' => $new_address->lastName, + 'invoiceMail' => $new_address->invoiceMail + ); + + array_push($billing_addresses, $new_billing_address); + } + + return update_user_meta( $customer_id, 'billing_addresses', $billing_addresses); + } + + public static function update_customer_profile_addresses($customer_id, $new_address){ + if (!self::validate_address($new_address)){ + return false; + } + $profile_addresses = self::get_customer_profile_addresses($customer_id); + + if ($new_address->id){ + $updated = array( + 'id' => $new_address->id, + 'countryName' => Wiaas_Countries::get_country_name_by_id($new_address->idCountrySelected), + 'deliveryMail' => $new_address->deliveryMail, + 'idCountrySelected' => $new_address->idCountrySelected, + 'city' => $new_address->city, + 'detailedAddress' => $new_address->detailedAddress, + 'zipCode' => $new_address->zipCode, + 'firstName' => $new_address->firstName, + 'lastName' => $new_address->lastName, + 'deliveryMail' => $new_address->deliveryMail + ); + foreach($profile_addresses as $key => $address){ + if ($address['id'] === $new_address->id){ + $profile_addresses[$key] = $updated; + break; + } + } + }else{ + $new_delivery_address = array( + 'id' => time(), + 'countryName' => Wiaas_Countries::get_country_name_by_id($new_address->idCountrySelected), + 'deliveryMail' => $new_address->deliveryMail, + 'idCountrySelected' => $new_address->idCountrySelected, + 'city' => $new_address->city, + 'detailedAddress' => $new_address->detailedAddress, + 'zipCode' => $new_address->zipCode, + 'firstName' => $new_address->firstName, + 'lastName' => $new_address->lastName, + 'deliveryMail' => $new_address->deliveryMail + ); + + array_push($profile_addresses, $new_delivery_address); + } + + return update_user_meta( $customer_id, 'profile_addresses', $profile_addresses); + } + + public static function delete_customer_profile_address($customer_id, $address_id){ + $profile_addresses = self::get_customer_profile_addresses($customer_id); + $counter = 0; + foreach($profile_addresses as $key => $address){ + if ($address['id'] == $address_id){ + array_splice($profile_addresses, $counter, 1); + break; + } + $counter++; + } + return update_user_meta( $customer_id, 'profile_addresses', $profile_addresses); + } + + public static function delete_customer_billing_address($customer_id, $address_id){ + $billing_addresses = self::get_customer_billing_addresses($customer_id); + $counter = 0; + foreach($billing_addresses as $key => $address){ + if ($address['id'] == $address_id){ + array_splice($billing_addresses, $counter, 1); + break; + } + $counter++; + } + return update_user_meta( $customer_id, 'billing_addresses', $billing_addresses); + } + + /** + * Check if address is valid + * + * @param $address + * + * @return bool + */ + private static function validate_address($address){ + if (empty($address->city)){ + return false; + } + + if (empty($address->detailedAddress)){ + return false; + } + + return is_numeric($address->zipCode); + } + + +} \ No newline at end of file From 31115b7c6adc3c482d8fdb1f933ac7b68d1a3cc5 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Wed, 19 Sep 2018 16:07:41 +0200 Subject: [PATCH 29/41] add helper to transform snake case to camel case --- frontend/src/helpers/CountryHelper.js | 6 ++++++ frontend/src/helpers/ProfileHelper.js | 15 +++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 frontend/src/helpers/CountryHelper.js create mode 100644 frontend/src/helpers/ProfileHelper.js diff --git a/frontend/src/helpers/CountryHelper.js b/frontend/src/helpers/CountryHelper.js new file mode 100644 index 0000000..e1c2ca6 --- /dev/null +++ b/frontend/src/helpers/CountryHelper.js @@ -0,0 +1,6 @@ +export const fromWiaasCountryList = (countryList) => { + return { + idCountry: countryList.country_id, + countryName: countryList.country_name + } +} \ No newline at end of file diff --git a/frontend/src/helpers/ProfileHelper.js b/frontend/src/helpers/ProfileHelper.js new file mode 100644 index 0000000..b03bf2b --- /dev/null +++ b/frontend/src/helpers/ProfileHelper.js @@ -0,0 +1,15 @@ +export const fromWiaasProfileInfo = (profileInfo) => { + return { + id: profileInfo.id, + idCompany: profileInfo.company_id, + companyName: profileInfo.company_name, + vatCode: profileInfo.vat_code, + isCompanyAdmin: profileInfo.is_company_admin, + mail: profileInfo.mail, + name: profileInfo.name, + phone: profileInfo.phone, + billingAddresses: profileInfo.billing_addresses, + profileAddresses: profileInfo.profile_addresses, + userType: profileInfo.user_type, + } +}; \ No newline at end of file From 498f5526a9c16467eb6b953f3fe54125b703f044 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Wed, 19 Sep 2018 16:08:33 +0200 Subject: [PATCH 30/41] use customer endpoint for profile actions --- .../actions/profileSettings/addressActions.js | 86 ++++++------------- .../profileSettings/profileSettingsActions.js | 44 +++------- 2 files changed, 41 insertions(+), 89 deletions(-) diff --git a/frontend/src/actions/profileSettings/addressActions.js b/frontend/src/actions/profileSettings/addressActions.js index 8b11aff..39558d3 100644 --- a/frontend/src/actions/profileSettings/addressActions.js +++ b/frontend/src/actions/profileSettings/addressActions.js @@ -12,6 +12,7 @@ import { import {updateMessages} from '../notification/notificationActions'; import {setDialogOpenFlag} from '../dialog/dialogActions'; import {recieveProfileInfo} from './profileSettingsActions'; +import { fromWiaasProfileInfo } from '../../helpers/ProfileHelper'; const client = new HtmlClient(); @@ -23,26 +24,18 @@ export const saveProfileAddress = (idUser, profileAddress) => { return dispatch => { dispatch(requestSaveAddress()); return client.fetch({ - url: `${API_SERVER}/wp-json/wc/v2/customers/${idUser}`, + url: `${API_SERVER}/wp-json/wiaas/customer/${idUser}/profile-addresses`, method: 'put', - data: {deliveryAddress: JSON.stringify(profileAddress)} + data: { + 'profile_address': JSON.stringify(profileAddress) + } }) .then(response => { - let messages = []; if(response.data){ - dispatch(recieveProfileInfo(response.data)); + dispatch(recieveProfileInfo(fromWiaasProfileInfo(response.data.data))); + dispatch(updateMessages(response.data.messages, profileTexts.messages)); dispatch(setDialogOpenFlag(false)); - messages.push({ - code:'success', - message:'PROFILE_ADDRESS_UPDATED' - }); - }else{ - messages.push({ - code:'error', - message:'INTERNAL_SERVER_ERROR' - }); } - dispatch(updateMessages(messages, profileTexts.messages)); }) .catch(error => { client.onError(error, dispatch); @@ -58,25 +51,17 @@ export const removeProfileAddress = (idUser, idProfileAddress) => { return dispatch => { dispatch(requestRemoveAddress()); return client.fetch({ - url: `${API_SERVER}/wp-json/wc/v2/customers/${idUser}`, - method: 'put', - data: {removeDeliveryAddress:idProfileAddress} + url: `${API_SERVER}/wp-json/wiaas/customer/${idUser}/profile-addresses`, + method: 'post', + data: { + 'address_id':idProfileAddress + } }) .then(response => { - let messages = []; if(response.data){ - dispatch(recieveProfileInfo(response.data)); - messages.push({ - code:'success', - message:'ADDRESS_REMOVED' - }); - }else{ - messages.push({ - code:'error', - message:'INTERNAL_SERVER_ERROR' - }); + dispatch(recieveProfileInfo(fromWiaasProfileInfo(response.data.data))); + dispatch(updateMessages(response.data.messages, profileTexts.messages)); } - dispatch(updateMessages(messages, profileTexts.messages)); }) .catch(error => { client.onError(error, dispatch); @@ -92,26 +77,18 @@ export const saveBillingAddress = (idUser, billingAddress) => { return dispatch => { dispatch(requestSaveBillingAddress()); return client.fetch({ - url: `${API_SERVER}/wp-json/wc/v2/customers/${idUser}`, + url: `${API_SERVER}/wp-json/wiaas/customer/${idUser}/billing-addresses`, method: 'put', - data: {billingAddress: JSON.stringify(billingAddress)} + data: { + 'billing_address': JSON.stringify(billingAddress) + } }) .then(response => { - let messages = []; if(response.data){ - dispatch(recieveProfileInfo(response.data)); + dispatch(recieveProfileInfo(fromWiaasProfileInfo(response.data.data))); + dispatch(updateMessages(response.data.messages, profileTexts.messages)); dispatch(setDialogOpenFlag(false)); - messages.push({ - code:'success', - message:'BILLING_ADDRESS_UPDATED' - }); - }else{ - messages.push({ - code:'error', - message:'INTERNAL_SERVER_ERROR' - }); } - dispatch(updateMessages(messages, profileTexts.messages)); }) .catch(error => { client.onError(error, dispatch); @@ -127,25 +104,18 @@ export const removeBillingAddress = (idUser, idBillingAddress) => { return dispatch => { dispatch(requestRemoveBillingAddress()); return client.fetch({ - url: `${API_SERVER}/wp-json/wc/v2/customers/${idUser}`, - method: 'put', - data: {removeBillingAddress:idBillingAddress} + url: `${API_SERVER}/wp-json/wiaas/customer/${idUser}/billing-addresses`, + method: 'POST', + data: { + 'address_id':idBillingAddress + } }) .then(response => { - let messages = []; if(response.data){ - dispatch(recieveProfileInfo(response.data)); - messages.push({ - code:'success', - message:'BILLING_ADDRESS_REMOVED' - }); - }else{ - messages.push({ - code:'error', - message:'INTERNAL_SERVER_ERROR' - }); + dispatch(recieveProfileInfo(fromWiaasProfileInfo(response.data.data))); + dispatch(updateMessages(response.data.messages, profileTexts.messages)); + dispatch(setDialogOpenFlag(false)); } - dispatch(updateMessages(messages, profileTexts.messages)); }) .catch(error => { client.onError(error, dispatch); diff --git a/frontend/src/actions/profileSettings/profileSettingsActions.js b/frontend/src/actions/profileSettings/profileSettingsActions.js index 2f7ad65..5d8a216 100644 --- a/frontend/src/actions/profileSettings/profileSettingsActions.js +++ b/frontend/src/actions/profileSettings/profileSettingsActions.js @@ -12,6 +12,8 @@ import { profileTexts } from '../../constants/profileSettingsConstants'; import {updateMessages} from '../notification/notificationActions'; +import { fromWiaasProfileInfo} from '../../helpers/ProfileHelper'; +import {fromWiaasCountryList} from '../../helpers/CountryHelper'; const client = new HtmlClient(); @@ -35,7 +37,7 @@ export const fetchProfileInfo = (idUser) => { }) .then(response => { if(response.data){ - dispatch(recieveProfileInfo(response.data)); + dispatch(recieveProfileInfo(fromWiaasProfileInfo(response.data))); } }); } @@ -50,8 +52,8 @@ export const saveProfileInfo = (idUser, profile) => { dispatch(requestSaveProfile()); const parsedFullName = profile.name.trim().split(' '); return client.fetch({ - url: `${API_SERVER}/wp-json/wc/v2/customers/${idUser}`, - method: 'put', + url: `${API_SERVER}/wp-json/wiaas/customer/${idUser}/personal-info`, + method: 'PUT', data: { 'first_name': parsedFullName[0], 'last_name': parsedFullName[1], @@ -59,20 +61,10 @@ export const saveProfileInfo = (idUser, profile) => { } }) .then(response => { - let messages = []; if(response.data){ - dispatch(recieveProfileInfo(response.data)); - messages.push({ - code:'success', - message:'PROFILE_UPDATED' - }); - }else{ - messages.push({ - code:'error', - message:'INTERNAL_SERVER_ERROR' - }); + dispatch(recieveProfileInfo(fromWiaasProfileInfo(response.data.data))); + dispatch(updateMessages(response.data.messages, profileTexts.messages)); } - dispatch(updateMessages(messages, profileTexts.messages)); }) .catch(error => { client.onError(error, dispatch); @@ -88,28 +80,18 @@ export const saveCompanyInfo = (idUser, companyInfo) => { return dispatch => { dispatch(requestSaveCompany()); return client.fetch({ - url: `${API_SERVER}/wp-json/wc/v2/customers/${idUser}`, + url: `${API_SERVER}/wp-json/wiaas/customer/${idUser}/company-info`, method: 'put', data: { - vatCode: companyInfo.vatCode, - companyName: companyInfo.companyName + 'vat_code': companyInfo.vatCode, + 'company_name': companyInfo.companyName } }) .then(response => { - let messages = []; if(response.data){ - dispatch(recieveProfileInfo(response.data)); - messages.push({ - code:'success', - message:'COMPANY_UPDATED' - }); - }else{ - messages.push({ - code:'error', - message:'INTERNAL_SERVER_ERROR' - }); + dispatch(recieveProfileInfo(fromWiaasProfileInfo(response.data.data))); + dispatch(updateMessages(response.data.messages, profileTexts.messages)); } - dispatch(updateMessages(messages, profileTexts.messages)); }) .catch(error => { client.onError(error, dispatch); @@ -137,7 +119,7 @@ export const fetchCountries = () => { }) .then(response => { if(response.data){ - dispatch(recieveCountries(response.data)); + dispatch(recieveCountries(response.data.map(country => fromWiaasCountryList(country)))); } }) .catch(error => { From 522417ca2862285af31999fac76dd4cf3f712c63 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Wed, 19 Sep 2018 16:09:17 +0200 Subject: [PATCH 31/41] use snake case --- backend/app/plugins/wiaas/includes/class-wiaas-countries.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-countries.php b/backend/app/plugins/wiaas/includes/class-wiaas-countries.php index f88e161..368f4c7 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-countries.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-countries.php @@ -48,8 +48,8 @@ class Wiaas_Countries { $result = []; foreach(self::$available_countries as $country){ array_push($result, array( - 'idCountry' => $country['id'], - 'countryName' => $country['name'] + 'country_id' => $country['id'], + 'country_name' => $country['name'] )); } return $result; From 0787cf834f0c6010a23302b5da598edd1940b386 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Wed, 19 Sep 2018 17:37:03 +0200 Subject: [PATCH 32/41] use snake case --- .../includes/user/class-wiaas-customer.php | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/backend/app/plugins/wiaas/includes/user/class-wiaas-customer.php b/backend/app/plugins/wiaas/includes/user/class-wiaas-customer.php index 5e78abf..b0426a4 100644 --- a/backend/app/plugins/wiaas/includes/user/class-wiaas-customer.php +++ b/backend/app/plugins/wiaas/includes/user/class-wiaas-customer.php @@ -87,15 +87,15 @@ class Wiaas_Customer { if ($new_address->id){ $updated = array( 'id' => $new_address->id, - 'countryName' => Wiaas_Countries::get_country_name_by_id($new_address->idCountrySelected), - 'deliveryMail' => $new_address->deliveryMail, - 'idCountrySelected' => $new_address->idCountrySelected, + 'country_name' => Wiaas_Countries::get_country_name_by_id($new_address->id_country_selected), + 'delivery_mail' => $new_address->delivery_mail, + 'id_country_selected' => $new_address->id_country_selected, 'city' => $new_address->city, - 'detailedAddress' => $new_address->detailedAddress, - 'zipCode' => $new_address->zipCode, - 'firstName' => $new_address->firstName, - 'lastName' => $new_address->lastName, - 'invoiceMail' => $new_address->invoiceMail + 'detailed_address' => $new_address->detailed_address, + 'zip_code' => $new_address->zip_code, + 'first_name' => $new_address->first_name, + 'last_name' => $new_address->last_name, + 'invoice_mail' => $new_address->invoice_mail ); foreach($billing_addresses as $key => $address){ if ($address['id'] === $new_address->id){ @@ -106,15 +106,15 @@ class Wiaas_Customer { }else{ $new_billing_address = array( 'id' => time(), - 'countryName' => Wiaas_Countries::get_country_name_by_id($new_address->idCountrySelected), - 'deliveryMail' => $new_address->deliveryMail, - 'idCountrySelected' => $new_address->idCountrySelected, + 'country_name' => Wiaas_Countries::get_country_name_by_id($new_address->id_country_selected), + 'delivery_mail' => $new_address->delivery_mail, + 'id_country_selected' => $new_address->id_country_selected, 'city' => $new_address->city, - 'detailedAddress' => $new_address->detailedAddress, - 'zipCode' => $new_address->zipCode, - 'firstName' => $new_address->firstName, - 'lastName' => $new_address->lastName, - 'invoiceMail' => $new_address->invoiceMail + 'detailed_address' => $new_address->detailed_address, + 'zip_code' => $new_address->zip_code, + 'first_name' => $new_address->first_name, + 'last_name' => $new_address->last_name, + 'invoice_mail' => $new_address->invoice_mail ); array_push($billing_addresses, $new_billing_address); @@ -132,15 +132,15 @@ class Wiaas_Customer { if ($new_address->id){ $updated = array( 'id' => $new_address->id, - 'countryName' => Wiaas_Countries::get_country_name_by_id($new_address->idCountrySelected), - 'deliveryMail' => $new_address->deliveryMail, - 'idCountrySelected' => $new_address->idCountrySelected, + 'country_name' => Wiaas_Countries::get_country_name_by_id($new_address->id_country_selected), + 'delivery_mail' => $new_address->delivery_mail, + 'id_country_selected' => $new_address->id_country_selected, 'city' => $new_address->city, - 'detailedAddress' => $new_address->detailedAddress, - 'zipCode' => $new_address->zipCode, - 'firstName' => $new_address->firstName, - 'lastName' => $new_address->lastName, - 'deliveryMail' => $new_address->deliveryMail + 'detailed_address' => $new_address->detailed_address, + 'zip_code' => $new_address->zip_code, + 'first_name' => $new_address->first_name, + 'last_name' => $new_address->last_name, + 'invoice_mail' => $new_address->invoice_mail ); foreach($profile_addresses as $key => $address){ if ($address['id'] === $new_address->id){ @@ -151,15 +151,15 @@ class Wiaas_Customer { }else{ $new_delivery_address = array( 'id' => time(), - 'countryName' => Wiaas_Countries::get_country_name_by_id($new_address->idCountrySelected), - 'deliveryMail' => $new_address->deliveryMail, - 'idCountrySelected' => $new_address->idCountrySelected, + 'country_name' => Wiaas_Countries::get_country_name_by_id($new_address->id_country_selected), + 'delivery_mail' => $new_address->delivery_mail, + 'id_country_selected' => $new_address->id_country_selected, 'city' => $new_address->city, - 'detailedAddress' => $new_address->detailedAddress, - 'zipCode' => $new_address->zipCode, - 'firstName' => $new_address->firstName, - 'lastName' => $new_address->lastName, - 'deliveryMail' => $new_address->deliveryMail + 'detailed_address' => $new_address->detailed_address, + 'zip_code' => $new_address->zip_code, + 'first_name' => $new_address->first_name, + 'last_name' => $new_address->last_name, + 'invoice_mail' => $new_address->invoice_mail ); array_push($profile_addresses, $new_delivery_address); @@ -206,11 +206,11 @@ class Wiaas_Customer { return false; } - if (empty($address->detailedAddress)){ + if (empty($address->detailed_address)){ return false; } - return is_numeric($address->zipCode); + return is_numeric($address->zip_code); } From 393158808a51d6cd23b4718ac14887d2a3e10646 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Wed, 19 Sep 2018 17:42:03 +0200 Subject: [PATCH 33/41] snake case - camel case transformations --- .../actions/profileSettings/addressActions.js | 5 ++-- frontend/src/helpers/AddressHelper.js | 29 +++++++++++++++++++ frontend/src/helpers/ProfileHelper.js | 6 ++-- 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 frontend/src/helpers/AddressHelper.js diff --git a/frontend/src/actions/profileSettings/addressActions.js b/frontend/src/actions/profileSettings/addressActions.js index 39558d3..83cd000 100644 --- a/frontend/src/actions/profileSettings/addressActions.js +++ b/frontend/src/actions/profileSettings/addressActions.js @@ -13,6 +13,7 @@ import {updateMessages} from '../notification/notificationActions'; import {setDialogOpenFlag} from '../dialog/dialogActions'; import {recieveProfileInfo} from './profileSettingsActions'; import { fromWiaasProfileInfo } from '../../helpers/ProfileHelper'; +import { toWiaasAddress } from '../../helpers/AddressHelper'; const client = new HtmlClient(); @@ -27,7 +28,7 @@ export const saveProfileAddress = (idUser, profileAddress) => { url: `${API_SERVER}/wp-json/wiaas/customer/${idUser}/profile-addresses`, method: 'put', data: { - 'profile_address': JSON.stringify(profileAddress) + 'profile_address': JSON.stringify(toWiaasAddress(profileAddress)) } }) .then(response => { @@ -80,7 +81,7 @@ export const saveBillingAddress = (idUser, billingAddress) => { url: `${API_SERVER}/wp-json/wiaas/customer/${idUser}/billing-addresses`, method: 'put', data: { - 'billing_address': JSON.stringify(billingAddress) + 'billing_address': JSON.stringify(toWiaasAddress(billingAddress)) } }) .then(response => { diff --git a/frontend/src/helpers/AddressHelper.js b/frontend/src/helpers/AddressHelper.js new file mode 100644 index 0000000..abd9fac --- /dev/null +++ b/frontend/src/helpers/AddressHelper.js @@ -0,0 +1,29 @@ +export const fromWiaasAddress = (address) => { + return { + id: address.id, + countryName: address.country_name, + deliveryMail: address.delivery_mail, + idCountrySelected: address.id_country_selected, + city: address.city, + detailedAddress: address.detailed_address, + zipCode: address.zip_code, + firstName: address.first_name, + lastName: address.lastName, + invoiceMail: address.invoice_mail + } +}; + +export const toWiaasAddress = (address) => { + return { + id: address.id, + country_name: address.countryName, + delivery_mail: address.deliveryMail, + id_country_selected: address.idCountrySelected, + city: address.city, + detailed_address: address.detailedAddress, + zip_code: address.zipCode, + first_name: address.firstName, + last_name: address.lastName, + invoice_mail: address.invoiceMail + } +}; \ No newline at end of file diff --git a/frontend/src/helpers/ProfileHelper.js b/frontend/src/helpers/ProfileHelper.js index b03bf2b..17d3d93 100644 --- a/frontend/src/helpers/ProfileHelper.js +++ b/frontend/src/helpers/ProfileHelper.js @@ -1,3 +1,5 @@ +import { fromWiaasAddress } from "./AddressHelper"; + export const fromWiaasProfileInfo = (profileInfo) => { return { id: profileInfo.id, @@ -8,8 +10,8 @@ export const fromWiaasProfileInfo = (profileInfo) => { mail: profileInfo.mail, name: profileInfo.name, phone: profileInfo.phone, - billingAddresses: profileInfo.billing_addresses, - profileAddresses: profileInfo.profile_addresses, + billingAddresses: profileInfo.billing_addresses.map(address => fromWiaasAddress(address)), + profileAddresses: profileInfo.profile_addresses.map(address => fromWiaasAddress(address)), userType: profileInfo.user_type, } }; \ No newline at end of file From fa2b691996bd142352d9cf6557e2556894f54189 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Wed, 19 Sep 2018 19:19:59 +0200 Subject: [PATCH 34/41] apply snake case change --- .../wiaas/tests/unit-tests/test-wiaas-rest-user-api.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-user-api.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-user-api.php index 80b7d85..6f8176a 100644 --- a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-user-api.php +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-user-api.php @@ -102,7 +102,7 @@ class Wiass_REST_User_Api_Test extends Wiaas_Unit_Test_Case { $country = $data[0]; $this->assertNotNull($country); $this->assertTrue(is_array($country)); - $this->assertArrayHasKey('idCountry', $country); - $this->assertArrayHasKey('countryName', $country); + $this->assertArrayHasKey('country_id', $country); + $this->assertArrayHasKey('country_name', $country); } } From c637880245f96f1d45bd19bc183846054e07027b Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Wed, 19 Sep 2018 22:27:41 +0200 Subject: [PATCH 35/41] fix personal info validation --- .../plugins/wiaas/includes/api/class-wiaas-rest-customer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-customer.php b/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-customer.php index af8d977..48d7ddb 100644 --- a/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-customer.php +++ b/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-customer.php @@ -114,7 +114,7 @@ class Wiaas_REST_Customer_API { $phone = $params['phone']; $name = $first_name . ' ' . $last_name; - if (!is_string($name) || strlen($name) < 1){ + if (!is_string($name) || strlen($name) === 1){ return self::generate_wiaas_response('ADD_NAME', 'error', Wiaas_Customer::get_customer_info($customer_id)); } From 3fa75daafd786f6293b82ad5b77d5c956ced7484 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Wed, 19 Sep 2018 22:36:27 +0200 Subject: [PATCH 36/41] fix typo --- .../plugins/wiaas/includes/api/class-wiaas-rest-customer.php | 2 +- frontend/src/constants/profileSettingsConstants.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-customer.php b/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-customer.php index 48d7ddb..87533b3 100644 --- a/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-customer.php +++ b/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-customer.php @@ -138,7 +138,7 @@ class Wiaas_REST_Customer_API { if (!is_string($company_name) || strlen($company_name) < 1){ - return self::generate_wiaas_response('ADD_COPMANY_NAME', 'error', Wiaas_Customer::get_customer_info($customer_id)); + return self::generate_wiaas_response('ADD_COMPANY_NAME', 'error', Wiaas_Customer::get_customer_info($customer_id)); } if (!is_string($vat_code) || strlen($vat_code) < 1){ diff --git a/frontend/src/constants/profileSettingsConstants.js b/frontend/src/constants/profileSettingsConstants.js index b6c6dba..8477a1e 100644 --- a/frontend/src/constants/profileSettingsConstants.js +++ b/frontend/src/constants/profileSettingsConstants.js @@ -57,7 +57,7 @@ export const profileTexts = { PROFILE_NOT_CHANGED: 'No changes!', NOT_COMPANY_ADMIN: 'You are not allowed to change the company information! Contact your company admin user.', ADD_VAT: 'The vat code can not be empty!', - ADD_COPMANY_NAME: 'The company name can not be empty!', + ADD_COMPANY_NAME: 'The company name can not be empty!', COMPANY_UPDATED: 'Company info updated!', COMPANY_NOT_CHANGED: 'No changes!', ADD_COMPANY: 'Invalid company!', From 4159f12854140514403deba13535d3b70b470102 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 20 Sep 2018 01:22:42 +0200 Subject: [PATCH 37/41] fix company info update --- .../app/plugins/wiaas/includes/user/class-wiaas-customer.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/app/plugins/wiaas/includes/user/class-wiaas-customer.php b/backend/app/plugins/wiaas/includes/user/class-wiaas-customer.php index b0426a4..e2c15fc 100644 --- a/backend/app/plugins/wiaas/includes/user/class-wiaas-customer.php +++ b/backend/app/plugins/wiaas/includes/user/class-wiaas-customer.php @@ -74,8 +74,9 @@ class Wiaas_Customer { } public static function update_customer_company_info($customer_id, $company_name, $vat_code){ - $result = update_user_meta( $customer_id, 'company_name', $company_name); - return $result || update_user_meta( $customer_id, 'vat_code', $vat_code ); + $result1 = update_user_meta( $customer_id, 'company_name', $company_name); + $result2 = update_user_meta( $customer_id, 'vat_code', $vat_code ); + return $result1 || $result2; } public static function update_customer_billing_addresses($customer_id, $new_address){ From 5da2353d04c99d660b4a253618643ee1d211cff0 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 20 Sep 2018 01:23:39 +0200 Subject: [PATCH 38/41] add tests for profile endpoints --- .../test-wiaas-rest-customer-api.php | 609 ++++++++++++++++++ 1 file changed, 609 insertions(+) create mode 100644 backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-customer-api.php diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-customer-api.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-customer-api.php new file mode 100644 index 0000000..85844e4 --- /dev/null +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-customer-api.php @@ -0,0 +1,609 @@ +server = $wp_rest_server = new \WP_REST_Server; + do_action( 'rest_api_init' ); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_profile_addresses + */ + function test_update_customer_profile_addresses_as_guest() { + wp_set_current_user(0); + + $dummy_address = self::create_dummy_address(); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/0/profile-addresses'); + $request->set_body_params(array( + 'profile_address' => $dummy_address + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertTrue($response->is_error()); + $this->assertEquals($response->get_status(), 401); + + $error_data = $response->as_error(); + $this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_profile_addresses + */ + function test_add_customer_profile_addresses_with_valid_address() { + wp_set_current_user(1); + + $dummy_address = self::create_dummy_address(); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/profile-addresses'); + $request->set_body_params(array( + 'profile_address' => json_encode($dummy_address) + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('profile_addresses', $profile_info); + $this->assertEquals($profile_info['profile_addresses'][0]['city'], $dummy_address['city']); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'PROFILE_ADDRESS_UPDATED'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_profile_addresses + */ + function test_add_customer_profile_addresses_with_invalid_address() { + wp_set_current_user(1); + + $dummy_address = self::create_dummy_address(); + $dummy_address['zip_code'] = 'this is not a zip code'; + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/profile-addresses'); + $request->set_body_params(array( + 'profile_address' => json_encode($dummy_address) + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('profile_addresses', $profile_info); + $this->assertEquals($profile_info['profile_addresses'][0], NULL); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'PROFILE_ADDRESS_NOT_CHANGED'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_billing_addresses + */ + function test_update_customer_billing_addresses_as_guest() { + wp_set_current_user(0); + + $dummy_address = self::create_dummy_address(); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/0/billing-addresses'); + $request->set_body_params(array( + 'billing_address' => $dummy_address + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertTrue($response->is_error()); + $this->assertEquals($response->get_status(), 401); + + $error_data = $response->as_error(); + $this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_billing_addresses + */ + function test_add_customer_billing_addresses_with_valid_address() { + wp_set_current_user(1); + + $dummy_address = self::create_dummy_address(); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/billing-addresses'); + $request->set_body_params(array( + 'billing_address' => json_encode($dummy_address) + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('billing_addresses', $profile_info); + $this->assertEquals($profile_info['billing_addresses'][0]['city'], $dummy_address['city']); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'BILLING_ADDRESS_UPDATED'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_billing_addresses + */ + function test_add_customer_billing_addresses_with_invalid_address() { + wp_set_current_user(1); + + $dummy_address = self::create_dummy_address(); + $dummy_address['zip_code'] = 'this is not a zip code'; + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/billing-addresses'); + $request->set_body_params(array( + 'billing_address' => json_encode($dummy_address) + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('billing_addresses', $profile_info); + $this->assertEquals($profile_info['billing_addresses'][0], NULL); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'BILLING_ADDRESS_NOT_CHANGED'); + } + + /** + * @covers Wiass_REST_Customer_API::delete_customer_billing_addresses + */ + function test_delete_customer_billing_addresses_as_guest() { + $address_id = self::insert_dummy_billing_address(); + wp_set_current_user(0); + + $request = new WP_REST_Request( 'POST', '/wiaas/customer/1/billing-addresses'); + $request->set_body_params(array( + 'address_id' => $address_id + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertTrue($response->is_error()); + $this->assertEquals($response->get_status(), 401); + + $error_data = $response->as_error(); + $this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.'); + } + + /** + * @covers Wiass_REST_Customer_API::delete_customer_billing_addresses + */ + function test_delete_customer_billing_addresses() { + $address_id = self::insert_dummy_billing_address(); + wp_set_current_user(1); + + $request = new WP_REST_Request( 'POST', '/wiaas/customer/1/billing-addresses'); + $request->set_body_params(array( + 'address_id' => $address_id + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('billing_addresses', $profile_info); + $this->assertEquals($profile_info['billing_addresses'][0], NULL); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'ADDRESS_REMOVED'); + } + + /** + * @covers Wiass_REST_Customer_API::delete_customer_profile_addresses + */ + function test_delete_customer_delivery_addresses_as_guest() { + $address_id = self::insert_dummy_profile_address(); + wp_set_current_user(0); + + $request = new WP_REST_Request( 'POST', '/wiaas/customer/1/profile-addresses'); + $request->set_body_params(array( + 'address_id' => $address_id + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertTrue($response->is_error()); + $this->assertEquals($response->get_status(), 401); + + $error_data = $response->as_error(); + $this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.'); + } + + /** + * @covers Wiass_REST_Customer_API::delete_customer_profile_addresses + */ + function test_delete_customer_profile_addresses() { + $address_id = self::insert_dummy_profile_address(); + wp_set_current_user(1); + + $request = new WP_REST_Request( 'POST', '/wiaas/customer/1/profile-addresses'); + $request->set_body_params(array( + 'address_id' => $address_id + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('profile_addresses', $profile_info); + $this->assertEquals($profile_info['profile_addresses'][0], NULL); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'ADDRESS_REMOVED'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_personal_info + */ + function test_update_customer_personal_info_as_guest() { + wp_set_current_user(0); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/personal-info'); + $request->set_body_params(array( + 'first_name' => 'Trunks', + 'last_name' => 'Goten', + 'phone' => 11111 + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertTrue($response->is_error()); + $this->assertEquals($response->get_status(), 401); + + $error_data = $response->as_error(); + $this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_personal_info + */ + function test_update_customer_personal_info_with_empty_phone() { + wp_set_current_user(1); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/personal-info'); + $request->set_body_params(array( + 'first_name' => 'Trunks', + 'last_name' => 'Goten', + 'phone' => '' + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('phone', $profile_info); + $this->assertEquals($profile_info['phone'], ''); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'ADD_PHONE_NUMBER'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_personal_info + */ + function test_update_customer_personal_info_with_empty_name() { + wp_set_current_user(1); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/personal-info'); + $request->set_body_params(array( + 'first_name' => '', + 'last_name' => '', + 'phone' => '23232' + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('phone', $profile_info); + $this->assertEquals($profile_info['phone'], ''); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'ADD_NAME'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_personal_info + */ + function test_update_customer_personal_info() { + wp_set_current_user(1); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/personal-info'); + $request->set_body_params(array( + 'first_name' => 'Trunks', + 'last_name' => 'Goten', + 'phone' => '3434' + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('phone', $profile_info); + $this->assertArrayHasKey('name', $profile_info); + $this->assertEquals($profile_info['phone'], '3434'); + $this->assertEquals($profile_info['name'], 'Trunks Goten'); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'PROFILE_UPDATED'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_company_info + */ + function test_update_customer_company_info_as_guest() { + wp_set_current_user(0); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/company-info'); + $request->set_body_params(array( + 'company_name' => 'Saburly', + 'vat_code' => '12345' + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertTrue($response->is_error()); + $this->assertEquals($response->get_status(), 401); + + $error_data = $response->as_error(); + $this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_company_info + */ + function test_update_customer_company_info_with_empty_company_name() { + wp_set_current_user(1); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/company-info'); + $request->set_body_params(array( + 'company_name' => '', + 'vat_code' => '12345' + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('company_name', $profile_info); + $this->assertArrayHasKey('vat_code', $profile_info); + $this->assertEquals($profile_info['company_name'], ''); + $this->assertEquals($profile_info['vat_code'], ''); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'ADD_COMPANY_NAME'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_company_info + */ + function test_update_customer_company_info_with_empty_vat() { + wp_set_current_user(1); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/company-info'); + $request->set_body_params(array( + 'company_name' => 'Saburly', + 'vat_code' => '' + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('company_name', $profile_info); + $this->assertArrayHasKey('vat_code', $profile_info); + $this->assertEquals($profile_info['company_name'], ''); + $this->assertEquals($profile_info['vat_code'], ''); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'ADD_VAT'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_company_info + */ + function test_update_customer_company_info() { + wp_set_current_user(1); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/company-info'); + $request->set_body_params(array( + 'company_name' => 'Saburly', + 'vat_code' => '123' + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('company_name', $profile_info); + $this->assertArrayHasKey('vat_code', $profile_info); + $this->assertEquals($profile_info['company_name'], 'Saburly'); + $this->assertEquals($profile_info['vat_code'], '123'); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'COMPANY_UPDATED'); + } + + + + //Helper functions + private function create_dummy_address(){ + return array( + 'country_name' => 'Sweden', + 'delivery_mail' => 'delivery@email.com', + 'id_country_selected' => 1, + 'city' => 'Stockholm', + 'detailed_address' => 'Dummy Street 7', + 'zip_code' => 124443, + 'first_name' => 'Tester', + 'last_name' => 'Retset', + 'invoice_mail' => 'invoice@mail.com' + ); + } + + private function insert_dummy_billing_address(){ + wp_set_current_user(1); + + $dummy_address = self::create_dummy_address(); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/billing-addresses'); + $request->set_body_params(array( + 'billing_address' => json_encode($dummy_address) + )); + $response = $this->server->dispatch( $request ); + + return $response->get_data()['data']['billing_addresses'][0]['id']; + } + + private function insert_dummy_profile_address(){ + wp_set_current_user(1); + + $dummy_address = self::create_dummy_address(); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/delivery-addresses'); + $request->set_body_params(array( + 'delivery_address' => json_encode($dummy_address) + )); + $response = $this->server->dispatch( $request ); + + return $response->get_data()['data']['delivery_addresses'][0]['id']; + } +} From 41e92e2390b890f03431d84753bf5f66b4c4b016 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 20 Sep 2018 13:57:01 +0200 Subject: [PATCH 39/41] use DELETE instead of POST for delete action --- .../includes/api/class-wiaas-rest-customer.php | 14 ++++++-------- .../src/actions/profileSettings/addressActions.js | 14 ++++---------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-customer.php b/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-customer.php index 87533b3..487e8b8 100644 --- a/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-customer.php +++ b/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-customer.php @@ -22,8 +22,8 @@ class Wiaas_REST_Customer_API { 'permission_callback' => 'is_user_logged_in' ) ); - register_rest_route( self::$namespace, 'customer/(?P\d+)/profile-addresses', array( - 'methods' => 'POST', + register_rest_route( self::$namespace, 'customer/(?P\d+)/profile-addresses/(?P\d+)', array( + 'methods' => 'DELETE', 'callback' => array(__CLASS__, 'delete_customer_profile_address'), 'permission_callback' => 'is_user_logged_in' ) ); @@ -34,8 +34,8 @@ class Wiaas_REST_Customer_API { 'permission_callback' => 'is_user_logged_in' ) ); - register_rest_route( self::$namespace, 'customer/(?P\d+)/billing-addresses', array( - 'methods' => 'POST', + register_rest_route( self::$namespace, 'customer/(?P\d+)/billing-addresses/(?P\d+)', array( + 'methods' => 'DELETE', 'callback' => array(__CLASS__, 'delete_customer_billing_addresses'), 'permission_callback' => 'is_user_logged_in' ) ); @@ -70,8 +70,7 @@ class Wiaas_REST_Customer_API { public static function delete_customer_profile_address(WP_REST_Request $request){ $customer_id = $request['id']; - $params = $request->get_body_params(); - $address_id = $params['address_id']; + $address_id = $request['address_id']; if (!Wiaas_Customer::delete_customer_profile_address($customer_id, $address_id)){ return self::generate_wiaas_response('ADDRESS_ERROR', 'error', Wiaas_Customer::get_customer_info($customer_id)); @@ -95,8 +94,7 @@ class Wiaas_REST_Customer_API { public static function delete_customer_billing_addresses(WP_REST_Request $request){ $customer_id = $request['id']; - $params = $request->get_body_params(); - $address_id = json_decode($params['address_id']); + $address_id = $request['address_id']; if (!Wiaas_Customer::delete_customer_billing_address($customer_id, $address_id)){ return self::generate_wiaas_response('ADDRESS_ERROR', 'error', Wiaas_Customer::get_customer_info($customer_id)); diff --git a/frontend/src/actions/profileSettings/addressActions.js b/frontend/src/actions/profileSettings/addressActions.js index 83cd000..3db8807 100644 --- a/frontend/src/actions/profileSettings/addressActions.js +++ b/frontend/src/actions/profileSettings/addressActions.js @@ -52,11 +52,8 @@ export const removeProfileAddress = (idUser, idProfileAddress) => { return dispatch => { dispatch(requestRemoveAddress()); return client.fetch({ - url: `${API_SERVER}/wp-json/wiaas/customer/${idUser}/profile-addresses`, - method: 'post', - data: { - 'address_id':idProfileAddress - } + url: `${API_SERVER}/wp-json/wiaas/customer/${idUser}/profile-addresses/${idProfileAddress}`, + method: 'delete' }) .then(response => { if(response.data){ @@ -105,11 +102,8 @@ export const removeBillingAddress = (idUser, idBillingAddress) => { return dispatch => { dispatch(requestRemoveBillingAddress()); return client.fetch({ - url: `${API_SERVER}/wp-json/wiaas/customer/${idUser}/billing-addresses`, - method: 'POST', - data: { - 'address_id':idBillingAddress - } + url: `${API_SERVER}/wp-json/wiaas/customer/${idUser}/billing-addresses/${idBillingAddress}`, + method: 'delete' }) .then(response => { if(response.data){ From cbf1de340325284949cb6e4c8a9f4dd8d4837747 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 20 Sep 2018 14:08:32 +0200 Subject: [PATCH 40/41] modify test to use DELTE instead of POST --- .../test-wiaas-rest-customer-api.php | 26 +++++-------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-customer-api.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-customer-api.php index 85844e4..ba5f79f 100644 --- a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-customer-api.php +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-customer-api.php @@ -205,10 +205,7 @@ class Wiass_REST_Customer_Api_Test extends Wiaas_Unit_Test_Case { $address_id = self::insert_dummy_billing_address(); wp_set_current_user(0); - $request = new WP_REST_Request( 'POST', '/wiaas/customer/1/billing-addresses'); - $request->set_body_params(array( - 'address_id' => $address_id - )); + $request = new WP_REST_Request( 'DELETE', '/wiaas/customer/1/billing-addresses/' . $address_id); $response = $this->server->dispatch( $request ); $this->assertNotNull($response); @@ -227,10 +224,7 @@ class Wiass_REST_Customer_Api_Test extends Wiaas_Unit_Test_Case { $address_id = self::insert_dummy_billing_address(); wp_set_current_user(1); - $request = new WP_REST_Request( 'POST', '/wiaas/customer/1/billing-addresses'); - $request->set_body_params(array( - 'address_id' => $address_id - )); + $request = new WP_REST_Request( 'DELETE', '/wiaas/customer/1/billing-addresses/' . $address_id); $response = $this->server->dispatch( $request ); $this->assertNotNull($response); @@ -260,10 +254,7 @@ class Wiass_REST_Customer_Api_Test extends Wiaas_Unit_Test_Case { $address_id = self::insert_dummy_profile_address(); wp_set_current_user(0); - $request = new WP_REST_Request( 'POST', '/wiaas/customer/1/profile-addresses'); - $request->set_body_params(array( - 'address_id' => $address_id - )); + $request = new WP_REST_Request( 'DELETE', '/wiaas/customer/1/profile-addresses/' . $address_id); $response = $this->server->dispatch( $request ); $this->assertNotNull($response); @@ -282,10 +273,7 @@ class Wiass_REST_Customer_Api_Test extends Wiaas_Unit_Test_Case { $address_id = self::insert_dummy_profile_address(); wp_set_current_user(1); - $request = new WP_REST_Request( 'POST', '/wiaas/customer/1/profile-addresses'); - $request->set_body_params(array( - 'address_id' => $address_id - )); + $request = new WP_REST_Request( 'DELETE', '/wiaas/customer/1/profile-addresses/' . $address_id); $response = $this->server->dispatch( $request ); $this->assertNotNull($response); @@ -598,12 +586,12 @@ class Wiass_REST_Customer_Api_Test extends Wiaas_Unit_Test_Case { $dummy_address = self::create_dummy_address(); - $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/delivery-addresses'); + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/profile-addresses'); $request->set_body_params(array( - 'delivery_address' => json_encode($dummy_address) + 'profile_address' => json_encode($dummy_address) )); $response = $this->server->dispatch( $request ); - return $response->get_data()['data']['delivery_addresses'][0]['id']; + return $response->get_data()['data']['profile_addresses'][0]['id']; } } From 0065b25fef92cc6a29e7153017456c8d275db070 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 20 Sep 2018 02:51:49 +0200 Subject: [PATCH 41/41] add rewrite rule to apache for frontend --- docker/frontend/000-default.conf | 41 ++++++++++++++++++++++++++++++++ frontend.dockerfile | 2 ++ 2 files changed, 43 insertions(+) create mode 100644 docker/frontend/000-default.conf diff --git a/docker/frontend/000-default.conf b/docker/frontend/000-default.conf new file mode 100644 index 0000000..82a3f0c --- /dev/null +++ b/docker/frontend/000-default.conf @@ -0,0 +1,41 @@ + + # The ServerName directive sets the request scheme, hostname and port that + # the server uses to identify itself. This is used when creating + # redirection URLs. In the context of virtual hosts, the ServerName + # specifies what hostname must appear in the request's Host: header to + # match this virtual host. For the default virtual host (this file) this + # value is not decisive as it is used as a last resort host regardless. + # However, you must set it for any further virtual host explicitly. + #ServerName www.example.com + + ServerAdmin webmaster@localhost + DocumentRoot /var/www/html + + + RewriteEngine on + # Don't rewrite files or directories + RewriteCond %{REQUEST_FILENAME} -f [OR] + RewriteCond %{REQUEST_FILENAME} -d + RewriteRule ^ - [L] + # Rewrite everything else to index.html to allow html5 state links + RewriteRule ^ index.html [L] + + + # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, + # error, crit, alert, emerg. + # It is also possible to configure the loglevel for particular + # modules, e.g. + #LogLevel info ssl:warn + + ErrorLog ${APACHE_LOG_DIR}/error.log + CustomLog ${APACHE_LOG_DIR}/access.log combined + + # For most configuration files from conf-available/, which are + # enabled or disabled at a global level, it is possible to + # include a line for only one particular virtual host. For example the + # following line enables the CGI configuration for this host only + # after it has been globally disabled with "a2disconf". + #Include conf-available/serve-cgi-bin.conf + + +# vim: syntax=apache ts=4 sw=4 sts=4 sr noet diff --git a/frontend.dockerfile b/frontend.dockerfile index 77bb2f2..0904278 100644 --- a/frontend.dockerfile +++ b/frontend.dockerfile @@ -8,6 +8,8 @@ RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - RUN apt-get install -y nodejs COPY frontend /home/wiaas/frontend +RUN a2enmod rewrite +COPY docker/frontend/000-default.conf /etc/apache2/sites-available WORKDIR /home/wiaas/frontend RUN npm install && npm rebuild node-sass --force && npm run build