An invoice PDF looks trivial until you add tenants, customers, and drafts. Then every download is an access-control decision. Here is how we handle it in StoreFlux, and where the edges are.
Who can download what
There are two download routes. Staff use the admin route, and customers use the storefront route for their own orders. Both run the same query handler.
The handler resolves the invoice first. For an order, it looks up the invoice that belongs to that order and the current tenant. If no invoice exists, the answer is not found, because there is nothing to render.
Then it checks ownership. A super admin can read across tenants. Everyone else must match the invoice's tenant, or the request throws an unauthorized tenant error.
A customer gets one more rule. They can only open invoices where the invoice's user is them, and a draft is never shown to a customer. For anyone else, the handler returns nothing, which becomes a not-found response.…