refactor: run invoice and expense admin through the use case gate #89
Qodo reviews are paused for this user.
Troubleshooting steps vary by plan Learn more →
On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →
Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →
Claude finished @smahelos's task in 24s —— View job
---
Review of PR #89
Summary
I found no bugs or security problems in the diff. I only read the diff and did not run the tests or checks. The 2765-test result comes from the PR description, and I did not confirm it.
…ForUser ports in favour of ReadScope / WriteScope is consistent. The new handlers (CreateExpense, UpdateExpense and the document-party queries) take the scope from the gate. The new allowed: route middleware names matching use cases. The new expense.create / expense.update permissions on the web guard have a catalog entry and a migration, as the add-permission workflow requires.user_id and passes the stored contact id through $request->attributes. That removes the second, ungated read that keepsContact() did before, and a changed owner can no longer cause a not-found error.1.
ExpenseFormFields disables $parties[0] by position. A lookup by field name would be safer. A test that posts an update without user_id would pin the controller's merge.2.
CreateExpenseHandler checks the entity limit only after logo and attachment handling. Cleanup covers a failure, but an early check would skip the wasted work.ExpenseUseCasesTest and InvoicePartiesAssemblerTest now run through the gate. I did not see a test for the new 403 when an admin stores an expense for an owner who lacks expense.create, nor for the picker routes' allowed: middleware. Please confirm those are covered.$parties[0] assumes the owner field is always the first entry returned by AdminDocumentFormFields::parties(). If that order changes, the wrong field is disabled and nothing flags it. Look the field up by name (user_id) or have parties() take a lockOwner flag. A disabled field is also not submitted, so the update depends entirely on the controller's merge(['user_id' => ...]). A test that posts an update without user_id would pin that.
Nit: the entity limit is checked inside the transaction, after the logo and attachments are processed (saveWithLogo and the upload callback). A user over their limit still triggers logo handling and, depending on the order, uploads before assertCanCreate throws. The cleanup in ExpenseAttachments covers that, but a cheap early assertCanCreate before the uploads avoids the wasted work. Keep the in-transaction check for the race.
Fixed in e4c85298: AdminDocumentFormFields::parties() now disables the user_id field itself when it gets a stored document, so neither ExpenseFormFields nor AdminInvoiceFormFields depends on the field position any more. The controller merge is already pinned by editing_keeps_the_stored_owner_whatever_owner_is_submitted (it submits another owner and asserts the stored one is kept).
Keeping the single check inside the transaction. assertCanCreate locks the owner's users row (SELECT … FOR UPDATE) and holds it until the insert commits; an early check outside the transaction would only duplicate it, and .claude/rules/permissions.md keeps the limit check once, inside the transaction. Attachments are uploaded after the check: $attachments() runs inside the transaction after assertCanCreate. Only the logo store/copy precedes it, and saveWithLogo deletes it in its finally when the save fails.
Claude finished @smahelos's task in 15s —— View job
---
Review of PR #89
I read the diff and the rules in AGENTS.md. I did not run the tests or the checks, so the 2765-test result is taken from the PR description.
No new bugs or security problems found.
…ForUser ports are replaced by ReadScope / WriteScope. CreateExpense and UpdateExpense use permissionWithOwner and run through the gate. CreateExpense implements CallsOutsideDatabaseInterface because it uploads files, and its handler opens the transaction only around the writes. The entity limit is checked inside that transaction, as application.md and permissions.md require.allowed: middleware naming the use case their action runs. This matches the existing GetEntityUsage pattern.user_id and passes the stored contact id through AdminDocumentPartyInput::STORED_CONTACT. This removes the second, ungated read.expense.create and expense.update have a PermissionCatalog entry and a sync migration, with an empty down() as the PR describes.Open points
1. The earlier inline comments on ExpenseFormFields ($parties[0] is positional) and on CreateExpenseHandler (the limit check runs after the file handling) still apply. Both are minor, and neither blocks the merge.
2. I did not see tests for these changes. Please confirm they exist, since the PR describes both as behaviour changes:
- the 403 when an admin stores or updates an expense for an owner without the expense permission;
- an expense update posted without user_id, which depends on the controller's merge.
3. The allowed: middleware on the picker and items routes is untested as far as I can see. A test that a user lacking the invoice and expense permissions gets 403 would pin it.
4. The admin edit page with the disabled owner field and the picker endpoints were not checked in a browser. Those are worth a manual look.
Re-review follow-up:
1. The two inline findings were answered before this re-review finished: the positional $parties[0] is fixed in e4c85298 (AdminDocumentFormFields::parties() disables user_id itself), and the early limit check is declined with the reason in the thread.
2. Tests of the expense behaviour changes:
- An update without user_id relies on the controller merge, pinned by editing_keeps_the_stored_owner_whatever_owner_is_submitted (it submits another owner and asserts the stored one is kept).
- The 403 for an owner without the expense permission was missing; added in 6f9f3d31 as an_owner_without_a_plan_role_gets_no_expense_whatever_the_admin_may_do. It fails (302) when CreateExpense declares Access::permission instead of permissionWithOwner.
3. The allowed: middleware on the picker and items routes is covered by DocumentPartyCrudControllerTest::guests_and_users_without_document_permissions_cannot_search and InvoiceItemsControllerTest::catalog_and_preview_require_invoice_permission.
Claude finished @smahelos's task in 10s —— View job
---
Review of PR #89 (after the latest two commits)
I read the new commits e4c8529 and 6f9f3d3. I did not run the tests or checks.
No new bugs or security problems found.
e4c8529: AdminDocumentFormFields::parties() now disables user_id itself when it gets a stored document. ExpenseFormFields and AdminInvoiceFormFields no longer depend on field position. This resolves my earlier $parties[0] comment.6f9f3d3: the new test covers the 403 when an admin stores an expense for an owner without the expense permission. This resolves my earlier test question for the store case.CreateExpenseHandler limit check: your reasoning holds. assertCanCreate locks the owner row and belongs once inside the transaction (permissions.md). I'm withdrawing the nit.Remaining, minor and non-blocking
1. The new 403 test only covers store. Please confirm the update path for an owner without the permission is covered somewhere, since the PR describes both.
2. I did not see a test for the allowed: middleware on the picker and items routes (403 for a user without the invoice and expense permissions).
3. The disabled owner field on the expense edit page and the picker endpoints were not checked in a browser.
Claude finished @smahelos's task in 10s —— View job
---
Review of PR #89 (after commit 122ee1d)
I read the new commit. I did not run the tests or checks.
No new bugs or security problems found.
122ee1d: an_update_for_an_owner_without_a_plan_role_is_refused puts an update for an owner without the expense permission. It asserts a 403 and that the stored description is still Kept. This closes my earlier question about the update path, so the 403 is now covered for both store and update.allowed: middleware on the picker and items routes (guests_and_users_without_document_permissions_cannot_search and catalog_and_preview_require_invoice_permission). I accept that.Still open (not blocking): the disabled owner field on the expense edit page and the picker endpoints were not checked in a browser. They are worth a quick manual look before merge.
Claude finished @smahelos's task in 9s —— View job
---
Review of PR #89 (after commit ddb240e)
I read the new commit. I did not run the tests or checks.
No new bugs or security problems found.
ddb240e: a docs-only change. It adds a docs/TECH_DEBT.md entry for the two admin browser scripts that fail on main, with where, problem and fix. It matches the file's existing format and the policy that rule violations and known debt are tracked there. It touches no code or tests.$parties[0] position, the 403 tests for store and update, the allowed: route tests). I accepted the limit-check reasoning earlier.Still open (not blocking): the disabled owner field on the expense edit page and the picker endpoints were not checked in a browser. The new debt entry says the browser scripts for admin document forms currently fail, so a manual look before merge is the only browser check available.
Manual browser check (branch at ddb240e1 code, billex-dev.local, development data, signed in as the admin):
/admin/expense/1/edit, owner User 1): the owner select is disabled and shows the stored owner; business, contact and account load from the stored expense.document-parties/businesses, contacts (also by id) and accounts (of a business and of a contact) answer 200 with the owner's records; a request without user_id answers 422.document-parties/contacts?q=… (200) and lists the owner's contacts; the previous request is aborted by Select2 while typing, as expected.tests/Browser/ fail the same way on main, see the new docs/TECH_DEBT.md entry.
smahelos
Summary
Phase 5b of the permission conversion (
docs/TECH_DEBT.md, step 11): the invoice and expense admin, the invoice items and the document party pickers run only through Commands and Queries behind the authorizing gate. No port takes a bareUserIdfor ownership any more: every…ForUserlookup is replaced by aReadScope/WriteScopethe gate issues.Use cases (
app/Application/Invoice/{Commands,Queries})CreateExpense,UpdateExpense(permissionWithOwnerexpense.create/update) with their own flow like invoices; only the attachment and logo upload/cleanup is shared inSupport/ExpenseAttachments.ListInvoiceCatalogProducts,PreviewInvoiceTotals(anyOf invoice.create/update).ListDocumentBusinesses,ListActiveDocumentBusinesses,ListDocumentContacts,ListDocumentBankAccounts,GetDocumentBusiness,GetDocumentContact,FindDefaultDocumentBusiness,FindDefaultDocumentBankAccount,FindDocumentContactDuplicates(anyOf invoice/expense create/update) andGetInvoiceParties(invoice.update). The adminDocumentPartyCrudControllerand the frontendDocumentPartyPickerboth run them.Ports: Party
findByIdForUser(business, contact, account),CatalogProductReadRepositoryInterface::searchForUser,InvoiceDtoReadRepositoryInterface::findByIdForUserandExpenseReadRepositoryInterface::findByIdForUserare gone; the assemblers andInvoiceProductOwnershiptake the scope.Requests:
authorize()ofExpenseRequest,InvoiceItemsRequestandDocumentPartySelectionRequestis replaced byallowed:on the routes.keepsContactof the admin invoice and expense requests reads the stored contact the controller passes in$request->attributes(AdminDocumentPartyInput), not a second ungated read.Deleted:
ExpenseApplicationService,InvoiceItemsApplicationService,InvoiceListingApplicationServicewith its interface,ExpenseAccessDeniedException,InvoiceAccessDeniedException,UserPermissionServiceInterfacewithLaravelUserPermissionService.Permissions:
expense.createandexpense.updateare added to thewebguard and both frontend roles (PermissionCatalog, migration2026_11_12_000002_add_web_expense_permissions).Behaviour changes
Expenses still live in the Invoice context; their split into an
Expensecontext (with the shared document assemblers moving to Party) follows in its own branch and is listed indocs/TECH_DEBT.md.Evidence
…ForUserports,UserPermissionServiceandauthorize()in form requests; the picker and the admin party endpoints read Eloquent or an ungated listing service.ddd:guard, the baselines (provider snapshot and controller thinness regenerated) and the dependency audit pass.ExpenseUseCasesTestruns a frontend user through the gate;InvoicePartiesAssemblerTestrunsCreateInvoice/UpdateInvoicethrough the gate instead of issuing a scope.Merge Danger
Door: two-way
One migration syncs the permission catalog (
down()is empty: reverting the code leaves the two extrawebpermissions in the database, unused). Reverting the commit restores the deleted services. Billex is not in production.🤖 Generated with Claude Code