Adds the new /customer/default-driver-template CRUD endpoint so the kundeportal 'tilladelser' tab can store and read the customer's preferred default driver access template. The endpoint reuses the existing subuser_permission_templates_service for validation and falls back to the caller's own customer number when the customer_number parameter is omitted.
140 lines
4.5 KiB
PHP
140 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace objects;
|
|
|
|
use classes\db;
|
|
use classes\object_property;
|
|
use classes\subuser_permission_templates_service;
|
|
use Exception;
|
|
use traits\db_object_t;
|
|
|
|
class customer_default_driver_template_o extends db
|
|
{
|
|
use db_object_t;
|
|
|
|
public object_property $customer_number;
|
|
public object_property $template_key;
|
|
public object_property $created_at;
|
|
public object_property $updated_at;
|
|
|
|
/**
|
|
* Convert the object to an array.
|
|
* @return array
|
|
* @throws Exception If the object is not selected
|
|
*/
|
|
public function asArray(): array
|
|
{
|
|
self::requireSelected();
|
|
return [
|
|
'id' => (int)$this->id,
|
|
'customer_number' => (int)$this->customer_number->value(),
|
|
'template_key' => (string)$this->template_key->value(),
|
|
'created_at' => (string)$this->created_at->value(),
|
|
'updated_at' => (string)$this->updated_at->value(),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @throws Exception If the creation of the object fails
|
|
*/
|
|
public function add(int $customer_number, string $template_key): void
|
|
{
|
|
$normalized = (new subuser_permission_templates_service())->normalizeTemplateKey($template_key);
|
|
if ($normalized === null || $normalized === subuser_permission_templates_service::TEMPLATE_CUSTOM) {
|
|
throw new \InvalidArgumentException('Unknown driver access template.');
|
|
}
|
|
$tmp_id = self::add_object([
|
|
'customer_number' => (int)$customer_number,
|
|
'template_key' => $normalized,
|
|
]);
|
|
self::select((int)$tmp_id);
|
|
self::objectChanged();
|
|
}
|
|
|
|
public function update(int $customer_number, string $template_key): void
|
|
{
|
|
$normalized = (new subuser_permission_templates_service())->normalizeTemplateKey($template_key);
|
|
if ($normalized === null || $normalized === subuser_permission_templates_service::TEMPLATE_CUSTOM) {
|
|
throw new \InvalidArgumentException('Unknown driver access template.');
|
|
}
|
|
if (!$this->doesUserHaveDefaultTemplate($customer_number)) {
|
|
$this->add($customer_number, $normalized);
|
|
return;
|
|
}
|
|
$this->selectByCustomerNumber($customer_number);
|
|
self::update_object((int)$this->id, [
|
|
'template_key' => $normalized,
|
|
]);
|
|
self::select((int)$this->id);
|
|
self::objectChanged();
|
|
}
|
|
|
|
public function objectChanged(): void
|
|
{
|
|
// No cache layer for this object.
|
|
}
|
|
|
|
public function structure(): void
|
|
{
|
|
$this->setTable('customer_default_driver_template');
|
|
}
|
|
|
|
public function getObjectProperties(): void
|
|
{
|
|
$this->customer_number = new object_property($this->table, $this->id, 'customer_number', 'int', true);
|
|
$this->template_key = new object_property($this->table, $this->id, 'template_key', 'string', true);
|
|
$this->created_at = new object_property($this->table, $this->id, 'created_at', 'timestamp', true);
|
|
$this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'timestamp', true);
|
|
}
|
|
|
|
public function getDefaultTemplate(int $customer_number): ?string
|
|
{
|
|
if (!$this->doesUserHaveDefaultTemplate($customer_number)) {
|
|
return null;
|
|
}
|
|
$this->selectByCustomerNumber($customer_number);
|
|
if ($this->exists()) {
|
|
return (string)$this->template_key->value();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function doesUserHaveDefaultTemplate(int $customer_number): bool
|
|
{
|
|
return $this->getIdByCustomerNumber($customer_number) !== null;
|
|
}
|
|
|
|
public function getIdByCustomerNumber(int $customer_number): ?int
|
|
{
|
|
$matches = self::getFieldsWhere([
|
|
'customer_number' => (int)$customer_number,
|
|
], [
|
|
'id'
|
|
]);
|
|
if (count($matches) > 0) {
|
|
return (int)$matches[0]['id'];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function selectByCustomerNumber(int $customer_number): ?customer_default_driver_template_o
|
|
{
|
|
$id = $this->getIdByCustomerNumber($customer_number);
|
|
if ($id === null) {
|
|
return null;
|
|
}
|
|
$this->select($id);
|
|
if ($this->exists()) {
|
|
return $this;
|
|
}
|
|
throw new Exception('Unable to select customer default driver template for customer number: ' . $customer_number);
|
|
}
|
|
|
|
public function delete(): void
|
|
{
|
|
self::requireSelected();
|
|
self::delete_object((int)$this->id);
|
|
self::objectChanged();
|
|
}
|
|
}
|