1919use MRussell \REST \Traits \PsrLoggerTrait ;
2020use Sugarcrm \REST \Endpoint \Data \FilterData ;
2121use Sugarcrm \REST \Endpoint \AuditLog ;
22+ use Sugarcrm \REST \Endpoint \Integrate ;
2223use Sugarcrm \REST \Endpoint \SugarEndpointInterface ;
2324use Sugarcrm \REST \Endpoint \Traits \CompileRequestTrait ;
2425use Sugarcrm \REST \Endpoint \Traits \FieldsDataTrait ;
26+ use Sugarcrm \REST \Endpoint \Traits \IntegrateSyncKeyTrait ;
2527use Sugarcrm \REST \Endpoint \Traits \ModuleAwareTrait ;
2628
2729/**
3941 * @method $this audit()
4042 * @method $this file()
4143 * @method $this duplicateCheck()
44+ * @method $this upsert()
4245 */
4346abstract class AbstractSugarBeanEndpoint extends ModelEndpoint implements SugarEndpointInterface
4447{
45- use CompileRequestTrait;
48+ use CompileRequestTrait {
49+ compileRequest as private doCompileRequest;
50+ }
4651 use PsrLoggerTrait;
4752 use ModuleAwareTrait;
4853 use FieldsDataTrait;
4954 use FileUploadsTrait;
55+ use IntegrateSyncKeyTrait;
5056
5157 public const MODEL_ACTION_VAR = 'action ' ;
5258
@@ -80,19 +86,21 @@ abstract class AbstractSugarBeanEndpoint extends ModelEndpoint implements SugarE
8086
8187 public const BEAN_ACTION_DUPLICATE_CHECK = 'duplicateCheck ' ;
8288
83- public const BEAN_ACTION_ARG1_VAR = 'actionArg1 ' ;
89+ public const BEAN_ACTION_UPSERT = 'upsert ' ;
90+
91+ public const BEAN_ACTION_ARG1_VAR = 'actArg1 ' ;
8492
85- public const BEAN_ACTION_ARG2_VAR = 'actionArg2 ' ;
93+ public const BEAN_ACTION_ARG2_VAR = 'actArg2 ' ;
8694
87- public const BEAN_ACTION_ARG3_VAR = 'actionArg3 ' ;
95+ public const BEAN_ACTION_ARG3_VAR = 'actArg3 ' ;
8896
8997 public const BEAN_MODULE_URL_ARG = 'module ' ;
9098
9199 /**
92100 * @inheritdoc
93101 */
94102 protected static array $ _DEFAULT_PROPERTIES = [
95- self ::PROPERTY_URL => '$module/$id/$:action/$:actionArg1 /$:actionArg2 /$:actionArg3 ' ,
103+ self ::PROPERTY_URL => '$module/$: id/$:action/$:actArg1 /$:actArg2 /$:actArg3 ' ,
96104 self ::PROPERTY_HTTP_METHOD => 'GET ' ,
97105 self ::PROPERTY_AUTH => true ,
98106 ];
@@ -116,6 +124,7 @@ abstract class AbstractSugarBeanEndpoint extends ModelEndpoint implements SugarE
116124 self ::BEAN_ACTION_ATTACH_FILE => "POST " ,
117125 self ::BEAN_ACTION_TEMP_FILE_UPLOAD => "POST " ,
118126 self ::BEAN_ACTION_DUPLICATE_CHECK => "POST " ,
127+ self ::BEAN_ACTION_UPSERT => "PATCH " ,
119128 ];
120129
121130 /**
@@ -139,21 +148,16 @@ public function __construct(array $urlArgs = [], array $properties = [])
139148 }
140149
141150 /**
142- * Passed in options get changed such that 1st Option (key 0) becomes Module
143- * 2nd Option (Key 1) becomes ID
144151 * @inheritdoc
145152 */
146153 public function setUrlArgs (array $ args ): static
147154 {
148- $ args = $ this ->configureModuleUrlArg ($ args );
149- if (isset ($ args [1 ])) {
150- $ this ->set ($ this ->getKeyProperty (), $ args [1 ]);
151- unset($ args [1 ]);
152- }
153-
154- return parent ::setUrlArgs ($ args );
155+ parent ::setUrlArgs ($ args );
156+ $ this ->syncModuleAndUrlArgs ();
157+ return $ this ;
155158 }
156159
160+
157161 /**
158162 * Configure Uploads on Request
159163 * @inheritdoc
@@ -179,6 +183,16 @@ protected function configurePayload(): mixed
179183 {
180184 $ data = $ this ->getData ();
181185 switch ($ this ->getCurrentAction ()) {
186+ case self ::BEAN_ACTION_UPSERT :
187+ $ data ->reset ();
188+ $ data ->set ($ this ->toArray ());
189+ $ syncKeyField = $ this ->getSyncKeyField ();
190+ if (!empty ($ syncKeyField )) {
191+ $ data [Integrate::DATA_SYNC_KEY_FIELD ] = $ syncKeyField ;
192+ }
193+
194+ $ data [Integrate::DATA_SYNC_KEY_VALUE ] = $ this ->getSyncKey ();
195+ return $ data ;
182196 case self ::MODEL_ACTION_CREATE :
183197 case self ::MODEL_ACTION_UPDATE :
184198 $ data ->reset ();
@@ -220,6 +234,10 @@ protected function parseResponse(Response $response): void
220234 $ this ->clear ();
221235 $ this ->syncFromApi ($ this ->parseResponseBodyToArray ($ body , $ this ->getModelResponseProp ()));
222236 return ;
237+ case self ::BEAN_ACTION_UPSERT :
238+ $ body = $ this ->getResponseContent ($ response );
239+ $ this ->syncFromApi ($ this ->parseResponseBodyToArray ($ body , Integrate::INTEGRATE_RESPONSE_PROP ));
240+ return ;
223241 }
224242 }
225243
@@ -266,13 +284,21 @@ protected function resetUploads(): void
266284 protected function configureURL (array $ urlArgs ): string
267285 {
268286 $ action = null ;
269- $ urlArgs = $ this ->configureModuleUrlArg ($ urlArgs );
287+ $ urlArgs = $ this ->addModuleToUrlArgs ($ urlArgs );
270288 switch ($ this ->getCurrentAction ()) {
271289 case self ::BEAN_ACTION_CREATE_RELATED :
272290 case self ::BEAN_ACTION_MASS_RELATE :
273291 case self ::BEAN_ACTION_UNLINK :
274292 case self ::BEAN_ACTION_FILTER_RELATED :
275293 $ action = self ::BEAN_ACTION_RELATE ;
294+ break ;
295+ case self ::BEAN_ACTION_UPSERT :
296+ $ urlArgs [self ::MODEL_ID_VAR ] = Integrate::SYNC_KEY ;
297+ $ syncKey = $ this ->getSyncKey ();
298+ if (!empty ($ syncKey )) {
299+ $ action = $ syncKey ;
300+ }
301+
276302 break ;
277303 case self ::BEAN_ACTION_TEMP_FILE_UPLOAD :
278304 $ urlArgs [self ::MODEL_ID_VAR ] = 'temp ' ;
@@ -310,39 +336,43 @@ protected function configureURL(array $urlArgs): string
310336 protected function configureAction (string $ action , array $ arguments = []): void
311337 {
312338 $ urlArgs = $ this ->getUrlArgs ();
313- if (isset ($ urlArgs [self ::BEAN_ACTION_ARG1_VAR ])) {
314- unset($ urlArgs [self ::BEAN_ACTION_ARG1_VAR ]);
315- }
316-
317- if (isset ($ urlArgs [self ::BEAN_ACTION_ARG2_VAR ])) {
318- unset($ urlArgs [self ::BEAN_ACTION_ARG2_VAR ]);
319- }
320-
321- if (isset ($ urlArgs [self ::BEAN_ACTION_ARG3_VAR ])) {
322- unset($ urlArgs [self ::BEAN_ACTION_ARG3_VAR ]);
339+ if (isset ($ urlArgs [self ::MODEL_ACTION_VAR ]) && $ urlArgs [self ::MODEL_ACTION_VAR ] != $ action ) {
340+ unset($ urlArgs [self ::MODEL_ACTION_VAR ]);
323341 }
324342
325- if (!empty ($ arguments )) {
326- switch ($ action ) {
327- case self ::BEAN_ACTION_TEMP_FILE_UPLOAD :
328- case self ::BEAN_ACTION_ATTACH_FILE :
329- $ this ->_upload = true ;
330- // no break
331- case self ::BEAN_ACTION_RELATE :
332- case self ::BEAN_ACTION_DOWNLOAD_FILE :
333- case self ::BEAN_ACTION_UNLINK :
334- case self ::BEAN_ACTION_CREATE_RELATED :
335- case self ::BEAN_ACTION_FILTER_RELATED :
336- if (isset ($ arguments [0 ])) {
337- $ urlArgs [self ::BEAN_ACTION_ARG1_VAR ] = $ arguments [0 ];
338- if (isset ($ arguments [1 ])) {
339- $ urlArgs [self ::BEAN_ACTION_ARG2_VAR ] = $ arguments [1 ];
340- if (isset ($ arguments [2 ])) {
341- $ urlArgs [self ::BEAN_ACTION_ARG3_VAR ] = $ arguments [2 ];
342- }
343+ switch ($ action ) {
344+ case self ::BEAN_ACTION_TEMP_FILE_UPLOAD :
345+ case self ::BEAN_ACTION_ATTACH_FILE :
346+ $ this ->_upload = true ;
347+ // no break
348+ case self ::BEAN_ACTION_RELATE :
349+ case self ::BEAN_ACTION_DOWNLOAD_FILE :
350+ case self ::BEAN_ACTION_UNLINK :
351+ case self ::BEAN_ACTION_CREATE_RELATED :
352+ case self ::BEAN_ACTION_FILTER_RELATED :
353+ if (isset ($ arguments [0 ])) {
354+ $ urlArgs [self ::BEAN_ACTION_ARG1_VAR ] = $ arguments [0 ];
355+ if (isset ($ arguments [1 ])) {
356+ $ urlArgs [self ::BEAN_ACTION_ARG2_VAR ] = $ arguments [1 ];
357+ if (isset ($ arguments [2 ])) {
358+ $ urlArgs [self ::BEAN_ACTION_ARG3_VAR ] = $ arguments [2 ];
343359 }
344360 }
345- }
361+ }
362+
363+ break ;
364+ default :
365+ //If action is not defined above, remove action args
366+ $ actionArgs = [
367+ self ::BEAN_ACTION_ARG1_VAR ,
368+ self ::BEAN_ACTION_ARG2_VAR ,
369+ self ::BEAN_ACTION_ARG3_VAR ,
370+ ];
371+ foreach ($ actionArgs as $ actionArg ) {
372+ if (isset ($ urlArgs [$ actionArg ])) {
373+ unset($ urlArgs [$ actionArg ]);
374+ }
375+ }
346376 }
347377
348378 $ this ->setUrlArgs ($ urlArgs );
@@ -542,13 +572,11 @@ protected function configureFileUploadQueryParams(): array
542572 'delete_if_fails ' => $ this ->_deleteFileOnFail ,
543573 ];
544574
545- if ($ this ->_deleteFileOnFail ) {
546- if (!empty ($ this ->_client )) {
547- $ data ['platform ' ] = $ this ->getClient ()->getPlatform ();
548- $ token = $ this ->getClient ()->getAuth ()->getTokenProp ('access_token ' );
549- if ($ token ) {
550- $ data ['oauth_token ' ] = $ this ->getClient ()->getAuth ()->getTokenProp ('access_token ' );
551- }
575+ if ($ this ->_deleteFileOnFail && !empty ($ this ->_client )) {
576+ $ data ['platform ' ] = $ this ->getClient ()->getPlatform ();
577+ $ token = $ this ->getClient ()->getAuth ()->getTokenProp ('access_token ' );
578+ if ($ token ) {
579+ $ data ['oauth_token ' ] = $ this ->getClient ()->getAuth ()->getTokenProp ('access_token ' );
552580 }
553581 }
554582
0 commit comments