34853-vm/api/check_availability.php
2025-10-11 05:03:50 +00:00

55 lines
1.6 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../db/config.php';
$response = [
'success' => false,
'message' => 'An error occurred.',
'serviceable' => false
];
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
$response['message'] = 'Invalid request method.';
echo json_encode($response);
exit;
}
$input = json_decode(file_get_contents('php://input'), true);
$address = $input['address'] ?? '';
if (empty($address)) {
$response['message'] = 'Address cannot be empty.';
echo json_encode($response);
exit;
}
// This is a very simple search. A real-world application would need a more robust
// address parsing and matching engine (e.g., using Google Geocoding API and spatial data).
// For this demo, we'll just do a broad search on the input string.
try {
$pdo = db();
$stmt = $pdo->prepare(
"SELECT COUNT(*) FROM serviceable_addresses WHERE CONCAT_WS(' ', street, suburb, state, postcode) LIKE ?"
);
$stmt->execute(['%' . $address . '%']);
$count = $stmt->fetchColumn();
if ($count > 0) {
$response['serviceable'] = true;
$response['message'] = 'Great news! Your address is serviceable.';
} else {
$response['serviceable'] = false;
$response['message'] = "Sorry, we don't seem to be in your area yet. Please contact us for more information.";
}
$response['success'] = true;
} catch (PDOException $e) {
// In a real app, you would log this error, not expose it to the user.
$response['message'] = 'Database error.'; // Generic message for the user
// error_log($e->getMessage()); // Example of logging
}
echo json_encode($response);