Add public availability API and settings panel
Public saatavuus endpoint with API key + CORS protection for cuitunet.fi website integration. Admin settings tab for API key management and testing. Includes standalone widget page. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,6 +3,7 @@ data/users.json
|
|||||||
data/changelog.json
|
data/changelog.json
|
||||||
data/archive.json
|
data/archive.json
|
||||||
data/leads.json
|
data/leads.json
|
||||||
|
data/config.json
|
||||||
data/reset_tokens.json
|
data/reset_tokens.json
|
||||||
data/login_attempts.json
|
data/login_attempts.json
|
||||||
data/backups/
|
data/backups/
|
||||||
|
|||||||
137
api.php
137
api.php
@@ -17,6 +17,7 @@ define('ARCHIVE_FILE', DATA_DIR . '/archive.json');
|
|||||||
define('LEADS_FILE', DATA_DIR . '/leads.json');
|
define('LEADS_FILE', DATA_DIR . '/leads.json');
|
||||||
define('TOKENS_FILE', DATA_DIR . '/reset_tokens.json');
|
define('TOKENS_FILE', DATA_DIR . '/reset_tokens.json');
|
||||||
define('RATE_FILE', DATA_DIR . '/login_attempts.json');
|
define('RATE_FILE', DATA_DIR . '/login_attempts.json');
|
||||||
|
define('CONFIG_FILE', DATA_DIR . '/config.json');
|
||||||
define('SITE_URL', 'https://intra.cuitunet.fi');
|
define('SITE_URL', 'https://intra.cuitunet.fi');
|
||||||
|
|
||||||
// Sähköpostiasetukset
|
// Sähköpostiasetukset
|
||||||
@@ -88,6 +89,23 @@ function getClientIp(): string {
|
|||||||
return $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
|
return $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ==================== CONFIG ====================
|
||||||
|
|
||||||
|
function loadConfig(): array {
|
||||||
|
if (!file_exists(CONFIG_FILE)) return [];
|
||||||
|
return json_decode(file_get_contents(CONFIG_FILE), true) ?: [];
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveConfig(array $config): void {
|
||||||
|
file_put_contents(CONFIG_FILE, json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizeAddress(string $addr): string {
|
||||||
|
$addr = mb_strtolower(trim($addr));
|
||||||
|
$addr = preg_replace('/\s+/', ' ', $addr);
|
||||||
|
return $addr;
|
||||||
|
}
|
||||||
|
|
||||||
// ==================== EMAIL ====================
|
// ==================== EMAIL ====================
|
||||||
|
|
||||||
function sendMail(string $to, string $subject, string $htmlBody): bool {
|
function sendMail(string $to, string $subject, string $htmlBody): bool {
|
||||||
@@ -247,6 +265,125 @@ function parseLiittymat(array $input): array {
|
|||||||
|
|
||||||
switch ($action) {
|
switch ($action) {
|
||||||
|
|
||||||
|
// ---------- SAATAVUUS (julkinen API) ----------
|
||||||
|
case 'saatavuus':
|
||||||
|
// CORS - salli cuitunet.fi
|
||||||
|
$config = loadConfig();
|
||||||
|
$allowedOrigins = $config['cors_origins'] ?? ['https://cuitunet.fi', 'https://www.cuitunet.fi'];
|
||||||
|
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
|
||||||
|
if (in_array($origin, $allowedOrigins)) {
|
||||||
|
header("Access-Control-Allow-Origin: $origin");
|
||||||
|
header('Access-Control-Allow-Methods: GET, OPTIONS');
|
||||||
|
header('Access-Control-Allow-Headers: Content-Type, X-Api-Key');
|
||||||
|
}
|
||||||
|
if ($method === 'OPTIONS') { http_response_code(204); break; }
|
||||||
|
|
||||||
|
// API-avain tarkistus
|
||||||
|
$apiKey = $config['api_key'] ?? '';
|
||||||
|
$providedKey = $_GET['key'] ?? ($_SERVER['HTTP_X_API_KEY'] ?? '');
|
||||||
|
if (empty($apiKey) || $providedKey !== $apiKey) {
|
||||||
|
http_response_code(403);
|
||||||
|
echo json_encode(['error' => 'Virheellinen API-avain']);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$query = normalizeAddress($_GET['osoite'] ?? '');
|
||||||
|
$postinumero = trim($_GET['postinumero'] ?? '');
|
||||||
|
|
||||||
|
if (empty($query) && empty($postinumero)) {
|
||||||
|
http_response_code(400);
|
||||||
|
echo json_encode(['error' => 'Anna osoite tai postinumero']);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$customers = loadCustomers();
|
||||||
|
$matches = [];
|
||||||
|
foreach ($customers as $c) {
|
||||||
|
foreach ($c['liittymat'] ?? [] as $l) {
|
||||||
|
$addr = normalizeAddress($l['asennusosoite'] ?? '');
|
||||||
|
$zip = trim($l['postinumero'] ?? '');
|
||||||
|
$city = mb_strtolower(trim($l['kaupunki'] ?? ''));
|
||||||
|
$hit = false;
|
||||||
|
|
||||||
|
// Postinumero-haku
|
||||||
|
if (!empty($postinumero) && $zip === $postinumero) {
|
||||||
|
$hit = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Osoitehaku (sisältää haun)
|
||||||
|
if (!empty($query) && !empty($addr)) {
|
||||||
|
if (str_contains($addr, $query) || str_contains($query, $addr)) {
|
||||||
|
$hit = true;
|
||||||
|
}
|
||||||
|
// Kadunnimi-match (ilman numeroa)
|
||||||
|
$queryStreet = preg_replace('/\d+.*$/', '', $query);
|
||||||
|
$addrStreet = preg_replace('/\d+.*$/', '', $addr);
|
||||||
|
if (!empty(trim($queryStreet)) && !empty(trim($addrStreet)) && str_contains(trim($addrStreet), trim($queryStreet))) {
|
||||||
|
$hit = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($hit) {
|
||||||
|
// Palauta VAIN osoitetieto ja nopeus - ei asiakastietoja
|
||||||
|
$matches[] = [
|
||||||
|
'osoite' => $l['asennusosoite'] ?? '',
|
||||||
|
'postinumero' => $zip,
|
||||||
|
'kaupunki' => $l['kaupunki'] ?? '',
|
||||||
|
'nopeus' => $l['liittymanopeus'] ?? '',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Poista duplikaatit (sama osoite eri asiakkailla)
|
||||||
|
$unique = [];
|
||||||
|
$seen = [];
|
||||||
|
foreach ($matches as $m) {
|
||||||
|
$key = normalizeAddress($m['osoite'] . $m['postinumero']);
|
||||||
|
if (!isset($seen[$key])) {
|
||||||
|
$unique[] = $m;
|
||||||
|
$seen[$key] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
echo json_encode([
|
||||||
|
'saatavilla' => count($unique) > 0,
|
||||||
|
'kohteet' => $unique,
|
||||||
|
'maara' => count($unique),
|
||||||
|
]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
// ---------- CONFIG (admin) ----------
|
||||||
|
case 'config':
|
||||||
|
requireAdmin();
|
||||||
|
echo json_encode(loadConfig());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'config_update':
|
||||||
|
requireAdmin();
|
||||||
|
if ($method !== 'POST') break;
|
||||||
|
$input = json_decode(file_get_contents('php://input'), true);
|
||||||
|
$config = loadConfig();
|
||||||
|
if (isset($input['api_key'])) $config['api_key'] = trim($input['api_key']);
|
||||||
|
if (isset($input['cors_origins'])) {
|
||||||
|
$origins = array_filter(array_map('trim', explode("\n", $input['cors_origins'])));
|
||||||
|
$config['cors_origins'] = array_values($origins);
|
||||||
|
}
|
||||||
|
saveConfig($config);
|
||||||
|
addLog('config_update', '', '', 'Päivitti asetukset');
|
||||||
|
echo json_encode($config);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'generate_api_key':
|
||||||
|
requireAdmin();
|
||||||
|
if ($method !== 'POST') break;
|
||||||
|
$config = loadConfig();
|
||||||
|
$config['api_key'] = bin2hex(random_bytes(16));
|
||||||
|
saveConfig($config);
|
||||||
|
addLog('config_update', '', '', 'Generoi uuden API-avaimen');
|
||||||
|
echo json_encode($config);
|
||||||
|
break;
|
||||||
|
|
||||||
// ---------- CAPTCHA ----------
|
// ---------- CAPTCHA ----------
|
||||||
case 'captcha':
|
case 'captcha':
|
||||||
$a = rand(1, 20);
|
$a = rand(1, 20);
|
||||||
|
|||||||
46
index.html
46
index.html
@@ -76,6 +76,7 @@
|
|||||||
<button class="tab" data-tab="archive">Arkisto</button>
|
<button class="tab" data-tab="archive">Arkisto</button>
|
||||||
<button class="tab" data-tab="changelog">Muutosloki</button>
|
<button class="tab" data-tab="changelog">Muutosloki</button>
|
||||||
<button class="tab" data-tab="users" id="tab-users" style="display:none">Käyttäjät</button>
|
<button class="tab" data-tab="users" id="tab-users" style="display:none">Käyttäjät</button>
|
||||||
|
<button class="tab" data-tab="settings" id="tab-settings" style="display:none">Asetukset</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Tab: Asiakkaat -->
|
<!-- Tab: Asiakkaat -->
|
||||||
@@ -258,6 +259,51 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Tab: Asetukset (vain admin) -->
|
||||||
|
<div class="tab-content" id="tab-content-settings">
|
||||||
|
<div class="main-container">
|
||||||
|
<div class="table-card" style="padding:1.5rem;">
|
||||||
|
<h3 style="color:#0f3460;margin-bottom:1rem;border-bottom:2px solid #f0f2f5;padding-bottom:0.5rem;">Saatavuus-API</h3>
|
||||||
|
<p style="color:#666;font-size:0.85rem;margin-bottom:1rem;">Julkinen API jolla cuitunet.fi voi tarkistaa kuituverkon saatavuuden osoitteessa. Palauttaa vain osoite + nopeus - ei asiakastietoja.</p>
|
||||||
|
<div class="form-grid" style="max-width:600px;">
|
||||||
|
<div class="form-group full-width">
|
||||||
|
<label>API-avain</label>
|
||||||
|
<div style="display:flex;gap:0.5rem;">
|
||||||
|
<input type="text" id="settings-api-key" readonly style="flex:1;font-family:monospace;background:#f8f9fb;">
|
||||||
|
<button class="btn-primary" id="btn-generate-key" style="white-space:nowrap;">Generoi uusi</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="form-group full-width">
|
||||||
|
<label>Sallitut originit (CORS) - yksi per rivi</label>
|
||||||
|
<textarea id="settings-cors" rows="3" style="font-family:monospace;font-size:0.85rem;" placeholder="https://cuitunet.fi https://www.cuitunet.fi"></textarea>
|
||||||
|
</div>
|
||||||
|
<div class="form-group full-width">
|
||||||
|
<button class="btn-primary" id="btn-save-settings">Tallenna asetukset</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<h3 style="color:#0f3460;margin:1.5rem 0 1rem;border-bottom:2px solid #f0f2f5;padding-bottom:0.5rem;">API-ohjeet</h3>
|
||||||
|
<div style="background:#f8f9fb;padding:1rem;border-radius:8px;font-size:0.85rem;font-family:monospace;overflow-x:auto;">
|
||||||
|
<div style="margin-bottom:0.75rem;"><strong>Endpoint:</strong><br>GET https://intra.cuitunet.fi/api.php?action=saatavuus</div>
|
||||||
|
<div style="margin-bottom:0.75rem;"><strong>Parametrit:</strong><br>
|
||||||
|
• <code>key</code> = API-avain (pakollinen)<br>
|
||||||
|
• <code>osoite</code> = Haettava osoite (esim. "Kauppakatu 5")<br>
|
||||||
|
• <code>postinumero</code> = Postinumero (esim. "20100")<br>
|
||||||
|
Anna vähintään toinen: osoite tai postinumero.</div>
|
||||||
|
<div style="margin-bottom:0.75rem;"><strong>Esimerkki:</strong><br>
|
||||||
|
<code id="api-example-url">api.php?action=saatavuus&key=AVAIN&osoite=Kauppakatu+5</code></div>
|
||||||
|
<div><strong>Vastaus:</strong><br>
|
||||||
|
<code>{"saatavilla":true,"kohteet":[{"osoite":"...","postinumero":"...","kaupunki":"...","nopeus":"..."}],"maara":1}</code></div>
|
||||||
|
</div>
|
||||||
|
<h3 style="color:#0f3460;margin:1.5rem 0 1rem;border-bottom:2px solid #f0f2f5;padding-bottom:0.5rem;">Testaa API</h3>
|
||||||
|
<div style="display:flex;gap:0.5rem;max-width:500px;">
|
||||||
|
<input type="text" id="test-api-address" placeholder="Osoite tai postinumero" style="flex:1;">
|
||||||
|
<button class="btn-primary" id="btn-test-api">Testaa</button>
|
||||||
|
</div>
|
||||||
|
<pre id="test-api-result" style="margin-top:0.75rem;background:#f8f9fb;padding:1rem;border-radius:8px;font-size:0.85rem;display:none;overflow-x:auto;"></pre>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<footer>
|
<footer>
|
||||||
<p>CuituNet Intra — Asiakashallintajärjestelmä</p>
|
<p>CuituNet Intra — Asiakashallintajärjestelmä</p>
|
||||||
</footer>
|
</footer>
|
||||||
|
|||||||
108
saatavuus-widget.html
Normal file
108
saatavuus-widget.html
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
<!--
|
||||||
|
CuituNet Saatavuuswidget
|
||||||
|
|
||||||
|
Upota cuitunet.fi-sivulle kopioimalla tämän tiedoston sisältö haluamaasi paikkaan,
|
||||||
|
tai lataa script-tagi:
|
||||||
|
|
||||||
|
<div id="cuitunet-saatavuus"></div>
|
||||||
|
<script src="https://intra.cuitunet.fi/saatavuus-widget.js" data-api-key="SINUN_API_AVAIN"></script>
|
||||||
|
|
||||||
|
Tämä on itsenäinen esimerkkisivu testausta varten.
|
||||||
|
-->
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="fi">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Kuituverkon saatavuus - CuituNet</title>
|
||||||
|
<style>
|
||||||
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||||
|
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #f0f2f5; min-height: 100vh; display: flex; align-items: center; justify-content: center; }
|
||||||
|
.saatavuus-container { background: #fff; border-radius: 16px; box-shadow: 0 4px 24px rgba(0,0,0,0.1); padding: 2.5rem; max-width: 500px; width: 100%; margin: 1rem; }
|
||||||
|
.saatavuus-container h2 { color: #0f3460; font-size: 1.4rem; margin-bottom: 0.5rem; }
|
||||||
|
.saatavuus-container p { color: #666; font-size: 0.9rem; margin-bottom: 1.5rem; }
|
||||||
|
.saatavuus-form { display: flex; gap: 0.5rem; }
|
||||||
|
.saatavuus-form input { flex: 1; padding: 12px 16px; border: 2px solid #e0e0e0; border-radius: 10px; font-size: 1rem; transition: border-color 0.2s; }
|
||||||
|
.saatavuus-form input:focus { outline: none; border-color: #0f3460; }
|
||||||
|
.saatavuus-form button { padding: 12px 24px; background: #0f3460; color: #fff; border: none; border-radius: 10px; font-size: 1rem; font-weight: 600; cursor: pointer; transition: background 0.2s; white-space: nowrap; }
|
||||||
|
.saatavuus-form button:hover { background: #16213e; }
|
||||||
|
.saatavuus-result { margin-top: 1.25rem; padding: 1rem; border-radius: 10px; display: none; }
|
||||||
|
.saatavuus-result.ok { background: #d4edda; border: 1px solid #c3e6cb; color: #155724; }
|
||||||
|
.saatavuus-result.nok { background: #f8d7da; border: 1px solid #f5c6cb; color: #721c24; }
|
||||||
|
.saatavuus-result.loading { background: #f8f9fb; border: 1px solid #e8ebf0; color: #666; }
|
||||||
|
.saatavuus-result h3 { font-size: 1rem; margin-bottom: 0.25rem; }
|
||||||
|
.saatavuus-result p { margin-bottom: 0.25rem; color: inherit; font-size: 0.9rem; }
|
||||||
|
.saatavuus-kohde { background: rgba(255,255,255,0.5); padding: 0.5rem 0.75rem; border-radius: 8px; margin-top: 0.5rem; font-size: 0.9rem; }
|
||||||
|
.saatavuus-kohde strong { color: #0f3460; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="saatavuus-container">
|
||||||
|
<h2>Tarkista kuituverkon saatavuus</h2>
|
||||||
|
<p>Syötä osoitteesi tai postinumerosi ja tarkista onko kuituliittymä saatavilla.</p>
|
||||||
|
<div class="saatavuus-form">
|
||||||
|
<input type="text" id="saatavuus-input" placeholder="Osoite tai postinumero" autofocus>
|
||||||
|
<button id="saatavuus-btn">Tarkista</button>
|
||||||
|
</div>
|
||||||
|
<div id="saatavuus-result" class="saatavuus-result"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
(function() {
|
||||||
|
// MUUTA NÄMÄ
|
||||||
|
const API_URL = 'https://intra.cuitunet.fi/api.php';
|
||||||
|
const API_KEY = 'VAIHDA_TÄHÄN_API_AVAIMESI';
|
||||||
|
|
||||||
|
const input = document.getElementById('saatavuus-input');
|
||||||
|
const btn = document.getElementById('saatavuus-btn');
|
||||||
|
const result = document.getElementById('saatavuus-result');
|
||||||
|
|
||||||
|
async function tarkista() {
|
||||||
|
const q = input.value.trim();
|
||||||
|
if (!q) return;
|
||||||
|
|
||||||
|
result.className = 'saatavuus-result loading';
|
||||||
|
result.style.display = 'block';
|
||||||
|
result.innerHTML = '<p>Tarkistetaan saatavuutta...</p>';
|
||||||
|
|
||||||
|
try {
|
||||||
|
const isZip = /^\d{5}$/.test(q);
|
||||||
|
const param = isZip ? 'postinumero=' + encodeURIComponent(q) : 'osoite=' + encodeURIComponent(q);
|
||||||
|
const res = await fetch(API_URL + '?action=saatavuus&key=' + encodeURIComponent(API_KEY) + '&' + param);
|
||||||
|
const data = await res.json();
|
||||||
|
|
||||||
|
if (data.error) {
|
||||||
|
result.className = 'saatavuus-result nok';
|
||||||
|
result.innerHTML = '<h3>Virhe</h3><p>' + data.error + '</p>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.saatavilla) {
|
||||||
|
let html = '<h3>Kuituliittymä on saatavilla!</h3>';
|
||||||
|
html += '<p>Löysimme ' + data.maara + ' kohde' + (data.maara > 1 ? 'tta' : 'n') + ' alueellasi:</p>';
|
||||||
|
data.kohteet.forEach(function(k) {
|
||||||
|
html += '<div class="saatavuus-kohde">';
|
||||||
|
html += '<strong>' + k.osoite + '</strong>';
|
||||||
|
if (k.postinumero) html += ', ' + k.postinumero;
|
||||||
|
if (k.kaupunki) html += ' ' + k.kaupunki;
|
||||||
|
if (k.nopeus) html += ' — ' + k.nopeus;
|
||||||
|
html += '</div>';
|
||||||
|
});
|
||||||
|
result.className = 'saatavuus-result ok';
|
||||||
|
result.innerHTML = html;
|
||||||
|
} else {
|
||||||
|
result.className = 'saatavuus-result nok';
|
||||||
|
result.innerHTML = '<h3>Ei saatavuutta vielä</h3><p>Kuituliittymää ei löytynyt antamallasi osoitteella. Kuituverkkomme laajenee jatkuvasti!</p>';
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
result.className = 'saatavuus-result nok';
|
||||||
|
result.innerHTML = '<h3>Yhteysvirhe</h3><p>Saatavuuskyselyä ei voitu suorittaa. Yritä hetken kuluttua uudelleen.</p>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
btn.addEventListener('click', tarkista);
|
||||||
|
input.addEventListener('keydown', function(e) { if (e.key === 'Enter') tarkista(); });
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
49
script.js
49
script.js
@@ -169,6 +169,7 @@ async function showDashboard() {
|
|||||||
document.getElementById('user-info').textContent = currentUser.nimi || currentUser.username;
|
document.getElementById('user-info').textContent = currentUser.nimi || currentUser.username;
|
||||||
// Näytä Käyttäjät-tab vain adminille
|
// Näytä Käyttäjät-tab vain adminille
|
||||||
document.getElementById('tab-users').style.display = currentUser.role === 'admin' ? '' : 'none';
|
document.getElementById('tab-users').style.display = currentUser.role === 'admin' ? '' : 'none';
|
||||||
|
document.getElementById('tab-settings').style.display = currentUser.role === 'admin' ? '' : 'none';
|
||||||
await loadCustomers();
|
await loadCustomers();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,6 +187,7 @@ document.querySelectorAll('.tab').forEach(tab => {
|
|||||||
if (target === 'archive') loadArchive();
|
if (target === 'archive') loadArchive();
|
||||||
if (target === 'changelog') loadChangelog();
|
if (target === 'changelog') loadChangelog();
|
||||||
if (target === 'users') loadUsers();
|
if (target === 'users') loadUsers();
|
||||||
|
if (target === 'settings') loadSettings();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -818,6 +820,7 @@ const actionLabels = {
|
|||||||
lead_update: 'Muokkasi liidiä',
|
lead_update: 'Muokkasi liidiä',
|
||||||
lead_delete: 'Poisti liidin',
|
lead_delete: 'Poisti liidin',
|
||||||
lead_to_customer: 'Muutti liidin asiakkaaksi',
|
lead_to_customer: 'Muutti liidin asiakkaaksi',
|
||||||
|
config_update: 'Päivitti asetukset',
|
||||||
};
|
};
|
||||||
|
|
||||||
async function loadChangelog() {
|
async function loadChangelog() {
|
||||||
@@ -917,6 +920,52 @@ document.getElementById('user-form').addEventListener('submit', async (e) => {
|
|||||||
} catch (e) { alert(e.message); }
|
} catch (e) { alert(e.message); }
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ==================== SETTINGS ====================
|
||||||
|
|
||||||
|
async function loadSettings() {
|
||||||
|
try {
|
||||||
|
const config = await apiCall('config');
|
||||||
|
document.getElementById('settings-api-key').value = config.api_key || '';
|
||||||
|
document.getElementById('settings-cors').value = (config.cors_origins || ['https://cuitunet.fi', 'https://www.cuitunet.fi']).join('\n');
|
||||||
|
const key = config.api_key || 'AVAIN';
|
||||||
|
document.getElementById('api-example-url').textContent = `api.php?action=saatavuus&key=${key}&osoite=Kauppakatu+5`;
|
||||||
|
} catch (e) { console.error(e); }
|
||||||
|
}
|
||||||
|
|
||||||
|
document.getElementById('btn-generate-key').addEventListener('click', async () => {
|
||||||
|
try {
|
||||||
|
const config = await apiCall('generate_api_key', 'POST');
|
||||||
|
document.getElementById('settings-api-key').value = config.api_key || '';
|
||||||
|
document.getElementById('api-example-url').textContent = `api.php?action=saatavuus&key=${config.api_key}&osoite=Kauppakatu+5`;
|
||||||
|
} catch (e) { alert(e.message); }
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById('btn-save-settings').addEventListener('click', async () => {
|
||||||
|
try {
|
||||||
|
const config = await apiCall('config_update', 'POST', {
|
||||||
|
api_key: document.getElementById('settings-api-key').value,
|
||||||
|
cors_origins: document.getElementById('settings-cors').value,
|
||||||
|
});
|
||||||
|
alert('Asetukset tallennettu!');
|
||||||
|
} catch (e) { alert(e.message); }
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById('btn-test-api').addEventListener('click', async () => {
|
||||||
|
const address = document.getElementById('test-api-address').value.trim();
|
||||||
|
const apiKey = document.getElementById('settings-api-key').value;
|
||||||
|
if (!address) { alert('Anna osoite tai postinumero'); return; }
|
||||||
|
const result = document.getElementById('test-api-result');
|
||||||
|
result.style.display = 'block';
|
||||||
|
result.textContent = 'Haetaan...';
|
||||||
|
try {
|
||||||
|
const isZip = /^\d{5}$/.test(address);
|
||||||
|
const param = isZip ? `postinumero=${encodeURIComponent(address)}` : `osoite=${encodeURIComponent(address)}`;
|
||||||
|
const res = await fetch(`${API}?action=saatavuus&key=${encodeURIComponent(apiKey)}&${param}`);
|
||||||
|
const data = await res.json();
|
||||||
|
result.textContent = JSON.stringify(data, null, 2);
|
||||||
|
} catch (e) { result.textContent = 'Virhe: ' + e.message; }
|
||||||
|
});
|
||||||
|
|
||||||
// ==================== MODALS ====================
|
// ==================== MODALS ====================
|
||||||
|
|
||||||
customerModal.addEventListener('click', (e) => { if (e.target === customerModal) customerModal.style.display = 'none'; });
|
customerModal.addEventListener('click', (e) => { if (e.target === customerModal) customerModal.style.display = 'none'; });
|
||||||
|
|||||||
Reference in New Issue
Block a user