Auto-disable department self-serve at opening (#323)
Auto-disable department self-serve at opening
This commit is contained in:
@@ -4,9 +4,12 @@ namespace routes;
|
||||
|
||||
use classes\authentication;
|
||||
use classes\selfserve;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
use objects\categories_o;
|
||||
use objects\department_categories_o;
|
||||
use objects\department_lanes_o;
|
||||
use objects\department_time_bookings_opening_hours_o;
|
||||
use objects\department_variables_o;
|
||||
use objects\departments_o;
|
||||
use objects\logs_o;
|
||||
@@ -330,9 +333,9 @@ class departmentsRoute
|
||||
$enabled = $department_variables->getVariable('selfserve_enabled');
|
||||
|
||||
// Return the status
|
||||
$response->success([
|
||||
'enabled' => $enabled === true
|
||||
]);
|
||||
$response->success(
|
||||
$this->buildDepartmentSelfServeEnabledPayload((int)$department->id, $enabled === true)
|
||||
);
|
||||
} else {
|
||||
// Return an error
|
||||
$response->error('Invalid session', 400);
|
||||
@@ -368,14 +371,22 @@ class departmentsRoute
|
||||
// Set the department variable
|
||||
$department_variables = (new department_variables_o())->selectDepartment($department->id);
|
||||
$enabled = self::getParameter('enabled') === 'true' || self::getParameter('enabled') === true || self::getParameter('enabled') === 1 || self::getParameter('enabled') === '1';
|
||||
$department_variables->set('selfserve_enabled', $enabled ? 'true' : 'false');
|
||||
$this->syncDepartmentSelfServeRelayStates((int)$department->id, $enabled);
|
||||
department_variables_o::withSelfServeTransitionLock(
|
||||
(int)$department->id,
|
||||
function () use ($department_variables, $department, $enabled): void {
|
||||
$department_variables->set('selfserve_enabled', $enabled ? 'true' : 'false');
|
||||
$this->syncDepartmentSelfServeRelayStates((int)$department->id, $enabled);
|
||||
}
|
||||
);
|
||||
|
||||
// Log the incident
|
||||
(new logs_o())->add('departments', $department->id, 1, $user->id, 'EDIT_DEPARTMENT_SELFSERVE_ENABLED', 'Successfully edited department self-serve enabled status to ' . ($enabled ? 'true' : 'false'));
|
||||
|
||||
// Return a success message
|
||||
$response->success(['message' => 'Department self-serve enabled status updated successfully']);
|
||||
$response->success([
|
||||
'message' => 'Department self-serve enabled status updated successfully',
|
||||
...$this->buildDepartmentSelfServeEnabledPayload((int)$department->id, $enabled)
|
||||
]);
|
||||
} else {
|
||||
// Return an error
|
||||
$response->error('Invalid session', 400);
|
||||
@@ -593,13 +604,13 @@ class departmentsRoute
|
||||
if (!$enabled) {
|
||||
// Self-serve disabled: restore normal/manual relay operation.
|
||||
$this->setOptionalLaneRelayState($lane, 'relay_machine_program_picker_id', static function () use ($lane): void {
|
||||
$lane->setMachineProgramPickerRelayStatusHard(true);
|
||||
$lane->setMachineProgramPickerRelayStatusForDepartmentOperation(true);
|
||||
});
|
||||
$this->setOptionalLaneRelayState($lane, 'relay_machine_cleaner_id', static function () use ($lane): void {
|
||||
$lane->setMachineCleanerRelayStatusHard(true);
|
||||
$lane->setMachineCleanerRelayStatusForDepartmentOperation(true);
|
||||
});
|
||||
$this->setOptionalLaneRelayState($lane, 'relay_machine_id', static function () use ($lane): void {
|
||||
$lane->setMachineRelayStatusHard(true);
|
||||
$lane->setMachineRelayStatusForDepartmentOperation(true);
|
||||
});
|
||||
continue;
|
||||
}
|
||||
@@ -643,4 +654,38 @@ class departmentsRoute
|
||||
// Best effort only; this endpoint should still update the department variable.
|
||||
}
|
||||
}
|
||||
|
||||
private function buildDepartmentSelfServeEnabledPayload(int $departmentId, bool $enabled): array
|
||||
{
|
||||
$nextDeactivationAt = $enabled
|
||||
? $this->getDepartmentSelfServeAutoDeactivationAt($departmentId)
|
||||
: null;
|
||||
|
||||
return [
|
||||
'enabled' => $enabled,
|
||||
'auto_deactivation' => [
|
||||
'at' => $nextDeactivationAt?->format(DATE_ATOM),
|
||||
'timezone' => 'Europe/Copenhagen',
|
||||
'label' => $nextDeactivationAt === null
|
||||
? 'NEVER'
|
||||
: $nextDeactivationAt->format('Y-m-d H:i:s'),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function getDepartmentSelfServeAutoDeactivationAt(int $departmentId): ?DateTimeImmutable
|
||||
{
|
||||
try {
|
||||
$openingHours = new department_time_bookings_opening_hours_o();
|
||||
if (!$openingHours->selectExistingByDepartment($departmentId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $openingHours->getNextOpeningStart(
|
||||
new DateTimeImmutable('now', new DateTimeZone('Europe/Copenhagen'))
|
||||
);
|
||||
} catch (\Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user