[ "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;