send data to external service
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use ProcessMaker\Model\Process as ProcessModel;
|
use ProcessMaker\Model\Process as ProcessModel;
|
||||||
use ProcessMaker\Validation\ValidationUploadedFiles;
|
use ProcessMaker\Validation\ValidationUploadedFiles;
|
||||||
|
use ProcessMaker\Core\System;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
//validate the data post
|
//validate the data post
|
||||||
@@ -117,6 +117,9 @@ try {
|
|||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Capture the original case data (before any modifications)
|
||||||
|
$originalCaseData = $Fields['APP_DATA']; // this is the data that came from the DB
|
||||||
|
|
||||||
if ($swpmdynaform) {
|
if ($swpmdynaform) {
|
||||||
$dataFields = $Fields["APP_DATA"];
|
$dataFields = $Fields["APP_DATA"];
|
||||||
$dataFields["CURRENT_DYNAFORM"] = $_GET['UID'];
|
$dataFields["CURRENT_DYNAFORM"] = $_GET['UID'];
|
||||||
@@ -124,6 +127,9 @@ try {
|
|||||||
$oPmDynaform = new PmDynaform($dataFields);
|
$oPmDynaform = new PmDynaform($dataFields);
|
||||||
$pmdynaform = $oPmDynaform->validatePost($pmdynaform);
|
$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 );
|
$Fields["APP_DATA"] = array_merge( $Fields["APP_DATA"], $pmdynaform );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -282,11 +288,39 @@ try {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Capture the original case data (before any modifications)
|
// Prepare data to send to external system
|
||||||
$originalCaseData = $Fields['APP_DATA']; // this is the data that came from the DB
|
|
||||||
|
|
||||||
// Capture the data that will be written to the custom table
|
// Get the form data with values from the case
|
||||||
$customTableData = $newValues; // may be empty if nothing new
|
$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
|
// Build the payload
|
||||||
$payload = [
|
$payload = [
|
||||||
@@ -294,10 +328,10 @@ try {
|
|||||||
'dyn_uid' => $_GET['UID'], // DYN_UID (form UID)
|
'dyn_uid' => $_GET['UID'], // DYN_UID (form UID)
|
||||||
'process_uid' => $_SESSION['PROCESS'], // PRO_UID
|
'process_uid' => $_SESSION['PROCESS'], // PRO_UID
|
||||||
'case_number' => $Fields['APP_NUMBER'], // optional
|
'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
|
'modified_case' => $Fields['APP_DATA'], // after all field‑mapping logic
|
||||||
'custom_table' => $customTableData,
|
'user_uid' => $_SESSION['USER_LOGGED'], // USER_UID
|
||||||
'next_step_page' => $aNextStep['PAGE'] ?? null, // optional
|
'task_uid' => $_SESSION['TASK'], // TAS_UID
|
||||||
];
|
];
|
||||||
|
|
||||||
send_case_data_to_external($payload);
|
send_case_data_to_external($payload);
|
||||||
@@ -400,12 +434,15 @@ try {
|
|||||||
function send_case_data_to_external( $payload ) {
|
function send_case_data_to_external( $payload ) {
|
||||||
// send case data to external system
|
// send case data to external system
|
||||||
$appUid = $_SESSION['APPLICATION'];
|
$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 (empty($caseDataHost)) {
|
||||||
// If no external endpoint is configured, just return
|
// If no external endpoint is configured, just return
|
||||||
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
|
// Send it to the external endpoint
|
||||||
$client = new \GuzzleHttp\Client();
|
$client = new \GuzzleHttp\Client();
|
||||||
@@ -417,7 +454,7 @@ function send_case_data_to_external( $payload ) {
|
|||||||
|
|
||||||
// Optional: log the response
|
// Optional: log the response
|
||||||
$log = json_decode((string)$response->getBody(), true);
|
$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);
|
Log::info( $message);
|
||||||
} catch (\Exception $ex) {
|
} catch (\Exception $ex) {
|
||||||
// If the external call fails you can decide whether to abort or just log
|
// If the external call fails you can decide whether to abort or just log
|
||||||
|
|||||||
Reference in New Issue
Block a user