92 lines
3.4 KiB
PHP
92 lines
3.4 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once '../db/config.php';
|
|
|
|
// Get the incoming data
|
|
$body = file_get_contents('php://input');
|
|
$data = json_decode($body, true);
|
|
|
|
if (!$data) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'error' => 'No data received or invalid JSON.']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Handle snapshot update
|
|
if (isset($data['type']) && $data['type'] === 'snapshot') {
|
|
$stmt = $pdo->prepare("
|
|
INSERT INTO review_snapshot (location_id, total_reviews, avg_rating, reviews_this_week, reviews_this_month, google_reviews, google_avg, yelp_reviews, yelp_avg, facebook_reviews, facebook_avg, last_synced)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())
|
|
ON DUPLICATE KEY UPDATE
|
|
total_reviews = VALUES(total_reviews),
|
|
avg_rating = VALUES(avg_rating),
|
|
reviews_this_week = VALUES(reviews_this_week),
|
|
reviews_this_month = VALUES(reviews_this_month),
|
|
google_reviews = VALUES(google_reviews),
|
|
google_avg = VALUES(google_avg),
|
|
yelp_reviews = VALUES(yelp_reviews),
|
|
yelp_avg = VALUES(yelp_avg),
|
|
facebook_reviews = VALUES(facebook_reviews),
|
|
facebook_avg = VALUES(facebook_avg),
|
|
last_synced = NOW()
|
|
");
|
|
$stmt->execute([
|
|
$data['location_id'] ?? 'charlotte-heating',
|
|
$data['total_reviews'] ?? 0,
|
|
$data['avg_rating'] ?? 0,
|
|
$data['reviews_this_week'] ?? 0,
|
|
$data['reviews_this_month'] ?? 0,
|
|
$data['google_reviews'] ?? 0,
|
|
$data['google_avg'] ?? 0,
|
|
$data['yelp_reviews'] ?? 0,
|
|
$data['yelp_avg'] ?? 0,
|
|
$data['facebook_reviews'] ?? 0,
|
|
$data['facebook_avg'] ?? 0
|
|
]);
|
|
echo json_encode(['success' => true, 'message' => 'Snapshot updated']);
|
|
exit;
|
|
}
|
|
|
|
// Handle individual review insert
|
|
if (isset($data['type']) && $data['type'] === 'review') {
|
|
$stmt = $pdo->prepare("
|
|
INSERT IGNORE INTO reviews (location_id, site, review_id, reviewer_name, rating, review_text, review_date)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
");
|
|
$stmt->execute([
|
|
$data['location_id'] ?? 'charlotte-heating',
|
|
$data['site'] ?? 'google',
|
|
$data['review_id'] ?? uniqid(),
|
|
$data['reviewer_name'] ?? 'Anonymous',
|
|
$data['rating'] ?? null,
|
|
$data['review_text'] ?? '',
|
|
$data['review_date'] ?? null
|
|
]);
|
|
|
|
if ($stmt->rowCount() > 0) {
|
|
echo json_encode(['success' => true, 'message' => 'Review inserted']);
|
|
} else {
|
|
echo json_encode(['success' => true, 'message' => 'Review skipped (already exists)']);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
// Fallback for unknown type
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'error' => 'Invalid data type specified.']);
|
|
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
// In a real app, you'd log this securely.
|
|
error_log('Webhook DB Error: ' . $e->getMessage());
|
|
echo json_encode(['success' => false, 'error' => 'A database error occurred.']);
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
error_log('Webhook General Error: ' . $e->getMessage());
|
|
echo json_encode(['success' => false, 'error' => 'An internal server error occurred.']);
|
|
}
|
|
|
|
?>
|