fix(api): show an error if the same 2 urls are provided

This commit is contained in:
peaklabs-dev
2026-01-13 20:04:44 +01:00
parent 0628268875
commit c5196e12d2

View File

@@ -38,70 +38,85 @@ class ServicesController extends Controller
return serializeApiResponse($service); return serializeApiResponse($service);
} }
private function applyServiceUrls(Service $service, array $urls, string $teamId, bool $forceDomainOverride = false): ?array private function applyServiceUrls(Service $service, array $urlsArray, string $teamId, bool $forceDomainOverride = false): ?array
{ {
$errors = []; $errors = [];
$conflicts = []; $conflicts = [];
foreach ($urls as $url) { $urls = collect($urlsArray)->flatMap(function ($item) {
$name = data_get($url, 'name'); $urlValue = data_get($item, 'url');
$urls = data_get($url, 'url'); if (blank($urlValue)) {
return [];
}
return str($urlValue)->replaceStart(',', '')->replaceEnd(',', '')->trim()->explode(',')->map(fn ($url) => trim($url))->filter();
});
$urls = $urls->map(function ($url) use (&$errors) {
if (! filter_var($url, FILTER_VALIDATE_URL)) {
$errors[] = "Invalid URL: {$url}";
return $url;
}
$scheme = parse_url($url, PHP_URL_SCHEME) ?? '';
if (! in_array(strtolower($scheme), ['http', 'https'])) {
$errors[] = "Invalid URL scheme: {$scheme} for URL: {$url}. Only http and https are supported.";
}
return $url;
});
$duplicates = $urls->duplicates()->unique()->values();
if ($duplicates->isNotEmpty() && ! $forceDomainOverride) {
$errors[] = 'The current request contains conflicting URLs across containers: '.implode(', ', $duplicates->toArray());
}
if (count($errors) > 0) {
return ['errors' => $errors];
}
collect($urlsArray)->each(function ($item) use ($service, $teamId, $forceDomainOverride, &$errors, &$conflicts) {
$name = data_get($item, 'name');
$containerUrls = data_get($item, 'url');
if (blank($name)) { if (blank($name)) {
$errors[] = 'Service container name is required to apply URLs.'; $errors[] = 'Service container name is required to apply URLs.';
continue; return;
} }
$application = $service->applications()->where('name', $name)->first(); $application = $service->applications()->where('name', $name)->first();
if (! $application) { if (! $application) {
$errors[] = "Service container with '{$name}' not found."; $errors[] = "Service container with '{$name}' not found.";
continue; return;
} }
if (filled($urls)) { if (filled($containerUrls)) {
$urls = str($urls)->replaceStart(',', '')->replaceEnd(',', '')->trim(); $containerUrls = str($containerUrls)->replaceStart(',', '')->replaceEnd(',', '')->trim();
$urls = str($urls)->explode(',')->map(function ($url) use (&$errors) { $containerUrls = str($containerUrls)->explode(',')->map(fn ($url) => str(trim($url))->lower());
$url = trim($url);
if (! filter_var($url, FILTER_VALIDATE_URL)) {
$errors[] = 'Invalid URL: '.$url;
return $url; $result = checkIfDomainIsAlreadyUsedViaAPI($containerUrls, $teamId, $application->uuid);
}
$scheme = parse_url($url, PHP_URL_SCHEME) ?? '';
if (! in_array(strtolower($scheme), ['http', 'https'])) {
$errors[] = "Invalid URL scheme: {$scheme} for URL: {$url}. Only http and https are supported.";
}
return str($url)->lower();
});
if (count($errors) > 0) {
continue;
}
$result = checkIfDomainIsAlreadyUsedViaAPI($urls, $teamId, $application->uuid);
if (isset($result['error'])) { if (isset($result['error'])) {
$errors[] = $result['error']; $errors[] = $result['error'];
continue; return;
} }
if ($result['hasConflicts'] && ! $forceDomainOverride) { if ($result['hasConflicts'] && ! $forceDomainOverride) {
$conflicts = array_merge($conflicts, $result['conflicts']); $conflicts = array_merge($conflicts, $result['conflicts']);
continue; return;
} }
$urls = $urls->filter(fn ($u) => filled($u))->unique()->implode(','); $containerUrls = $containerUrls->filter(fn ($u) => filled($u))->unique()->implode(',');
} else { } else {
$urls = null; $containerUrls = null;
} }
$application->fqdn = $urls; $application->fqdn = $containerUrls;
$application->save(); $application->save();
} });
if (! empty($errors)) { if (! empty($errors)) {
return ['errors' => $errors]; return ['errors' => $errors];