feat: Phase 3 - public registration pages and Mailwizz config
This commit is contained in:
@@ -13,34 +13,143 @@ class MailwizzApiController extends Controller
|
||||
{
|
||||
public function lists(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate(['api_key' => 'required|string']);
|
||||
$request->validate(['api_key' => ['required', 'string']]);
|
||||
|
||||
$response = Http::withHeaders([
|
||||
'X-Api-Key' => $request->api_key,
|
||||
'X-Api-Key' => $request->string('api_key')->toString(),
|
||||
])->get('https://www.mailwizz.nl/api/lists');
|
||||
|
||||
if ($response->failed()) {
|
||||
return response()->json(['error' => 'Invalid API key or connection failed'], 422);
|
||||
return response()->json(['message' => __('Invalid API key or connection failed.')], 422);
|
||||
}
|
||||
|
||||
return response()->json($response->json());
|
||||
$json = $response->json();
|
||||
if (is_array($json) && ($json['status'] ?? null) === 'error') {
|
||||
return response()->json(['message' => __('Invalid API key or connection failed.')], 422);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'lists' => $this->normalizeListsPayload(is_array($json) ? $json : []),
|
||||
]);
|
||||
}
|
||||
|
||||
public function fields(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'api_key' => 'required|string',
|
||||
'list_uid' => 'required|string',
|
||||
'api_key' => ['required', 'string'],
|
||||
'list_uid' => ['required', 'string'],
|
||||
]);
|
||||
|
||||
$listUid = $request->string('list_uid')->toString();
|
||||
$response = Http::withHeaders([
|
||||
'X-Api-Key' => $request->api_key,
|
||||
])->get("https://www.mailwizz.nl/api/lists/{$request->list_uid}/fields");
|
||||
'X-Api-Key' => $request->string('api_key')->toString(),
|
||||
])->get("https://www.mailwizz.nl/api/lists/{$listUid}/fields");
|
||||
|
||||
if ($response->failed()) {
|
||||
return response()->json(['error' => 'Failed to fetch list fields'], 422);
|
||||
return response()->json(['message' => __('Failed to fetch list fields.')], 422);
|
||||
}
|
||||
|
||||
return response()->json($response->json());
|
||||
$json = $response->json();
|
||||
if (is_array($json) && ($json['status'] ?? null) === 'error') {
|
||||
return response()->json(['message' => __('Failed to fetch list fields.')], 422);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'fields' => $this->normalizeFieldsPayload(is_array($json) ? $json : []),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{list_uid: string, name: string}>
|
||||
*/
|
||||
private function normalizeListsPayload(array $json): array
|
||||
{
|
||||
$out = [];
|
||||
$records = data_get($json, 'data.records');
|
||||
if (! is_array($records)) {
|
||||
return $out;
|
||||
}
|
||||
|
||||
foreach ($records as $row) {
|
||||
if (! is_array($row)) {
|
||||
continue;
|
||||
}
|
||||
$uid = data_get($row, 'general.list_uid') ?? data_get($row, 'list_uid');
|
||||
$name = data_get($row, 'general.name') ?? data_get($row, 'name');
|
||||
if (is_string($uid) && $uid !== '' && is_string($name) && $name !== '') {
|
||||
$out[] = ['list_uid' => $uid, 'name' => $name];
|
||||
}
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{tag: string, label: string, type_identifier: string, options: array<string, string>}>
|
||||
*/
|
||||
private function normalizeFieldsPayload(array $json): array
|
||||
{
|
||||
$out = [];
|
||||
$records = data_get($json, 'data.records');
|
||||
if (! is_array($records)) {
|
||||
return $out;
|
||||
}
|
||||
|
||||
foreach ($records as $row) {
|
||||
if (! is_array($row)) {
|
||||
continue;
|
||||
}
|
||||
$tag = data_get($row, 'tag');
|
||||
$label = data_get($row, 'label');
|
||||
$typeId = data_get($row, 'type.identifier');
|
||||
if (! is_string($tag) || $tag === '' || ! is_string($label)) {
|
||||
continue;
|
||||
}
|
||||
$typeIdentifier = is_string($typeId) ? $typeId : '';
|
||||
$rawOptions = data_get($row, 'options');
|
||||
$options = $this->normalizeFieldOptions($rawOptions);
|
||||
|
||||
$out[] = [
|
||||
'tag' => $tag,
|
||||
'label' => $label,
|
||||
'type_identifier' => $typeIdentifier,
|
||||
'options' => $options,
|
||||
];
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private function normalizeFieldOptions(mixed $rawOptions): array
|
||||
{
|
||||
if ($rawOptions === null || $rawOptions === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (is_string($rawOptions)) {
|
||||
$decoded = json_decode($rawOptions, true);
|
||||
if (is_array($decoded)) {
|
||||
$rawOptions = $decoded;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
if (! is_array($rawOptions)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$out = [];
|
||||
foreach ($rawOptions as $key => $value) {
|
||||
if (is_string($key) || is_int($key)) {
|
||||
$k = (string) $key;
|
||||
$out[$k] = is_scalar($value) ? (string) $value : '';
|
||||
}
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user