13 - Support partial updates and starting_amount in bucket API
This commit is contained in:
parent
969c97b165
commit
969a88bc53
3 changed files with 37 additions and 24 deletions
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue