From 5af44bf5f8a0f88ca1976b37fb7923a2a70a9c5b Mon Sep 17 00:00:00 2001 From: Marijo Pavlov Date: Thu, 11 Sep 2025 15:12:37 +0200 Subject: [PATCH 1/4] Use identifier value in remote custom field cache key --- src/App/Models/RemoteType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App/Models/RemoteType.php b/src/App/Models/RemoteType.php index 3837f18..b948fdb 100644 --- a/src/App/Models/RemoteType.php +++ b/src/App/Models/RemoteType.php @@ -80,7 +80,7 @@ private function fetchData(?string $value = null, bool $search = false) public function getRemoteData(?string $identifierValue = null) { - $cacheKey = 'remote_custom_field_' . $this->id; + $cacheKey = 'remote_custom_field_' . $this->id . '_' . $identifierValue; if (config('asseco-custom-fields.should_cache_remote') && Cache::has($cacheKey)) { return Cache::get($cacheKey); } From abfc5e8752affcd425567c2e3fbf87e8bfa5b88c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Ga=C5=A1pari=C4=87?= Date: Fri, 12 Sep 2025 13:47:00 +0200 Subject: [PATCH 2/4] always include identifier_property in mapSingle result --- .../Controllers/RemoteCustomFieldController.php | 9 +++++---- src/App/Models/RemoteType.php | 8 ++++++-- src/App/Traits/TransformsOutput.php | 15 ++++++++++----- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/App/Http/Controllers/RemoteCustomFieldController.php b/src/App/Http/Controllers/RemoteCustomFieldController.php index 875a4f5..2c24a4b 100644 --- a/src/App/Http/Controllers/RemoteCustomFieldController.php +++ b/src/App/Http/Controllers/RemoteCustomFieldController.php @@ -52,8 +52,9 @@ public function index(): JsonResponse * * @append remote RemoteType * - * @param RemoteCustomFieldRequest $request + * @param RemoteCustomFieldRequest $request * @return JsonResponse + * @throws \Throwable */ public function store(RemoteCustomFieldRequest $request): JsonResponse { @@ -107,7 +108,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 +128,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 +144,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 b948fdb..16f7c14 100644 --- a/src/App/Models/RemoteType.php +++ b/src/App/Models/RemoteType.php @@ -80,13 +80,17 @@ private function fetchData(?string $value = null, bool $search = false) public function getRemoteData(?string $identifierValue = null) { - $cacheKey = 'remote_custom_field_' . $this->id . '_' . $identifierValue; + $cacheKey = 'remote_custom_field_' . $this->id; if (config('asseco-custom-fields.should_cache_remote') && Cache::has($cacheKey)) { return Cache::get($cacheKey); } $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..44abdf9 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,18 +14,19 @@ 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; } /** - * @param array $mappings - * @param array $item + * @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) { + $data[ $idProperty] = $item[ $idProperty ] ?? null; + } + return $data; } } From 1549cfb662888d768910433a872316825cc4181e Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 12 Sep 2025 11:47:11 +0000 Subject: [PATCH 3/4] Apply fixes from StyleCI --- src/App/Http/Controllers/RemoteCustomFieldController.php | 3 ++- src/App/Traits/TransformsOutput.php | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/App/Http/Controllers/RemoteCustomFieldController.php b/src/App/Http/Controllers/RemoteCustomFieldController.php index 2c24a4b..46e4027 100644 --- a/src/App/Http/Controllers/RemoteCustomFieldController.php +++ b/src/App/Http/Controllers/RemoteCustomFieldController.php @@ -52,8 +52,9 @@ public function index(): JsonResponse * * @append remote RemoteType * - * @param RemoteCustomFieldRequest $request + * @param RemoteCustomFieldRequest $request * @return JsonResponse + * * @throws \Throwable */ public function store(RemoteCustomFieldRequest $request): JsonResponse diff --git a/src/App/Traits/TransformsOutput.php b/src/App/Traits/TransformsOutput.php index 44abdf9..586444f 100644 --- a/src/App/Traits/TransformsOutput.php +++ b/src/App/Traits/TransformsOutput.php @@ -21,9 +21,9 @@ protected function transform(array $response, ?array $mappings, ?string $idPrope } /** - * @param array $mappings - * @param array $item - * @param string|null $idProperty + * @param array $mappings + * @param array $item + * @param string|null $idProperty * @return array */ protected function mapSingle(array $mappings, array $item, ?string $idProperty = null): array @@ -40,7 +40,7 @@ protected function mapSingle(array $mappings, array $item, ?string $idProperty = } if ($idProperty) { - $data[ $idProperty] = $item[ $idProperty ] ?? null; + $data[$idProperty] = $item[$idProperty] ?? null; } return $data; From 6e956597b7dfd862b7897b76b1733a381206b7c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Ga=C5=A1pari=C4=87?= Date: Fri, 12 Sep 2025 14:05:50 +0200 Subject: [PATCH 4/4] checks --- src/App/Traits/TransformsOutput.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/App/Traits/TransformsOutput.php b/src/App/Traits/TransformsOutput.php index 586444f..d615d12 100644 --- a/src/App/Traits/TransformsOutput.php +++ b/src/App/Traits/TransformsOutput.php @@ -39,8 +39,8 @@ protected function mapSingle(array $mappings, array $item, ?string $idProperty = ]); } - if ($idProperty) { - $data[$idProperty] = $item[$idProperty] ?? null; + if ($idProperty && array_key_exists($idProperty, $item) && !array_key_exists($idProperty, $data)) { + $data[$idProperty] = $item[$idProperty]; } return $data;