|
3 | 3 | use App\Enums\BuildPackTypes; |
4 | 4 | use App\Enums\RedirectTypes; |
5 | 5 | use App\Enums\StaticImageTypes; |
| 6 | +use App\Models\Environment; |
6 | 7 | use App\Rules\ValidGitBranch; |
7 | 8 | use App\Support\ValidationPatterns; |
8 | 9 | use Illuminate\Database\Eloquent\Collection; |
9 | 10 | use Illuminate\Database\Eloquent\Model; |
| 11 | +use Illuminate\Http\JsonResponse; |
10 | 12 | use Illuminate\Http\Request; |
| 13 | +use Illuminate\Support\Facades\Gate; |
| 14 | +use Illuminate\Support\Facades\Validator; |
11 | 15 | use Illuminate\Validation\Rule; |
12 | 16 |
|
13 | 17 | function getTeamIdFromToken() |
@@ -172,6 +176,64 @@ function sharedDataApplications() |
172 | 176 | ]; |
173 | 177 | } |
174 | 178 |
|
| 179 | +function moveResourceToEnvironment(Request $request, $resource, string $resourceType, int $teamId): JsonResponse |
| 180 | +{ |
| 181 | + |
| 182 | + $validator = Validator::make($request->all(), [ |
| 183 | + 'environment_uuid' => 'required|string', |
| 184 | + ]); |
| 185 | + |
| 186 | + if ($validator->fails()) { |
| 187 | + return response()->json([ |
| 188 | + 'message' => 'Validation failed.', |
| 189 | + 'errors' => $validator->errors(), |
| 190 | + ], 422); |
| 191 | + } |
| 192 | + |
| 193 | + $extraFields = array_diff(array_keys($request->all()), ['environment_uuid']); |
| 194 | + if (! empty($extraFields)) { |
| 195 | + return response()->json([ |
| 196 | + 'message' => 'Validation failed.', |
| 197 | + 'errors' => collect($extraFields)->mapWithKeys(fn ($field) => [$field => 'This field is not allowed.'])->toArray(), |
| 198 | + ], 422); |
| 199 | + } |
| 200 | + |
| 201 | + $newEnvironment = Environment::ownedByCurrentTeamAPI($teamId) |
| 202 | + ->whereUuid($request->environment_uuid) |
| 203 | + ->first(); |
| 204 | + |
| 205 | + if (! $newEnvironment) { |
| 206 | + return response()->json(['message' => 'Target environment not found or not owned by your team.'], 404); |
| 207 | + } |
| 208 | + |
| 209 | + Gate::authorize('update', $newEnvironment); |
| 210 | + |
| 211 | + if ($resource->environment_id === $newEnvironment->id) { |
| 212 | + return response()->json(['message' => "$resourceType is already in this environment."], 400); |
| 213 | + } |
| 214 | + |
| 215 | + $oldEnvironment = $resource->environment()->with('project')->first(); |
| 216 | + |
| 217 | + $resource->update(['environment_id' => $newEnvironment->id]); |
| 218 | + |
| 219 | + auditLog('api.'.str($resourceType)->lower()->value().'.moved', [ |
| 220 | + 'team_id' => $teamId, |
| 221 | + 'resource_uuid' => $resource->uuid, |
| 222 | + 'resource_type' => str($resourceType)->lower()->value(), |
| 223 | + 'from_project_uuid' => $oldEnvironment?->project?->uuid, |
| 224 | + 'from_environment_uuid' => $oldEnvironment?->uuid, |
| 225 | + 'to_project_uuid' => $newEnvironment->project->uuid, |
| 226 | + 'to_environment_uuid' => $newEnvironment->uuid, |
| 227 | + ]); |
| 228 | + |
| 229 | + return response()->json([ |
| 230 | + 'message' => "$resourceType moved successfully.", |
| 231 | + 'uuid' => $resource->uuid, |
| 232 | + 'project_uuid' => $newEnvironment->project->uuid, |
| 233 | + 'environment_uuid' => $newEnvironment->uuid, |
| 234 | + ]); |
| 235 | +} |
| 236 | + |
175 | 237 | function validateIncomingRequest(Request $request) |
176 | 238 | { |
177 | 239 | // check if request is json |
|
0 commit comments