Skip to content

Refactor RewardOperation behavior and propagate reward update results #4738

Open

Description

@coderabbitai

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.php already defines Deduct, Restore, and Adjust.
  • app/Libraries/Reward_lib.php already accepts RewardOperation in adjustRewardPoints(), but calculates balances in a switch and returns true without checking the database update result.
  • app/Models/Customer.php implements update_reward_points_value() as an absolute balance setter with a void return type.
  • Adjust currently deducts the supplied amount, like Deduct. The suggested enum example instead treats Adjust as 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

  1. 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.
  2. Preserve the existing meaning of Adjust unless a deliberate contract change is agreed. If the contract changes, update all callers and tests.
  3. Make Customer::update_reward_points_value() return the QueryBuilder update result as bool if this setter remains the persistence path. Propagate persistence failure from Reward_lib::adjustRewardPoints() instead of returning unconditional success.
  4. Review the existing atomic delta-update method so this refactor does not weaken protection against concurrent deductions or negative balances.
  5. Keep numeric handling compatible with the persisted points type. Review the current integer setter parameter against float calculations without introducing silent truncation.
  6. Add or update coverage in tests/Libraries/Reward_libTest.php and 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 false through 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

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 pattern app/Constants/MVCEntityConstants.php, rather than a separate file for each enum under app/Enums/.
  • For rewards, use app/Constants/RewardsConstants.php to group RewardOperation with 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_lib as 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_lib alternative.
  • Rewards-specific declarations do not expand app/Config/Constants.php.
  • RewardOperation remains loadable from all callers and tests.

Source discussion: #4444 (comment)

Activity

  1. objecttothis commented on Oct 23, 2026

    @objecttothis
    Member

    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.php and no Reward_lib.php anywhere in master — both of which the issue describes as already existing with a RewardOperation enum / adjustRewardPoints() switch. Instead, the actual state of master is:

    • app/Models/Customer.php::adjustRewardPoints(int $customerId, float $delta): bool — an atomic, signed-delta update that already returns a bool result and is covered by tests/Models/CustomerRewardPointsTest.php (including a concurrency/race regression test via tests/Support/RaceWorker.php).
    • app/Models/Sale.php calls $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 RewardOperation enum / 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 for RewardOperation or RewardsConstants anywhere in the codebase. This work still appears open, though note the "Current behavior" section of the issue no longer accurately reflects master (there is no switch-based Reward_lib::adjustRewardPoints() to refactor — the atomic/bool-returning model-level method already exists).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions