admin-portal/tech/api/updateProductList.php
Frederico Falcao f2a6525224 init
2025-05-30 10:46:17 +01:00

97 lines
2.7 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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;