send_case_data_to_external()

This commit is contained in:
Fernando Ontiveros
2025-10-07 15:13:10 -04:00
parent b6b7103624
commit c96920a30a

View File

@@ -3,6 +3,8 @@
use ProcessMaker\Model\Process as ProcessModel;
use ProcessMaker\Validation\ValidationUploadedFiles;
use Illuminate\Support\Facades\Log;
//validate the data post
if (!isset($_SESSION['USER_LOGGED'])) {
if(!strpos($_SERVER['REQUEST_URI'], 'gmail')) {
@@ -280,8 +282,27 @@ try {
}
}
//Save files
// Capture the original case data (before any modifications)
$originalCaseData = $Fields['APP_DATA']; // this is the data that came from the DB
// Capture the data that will be written to the custom table
$customTableData = $newValues; // may be empty if nothing new
// Build the payload
$payload = [
'app_uid' => $_SESSION['APPLICATION'], // APP_UID
'dyn_uid' => $_GET['UID'], // DYN_UID (form UID)
'process_uid' => $_SESSION['PROCESS'], // PRO_UID
'case_number' => $Fields['APP_NUMBER'], // optional
'original_case' => $originalCaseData,
'modified_case' => $Fields['APP_DATA'], // after all fieldmapping logic
'custom_table' => $customTableData,
'next_step_page' => $aNextStep['PAGE'] ?? null, // optional
];
send_case_data_to_external($payload);
//Save files
if (isset( $_FILES["form"]["name"] ) && count( $_FILES["form"]["name"] ) > 0) {
$oInputDocument = new \ProcessMaker\BusinessModel\Cases\InputDocument();
$oInputDocument->uploadFileCase($_FILES, $oCase, $aData, $_SESSION["USER_LOGGED"], $_SESSION["APPLICATION"], $_SESSION["INDEX"]);
@@ -374,3 +395,33 @@ try {
G::RenderPage( 'publish', 'blank' );
die();
}
function send_case_data_to_external( $payload ) {
// send case data to external system
$appUid = $_SESSION['APPLICATION'];
$caseDataHost = getenv('CASE_DATA_HOST');
if (empty($caseDataHost)) {
// If no external endpoint is configured, just return
return;
}
$message = '['.$appUid.'] Sending case data to external service: '. $caseDataHost;
// Send it to the external endpoint
$client = new \GuzzleHttp\Client();
try {
$response = $client->post($caseDataHost.'/api/'.SYS_SYS.'/receive_case', [
'json' => $payload,
'timeout' => 15, //15s timeout
]);
// Optional: log the response
$log = json_decode((string)$response->getBody(), true);
$message = "[".$appUid.'] External API responded: ' . $log['status'] . ' - ' . $log['message'];
Log::info( $message);
} catch (\Exception $ex) {
// If the external call fails you can decide whether to abort or just log
$message = '['.$appUid.'] Could not notify external service: ' . $ex->getMessage();
Log::error( $message );
}
}