Add customer synchronization support in xlvask with caching, filtering, and external ID handling
This commit is contained in:
@@ -105,7 +105,7 @@ class xlvask_cache implements xlvask_cache_i
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getCustomerCache(int $customer_number): object
|
||||
public function getCustomerCache(int $customer_number): xlvask_customer
|
||||
{
|
||||
if (self::isCustomerCached($customer_number)) {
|
||||
$customer_data = redis->get(self::getCustomerCacheKey($customer_number));
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace helpers;
|
||||
|
||||
use Exception;
|
||||
use objects\users_o;
|
||||
|
||||
class xlvask_tasks
|
||||
{
|
||||
@@ -44,15 +45,111 @@ class xlvask_tasks
|
||||
return null; // Synchronization is not enabled, do nothing
|
||||
}
|
||||
// Get the users from XL Vask
|
||||
$customer_objects = $this->fetchCustomers();
|
||||
// Return the customers with an external ID
|
||||
$customers = [
|
||||
'with_external_id' => [...self::filterCustomersWithExternalId($customer_objects)],
|
||||
'without_external_id' => [...self::filterCustomersWithoutExternalId($customer_objects)],
|
||||
'all' => $customer_objects,
|
||||
];
|
||||
|
||||
return self::synchronize_customers($customers['with_external_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the customers registered in XL Vask
|
||||
* This method fetches the customers from XL Vask and returns them as an array of customer objects.
|
||||
* @return array[xlvask_customer] An array of customer objects fetched from XL Vask
|
||||
* @throws Exception If the task fails
|
||||
*/
|
||||
public function fetchCustomers(): array
|
||||
{
|
||||
// Define the XL Vask object
|
||||
$xlvask = new \classes\xlvask();
|
||||
// Require the module to be enabled
|
||||
$xlvask->requireModuleEnabled();
|
||||
// Get the users from XL Vask
|
||||
$customer_objects_raw = $xlvask->getCustomers();
|
||||
// Parse the users into customer objects
|
||||
$customer_objects = array_map(function ($customer) use ($xlvask) {
|
||||
return array_map(function ($customer) use ($xlvask) {
|
||||
return (new $xlvask->helpers->xlvask_customer())->setProperties($customer);
|
||||
}, $customer_objects_raw);
|
||||
$tmp_xlvask = [
|
||||
'customers' => $customer_objects,
|
||||
];
|
||||
//echo 'XL Vask users fetched:' . count($tmp_xlvask['customers']) . PHP_EOL;
|
||||
return $tmp_xlvask['customers'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the customers with an external ID
|
||||
* @param array $customers Customers to filter
|
||||
* @return array The customers with an external ID
|
||||
*/
|
||||
public static function filterCustomersWithExternalId(array $customers): array
|
||||
{
|
||||
return array_filter($customers, function ($customer) {
|
||||
/** @var xlvask_customer $customer */
|
||||
return !empty($customer->externId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the customers without an external ID
|
||||
* @param array $customers Customers to filter
|
||||
* @return array The customers without an external ID
|
||||
*/
|
||||
public static function filterCustomersWithoutExternalId(array $customers): array
|
||||
{
|
||||
return array_filter($customers, function ($customer) {
|
||||
/** @var xlvask_customer $customer */
|
||||
return empty($customer->externId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronize customers with external IDs
|
||||
* This method synchronizes the customers with external IDs from XL Vask.
|
||||
* It checks if the customers exist in this system, creates them if they do not exist,
|
||||
* and adds/removes the associated external ID to the users in this system.
|
||||
* @param array $with_external_id Customers with external IDs to synchronize
|
||||
* @return array|null The synchronized customers, null if synchronization is not enabled
|
||||
* @throws Exception
|
||||
*/
|
||||
private static function synchronize_customers(array $with_external_id): array|null
|
||||
{
|
||||
// Define the XL Vask object
|
||||
$xlvask = new \classes\xlvask();
|
||||
// Require the module to be enabled
|
||||
$xlvask->requireModuleEnabled();
|
||||
// Check if synchronization is enabled
|
||||
if (!$xlvask->config->synchronization_enabled->isTrue()) {
|
||||
return null; // Synchronization is not enabled, do nothing
|
||||
}
|
||||
|
||||
// Get a list of customer numbers that we need to synchronize
|
||||
$customer_numbers = array_map(function ($customer) {
|
||||
/** @var xlvask_customer $customer */
|
||||
return $customer->externId; // The customer number is stored in the externId property in XL Vask
|
||||
}, $with_external_id);
|
||||
|
||||
// Get all the user ids associated with the customer numbers
|
||||
$users = (new users_o())->getUsersByCustomerNumbers($customer_numbers, true);
|
||||
|
||||
$cache = $xlvask->getCache();
|
||||
|
||||
/** @var xlvask_customer $customer */
|
||||
foreach ( $with_external_id as $customer ) {
|
||||
// Check if the customer exists in this system
|
||||
if (isset($users[$customer->externId])) {
|
||||
// The user exists, update the user with the external ID
|
||||
/** @var users_o $user */
|
||||
$user = $users[$customer->externId];
|
||||
// Cache the customer
|
||||
$cache->setCustomerCache((int)$customer->externId, $customer);
|
||||
} else {
|
||||
//echo 'MISSING USER: ' . $customer->name . ' (' . $customer->externId . ')' . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $with_external_id; // Return the synchronized customers
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1176,4 +1176,31 @@ class users_o extends db
|
||||
}, $customer_numbers);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getUsersByCustomerNumbers(array $customer_numbers, bool $customerNumberAndUserArray = false): array
|
||||
{
|
||||
global $db;
|
||||
// Get the users by customer numbers
|
||||
$customer_numbers = array_map('intval', $customer_numbers);
|
||||
$customer_numbers = implode(',', $customer_numbers);
|
||||
$sql = "SELECT id, customer_number FROM $this->table WHERE customer_number IN ($customer_numbers)";
|
||||
$result = $db->query($sql);
|
||||
// Create an array of user objects
|
||||
$users = [];
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$user = new users_o();
|
||||
$user->select((int)$row['id']);
|
||||
if ($customerNumberAndUserArray) {
|
||||
// If the customerNumberAndUserArray is true, return an array with the customer number and user object
|
||||
$users[(int)$row['customer_number']] = $user;
|
||||
continue;
|
||||
}
|
||||
// Otherwise, return the user object
|
||||
$users[] = $user;
|
||||
}
|
||||
return $users;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user