Skip to content

Conversation

kofimokome
Copy link
Collaborator

No description provided.

Copy link

sourceant bot commented Sep 29, 2025

Code Review Summary

✨ This pull request introduces group functionality to transactions, enhances the handling of recurring transactions, and updates OpenAPI documentation. Key areas of change include the TransactionController, the new Groupable trait, and relevant database migrations and tests. The overall structure is improved by integrating group relationships and refining transaction updates.

🚀 Key Improvements

  • The addition of the Groupable trait (app/Traits/Groupable.php) provides a clean way to manage polymorphic group relationships for Transaction models, improving code organization.
  • The Transaction model (app/Models/Transaction.php) now correctly integrates the Groupable trait and includes group in its appended attributes, making group data easily accessible.
  • Improved validation in TransactionController's update method (app/Http/Controllers/API/v1/TransactionController.php) to prevent updated_at timestamps from being set to a value older than created_at.

💡 Minor Suggestions

  • Consider refactoring the updated_at validation logic in TransactionController into a private helper method to reduce duplication in both store and update methods.
  • For groups()->sync($data['group_id']), explicitly passing an array ([$data['group_id']]) is more explicit, especially since group_id is validated as a single integer.

🚨 Critical Issues

  • The change in app/Http/Traits/ApiQueryable.php from updated_at >= $date to updated_at > $date in the synced_since query might cause records updated at the exact synced_since timestamp to be missed during synchronization. Review if this change in behavior is intended and safe for all use cases.

Copy link

@sourceant sourceant bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review complete. See the overview comment for a summary.

@kofimokome kofimokome requested a review from nfebe September 29, 2025 20:01
Copy link
Contributor

@nfebe nfebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use groupables instead of modifying that table. The idea is that the number of things that can be grouped can change.

Copy link

sourceant bot commented Sep 30, 2025

🔍 Code Review

💡 1. **app/Http/Controllers/API/v1/TransactionController.php** (Lines 272-274) - CLARITY

When syncing a single group, it's clearer to explicitly pass an array to sync() to match its expected signature for many-to-many relationships, even though it may implicitly handle a single scalar value. This enhances readability and prevents potential issues if the type of group_id were to change (e.g., to an array of groups).

Suggested Code:

                if (isset($data['group_id'])) {{"\n"}}                    $transaction->groups()->sync([$data['group_id']]);{{"\n"}}                }

Current Code:

                if (isset($data['group_id'])) {{"\n"}}                    $transaction->groups()->sync($data['group_id']);{{"\n"}}                }
💡 2. **app/Http/Controllers/API/v1/TransactionController.php** (Lines 534-551) - REFACTOR

The logic to ensure updated_at is not older than created_at is duplicated within the store and update methods. Extracting this validation into a private helper method would improve maintainability and reduce redundancy. This helps keep controller actions lean and focused on their primary responsibility.

Suggested Code:

                $this->ensureUpdatedAtNotOlderThanCreatedAt($transaction, $validatedData);{{"\n"}}

Current Code:

                if (isset($validatedData['updated_at'])) {{"\n"}}                    // Ensure the updated at is always greater than the created at{{"\n"}}                    $created_at = $transaction->created_at;{{"\n"}}                    $updated_at = Carbon::parse($validatedData['updated_at']);{{"\n"}}                    if ($updated_at->lt($created_at)) {{"\n"}}                        // Remove the value so that laravel defaults to now();{{"\n"}}                        unset($validatedData['updated_at']);{{"\n"}}                    }{{"\n"}}                }{{"\n"}}
💡 3. **app/Http/Controllers/API/v1/TransactionController.php** (Lines 560-562) - CLARITY

Similar to the store method, explicitly passing an array to sync() for a single group ID ([$validatedData['group_id']]) clarifies the intent for the many-to-many relationship, enhancing code readability.

Suggested Code:

                if (isset($validatedData['group_id'])) {{"\n"}}                    $transaction->groups()->sync([$validatedData['group_id']]);{{"\n"}}                }

Current Code:

                if (isset($validatedData['group_id'])) {{"\n"}}                    $transaction->groups()->sync([$validatedData['group_id']]);{{"\n"}}                }
💡 4. **app/Http/Traits/ApiQueryable.php** (Line 25) - BUG

Changing from >= to > in the synced_since query for updated_at can potentially lead to missing records if the updated_at timestamp exactly matches the synced_since value. While this might be intentional to only fetch strictly newer records, it's worth noting the implication for data synchronization. If the intention is to include records updated at the exact synced_since time, >= should be used.

Suggested Code:

                $query = $query->where('updated_at', '>=', $date)->withTrashed();

Current Code:

                $query = $query->where('updated_at', '>', $date)->withTrashed();

Verdict: APPROVE

Posted as a comment because posting a review failed.

Copy link

@sourceant sourceant bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review complete. See the overview comment for a summary.

Copy link

@sourceant sourceant bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review complete. See the overview comment for a summary.

Copy link
Contributor

@nfebe nfebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue with updated_at and created_at does not only affect transactions. It affects all models that are syncable.

  • I would recommend creating a validation rule (validator) and using it in all the controllers validation logic.

  • It would have one message defined in one place and all you need to do is add it to the rules array.

]);
}

public function test_api_user_can_not_update_their_transactions_with_an_older_date()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function test_api_user_can_not_update_their_transactions_with_an_older_date()
public function test_api_user_can_not_update_their_transactions_with_an_older_date() // Is this name accurate?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you suggest a better name?

@kofimokome
Copy link
Collaborator Author

I would recommend creating a validation rule (validator) and using it in all the controllers validation logic.
It would have one message defined in one place and all you need to do is add it to the rules array.

  1. I can't make it a custom rule because I need to compare the updated_at in the request with the created_at from the model. Instead created a method for it.
  2. I decided to default to the current time if the updated_at in the request is less than the created_at from the model so that the syncing process should not fail.

@kofimokome kofimokome requested a review from nfebe October 2, 2025 18:43
@nfebe
Copy link
Contributor

nfebe commented Oct 4, 2025

I decided to default to the current time if the updated_at in the request is less than the created_at from the model so that the syncing process should not fail.

Is @austin047 aware?

@kofimokome kofimokome changed the title Minor Bug Fixes fix: Prevent synables from having an updated at greater than the created_at Oct 4, 2025
@kofimokome kofimokome changed the title fix: Prevent synables from having an updated at greater than the created_at fix: Prevent synables from having an updated at less than the created_at Oct 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants