Implement session mutation locking and enhance session management methods
This commit is contained in:
@@ -158,6 +158,18 @@ class selfserve_wash_sessions_o extends db
|
||||
}
|
||||
|
||||
public function markCompleted(?int $orderId = null): void
|
||||
{
|
||||
if (!$this->closeIfOpen(selfserve_wash_session_status::COMPLETED, $orderId)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public function markCompletedIfOpen(?int $orderId = null): bool
|
||||
{
|
||||
return $this->closeIfOpen(selfserve_wash_session_status::COMPLETED, $orderId);
|
||||
}
|
||||
|
||||
private function markCompletedInMemory(?int $orderId = null): void
|
||||
{
|
||||
$this->completed_at->set(date('Y-m-d H:i:s'));
|
||||
if ($orderId !== null) {
|
||||
@@ -167,6 +179,18 @@ class selfserve_wash_sessions_o extends db
|
||||
}
|
||||
|
||||
public function markForceStopped(?int $orderId = null, ?array $metadata = null): void
|
||||
{
|
||||
if (!$this->closeIfOpen(selfserve_wash_session_status::FORCE_STOPPED, $orderId, $metadata)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public function markForceStoppedIfOpen(?int $orderId = null, ?array $metadata = null): bool
|
||||
{
|
||||
return $this->closeIfOpen(selfserve_wash_session_status::FORCE_STOPPED, $orderId, $metadata);
|
||||
}
|
||||
|
||||
private function markForceStoppedInMemory(?int $orderId = null, ?array $metadata = null): void
|
||||
{
|
||||
$this->completed_at->set(date('Y-m-d H:i:s'));
|
||||
if ($orderId !== null) {
|
||||
@@ -181,6 +205,53 @@ class selfserve_wash_sessions_o extends db
|
||||
$this->status->set(selfserve_wash_session_status::FORCE_STOPPED->value);
|
||||
}
|
||||
|
||||
private function closeIfOpen(selfserve_wash_session_status $status, ?int $orderId = null, ?array $metadata = null): bool
|
||||
{
|
||||
if (!$this->isPersistedSession()) {
|
||||
if ($status === selfserve_wash_session_status::COMPLETED) {
|
||||
$this->markCompletedInMemory($orderId);
|
||||
} else {
|
||||
$this->markForceStoppedInMemory($orderId, $metadata);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
global $db;
|
||||
|
||||
$updates = [
|
||||
"`completed_at` = NOW()",
|
||||
"`status` = '" . $db->escape_string($status->value) . "'",
|
||||
];
|
||||
|
||||
if ($orderId !== null) {
|
||||
$updates[] = "`order_id` = " . (int)$orderId;
|
||||
}
|
||||
|
||||
if ($metadata !== null) {
|
||||
$existing = $this->metadata_json->value();
|
||||
$existing = is_array($existing) ? $existing : [];
|
||||
$existing['force_stop'] = $metadata;
|
||||
$updates[] = "`metadata_json` = '" . $db->escape_string(json_encode($existing, JSON_THROW_ON_ERROR)) . "'";
|
||||
}
|
||||
|
||||
$terminalStatuses = self::terminalStatusSqlList();
|
||||
$db->query(
|
||||
"UPDATE `selfserve_wash_sessions` SET " . implode(', ', $updates) .
|
||||
" WHERE `id` = " . (int)$this->id .
|
||||
" AND `completed_at` IS NULL" .
|
||||
" AND UPPER(TRIM(`status`)) NOT IN ($terminalStatuses)"
|
||||
);
|
||||
|
||||
$changed = $db->conn()->affected_rows > 0;
|
||||
$this->select((int)$this->id);
|
||||
return $changed;
|
||||
}
|
||||
|
||||
private function isPersistedSession(): bool
|
||||
{
|
||||
return isset($this->id) && (int)$this->id > 0 && $this->exists();
|
||||
}
|
||||
|
||||
public function selectLatestOpenByLane(int $laneId, ?int $customerNumber = null): self
|
||||
{
|
||||
$filters = [
|
||||
|
||||
Reference in New Issue
Block a user