Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/App/Http/Controllers/RemoteCustomFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public function index(): JsonResponse
*
* @param RemoteCustomFieldRequest $request
* @return JsonResponse
*
* @throws \Throwable
*/
public function store(RemoteCustomFieldRequest $request): JsonResponse
{
Expand Down Expand Up @@ -107,7 +109,7 @@ public function resolve(RemoteType $remoteType): JsonResponse
$data = $remoteType->getRemoteData();

$data = $remoteType->data_path ? Arr::get($data, $remoteType->data_path) : $data;
$transformed = $this->transform($data, $remoteType->mappings);
$transformed = $this->transform($data, $remoteType->mappings, $remoteType->identifier_property);

return response()->json($transformed);
}
Expand All @@ -127,7 +129,7 @@ public function resolveByIdentifierValue(RemoteType $remoteType, string $identif

$data = collect($data)->where($remoteType->identifier_property, $identifierValue)->first();

$transformed = is_array($data) ? $this->mapSingle($remoteType->mappings, $data) : $data;
$transformed = is_array($data) ? $this->mapSingle($remoteType->mappings, $data, $remoteType->identifier_property) : $data;

return response()->json($transformed);
}
Expand All @@ -143,7 +145,7 @@ public function search(Request $request, RemoteType $remoteType, string $q = '')
}

$data = $remoteType->data_path ? Arr::get($data, $remoteType->data_path) : $data;
$transformed = $this->transform($data, $remoteType->mappings);
$transformed = $this->transform($data, $remoteType->mappings, $remoteType->identifier_property);

return response()->json($transformed);
}
Expand Down
6 changes: 5 additions & 1 deletion src/App/Models/RemoteType.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ public function getRemoteData(?string $identifierValue = null)
}

$response = $this->fetchData($identifierValue, false);
Cache::put($cacheKey, $response, config('asseco-custom-fields.remote_cache_ttl'));

if (!$identifierValue) {
// cache only all
Cache::put($cacheKey, $response, config('asseco-custom-fields.remote_cache_ttl'));
}

return $response;
}
Expand Down
11 changes: 8 additions & 3 deletions src/App/Traits/TransformsOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

trait TransformsOutput
{
protected function transform(array $response, ?array $mappings): array
protected function transform(array $response, ?array $mappings, ?string $idProperty = null): array
{
if (!$mappings) {
return $response;
}

$transformed = [];
foreach ($response as $item) {
$transformed[] = $this->mapSingle($mappings, $item);
$transformed[] = $this->mapSingle($mappings, $item, $idProperty);
}

return $transformed;
Expand All @@ -23,9 +23,10 @@ protected function transform(array $response, ?array $mappings): array
/**
* @param array $mappings
* @param array $item
* @param string|null $idProperty
* @return array
*/
protected function mapSingle(array $mappings, array $item): array
protected function mapSingle(array $mappings, array $item, ?string $idProperty = null): array
{
$data = [];
foreach ($mappings as $remoteKey => $localKey) {
Expand All @@ -38,6 +39,10 @@ protected function mapSingle(array $mappings, array $item): array
]);
}

if ($idProperty && array_key_exists($idProperty, $item) && !array_key_exists($idProperty, $data)) {
$data[$idProperty] = $item[$idProperty];
}

return $data;
}
}