13 - Support partial updates and starting_amount in bucket API

This commit is contained in:
myrmidex 2026-03-21 09:43:50 +01:00
parent 969c97b165
commit 969a88bc53
3 changed files with 37 additions and 24 deletions

View file

@ -72,45 +72,50 @@ public function store(Request $request, Scenario $scenario): JsonResponse
public function update(Request $request, Bucket $bucket): JsonResponse public function update(Request $request, Bucket $bucket): JsonResponse
{ {
$validated = $request->validate([ $validated = $request->validate([
'name' => 'required|string|max:255', 'name' => 'sometimes|required|string|max:255',
'type' => 'required|in:'.implode(',', BucketTypeEnum::values()), 'type' => 'sometimes|required|in:'.implode(',', BucketTypeEnum::values()),
'allocation_type' => 'required|in:'.implode(',', BucketAllocationTypeEnum::values()), 'allocation_type' => 'sometimes|required|in:'.implode(',', BucketAllocationTypeEnum::values()),
'allocation_value' => 'nullable|numeric', 'allocation_value' => 'nullable|numeric',
'buffer_multiplier' => 'sometimes|numeric|min:0', 'buffer_multiplier' => 'sometimes|numeric|min:0',
'starting_amount' => 'sometimes|integer|min:0',
'priority' => 'nullable|integer|min:1', 'priority' => 'nullable|integer|min:1',
]); ]);
$type = BucketTypeEnum::from($validated['type']); $type = isset($validated['type']) ? BucketTypeEnum::from($validated['type']) : $bucket->type;
$allocationType = BucketAllocationTypeEnum::from($validated['allocation_type']); $allocationType = isset($validated['allocation_type']) ? BucketAllocationTypeEnum::from($validated['allocation_type']) : $bucket->allocation_type;
// Prevent changing overflow bucket's type away from overflow // Prevent changing overflow bucket's type away from overflow
// (changing TO overflow is handled by validateBucketTypeConstraints below) if (isset($validated['type']) && $bucket->type === BucketTypeEnum::OVERFLOW && $type !== BucketTypeEnum::OVERFLOW) {
if ($bucket->type === BucketTypeEnum::OVERFLOW && $type !== BucketTypeEnum::OVERFLOW) {
return response()->json([ return response()->json([
'message' => 'Validation failed.', 'message' => 'Validation failed.',
'errors' => ['type' => ['The overflow bucket\'s type cannot be changed.']], 'errors' => ['type' => ['The overflow bucket\'s type cannot be changed.']],
], 422); ], 422);
} }
$constraintError = $this->validateBucketTypeConstraints($type, $allocationType, $bucket->scenario, $bucket); // Run type/allocation constraint validation when relevant fields change
if ($constraintError) { if (isset($validated['type']) || isset($validated['allocation_type'])) {
return $constraintError; $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 // Validate allocation_value when it or allocation_type changes
$allocationValueRules = Bucket::allocationValueRules($allocationType); if (array_key_exists('allocation_value', $validated) || isset($validated['allocation_type'])) {
$request->validate([ $allocationValueRules = Bucket::allocationValueRules($allocationType);
'allocation_value' => $allocationValueRules, $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;
} }
// Handle priority change if needed // Handle priority change if needed
@ -231,6 +236,7 @@ private function formatBucketResponse(Bucket $bucket): array
'allocation_type_label' => $bucket->getAllocationTypeLabel(), 'allocation_type_label' => $bucket->getAllocationTypeLabel(),
'buffer_multiplier' => (float) $bucket->buffer_multiplier, 'buffer_multiplier' => (float) $bucket->buffer_multiplier,
'effective_capacity' => $bucket->getEffectiveCapacity(), 'effective_capacity' => $bucket->getEffectiveCapacity(),
'starting_amount' => $bucket->starting_amount,
'current_balance' => $bucket->getCurrentBalance(), 'current_balance' => $bucket->getCurrentBalance(),
'has_available_space' => $bucket->hasAvailableSpace(), 'has_available_space' => $bucket->hasAvailableSpace(),
'available_space' => $bucket->getAvailableSpace(), 'available_space' => $bucket->getAvailableSpace(),

View file

@ -21,6 +21,7 @@ public function toArray(Request $request): array
'allocation_type_label' => $this->getAllocationTypeLabel(), 'allocation_type_label' => $this->getAllocationTypeLabel(),
'buffer_multiplier' => (float) $this->buffer_multiplier, 'buffer_multiplier' => (float) $this->buffer_multiplier,
'effective_capacity' => $this->getEffectiveCapacity(), 'effective_capacity' => $this->getEffectiveCapacity(),
'starting_amount' => $this->starting_amount,
'current_balance' => $this->getCurrentBalance(), 'current_balance' => $this->getCurrentBalance(),
'has_available_space' => $this->hasAvailableSpace(), 'has_available_space' => $this->hasAvailableSpace(),
'available_space' => $this->getAvailableSpace(), 'available_space' => $this->getAvailableSpace(),

View file

@ -180,6 +180,12 @@ parameters:
count: 1 count: 1
path: app/Http/Resources/BucketResource.php 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\(\)\.$#' message: '#^Call to an undefined method App\\Http\\Resources\\BucketResource\:\:getEffectiveCapacity\(\)\.$#'
identifier: method.notFound identifier: method.notFound