Ported the api parser encode and decode into the model classes

This commit is contained in:
Alex Tselegidis 2021-11-05 09:32:34 +01:00
parent 40094390ed
commit ba77658551
8 changed files with 720 additions and 37 deletions

View file

@ -551,7 +551,7 @@ class Admins_model extends EA_Model {
*/
public function api_encode(array &$admin)
{
$encoded_response = [
$encoded_resource = [
'id' => array_key_exists('id', $admin) ? (int)$admin['id'] : NULL,
'firstName' => $admin['first_name'],
'lastName' => $admin['last_name'],
@ -571,108 +571,108 @@ class Admins_model extends EA_Model {
]
];
$admin = $encoded_response;
$admin = $encoded_resource;
}
/**
* Convert the API resource to the equivalent database admin record.
*
* @param array &$admin API resource.
* @param array $admin API resource.
* @param array|null $base Base admin data to be overwritten with the provided values (useful for updates).
*/
public function api_decode(array &$admin, array $base = NULL)
{
$decoded_response = $base ?? [];
$decoded_resource = $base ?? [];
if (array_key_exists('id', $admin))
{
$decoded_response['id'] = $admin['id'];
$decoded_resource['id'] = $admin['id'];
}
if (array_key_exists('firstName', $admin))
{
$decoded_response['first_name'] = $admin['firstName'];
$decoded_resource['first_name'] = $admin['firstName'];
}
if (array_key_exists('lastName', $admin))
{
$decoded_response['last_name'] = $admin['lastName'];
$decoded_resource['last_name'] = $admin['lastName'];
}
if (array_key_exists('email', $admin))
{
$decoded_response['email'] = $admin['email'];
$decoded_resource['email'] = $admin['email'];
}
if (array_key_exists('mobile', $admin))
{
$decoded_response['mobile_number'] = $admin['mobile'];
$decoded_resource['mobile_number'] = $admin['mobile'];
}
if (array_key_exists('phone', $admin))
{
$decoded_response['phone_number'] = $admin['phone'];
$decoded_resource['phone_number'] = $admin['phone'];
}
if (array_key_exists('address', $admin))
{
$decoded_response['address'] = $admin['address'];
$decoded_resource['address'] = $admin['address'];
}
if (array_key_exists('city', $admin))
{
$decoded_response['city'] = $admin['city'];
$decoded_resource['city'] = $admin['city'];
}
if (array_key_exists('state', $admin))
{
$decoded_response['state'] = $admin['state'];
$decoded_resource['state'] = $admin['state'];
}
if (array_key_exists('zip', $admin))
{
$decoded_response['zip_code'] = $admin['zip'];
$decoded_resource['zip_code'] = $admin['zip'];
}
if (array_key_exists('notes', $admin))
{
$decoded_response['notes'] = $admin['notes'];
$decoded_resource['notes'] = $admin['notes'];
}
if (array_key_exists('timezone', $admin))
{
$decoded_response['timezone'] = $admin['timezone'];
$decoded_resource['timezone'] = $admin['timezone'];
}
if (array_key_exists('settings', $admin))
{
if (empty($decoded_response['settings']))
if (empty($decoded_resource['settings']))
{
$decoded_response['settings'] = [];
$decoded_resource['settings'] = [];
}
if (array_key_exists('username', $admin['settings']))
{
$decoded_response['settings']['username'] = $admin['settings']['username'];
$decoded_resource['settings']['username'] = $admin['settings']['username'];
}
if (array_key_exists('password', $admin['settings']))
{
$decoded_response['settings']['password'] = $admin['settings']['password'];
$decoded_resource['settings']['password'] = $admin['settings']['password'];
}
if (array_key_exists('notifications', $admin['settings']))
{
$decoded_response['settings']['notifications'] = filter_var($admin['settings']['notifications'],
$decoded_resource['settings']['notifications'] = filter_var($admin['settings']['notifications'],
FILTER_VALIDATE_BOOLEAN);
}
if (array_key_exists('calendarView', $admin['settings']))
{
$decoded_response['settings']['calendar_view'] = $admin['settings']['calendarView'];
$decoded_resource['settings']['calendar_view'] = $admin['settings']['calendarView'];
}
}
$admin = $decoded_response;
$admin = $decoded_resource;
}
}

View file

@ -552,4 +552,98 @@ class Appointments_model extends EA_Model {
}
}
}
/**
* Convert the database appointment record to the equivalent API resource.
*
* @param array $appointment Appointment data.
*/
public function api_encode(array &$appointment)
{
$encoded_resource = [
'id' => array_key_exists('id', $appointment) ? (int)$appointment['id'] : NULL,
'book' => $appointment['book_datetime'],
'start' => $appointment['start_datetime'],
'end' => $appointment['end_datetime'],
'hash' => $appointment['hash'],
'location' => $appointment['location'],
'notes' => $appointment['notes'],
'customerId' => $appointment['id_users_customer'] !== NULL ? (int)$appointment['id_users_customer'] : NULL,
'providerId' => $appointment['id_users_provider'] !== NULL ? (int)$appointment['id_users_provider'] : NULL,
'serviceId' => $appointment['id_services'] !== NULL ? (int)$appointment['id_services'] : NULL,
'googleCalendarId' => $appointment['id_google_calendar'] !== NULL ? (int)$appointment['id_google_calendar'] : NULL
];
$appointment = $encoded_resource;
}
/**
* Convert the API resource to the equivalent database appointment record.
*
* @param array $appointment API resource.
* @param array|null $base Base appointment data to be overwritten with the provided values (useful for updates).
*/
public function api_decode(array &$appointment, array $base = NULL)
{
$decoded_request = $base ?: [];
if (array_key_exists('id', $appointment))
{
$decoded_request['id'] = $appointment['id'];
}
if (array_key_exists('book', $appointment))
{
$decoded_request['book_datetime'] = $appointment['book'];
}
if (array_key_exists('start', $appointment))
{
$decoded_request['start_datetime'] = $appointment['start'];
}
if (array_key_exists('end', $appointment))
{
$decoded_request['end_datetime'] = $appointment['end'];
}
if (array_key_exists('hash', $appointment))
{
$decoded_request['hash'] = $appointment['hash'];
}
if (array_key_exists('location', $appointment))
{
$decoded_request['location'] = $appointment['location'];
}
if (array_key_exists('notes', $appointment))
{
$decoded_request['notes'] = $appointment['notes'];
}
if (array_key_exists('customerId', $appointment))
{
$decoded_request['id_users_customer'] = $appointment['customerId'];
}
if (array_key_exists('providerId', $appointment))
{
$decoded_request['id_users_provider'] = $appointment['providerId'];
}
if (array_key_exists('serviceId', $appointment))
{
$decoded_request['id_services'] = $appointment['serviceId'];
}
if (array_key_exists('googleCalendarId', $appointment))
{
$decoded_request['id_google_calendar'] = $appointment['googleCalendarId'];
}
$decoded_request['is_unavailable'] = FALSE;
$appointment = $decoded_request;
}
}

View file

@ -416,4 +416,84 @@ class Customers_model extends EA_Model {
{
// Customers do not currently have any related resources.
}
/**
* Convert the database customer record to the equivalent API resource.
*
* @param array $customer Customer data.
*/
public function api_encode(array &$customer)
{
$encoded_resource = [
'id' => array_key_exists('id', $customer) ? (int)$customer['id'] : NULL,
'firstName' => $customer['first_name'],
'lastName' => $customer['last_name'],
'email' => $customer['email'],
'phone' => $customer['phone_number'],
'address' => $customer['address'],
'city' => $customer['city'],
'zip' => $customer['zip_code'],
'notes' => $customer['notes']
];
$customer = $encoded_resource;
}
/**
* Convert the API resource to the equivalent database admin record.
*
* @param array $customer API resource.
* @param array|null $base Base customer data to be overwritten with the provided values (useful for updates).
*/
public function api_decode(array &$customer, array $base = NULL)
{
$decoded_resource = $base ?: [];
if (array_key_exists('id', $customer))
{
$decoded_resource['id'] = $customer['id'];
}
if (array_key_exists('firstName', $customer))
{
$decoded_resource['first_name'] = $customer['firstName'];
}
if (array_key_exists('lastName', $customer))
{
$decoded_resource['last_name'] = $customer['lastName'];
}
if (array_key_exists('email', $customer))
{
$decoded_resource['email'] = $customer['email'];
}
if (array_key_exists('phone', $customer))
{
$decoded_resource['phone_number'] = $customer['phone'];
}
if (array_key_exists('address', $customer))
{
$decoded_resource['address'] = $customer['address'];
}
if (array_key_exists('city', $customer))
{
$decoded_resource['city'] = $customer['city'];
}
if (array_key_exists('zip', $customer))
{
$decoded_resource['zip_code'] = $customer['zip'];
}
if (array_key_exists('notes', $customer))
{
$decoded_resource['notes'] = $customer['notes'];
}
$customer = $decoded_resource;
}
}

View file

@ -741,4 +741,207 @@ class Providers_model extends EA_Model {
}
}
}
/**
* Convert the database provider record to the equivalent API resource.
*
* @param array $provider Provider data.
*/
public function api_encode(array &$provider)
{
$encoded_resource = [
'id' => array_key_exists('id', $provider) ? (int)$provider['id'] : NULL,
'firstName' => $provider['first_name'],
'lastName' => $provider['last_name'],
'email' => $provider['email'],
'mobile' => $provider['mobile_number'],
'phone' => $provider['phone_number'],
'address' => $provider['address'],
'city' => $provider['city'],
'state' => $provider['state'],
'zip' => $provider['zip_code'],
'notes' => $provider['notes'],
'timezone' => $provider['timezone'],
];
if (array_key_exists('services', $provider))
{
$encoded_resource['services'] = $provider['services'];
}
if (array_key_exists('settings', $provider))
{
$encoded_resource['settings'] = [
'username' => $provider['settings']['username'],
'notifications' => filter_var($provider['settings']['notifications'], FILTER_VALIDATE_BOOLEAN),
'calendarView' => $provider['settings']['calendar_view'],
'googleSync' => array_key_exists('google_sync', $provider['settings'])
? filter_var($provider['settings']['google_sync'], FILTER_VALIDATE_BOOLEAN)
: NULL,
'googleCalendar' => array_key_exists('google_calendar', $provider['settings'])
? $provider['settings']['google_calendar']
: NULL,
'googleToken' => array_key_exists('google_token', $provider['settings'])
? $provider['settings']['google_token']
: NULL,
'syncFutureDays' => array_key_exists('sync_future_days', $provider['settings'])
? (int)$provider['settings']['sync_future_days']
: NULL,
'syncPastDays' => array_key_exists('sync_past_days', $provider['settings'])
? (int)$provider['settings']['sync_past_days']
: NULL,
'workingPlan' => array_key_exists('working_plan', $provider['settings'])
? json_decode($provider['settings']['working_plan'], TRUE)
: NULL,
'workingPlanExceptions' => array_key_exists('working_plan_exceptions', $provider['settings'])
? json_decode($provider['settings']['working_plan_exceptions'], TRUE)
: NULL,
];
}
$provider = $encoded_resource;
}
/**
* Convert the API resource to the equivalent database provider record.
*
* @param array $provider API resource.
* @param array|null $base Base provider data to be overwritten with the provided values (useful for updates).
*/
public function api_decode(array &$provider, array $base = NULL)
{
$decoded_resource = $base ?: [];
if (array_key_exists('id', $provider))
{
$decoded_resource['id'] = $provider['id'];
}
if (array_key_exists('firstName', $provider))
{
$decoded_resource['first_name'] = $provider['firstName'];
}
if (array_key_exists('lastName', $provider))
{
$decoded_resource['last_name'] = $provider['lastName'];
}
if (array_key_exists('email', $provider))
{
$decoded_resource['email'] = $provider['email'];
}
if (array_key_exists('mobile', $provider))
{
$decoded_resource['mobile_number'] = $provider['mobile'];
}
if (array_key_exists('phone', $provider))
{
$decoded_resource['phone_number'] = $provider['phone'];
}
if (array_key_exists('address', $provider))
{
$decoded_resource['address'] = $provider['address'];
}
if (array_key_exists('city', $provider))
{
$decoded_resource['city'] = $provider['city'];
}
if (array_key_exists('state', $provider))
{
$decoded_resource['state'] = $provider['state'];
}
if (array_key_exists('zip', $provider))
{
$decoded_resource['zip_code'] = $provider['zip'];
}
if (array_key_exists('notes', $provider))
{
$decoded_resource['notes'] = $provider['notes'];
}
if (array_key_exists('timezone', $provider))
{
$decoded_resource['timezone'] = $provider['timezone'];
}
if (array_key_exists('services', $provider))
{
$decoded_resource['services'] = $provider['services'];
}
if (array_key_exists('settings', $provider))
{
if (empty($decoded_resource['settings']))
{
$decoded_resource['settings'] = [];
}
if (array_key_exists('username', $provider['settings']))
{
$decoded_resource['settings']['username'] = $provider['settings']['username'];
}
if (array_key_exists('password', $provider['settings']))
{
$decoded_resource['settings']['password'] = $provider['settings']['password'];
}
if (array_key_exists('calendarView', $provider['settings']))
{
$decoded_resource['settings']['calendar_view'] = $provider['settings']['calendarView'];
}
if (array_key_exists('notifications', $provider['settings']))
{
$decoded_resource['settings']['notifications'] = filter_var($provider['settings']['notifications'],
FILTER_VALIDATE_BOOLEAN);
}
if (array_key_exists('googleSync', $provider['settings']))
{
$decoded_resource['settings']['google_sync'] = filter_var($provider['settings']['googleSync'],
FILTER_VALIDATE_BOOLEAN);
}
if (array_key_exists('googleCalendar', $provider['settings']))
{
$decoded_resource['settings']['google_calendar'] = $provider['settings']['googleCalendar'];
}
if (array_key_exists('googleToken', $provider['settings']))
{
$decoded_resource['settings']['google_token'] = $provider['settings']['googleToken'];
}
if (array_key_exists('syncFutureDays', $provider['settings']))
{
$decoded_resource['settings']['sync_future_days'] = $provider['settings']['syncFutureDays'];
}
if (array_key_exists('syncPastDays', $provider['settings']))
{
$decoded_resource['settings']['sync_past_days'] = $provider['settings']['syncPastDays'];
}
if (array_key_exists('workingPlan', $provider['settings']))
{
$decoded_resource['settings']['working_plan'] = json_encode($provider['settings']['workingPlan']);
}
if (array_key_exists('workingPlanExceptions', $provider['settings']))
{
$decoded_resource['settings']['working_plan_exceptions'] = json_encode($provider['settings']['workingPlanExceptions']);
}
}
$provider = $decoded_resource;
}
}

View file

@ -603,4 +603,142 @@ class Secretaries_model extends EA_Model {
}
}
}
/**
* Convert the database secretary record to the equivalent API resource.
*
* @param array $secretary Secretary data.
*/
public function api_encode(array &$secretary)
{
$encoded_resource = [
'id' => array_key_exists('id', $secretary) ? (int)$secretary['id'] : NULL,
'firstName' => $secretary['first_name'],
'lastName' => $secretary['last_name'],
'email' => $secretary['email'],
'mobile' => $secretary['mobile_number'],
'phone' => $secretary['phone_number'],
'address' => $secretary['address'],
'city' => $secretary['city'],
'state' => $secretary['state'],
'zip' => $secretary['zip_code'],
'notes' => $secretary['notes'],
'providers' => $secretary['providers'],
'timezone' => $secretary['timezone'],
'settings' => [
'username' => $secretary['settings']['username'],
'notifications' => filter_var($secretary['settings']['notifications'], FILTER_VALIDATE_BOOLEAN),
'calendarView' => $secretary['settings']['calendar_view']
]
];
$secretary = $encoded_resource;
}
/**
* Convert the API resource to the equivalent database secretary record.
*
* @param array $secretary API resource.
* @param array|null $base Base secretary data to be overwritten with the provided values (useful for updates).
*/
public function api_decode(array &$secretary, array $base = NULL)
{
$decoded_resource = $base ?: [];
if (array_key_exists('id', $secretary))
{
$decoded_resource['id'] = $secretary['id'];
}
if (array_key_exists('firstName', $secretary))
{
$decoded_resource['first_name'] = $secretary['firstName'];
}
if (array_key_exists('lastName', $secretary))
{
$decoded_resource['last_name'] = $secretary['lastName'];
}
if (array_key_exists('email', $secretary))
{
$decoded_resource['email'] = $secretary['email'];
}
if (array_key_exists('mobile', $secretary))
{
$decoded_resource['mobile_number'] = $secretary['mobile'];
}
if (array_key_exists('phone', $secretary))
{
$decoded_resource['phone_number'] = $secretary['phone'];
}
if (array_key_exists('address', $secretary))
{
$decoded_resource['address'] = $secretary['address'];
}
if (array_key_exists('city', $secretary))
{
$decoded_resource['city'] = $secretary['city'];
}
if (array_key_exists('state', $secretary))
{
$decoded_resource['state'] = $secretary['state'];
}
if (array_key_exists('zip', $secretary))
{
$decoded_resource['zip_code'] = $secretary['zip'];
}
if (array_key_exists('notes', $secretary))
{
$decoded_resource['notes'] = $secretary['notes'];
}
if (array_key_exists('timezone', $secretary))
{
$decoded_resource['timezone'] = $secretary['timezone'];
}
if (array_key_exists('providers', $secretary))
{
$decoded_resource['providers'] = $secretary['providers'];
}
if (array_key_exists('settings', $secretary))
{
if (empty($decoded_resource['settings']))
{
$decoded_resource['settings'] = [];
}
if (array_key_exists('username', $secretary['settings']))
{
$decoded_resource['settings']['username'] = $secretary['settings']['username'];
}
if (array_key_exists('password', $secretary['settings']))
{
$decoded_resource['settings']['password'] = $secretary['settings']['password'];
}
if (array_key_exists('notifications', $secretary['settings']))
{
$decoded_resource['settings']['notifications'] = filter_var($secretary['settings']['notifications'],
FILTER_VALIDATE_BOOLEAN);
}
if (array_key_exists('calendarView', $secretary['settings']))
{
$decoded_resource['settings']['calendar_view'] = $secretary['settings']['calendarView'];
}
}
$secretary = $decoded_resource;
}
}

View file

@ -25,7 +25,7 @@ class Service_categories_model extends EA_Model {
protected $casts = [
'id' => 'integer',
];
/**
* Save (insert or update) a service category.
*
@ -148,9 +148,9 @@ class Service_categories_model extends EA_Model {
}
$service_category = $this->db->get_where('service_categories', ['id' => $service_category_id])->row_array();
$this->cast($service_category);
$this->cast($service_category);
return $service_category;
}
@ -186,8 +186,8 @@ class Service_categories_model extends EA_Model {
// Check if the required field is part of the service category data.
$service_category = $query->row_array();
$this->cast($service_category);
$this->cast($service_category);
if ( ! array_key_exists($field, $service_category))
{
@ -220,12 +220,12 @@ class Service_categories_model extends EA_Model {
}
$service_categories = $this->db->get('service_categories', $limit, $offset)->result_array();
foreach($service_categories as $service_category)
foreach ($service_categories as $service_category)
{
$this->cast($service_category);
$this->cast($service_category);
}
return $service_categories;
}
@ -262,12 +262,12 @@ class Service_categories_model extends EA_Model {
->order_by($order_by)
->get()
->result_array();
foreach($service_categories as &$service_category)
foreach ($service_categories as &$service_category)
{
$this->cast($service_category);
$this->cast($service_category);
}
return $service_categories;
}
@ -283,4 +283,48 @@ class Service_categories_model extends EA_Model {
{
// Service categories do not currently have any related resources.
}
/**
* Convert the database service category record to the equivalent API resource.
*
* @param array $service_category Service category data.
*/
public function api_encode(array &$service_category)
{
$encoded_resource = [
'id' => array_key_exists('id', $service_category) ? (int)$service_category['id'] : NULL,
'name' => $service_category['name'],
'description' => array_key_exists('description', $service_category) ? $service_category['description'] : NULL
];
$service_category = $encoded_resource;
}
/**
* Convert the API resource to the equivalent database service category record.
*
* @param array $service_category API resource.
* @param array|null $base Base service category data to be overwritten with the provided values (useful for updates).
*/
public function api_decode(array &$service_category, array $base = NULL)
{
$decoded_resource = $base ?: [];
if (array_key_exists('id', $service_category))
{
$decoded_resource['id'] = $service_category['id'];
}
if (array_key_exists('name', $service_category))
{
$decoded_resource['name'] = $service_category['name'];
}
if (array_key_exists('description', $service_category))
{
$decoded_resource['description'] = $service_category['description'];
}
$service_category = $decoded_resource;
}
}

View file

@ -376,4 +376,90 @@ class Services_model extends EA_Model {
}
}
}
/**
* Convert the database service record to the equivalent API resource.
*
* @param array $service Service data.
*/
public function api_encode(array &$service)
{
$encoded_resource = [
'id' => array_key_exists('id', $service) ? (int)$service['id'] : NULL,
'name' => $service['name'],
'duration' => (int)$service['duration'],
'price' => (float)$service['price'],
'currency' => $service['currency'],
'description' => $service['description'],
'location' => $service['location'],
'availabilitiesType' => $service['availabilities_type'],
'attendantsNumber' => (int)$service['attendants_number'],
'categoryId' => $service['id_service_categories'] !== NULL ? (int)$service['id_service_categories'] : NULL
];
$service = $encoded_resource;
}
/**
* Convert the API resource to the equivalent database service record.
*
* @param array $service API resource.
* @param array|null $base Base service data to be overwritten with the provided values (useful for updates).
*/
public function api_decode(array &$service, array $base = NULL)
{
$decoded_resource = $base ?: [];
if (array_key_exists('id', $service))
{
$decoded_resource['id'] = $service['id'];
}
if (array_key_exists('name', $service))
{
$decoded_resource['name'] = $service['name'];
}
if (array_key_exists('duration', $service))
{
$decoded_resource['duration'] = $service['duration'];
}
if (array_key_exists('price', $service))
{
$decoded_resource['price'] = $service['price'];
}
if (array_key_exists('currency', $service))
{
$decoded_resource['currency'] = $service['currency'];
}
if (array_key_exists('description', $service))
{
$decoded_resource['description'] = $service['description'];
}
if (array_key_exists('location', $service))
{
$decoded_resource['location'] = $service['location'];
}
if (array_key_exists('availabilitiesType', $service))
{
$decoded_resource['availabilities_type'] = $service['availabilitiesType'];
}
if (array_key_exists('attendantsNumber', $service))
{
$decoded_resource['attendants_number'] = $service['attendantsNumber'];
}
if (array_key_exists('categoryId', $service))
{
$decoded_resource['id_service_categories'] = $service['categoryId'];
}
$service = $decoded_resource;
}
}

View file

@ -283,4 +283,42 @@ class Settings_model extends EA_Model {
{
// Users do not currently have any related resources.
}
/**
* Convert the database setting record to the equivalent API resource.
*
* @param array $setting Setting data.
*/
public function api_encode(array &$setting)
{
$encoded_resource = [
'name' => $setting['name'],
'value' => $setting['value']
];
$setting = $encoded_resource;
}
/**
* Convert the API resource to the equivalent database setting record.
*
* @param array $setting API resource.
* @param array|null $base Base setting data to be overwritten with the provided values (useful for updates).
*/
public function api_decode(array &$setting, array $base = NULL)
{
$decoded_resource = $base ?: [];
if (array_key_exists('name', $setting))
{
$decoded_resource['name'] = $setting['name'];
}
if (array_key_exists('value', $setting))
{
$decoded_resource['value'] = $setting['value'];
}
$setting = $decoded_resource;
}
}