Files
old-new-wiaas/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-customer.php
Bilal Catic 09fc3f8018 Rename class
2018-11-22 13:22:05 +01:00

185 lines
6.5 KiB
PHP

<?php
/**
* TODO: Refactor this class so it reflects the fact that customer is an organization
* TODO: Format should be `customer/(?P<organization_id>\d+)/user/(?P<id>\d+)
*
* Class Wiaas_REST_Customer_API
*/
class Wiaas_REST_Customer_API {
/**
* Endpoint namespace.
*
* @var string
*/
private static $namespace = 'wiaas';
public function __construct() {
include_once dirname( __FILE__ ) . '/../user/class-wiaas-customer.php';
}
public static function register_routes() {
register_rest_route( self::$namespace, 'customer/(?P<id>\d+)/shops', array(
'methods' => 'GET',
'callback' => array(__CLASS__, 'get_customer_shops'),
'permission_callback' => 'is_user_logged_in'
) );
register_rest_route( self::$namespace, 'customer/(?P<id>\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<id>\d+)/profile-addresses/(?P<address_id>\d+)', array(
'methods' => 'DELETE',
'callback' => array(__CLASS__, 'delete_customer_profile_address'),
'permission_callback' => 'is_user_logged_in'
) );
register_rest_route( self::$namespace, 'customer/(?P<id>\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<id>\d+)/billing-addresses/(?P<address_id>\d+)', array(
'methods' => 'DELETE',
'callback' => array(__CLASS__, 'delete_customer_billing_addresses'),
'permission_callback' => 'is_user_logged_in'
) );
register_rest_route( self::$namespace, 'customer/(?P<id>\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<id>\d+)/company-info', array(
'methods' => 'PUT',
'callback' => array(__CLASS__, 'update_customer_company_info'),
'permission_callback' => 'is_user_logged_in'
) );
}
public static function get_customer_shops() {
$customer_shops = Wiaas_Customer::get_customer_shops();
$customer_shops = array_map(function($customer_shop) {
return array(
'id' => $customer_shop['owner_id'],
'type' => $customer_shop['order_type'],
'name' => wiaas_get_organization_name($customer_shop['owner_id'])
);
}, $customer_shops);
return rest_ensure_response($customer_shops);
}
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 wiaas_api_notice('PROFILE_ADDRESS_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
}
return wiaas_api_notice('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'];
$address_id = $request['address_id'];
if (!Wiaas_Customer::delete_customer_profile_address($customer_id, $address_id)){
return wiaas_api_notice('ADDRESS_ERROR', 'error', Wiaas_Customer::get_customer_info($customer_id));
}
return wiaas_api_notice('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 wiaas_api_notice('BILLING_ADDRESS_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
}
return wiaas_api_notice('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'];
$address_id = $request['address_id'];
if (!Wiaas_Customer::delete_customer_billing_address($customer_id, $address_id)){
return wiaas_api_notice('ADDRESS_ERROR', 'error', Wiaas_Customer::get_customer_info($customer_id));
}
return wiaas_api_notice('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 wiaas_api_notice('ADD_NAME', 'error', Wiaas_Customer::get_customer_info($customer_id));
}
if (!is_string($phone) || strlen($phone) < 1){
return wiaas_api_notice('ADD_PHONE_NUMBER', 'error', Wiaas_Customer::get_customer_info($customer_id));
}
if (!Wiaas_Validation::is_phone($phone)){
return wiaas_api_notice('INVALID_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 wiaas_api_notice('PROFILE_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
}
return wiaas_api_notice('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 wiaas_api_notice('ADD_COMPANY_NAME', 'error', Wiaas_Customer::get_customer_info($customer_id));
}
if (!is_string($vat_code) || strlen($vat_code) < 1){
return wiaas_api_notice('ADD_VAT', 'error', Wiaas_Customer::get_customer_info($customer_id));
}
if (!Wiaas_Customer::update_customer_company_info($customer_id, $company_name, $vat_code)){
return wiaas_api_notice('COMPANY_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
}
return wiaas_api_notice('COMPANY_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
}
}