Korjaa saatavuuskyselyt: näytä kaikkien yritysten kyselyt

Endpoint näytti vain aktiivisen yrityksen kyselyt, mutta kyselyt
tallennetaan API-avaimen yrityksen alle. Nyt näytetään kaikkien
käyttäjän yritysten kyselyt + yrityssarake taulukkoon.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-13 02:23:40 +02:00
parent a38c5f4808
commit 64dc02f432
3 changed files with 18 additions and 8 deletions

18
api.php
View File

@@ -1240,16 +1240,24 @@ switch ($action) {
// ---------- SAATAVUUSKYSELYT ---------- // ---------- SAATAVUUSKYSELYT ----------
case 'availability_queries': case 'availability_queries':
requireAuth(); requireAuth();
$companyId = requireCompanyOrParam();
$limit = (int)($_GET['limit'] ?? 100); $limit = (int)($_GET['limit'] ?? 100);
$offset = (int)($_GET['offset'] ?? 0); $offset = (int)($_GET['offset'] ?? 0);
if ($limit > 500) $limit = 500; 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( $rows = _dbFetchAll(
"SELECT id, osoite, postinumero, kaupunki, saatavilla, ip_address, referer, created_at "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 WHERE company_id = ? ORDER BY created_at DESC LIMIT ? OFFSET ?", FROM availability_queries aq LEFT JOIN companies c ON c.id = aq.company_id
[$companyId, $limit, $offset] WHERE aq.company_id IN ($placeholders) ORDER BY aq.created_at DESC LIMIT ? OFFSET ?",
$params
); );
echo json_encode(['total' => $total, 'queries' => $rows]); echo json_encode(['total' => $total, 'queries' => $rows]);
break; break;

View File

@@ -4,7 +4,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Noxus HUB</title> <title>Noxus HUB</title>
<link rel="stylesheet" href="style.css?v=20260313j"> <link rel="stylesheet" href="style.css?v=20260313k">
</head> </head>
<body> <body>
<!-- Login --> <!-- Login -->
@@ -210,6 +210,7 @@
<th>Kaupunki</th> <th>Kaupunki</th>
<th>Tulos</th> <th>Tulos</th>
<th>Lähde</th> <th>Lähde</th>
<th>Yritys</th>
</tr> </tr>
</thead> </thead>
<tbody></tbody> <tbody></tbody>
@@ -2262,6 +2263,6 @@
</div> </div>
</div> </div>
<script src="script.js?v=20260313j"></script> <script src="script.js?v=20260313k"></script>
</body> </body>
</html> </html>

View File

@@ -3520,7 +3520,7 @@ async function loadAvailabilityQueries(page = 0) {
countEl.textContent = `Yhteensä ${data.total} kyselyä`; countEl.textContent = `Yhteensä ${data.total} kyselyä`;
if (data.queries.length === 0) { if (data.queries.length === 0) {
tbody.innerHTML = '<tr><td colspan="6" style="text-align:center;color:#888;padding:2rem;">Ei vielä kyselyjä</td></tr>'; tbody.innerHTML = '<tr><td colspan="7" style="text-align:center;color:#888;padding:2rem;">Ei vielä kyselyjä</td></tr>';
} else { } else {
tbody.innerHTML = data.queries.map(q => { tbody.innerHTML = data.queries.map(q => {
const date = q.created_at ? q.created_at.replace('T', ' ').substring(0, 16) : ''; const date = q.created_at ? q.created_at.replace('T', ' ').substring(0, 16) : '';
@@ -3539,6 +3539,7 @@ async function loadAvailabilityQueries(page = 0) {
<td>${esc(q.kaupunki)}</td> <td>${esc(q.kaupunki)}</td>
<td>${badge}</td> <td>${badge}</td>
<td style="font-size:0.8rem;color:#888;">${esc(source)}</td> <td style="font-size:0.8rem;color:#888;">${esc(source)}</td>
<td style="font-size:0.8rem;color:#888;">${esc(q.company_nimi || q.company_id || '')}</td>
</tr>`; </tr>`;
}).join(''); }).join('');
} }