19
19
use MRussell \REST \Traits \PsrLoggerTrait ;
20
20
use Sugarcrm \REST \Endpoint \Data \FilterData ;
21
21
use Sugarcrm \REST \Endpoint \AuditLog ;
22
+ use Sugarcrm \REST \Endpoint \Integrate ;
22
23
use Sugarcrm \REST \Endpoint \SugarEndpointInterface ;
23
24
use Sugarcrm \REST \Endpoint \Traits \CompileRequestTrait ;
24
25
use Sugarcrm \REST \Endpoint \Traits \FieldsDataTrait ;
26
+ use Sugarcrm \REST \Endpoint \Traits \IntegrateSyncKeyTrait ;
25
27
use Sugarcrm \REST \Endpoint \Traits \ModuleAwareTrait ;
26
28
27
29
/**
39
41
* @method $this audit()
40
42
* @method $this file()
41
43
* @method $this duplicateCheck()
44
+ * @method $this upsert()
42
45
*/
43
46
abstract class AbstractSugarBeanEndpoint extends ModelEndpoint implements SugarEndpointInterface
44
47
{
45
- use CompileRequestTrait;
48
+ use CompileRequestTrait {
49
+ compileRequest as private doCompileRequest;
50
+ }
46
51
use PsrLoggerTrait;
47
52
use ModuleAwareTrait;
48
53
use FieldsDataTrait;
49
54
use FileUploadsTrait;
55
+ use IntegrateSyncKeyTrait;
50
56
51
57
public const MODEL_ACTION_VAR = 'action ' ;
52
58
@@ -80,19 +86,21 @@ abstract class AbstractSugarBeanEndpoint extends ModelEndpoint implements SugarE
80
86
81
87
public const BEAN_ACTION_DUPLICATE_CHECK = 'duplicateCheck ' ;
82
88
83
- public const BEAN_ACTION_ARG1_VAR = 'actionArg1 ' ;
89
+ public const BEAN_ACTION_UPSERT = 'upsert ' ;
90
+
91
+ public const BEAN_ACTION_ARG1_VAR = 'actArg1 ' ;
84
92
85
- public const BEAN_ACTION_ARG2_VAR = 'actionArg2 ' ;
93
+ public const BEAN_ACTION_ARG2_VAR = 'actArg2 ' ;
86
94
87
- public const BEAN_ACTION_ARG3_VAR = 'actionArg3 ' ;
95
+ public const BEAN_ACTION_ARG3_VAR = 'actArg3 ' ;
88
96
89
97
public const BEAN_MODULE_URL_ARG = 'module ' ;
90
98
91
99
/**
92
100
* @inheritdoc
93
101
*/
94
102
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 ' ,
96
104
self ::PROPERTY_HTTP_METHOD => 'GET ' ,
97
105
self ::PROPERTY_AUTH => true ,
98
106
];
@@ -116,6 +124,7 @@ abstract class AbstractSugarBeanEndpoint extends ModelEndpoint implements SugarE
116
124
self ::BEAN_ACTION_ATTACH_FILE => "POST " ,
117
125
self ::BEAN_ACTION_TEMP_FILE_UPLOAD => "POST " ,
118
126
self ::BEAN_ACTION_DUPLICATE_CHECK => "POST " ,
127
+ self ::BEAN_ACTION_UPSERT => "PATCH " ,
119
128
];
120
129
121
130
/**
@@ -139,21 +148,16 @@ public function __construct(array $urlArgs = [], array $properties = [])
139
148
}
140
149
141
150
/**
142
- * Passed in options get changed such that 1st Option (key 0) becomes Module
143
- * 2nd Option (Key 1) becomes ID
144
151
* @inheritdoc
145
152
*/
146
153
public function setUrlArgs (array $ args ): static
147
154
{
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 ;
155
158
}
156
159
160
+
157
161
/**
158
162
* Configure Uploads on Request
159
163
* @inheritdoc
@@ -179,6 +183,16 @@ protected function configurePayload(): mixed
179
183
{
180
184
$ data = $ this ->getData ();
181
185
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 ;
182
196
case self ::MODEL_ACTION_CREATE :
183
197
case self ::MODEL_ACTION_UPDATE :
184
198
$ data ->reset ();
@@ -220,6 +234,10 @@ protected function parseResponse(Response $response): void
220
234
$ this ->clear ();
221
235
$ this ->syncFromApi ($ this ->parseResponseBodyToArray ($ body , $ this ->getModelResponseProp ()));
222
236
return ;
237
+ case self ::BEAN_ACTION_UPSERT :
238
+ $ body = $ this ->getResponseContent ($ response );
239
+ $ this ->syncFromApi ($ this ->parseResponseBodyToArray ($ body , Integrate::INTEGRATE_RESPONSE_PROP ));
240
+ return ;
223
241
}
224
242
}
225
243
@@ -266,13 +284,21 @@ protected function resetUploads(): void
266
284
protected function configureURL (array $ urlArgs ): string
267
285
{
268
286
$ action = null ;
269
- $ urlArgs = $ this ->configureModuleUrlArg ($ urlArgs );
287
+ $ urlArgs = $ this ->addModuleToUrlArgs ($ urlArgs );
270
288
switch ($ this ->getCurrentAction ()) {
271
289
case self ::BEAN_ACTION_CREATE_RELATED :
272
290
case self ::BEAN_ACTION_MASS_RELATE :
273
291
case self ::BEAN_ACTION_UNLINK :
274
292
case self ::BEAN_ACTION_FILTER_RELATED :
275
293
$ 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
+
276
302
break ;
277
303
case self ::BEAN_ACTION_TEMP_FILE_UPLOAD :
278
304
$ urlArgs [self ::MODEL_ID_VAR ] = 'temp ' ;
@@ -310,39 +336,43 @@ protected function configureURL(array $urlArgs): string
310
336
protected function configureAction (string $ action , array $ arguments = []): void
311
337
{
312
338
$ 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 ]);
323
341
}
324
342
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 ];
343
359
}
344
360
}
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
+ }
346
376
}
347
377
348
378
$ this ->setUrlArgs ($ urlArgs );
@@ -542,13 +572,11 @@ protected function configureFileUploadQueryParams(): array
542
572
'delete_if_fails ' => $ this ->_deleteFileOnFail ,
543
573
];
544
574
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 ' );
552
580
}
553
581
}
554
582
0 commit comments