From 53073dbb1ff36aed974ef51ac37595b75b77ac7d Mon Sep 17 00:00:00 2001 From: Fernando Ontiveros Date: Tue, 7 Oct 2025 22:08:05 -0400 Subject: [PATCH] send data to external service --- .../engine/methods/cases/cases_SaveData.php | 59 +++++++++++++++---- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/workflow/engine/methods/cases/cases_SaveData.php b/workflow/engine/methods/cases/cases_SaveData.php index fa4cc273b..41fe033cc 100644 --- a/workflow/engine/methods/cases/cases_SaveData.php +++ b/workflow/engine/methods/cases/cases_SaveData.php @@ -2,7 +2,7 @@ use ProcessMaker\Model\Process as ProcessModel; use ProcessMaker\Validation\ValidationUploadedFiles; - +use ProcessMaker\Core\System; use Illuminate\Support\Facades\Log; //validate the data post @@ -117,6 +117,9 @@ try { exit(); } + // Capture the original case data (before any modifications) + $originalCaseData = $Fields['APP_DATA']; // this is the data that came from the DB + if ($swpmdynaform) { $dataFields = $Fields["APP_DATA"]; $dataFields["CURRENT_DYNAFORM"] = $_GET['UID']; @@ -124,6 +127,9 @@ try { $oPmDynaform = new PmDynaform($dataFields); $pmdynaform = $oPmDynaform->validatePost($pmdynaform); + // get the keys of the fields defined on the form + $keysDefinedOnForm = array_keys($pmdynaform); + $Fields["APP_DATA"] = array_merge( $Fields["APP_DATA"], $pmdynaform ); } @@ -282,11 +288,39 @@ try { } } - // Capture the original case data (before any modifications) - $originalCaseData = $Fields['APP_DATA']; // this is the data that came from the DB + // Prepare data to send to external system - // Capture the data that will be written to the custom table - $customTableData = $newValues; // may be empty if nothing new + // Get the form data with values from the case + $first = $originalCaseData; // original case data is the first array + $second = $Fields['APP_DATA']; // array of fields with all modifications (form + triggers) + + // get keys + $keysFirst = array_keys($first); + + // compute keys + $bothKeys = array_values(array_intersect($keysFirst, $keysDefinedOnForm)); + $firstOnly = array_values(array_diff($keysFirst, $keysDefinedOnForm)); + $secondOnly = array_values(array_diff($keysDefinedOnForm, $keysFirst)); + + // compute items + $bothItems = array_intersect_key($first, array_flip($bothKeys)); + $secondItems = array_intersect_key($second, array_flip($secondOnly)); + + // build "both" with both values (first and second) + $formWithValues = []; + foreach ($bothKeys as $k) { + $formWithValues[$k] = [ + 'old' => $first[$k], + 'new' => $second[$k], + ]; + } + foreach ($secondOnly as $k) { + $formWithValues[$k] = [ + 'old' => null, + 'new' => $second[$k], + ]; + } + // now $formWithValues has the new and old values of the fields defined in the form); // Build the payload $payload = [ @@ -294,10 +328,10 @@ try { 'dyn_uid' => $_GET['UID'], // DYN_UID (form UID) 'process_uid' => $_SESSION['PROCESS'], // PRO_UID 'case_number' => $Fields['APP_NUMBER'], // optional - 'original_case' => $originalCaseData, + 'fields' => $formWithValues, // only fields defined in the form, with old and new values 'modified_case' => $Fields['APP_DATA'], // after all field‑mapping logic - 'custom_table' => $customTableData, - 'next_step_page' => $aNextStep['PAGE'] ?? null, // optional + 'user_uid' => $_SESSION['USER_LOGGED'], // USER_UID + 'task_uid' => $_SESSION['TASK'], // TAS_UID ]; send_case_data_to_external($payload); @@ -400,12 +434,15 @@ try { function send_case_data_to_external( $payload ) { // send case data to external system $appUid = $_SESSION['APPLICATION']; - $caseDataHost = getenv('CASE_DATA_HOST'); + $systemConfig = System::getSystemConfiguration('', '', config("system.workspace")); + $caseDataHost = isset($systemConfig['case_data_host']) ? $systemConfig['case_data_host']: null; + if (empty($caseDataHost)) { // If no external endpoint is configured, just return return; } - $message = '['.$appUid.'] Sending case data to external service: '. $caseDataHost; + $message = '['.$appUid.'] Send case data to external service: '. $caseDataHost; + Log::info( $message ); // Send it to the external endpoint $client = new \GuzzleHttp\Client(); @@ -417,7 +454,7 @@ function send_case_data_to_external( $payload ) { // Optional: log the response $log = json_decode((string)$response->getBody(), true); - $message = "[".$appUid.'] External API responded: ' . $log['status'] . ' - ' . $log['message']; + $message = "[".$appUid.'] External API response: ' . $log['status'] . ' - ' . $log['message']; Log::info( $message); } catch (\Exception $ex) { // If the external call fails you can decide whether to abort or just log