Korjaa todo_status/assign: poista dbLoadTodo-kutsu joka kaatui

dbLoadTodo hakee comments+time_entries lisätauluista jotka
saattoivat puuttua/kaatua -> tyhjä vastaus. Nyt käytetään
kevyttä SELECT type,company_id kyselyä oikeustarkistukseen
ja suoraa UPDATE-lausetta. Koko endpoint try-catchissä.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-11 14:11:20 +02:00
parent ea2fdcb316
commit 77aa809439

33
api.php
View File

@@ -2290,35 +2290,34 @@ switch ($action) {
requireAuth();
$companyId = requireCompany();
if ($method !== 'POST') { echo json_encode(['error' => 'POST required']); break; }
$rawInput = file_get_contents('php://input');
$input = json_decode($rawInput, true);
if (!$input) { echo json_encode(['error' => 'Invalid JSON input', 'raw' => substr($rawInput, 0, 200)]); break; }
try {
$input = json_decode(file_get_contents('php://input'), true);
$id = $input['id'] ?? '';
$status = $input['status'] ?? '';
$todo = dbLoadTodo($id);
if (!$todo || $todo['company_id'] !== $companyId) {
if (!$id || !$status) { echo json_encode(['error' => 'id ja status vaaditaan']); break; }
// Tarkista oikeudet kevyellä querylla (ei dbLoadTodo joka voi kaatua)
$rows = _dbFetchAll("SELECT type, company_id FROM todos WHERE id = ?", [$id]);
if (empty($rows) || $rows[0]['company_id'] !== $companyId) {
http_response_code(404);
echo json_encode(['error' => 'Tehtävää ei löytynyt']);
break;
}
// Feature request status: vain admin
if ($todo['type'] === 'feature_request' && !isAdmin()) {
$type = $rows[0]['type'];
if ($type === 'feature_request' && !isAdmin()) {
http_response_code(403);
echo json_encode(['error' => 'Vain admin voi muuttaa ehdotuksen statusta']);
break;
}
// Task status: vain admin
if ($todo['type'] === 'task' && !isAdmin()) {
if ($type === 'task' && !isAdmin()) {
http_response_code(403);
echo json_encode(['error' => 'Vain admin voi muuttaa tehtävän statusta']);
break;
}
try {
_dbExecute("UPDATE todos SET status = ?, muokattu = NOW(), muokkaaja = ? WHERE id = ?", [$status, currentUser(), $id]);
echo json_encode(['success' => true]);
} catch (\Throwable $e) {
http_response_code(500);
echo json_encode(['error' => 'Tallennus epäonnistui: ' . $e->getMessage()]);
echo json_encode(['error' => 'Virhe: ' . $e->getMessage()]);
}
break;
@@ -2326,22 +2325,22 @@ switch ($action) {
requireAuth();
requireAdmin();
$companyId = requireCompany();
if ($method !== 'POST') break;
if ($method !== 'POST') { echo json_encode(['error' => 'POST required']); break; }
try {
$input = json_decode(file_get_contents('php://input'), true);
$id = $input['id'] ?? '';
$todo = dbLoadTodo($id);
if (!$todo || $todo['company_id'] !== $companyId) {
$assignedTo = $input['assigned_to'] ?? '';
$rows = _dbFetchAll("SELECT company_id FROM todos WHERE id = ?", [$id]);
if (empty($rows) || $rows[0]['company_id'] !== $companyId) {
http_response_code(404);
echo json_encode(['error' => 'Tehtävää ei löytynyt']);
break;
}
try {
$assignedTo = $input['assigned_to'] ?? '';
_dbExecute("UPDATE todos SET assigned_to = ?, muokattu = NOW(), muokkaaja = ? WHERE id = ?", [$assignedTo, currentUser(), $id]);
echo json_encode(['success' => true]);
} catch (\Throwable $e) {
http_response_code(500);
echo json_encode(['error' => 'Tallennus epäonnistui: ' . $e->getMessage()]);
echo json_encode(['error' => 'Virhe: ' . $e->getMessage()]);
}
break;