97 lines
2.7 KiB
PHP
97 lines
2.7 KiB
PHP
<?php
|
||
require_once __DIR__. "/_authenticateApiRequest.php"; // ✅ Authenticated
|
||
require_once __DIR__."/_dbCredentials.php";
|
||
require_once __DIR__."/_dbFuncs.php";
|
||
require_once __DIR__."/_telegramCredentials.php";
|
||
require_once __DIR__."/_telegramFunctions.php";
|
||
|
||
header('Content-Type: application/json');
|
||
|
||
// Define only the suffixes, not the full "api::..." strings
|
||
$reactToContentTypes = [
|
||
"api::meal-prep.meal-prep" =>
|
||
[
|
||
"sqlTableName" => "MealPrep",
|
||
"columns" => [
|
||
"name"
|
||
],
|
||
"keyColumn" => "strapiDocumentId"
|
||
],
|
||
"api::meal-plan.meal-plan" =>
|
||
[
|
||
"sqlTableName" => "MealPlan",
|
||
"columns" => [
|
||
"name",
|
||
"numberOfItems",
|
||
"price",
|
||
],
|
||
"keyColumn" => "strapiDocumentId"
|
||
],
|
||
"api::single-meal.single-meal" =>
|
||
[
|
||
"sqlTableName" => "SingleMeal",
|
||
"columns" => [
|
||
"name"
|
||
],
|
||
"keyColumn" => "strapiDocumentId"
|
||
],
|
||
];
|
||
|
||
|
||
// Get raw POST body once
|
||
$rawInput = file_get_contents("php://input");
|
||
$event = json_decode($rawInput, true);
|
||
|
||
// Handle malformed JSON
|
||
if (!is_array($event) || !isset($event["uid"])) {
|
||
http_response_code(400);
|
||
echo json_encode(['error' => 'Invalid JSON or missing uid']);
|
||
exit;
|
||
}
|
||
|
||
telegramSendMessage("✏️ New content updated at STRAPI (".$event["uid"].")");
|
||
try {
|
||
// 🔎 Check if the CONTENT-TYPE (uid) is in the list we care about
|
||
if (in_array($event["uid"], array_keys($reactToContentTypes))) {
|
||
|
||
// 🎯 Get content type settings (SQL table & columns mapping)
|
||
$contentType = $reactToContentTypes[$event["uid"]];
|
||
$sqlTblName = $contentType["sqlTableName"];
|
||
|
||
// 🏗 Build the data array for upsert
|
||
$cols = [];
|
||
foreach ($contentType["columns"] as $c) {
|
||
if (isset($event["entry"][$c])) {
|
||
$cols[$c] = $event["entry"][$c];
|
||
} else {
|
||
// You might want to handle missing fields here
|
||
$cols[$c] = null;
|
||
}
|
||
}
|
||
|
||
// 🔑 Get the unique key column for upsert
|
||
$keyColumn = $contentType["keyColumn"];
|
||
|
||
// 🚀 Run the UPSERT (insert or update)
|
||
$success = upsert($sqlTblName, $cols, $keyColumn);
|
||
|
||
if ($success) {
|
||
telegramSendMessage("🔄 Data successfully propagated to SUPABASE");
|
||
} else {
|
||
telegramSendMessage("❌ Upsert failed");
|
||
}
|
||
|
||
} else {
|
||
// ⚠️ uid not in the list of content types we're watching
|
||
telegramSendMessage('ℹ️ No action taken for uid: ' . $event["uid"]);
|
||
}
|
||
|
||
} catch (Exception $e) {
|
||
// 🚨 Catch and report any exceptions
|
||
http_response_code(500);
|
||
echo json_encode(['error' => 'Server Error','details' => $e->getMessage()]);
|
||
telegramSendMessage("500 Server Error: (".__FILE__.") ".$e->getMessage());
|
||
}
|
||
|
||
exit;
|