@@ -100,6 +100,9 @@ class ApiPayload
100100 /** @var Schedule */
101101 protected $ scheduleModel ;
102102
103+ /** @var bool */
104+ protected $ updatedFields = false ;
105+
103106 /**
104107 * ApiPayload constructor.
105108 *
@@ -329,6 +332,7 @@ public function run()
329332 $ transport = $ this ->getTransport ();
330333 $ tokenHelper = $ this ->tokenHelper ->newSession ($ this ->contactClient , $ this ->contact , $ this ->payload );
331334 $ updatePayload = (bool ) $ this ->settings ['autoUpdate ' ];
335+ $ opsRemaining = count ($ this ->payload ->operations );
332336
333337 foreach ($ this ->payload ->operations as $ id => &$ operation ) {
334338 $ logs = [];
@@ -340,7 +344,7 @@ public function run()
340344 $ apiOperation ->run ();
341345 $ this ->valid = $ apiOperation ->getValid ();
342346 } catch (\Exception $ e ) {
343- // Delay this exception throw.. .
347+ // Delay this exception throw till after we can do some important logging .
344348 }
345349 $ logs = array_merge ($ apiOperation ->getLogs (), $ logs );
346350 $ this ->setLogs ($ logs , $ id );
@@ -351,7 +355,17 @@ public function run()
351355 } else {
352356 // Aggregate successful responses that are mapped to Contact fields.
353357 $ this ->responseMap = array_merge ($ this ->responseMap , $ apiOperation ->getResponseMap ());
354- $ this ->setAggregateActualResponses ($ apiOperation ->getResponseActual ());
358+ $ responseActual = $ apiOperation ->getResponseActual ();
359+ $ this ->setAggregateActualResponses ($ responseActual );
360+ --$ opsRemaining ;
361+ if ($ opsRemaining ) {
362+ // Update the contextual awareness for subsequent requests if needed.
363+ $ this ->applyResponseMap (true );
364+ // Update context to include actual previous payload responses.
365+ if ($ responseActual ) {
366+ $ this ->tokenHelper ->addContextPayload ($ this ->payload , $ id , $ responseActual );
367+ }
368+ }
355369 }
356370 }
357371
@@ -361,7 +375,7 @@ public function run()
361375 }
362376
363377 // Update the payload if enabled.
364- if ($ updatePayload ) {
378+ if ($ updatePayload && $ this -> test ) {
365379 $ this ->updatePayload ();
366380 }
367381
@@ -409,6 +423,44 @@ public function setAggregateActualResponses($responseActual, $types = ['headers'
409423 return $ this ;
410424 }
411425
426+ /**
427+ * Apply the responsemap to update a contact entity.
428+ *
429+ * @param bool $updateTokens
430+ *
431+ * @return bool
432+ */
433+ public function applyResponseMap ($ updateTokens = false )
434+ {
435+ $ responseMap = $ this ->getResponseMap ();
436+ // Check the responseMap to discern where field values should go.
437+ if (count ($ responseMap )) {
438+ foreach ($ responseMap as $ alias => $ value ) {
439+ $ oldValue = $ this ->contact ->getFieldValue ($ alias );
440+ if ($ oldValue !== $ value ) {
441+ $ this ->contact ->addUpdatedField ($ alias , $ value , $ oldValue );
442+ if ($ updateTokens ) {
443+ $ this ->tokenHelper ->addContext ([$ alias => $ value ]);
444+ }
445+ $ this ->setLogs ('Updating Contact: ' .$ alias .' = ' .$ value , 'fieldsUpdated ' );
446+ $ this ->updatedFields = true ;
447+ }
448+ }
449+ }
450+
451+ return $ this ->updatedFields ;
452+ }
453+
454+ /**
455+ * Return the aggregate responsemap of all valid operations.
456+ *
457+ * @return array
458+ */
459+ public function getResponseMap ()
460+ {
461+ return $ this ->responseMap ;
462+ }
463+
412464 /**
413465 * Update the payload with the parent ContactClient because we've updated the response expectation.
414466 */
@@ -492,40 +544,6 @@ public function setLogs($value, $type = null)
492544 }
493545 }
494546
495- /**
496- * Apply the responsemap to update a contact entity.
497- *
498- * @return bool
499- */
500- public function applyResponseMap ()
501- {
502- $ updated = false ;
503- $ responseMap = $ this ->getResponseMap ();
504- // Check the responseMap to discern where field values should go.
505- if (count ($ responseMap )) {
506- foreach ($ responseMap as $ alias => $ value ) {
507- $ oldValue = $ this ->contact ->getFieldValue ($ alias );
508- if ($ oldValue !== $ value ) {
509- $ this ->contact ->addUpdatedField ($ alias , $ value , $ oldValue );
510- $ this ->setLogs ('Updating Contact: ' .$ alias .' = ' .$ value );
511- $ updated = true ;
512- }
513- }
514- }
515-
516- return $ updated ;
517- }
518-
519- /**
520- * Return the aggregate responsemap of all valid operations.
521- *
522- * @return array
523- */
524- public function getResponseMap ()
525- {
526- return $ this ->responseMap ;
527- }
528-
529547 /**
530548 * Retrieve from the payload all outgoing fields that are set to overridable.
531549 *
0 commit comments