Repository navigation
Open
Description
Summary
Refactor reward-operation behavior into the existing RewardOperation enum and propagate database update results through the reward library.
Requested by @objecttothis as a follow-up to PR #4444. Leave this issue unassigned.
Current behavior
app/Enums/RewardOperation.phpalready definesDeduct,Restore, andAdjust.app/Libraries/Reward_lib.phpalready acceptsRewardOperationinadjustRewardPoints(), but calculates balances in a switch and returnstruewithout checking the database update result.app/Models/Customer.phpimplementsupdate_reward_points_value()as an absolute balance setter with avoidreturn type.Adjustcurrently deducts the supplied amount, likeDeduct. The suggested enum example instead treatsAdjustas an absolute replacement. Resolve this semantic difference explicitly before implementation.- The Customer model also exposes an atomic signed-delta method,
adjustRewardPoints(). Account for this existing method when choosing the persistence approach.
Required changes
- Move the operation-specific calculation into the existing enum, for example through an
apply()method, while retaining customer validation and insufficient-points checks in the appropriate service or persistence layer. - Preserve the existing meaning of
Adjustunless a deliberate contract change is agreed. If the contract changes, update all callers and tests. - Make
Customer::update_reward_points_value()return the QueryBuilder update result asboolif this setter remains the persistence path. Propagate persistence failure fromReward_lib::adjustRewardPoints()instead of returning unconditional success. - Review the existing atomic delta-update method so this refactor does not weaken protection against concurrent deductions or negative balances.
- Keep numeric handling compatible with the persisted points type. Review the current integer setter parameter against float calculations without introducing silent truncation.
- Add or update coverage in
tests/Libraries/Reward_libTest.phpand relevant Customer model tests.
Rationale
The enum already provides type safety and IDE support. Encapsulating operation behavior removes duplicated branching. Propagating the database result prevents callers from treating failed writes as successful reward updates.
Acceptance criteria
- Operations continue to use
RewardOperation, not raw strings. - Each enum case has documented, tested behavior.
- Deduct and restore operations produce the expected balances.
- Adjust semantics are explicit and covered by tests.
- Invalid customers, zero amounts, and insufficient balances retain the intended validation behavior.
- Database update failures return
falsethrough the reward-library API. - Balance updates do not introduce negative balances or weaken existing concurrency safeguards.
- Code follows PHP 8.2+, PSR-12, and the repository's import conventions.
References
- PR: Fix customer reward points consistency #4444
- Review discussion: Fix customer reward points consistency #4444 (comment)
Enum and constants organization guidance
Requested by @objecttothis in the linked discussion below. This guidance supplements the existing refactor requirements; the current path in the current-behavior section is not a required final location.
- Prefer entity-specific files under
app/Constants/, using the patternapp/Constants/MVCEntityConstants.php, rather than a separate file for each enum underapp/Enums/. - For rewards, use
app/Constants/RewardsConstants.phpto groupRewardOperationwith other rewards-specific constants. - Keep domain-specific constants and enums out of
app/Config/Constants.php. The goal is to reduce class clutter without expanding the global constants file or creating one file per enum. - The requester also proposed keeping the enum with
Reward_libas an alternative. Evaluate that alternative within PHP's declaration and autoloading constraints. PHP does not support declaring an enum inside a class. - Update namespace imports, loading, callers, and tests for the selected organization. Preserve enum type safety and the behavior requirements already listed in this issue.
Additional acceptance criteria:
- The selected organization follows the entity-specific grouping guidance, or the agreed
Reward_libalternative. - Rewards-specific declarations do not expand
app/Config/Constants.php. RewardOperationremains loadable from all callers and tests.
Source discussion: #4444 (comment)
Metadata
Metadata
Assignees
Labels
No labels
Activity
objecttothis commentedon Oct 23, 2026
Claude Issue Checker Agent (cc @objecttothis)
Investigated against current
master.Status: Still present (refactor not done — and the issue's described "current behavior" is already stale).
A repo-wide search found no
app/Enums/RewardOperation.phpand noReward_lib.phpanywhere inmaster— both of which the issue describes as already existing with aRewardOperationenum /adjustRewardPoints()switch. Instead, the actual state ofmasteris:app/Models/Customer.php::adjustRewardPoints(int $customerId, float $delta): bool— an atomic, signed-delta update that already returns aboolresult and is covered bytests/Models/CustomerRewardPointsTest.php(including a concurrency/race regression test viatests/Support/RaceWorker.php).app/Models/Sale.phpcalls$customer->adjustRewardPoints(...)directly and checks its boolean result (if (! $customer->adjustRewardPoints(...)) { $this->db->transRollback(); ... }), i.e. persistence failures are already propagated at this call site.So the specific
RewardOperationenum /Reward_lib::adjustRewardPoints()refactor this issue asks for has not been implemented, and the organizational guidance (e.g.app/Constants/RewardsConstants.php) hasn't been applied either — no matches forRewardOperationorRewardsConstantsanywhere in the codebase. This work still appears open, though note the "Current behavior" section of the issue no longer accurately reflectsmaster(there is no switch-basedReward_lib::adjustRewardPoints()to refactor — the atomic/bool-returning model-level method already exists).