diff --git a/api.php b/api.php index 6c40b6b..0a07f68 100644 --- a/api.php +++ b/api.php @@ -1240,16 +1240,24 @@ switch ($action) { // ---------- SAATAVUUSKYSELYT ---------- case 'availability_queries': requireAuth(); - $companyId = requireCompanyOrParam(); $limit = (int)($_GET['limit'] ?? 100); $offset = (int)($_GET['offset'] ?? 0); if ($limit > 500) $limit = 500; - $total = (int)_dbFetchScalar("SELECT COUNT(*) FROM availability_queries WHERE company_id = ?", [$companyId]); + // Näytä kaikkien käyttäjän yritysten kyselyt + $userCompanyIds = $_SESSION['companies'] ?? []; + if (empty($userCompanyIds)) { + echo json_encode(['total' => 0, 'queries' => []]); + break; + } + $placeholders = implode(',', array_fill(0, count($userCompanyIds), '?')); + $total = (int)_dbFetchScalar("SELECT COUNT(*) FROM availability_queries WHERE company_id IN ($placeholders)", $userCompanyIds); + $params = array_merge($userCompanyIds, [$limit, $offset]); $rows = _dbFetchAll( - "SELECT id, osoite, postinumero, kaupunki, saatavilla, ip_address, referer, created_at - FROM availability_queries WHERE company_id = ? ORDER BY created_at DESC LIMIT ? OFFSET ?", - [$companyId, $limit, $offset] + "SELECT aq.id, aq.company_id, c.nimi as company_nimi, aq.osoite, aq.postinumero, aq.kaupunki, aq.saatavilla, aq.ip_address, aq.referer, aq.created_at + FROM availability_queries aq LEFT JOIN companies c ON c.id = aq.company_id + WHERE aq.company_id IN ($placeholders) ORDER BY aq.created_at DESC LIMIT ? OFFSET ?", + $params ); echo json_encode(['total' => $total, 'queries' => $rows]); break; diff --git a/index.html b/index.html index d2621f1..8da7295 100644 --- a/index.html +++ b/index.html @@ -4,7 +4,7 @@