chore: Refactor checkIfDomainIsAlreadyUsed function

This commit is contained in:
Andras Bacsai 2024-07-11 10:02:35 +02:00
parent 7c4a722d72
commit 6b08100819

View File

@ -2168,7 +2168,7 @@ function ip_match($ip, $cidrs, &$match = null)
return false; return false;
} }
function checkIfDomainIsAlreadyUsed(Collection|array $domains, ?string $teamId, string $uuid) function checkIfDomainIsAlreadyUsed(Collection|array $domains, ?string $teamId = null, ?string $uuid = null)
{ {
if (is_null($teamId)) { if (is_null($teamId)) {
return response()->json(['error' => 'Team ID is required.'], 400); return response()->json(['error' => 'Team ID is required.'], 400);
@ -2184,8 +2184,12 @@ function checkIfDomainIsAlreadyUsed(Collection|array $domains, ?string $teamId,
return str($domain); return str($domain);
}); });
$applications = Application::ownedByCurrentTeamAPI($teamId)->get(['fqdn', 'uuid'])->filter(fn ($app) => $app->uuid !== $uuid); $applications = Application::ownedByCurrentTeamAPI($teamId)->get(['fqdn', 'uuid']);
$serviceApplications = ServiceApplication::ownedByCurrentTeamAPI($teamId)->get(['fqdn', 'uuid'])->filter(fn ($app) => $app->uuid !== $uuid); $serviceApplications = ServiceApplication::ownedByCurrentTeamAPI($teamId)->get(['fqdn', 'uuid']);
if ($uuid) {
$applications = $applications->filter(fn ($app) => $app->uuid !== $uuid);
$serviceApplications = $serviceApplications->filter(fn ($app) => $app->uuid !== $uuid);
}
$domainFound = false; $domainFound = false;
foreach ($applications as $app) { foreach ($applications as $app) {
if (is_null($app->fqdn)) { if (is_null($app->fqdn)) {