Skip to content

Commit abfc5e8

Browse files
committed
always include identifier_property in mapSingle result
1 parent 5af44bf commit abfc5e8

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

src/App/Http/Controllers/RemoteCustomFieldController.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ public function index(): JsonResponse
5252
*
5353
* @append remote RemoteType
5454
*
55-
* @param RemoteCustomFieldRequest $request
55+
* @param RemoteCustomFieldRequest $request
5656
* @return JsonResponse
57+
* @throws \Throwable
5758
*/
5859
public function store(RemoteCustomFieldRequest $request): JsonResponse
5960
{
@@ -107,7 +108,7 @@ public function resolve(RemoteType $remoteType): JsonResponse
107108
$data = $remoteType->getRemoteData();
108109

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

112113
return response()->json($transformed);
113114
}
@@ -127,7 +128,7 @@ public function resolveByIdentifierValue(RemoteType $remoteType, string $identif
127128

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

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

132133
return response()->json($transformed);
133134
}
@@ -143,7 +144,7 @@ public function search(Request $request, RemoteType $remoteType, string $q = '')
143144
}
144145

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

148149
return response()->json($transformed);
149150
}

src/App/Models/RemoteType.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,17 @@ private function fetchData(?string $value = null, bool $search = false)
8080

8181
public function getRemoteData(?string $identifierValue = null)
8282
{
83-
$cacheKey = 'remote_custom_field_' . $this->id . '_' . $identifierValue;
83+
$cacheKey = 'remote_custom_field_' . $this->id;
8484
if (config('asseco-custom-fields.should_cache_remote') && Cache::has($cacheKey)) {
8585
return Cache::get($cacheKey);
8686
}
8787

8888
$response = $this->fetchData($identifierValue, false);
89-
Cache::put($cacheKey, $response, config('asseco-custom-fields.remote_cache_ttl'));
89+
90+
if (!$identifierValue) {
91+
// cache only all
92+
Cache::put($cacheKey, $response, config('asseco-custom-fields.remote_cache_ttl'));
93+
}
9094

9195
return $response;
9296
}

src/App/Traits/TransformsOutput.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,27 @@
66

77
trait TransformsOutput
88
{
9-
protected function transform(array $response, ?array $mappings): array
9+
protected function transform(array $response, ?array $mappings, ?string $idProperty = null): array
1010
{
1111
if (!$mappings) {
1212
return $response;
1313
}
1414

1515
$transformed = [];
1616
foreach ($response as $item) {
17-
$transformed[] = $this->mapSingle($mappings, $item);
17+
$transformed[] = $this->mapSingle($mappings, $item, $idProperty);
1818
}
1919

2020
return $transformed;
2121
}
2222

2323
/**
24-
* @param array $mappings
25-
* @param array $item
24+
* @param array $mappings
25+
* @param array $item
26+
* @param string|null $idProperty
2627
* @return array
2728
*/
28-
protected function mapSingle(array $mappings, array $item): array
29+
protected function mapSingle(array $mappings, array $item, ?string $idProperty = null): array
2930
{
3031
$data = [];
3132
foreach ($mappings as $remoteKey => $localKey) {
@@ -38,6 +39,10 @@ protected function mapSingle(array $mappings, array $item): array
3839
]);
3940
}
4041

42+
if ($idProperty) {
43+
$data[ $idProperty] = $item[ $idProperty ] ?? null;
44+
}
45+
4146
return $data;
4247
}
4348
}

0 commit comments

Comments
 (0)