Add module visibility fix and configurable postal codes for saatavuus API

- Add 'hallinta' to ALL_MODULES so it appears in company settings
- Change fallback from ALL_MODULES to DEFAULT_MODULES (new modules not enabled by default)
- Hallinta tab requires both module enabled + superadmin role
- Add per-company configurable postal codes for "todennäköinen" availability
- Saatavuus API returns true/todennäköinen/false based on customer data + postal codes
- Show "Todennäköinen" badge in availability queries list

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-13 20:10:21 +02:00
parent 6ea62b075f
commit 50f34ac37b
3 changed files with 52 additions and 11 deletions

31
api.php
View File

@@ -1443,6 +1443,19 @@ switch ($action) {
}
}
// Todennäköinen saatavuus: postinumero saatavuuslistalla
$probable = false;
if (!$found) {
$saatInteg = dbGetIntegration($matchedCompany['id'], 'saatavuus_api');
$probablePostcodes = ($saatInteg && $saatInteg['config']) ? ($saatInteg['config']['probable_postcodes'] ?? []) : [];
if (in_array($queryPostinumero, $probablePostcodes)) {
$probable = true;
}
}
// Tulos: true, "todennäköinen", tai false
$result = $found ? true : ($probable ? 'todennäköinen' : false);
// Tallenna kysely tietokantaan (ohita duplikaatit: sama osoite+postinumero+kaupunki+yritys)
try {
$rawOsoite = $_GET['osoite'] ?? '';
@@ -1467,6 +1480,8 @@ switch ($action) {
$org = $ipData['org'] ?? $ipData['isp'] ?? '';
}
} catch (\Throwable $e) { /* IP-haku ei saa kaataa API:a */ }
// saatavilla: 1=kyllä, 2=todennäköinen, 0=ei
$saatavillaDb = $found ? 1 : ($probable ? 2 : 0);
_dbExecute(
"INSERT INTO availability_queries (company_id, osoite, postinumero, kaupunki, saatavilla, ip_address, hostname, org, user_agent, referer, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
@@ -1475,7 +1490,7 @@ switch ($action) {
$rawOsoite,
$rawPostinumero,
$rawKaupunki,
$found ? 1 : 0,
$saatavillaDb,
$ip,
$hostname,
$org,
@@ -1487,7 +1502,7 @@ switch ($action) {
}
} catch (\Throwable $e) { /* logitus ei saa kaataa API-vastausta */ }
echo json_encode(['saatavilla' => $found]);
echo json_encode(['saatavilla' => $result]);
break;
// ---------- SAATAVUUSKYSELYT ----------
@@ -1523,9 +1538,12 @@ switch ($action) {
// Telegram: yrityskohtainen config (bot_token + chat_id), globaali fallback
$teleInteg = dbGetIntegration($companyId, 'telegram');
$teleConf = ($teleInteg && $teleInteg['config']) ? $teleInteg['config'] : [];
$saatavuusInteg = dbGetIntegration($companyId, 'saatavuus_api');
$saatavuusConf = ($saatavuusInteg && $saatavuusInteg['config']) ? $saatavuusInteg['config'] : [];
echo json_encode([
'api_key' => dbGetCompanyApiKey($companyId),
'cors_origins' => dbGetCompanyCorsOrigins($companyId),
'probable_postcodes' => $saatavuusConf['probable_postcodes'] ?? [],
'telegram_bot_token' => $teleConf['bot_token'] ?? ($globalConf['telegram_bot_token'] ?? ''),
'telegram_chat_id' => $teleConf['chat_id'] ?? '',
]);
@@ -1556,6 +1574,15 @@ switch ($action) {
}
dbSaveIntegration($companyId, 'telegram', $teleEnabled, $teleConfig);
}
// Todennäköinen saatavuus -postinumerot
if (isset($input['probable_postcodes'])) {
$postcodes = array_filter(array_map('trim', explode("\n", $input['probable_postcodes'])));
$saatInteg = dbGetIntegration($companyId, 'saatavuus_api');
$saatConfig = ($saatInteg && $saatInteg['config']) ? $saatInteg['config'] : [];
$saatEnabled = $saatInteg ? $saatInteg['enabled'] : true;
$saatConfig['probable_postcodes'] = array_values($postcodes);
dbSaveIntegration($companyId, 'saatavuus_api', $saatEnabled, $saatConfig);
}
dbAddLog($companyId, currentUser(), 'config_update', '', '', 'Päivitti asetuksia');
echo json_encode([
'api_key' => dbGetCompanyApiKey($companyId),