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,34 +38,23 @@ class ServicesController extends Controller
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 = [];
$conflicts = [];
foreach ($urls as $url) {
$name = data_get($url, 'name');
$urls = data_get($url, 'url');
if (blank($name)) {
$errors[] = 'Service container name is required to apply URLs.';
continue;
$urls = collect($urlsArray)->flatMap(function ($item) {
$urlValue = data_get($item, 'url');
if (blank($urlValue)) {
return [];
}
$application = $service->applications()->where('name', $name)->first();
if (! $application) {
$errors[] = "Service container with '{$name}' not found.";
return str($urlValue)->replaceStart(',', '')->replaceEnd(',', '')->trim()->explode(',')->map(fn ($url) => trim($url))->filter();
});
continue;
}
if (filled($urls)) {
$urls = str($urls)->replaceStart(',', '')->replaceEnd(',', '')->trim();
$urls = str($urls)->explode(',')->map(function ($url) use (&$errors) {
$url = trim($url);
$urls = $urls->map(function ($url) use (&$errors) {
if (! filter_var($url, FILTER_VALIDATE_URL)) {
$errors[] = 'Invalid URL: '.$url;
$errors[] = "Invalid URL: {$url}";
return $url;
}
@@ -74,34 +63,60 @@ class ServicesController extends Controller
$errors[] = "Invalid URL scheme: {$scheme} for URL: {$url}. Only http and https are supported.";
}
return str($url)->lower();
return $url;
});
if (count($errors) > 0) {
continue;
$duplicates = $urls->duplicates()->unique()->values();
if ($duplicates->isNotEmpty() && ! $forceDomainOverride) {
$errors[] = 'The current request contains conflicting URLs across containers: '.implode(', ', $duplicates->toArray());
}
$result = checkIfDomainIsAlreadyUsedViaAPI($urls, $teamId, $application->uuid);
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)) {
$errors[] = 'Service container name is required to apply URLs.';
return;
}
$application = $service->applications()->where('name', $name)->first();
if (! $application) {
$errors[] = "Service container with '{$name}' not found.";
return;
}
if (filled($containerUrls)) {
$containerUrls = str($containerUrls)->replaceStart(',', '')->replaceEnd(',', '')->trim();
$containerUrls = str($containerUrls)->explode(',')->map(fn ($url) => str(trim($url))->lower());
$result = checkIfDomainIsAlreadyUsedViaAPI($containerUrls, $teamId, $application->uuid);
if (isset($result['error'])) {
$errors[] = $result['error'];
continue;
return;
}
if ($result['hasConflicts'] && ! $forceDomainOverride) {
$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 {
$urls = null;
$containerUrls = null;
}
$application->fqdn = $urls;
$application->fqdn = $containerUrls;
$application->save();
}
});
if (! empty($errors)) {
return ['errors' => $errors];