Skip to content

Commit 77cd98c

Browse files
Add identifier_property into mappedData
- Always include identifier_property into mapped data - Cache
1 parent 562acd3 commit 77cd98c

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

src/App/Http/Controllers/RemoteCustomFieldController.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public function index(): JsonResponse
5454
*
5555
* @param RemoteCustomFieldRequest $request
5656
* @return JsonResponse
57+
*
58+
* @throws \Throwable
5759
*/
5860
public function store(RemoteCustomFieldRequest $request): JsonResponse
5961
{
@@ -107,7 +109,7 @@ public function resolve(RemoteType $remoteType): JsonResponse
107109
$data = $remoteType->getRemoteData();
108110

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

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

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

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

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

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

148150
return response()->json($transformed);
149151
}

src/App/Models/RemoteType.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ public function getRemoteData(?string $identifierValue = null)
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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
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;
@@ -23,9 +23,10 @@ protected function transform(array $response, ?array $mappings): array
2323
/**
2424
* @param array $mappings
2525
* @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 && array_key_exists($idProperty, $item) && !array_key_exists($idProperty, $data)) {
43+
$data[$idProperty] = $item[$idProperty];
44+
}
45+
4146
return $data;
4247
}
4348
}

0 commit comments

Comments
 (0)