diff --git a/src/App/Http/Controllers/RemoteCustomFieldController.php b/src/App/Http/Controllers/RemoteCustomFieldController.php index 875a4f5..46e4027 100644 --- a/src/App/Http/Controllers/RemoteCustomFieldController.php +++ b/src/App/Http/Controllers/RemoteCustomFieldController.php @@ -54,6 +54,8 @@ public function index(): JsonResponse * * @param RemoteCustomFieldRequest $request * @return JsonResponse + * + * @throws \Throwable */ public function store(RemoteCustomFieldRequest $request): JsonResponse { @@ -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); } @@ -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); } @@ -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); } diff --git a/src/App/Models/RemoteType.php b/src/App/Models/RemoteType.php index 3837f18..16f7c14 100644 --- a/src/App/Models/RemoteType.php +++ b/src/App/Models/RemoteType.php @@ -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; } diff --git a/src/App/Traits/TransformsOutput.php b/src/App/Traits/TransformsOutput.php index 2b1aecd..d615d12 100644 --- a/src/App/Traits/TransformsOutput.php +++ b/src/App/Traits/TransformsOutput.php @@ -6,7 +6,7 @@ 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; @@ -14,7 +14,7 @@ protected function transform(array $response, ?array $mappings): array $transformed = []; foreach ($response as $item) { - $transformed[] = $this->mapSingle($mappings, $item); + $transformed[] = $this->mapSingle($mappings, $item, $idProperty); } return $transformed; @@ -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) { @@ -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; } }