diff --git a/app/Http/Controllers/BucketController.php b/app/Http/Controllers/BucketController.php index cab6a3c..e52b7b1 100644 --- a/app/Http/Controllers/BucketController.php +++ b/app/Http/Controllers/BucketController.php @@ -72,45 +72,50 @@ public function store(Request $request, Scenario $scenario): JsonResponse public function update(Request $request, Bucket $bucket): JsonResponse { $validated = $request->validate([ - 'name' => 'required|string|max:255', - 'type' => 'required|in:'.implode(',', BucketTypeEnum::values()), - 'allocation_type' => 'required|in:'.implode(',', BucketAllocationTypeEnum::values()), + 'name' => 'sometimes|required|string|max:255', + 'type' => 'sometimes|required|in:'.implode(',', BucketTypeEnum::values()), + 'allocation_type' => 'sometimes|required|in:'.implode(',', BucketAllocationTypeEnum::values()), 'allocation_value' => 'nullable|numeric', 'buffer_multiplier' => 'sometimes|numeric|min:0', + 'starting_amount' => 'sometimes|integer|min:0', 'priority' => 'nullable|integer|min:1', ]); - $type = BucketTypeEnum::from($validated['type']); - $allocationType = BucketAllocationTypeEnum::from($validated['allocation_type']); + $type = isset($validated['type']) ? BucketTypeEnum::from($validated['type']) : $bucket->type; + $allocationType = isset($validated['allocation_type']) ? BucketAllocationTypeEnum::from($validated['allocation_type']) : $bucket->allocation_type; // Prevent changing overflow bucket's type away from overflow - // (changing TO overflow is handled by validateBucketTypeConstraints below) - if ($bucket->type === BucketTypeEnum::OVERFLOW && $type !== BucketTypeEnum::OVERFLOW) { + if (isset($validated['type']) && $bucket->type === BucketTypeEnum::OVERFLOW && $type !== BucketTypeEnum::OVERFLOW) { return response()->json([ 'message' => 'Validation failed.', 'errors' => ['type' => ['The overflow bucket\'s type cannot be changed.']], ], 422); } - $constraintError = $this->validateBucketTypeConstraints($type, $allocationType, $bucket->scenario, $bucket); - if ($constraintError) { - return $constraintError; + // Run type/allocation constraint validation when relevant fields change + if (isset($validated['type']) || isset($validated['allocation_type'])) { + $constraintError = $this->validateBucketTypeConstraints($type, $allocationType, $bucket->scenario, $bucket); + if ($constraintError) { + return $constraintError; + } + + // Set allocation_value to null for unlimited buckets + if ($allocationType === BucketAllocationTypeEnum::UNLIMITED) { + $validated['allocation_value'] = null; + } + + // Buffer only applies to fixed_limit buckets — always reset on type change + if ($allocationType !== BucketAllocationTypeEnum::FIXED_LIMIT) { + $validated['buffer_multiplier'] = 0.0; + } } - // Validate allocation_value based on allocation_type - $allocationValueRules = Bucket::allocationValueRules($allocationType); - $request->validate([ - 'allocation_value' => $allocationValueRules, - ]); - - // Set allocation_value to null for unlimited buckets - if ($allocationType === BucketAllocationTypeEnum::UNLIMITED) { - $validated['allocation_value'] = null; - } - - // Buffer only applies to fixed_limit buckets — always reset on type change - if ($allocationType !== BucketAllocationTypeEnum::FIXED_LIMIT) { - $validated['buffer_multiplier'] = 0.0; + // Validate allocation_value when it or allocation_type changes + if (array_key_exists('allocation_value', $validated) || isset($validated['allocation_type'])) { + $allocationValueRules = Bucket::allocationValueRules($allocationType); + $request->validate([ + 'allocation_value' => $allocationValueRules, + ]); } // Handle priority change if needed @@ -231,6 +236,7 @@ private function formatBucketResponse(Bucket $bucket): array 'allocation_type_label' => $bucket->getAllocationTypeLabel(), 'buffer_multiplier' => (float) $bucket->buffer_multiplier, 'effective_capacity' => $bucket->getEffectiveCapacity(), + 'starting_amount' => $bucket->starting_amount, 'current_balance' => $bucket->getCurrentBalance(), 'has_available_space' => $bucket->hasAvailableSpace(), 'available_space' => $bucket->getAvailableSpace(), diff --git a/app/Http/Resources/BucketResource.php b/app/Http/Resources/BucketResource.php index ed08d94..8eec11e 100644 --- a/app/Http/Resources/BucketResource.php +++ b/app/Http/Resources/BucketResource.php @@ -21,6 +21,7 @@ public function toArray(Request $request): array 'allocation_type_label' => $this->getAllocationTypeLabel(), 'buffer_multiplier' => (float) $this->buffer_multiplier, 'effective_capacity' => $this->getEffectiveCapacity(), + 'starting_amount' => $this->starting_amount, 'current_balance' => $this->getCurrentBalance(), 'has_available_space' => $this->hasAvailableSpace(), 'available_space' => $this->getAvailableSpace(), diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 02f0b34..b93cf01 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -180,6 +180,12 @@ parameters: count: 1 path: app/Http/Resources/BucketResource.php + - + message: '#^Access to an undefined property App\\Http\\Resources\\BucketResource\:\:\$starting_amount\.$#' + identifier: property.notFound + count: 1 + path: app/Http/Resources/BucketResource.php + - message: '#^Call to an undefined method App\\Http\\Resources\\BucketResource\:\:getEffectiveCapacity\(\)\.$#' identifier: method.notFound