Skip to content

refactor: eliminate database queries and business logic from views#4

Open
cezargf wants to merge 1 commit into
masterfrom
fix-view-performance-2373530467167720773
Open

refactor: eliminate database queries and business logic from views#4
cezargf wants to merge 1 commit into
masterfrom
fix-view-performance-2373530467167720773

Conversation

@cezargf
Copy link
Copy Markdown
Owner

@cezargf cezargf commented May 16, 2026

This pull request refactors several view files to improve performance and adhere to MVC patterns. Views were making direct database queries or calling model logic inside loops, causing N+1 query inefficiencies.

Key changes:

  • painel.php: count_all database calls were moved to the Mapos controller.
  • os.php & vendas.php: Removed isEditable model method calls inside loops, replacing them with in-memory checks using loop variables and the automatically injected global configuration ($configuration['control_editos'], $configuration['control_edit_vendas']).
  • visualizarOs.php & editarOs.php: Shifted the generation of $texto_de_notificacao and $zapnumber to the controller where data formatting belongs.

PR created automatically by Jules for task 2373530467167720773 started by @cezargf

- Moved `$this->db->count_all` queries from `painel.php` to the `Mapos` controller to prevent views from directly querying the DB.
- Replaced model calls like `isEditable` within loops in `os.php` and `vendas.php` with inline evaluations using existing row data and global configurations. This resolves severe N+1 query problems.
- Migrated business logic (calculating `$texto_de_notificacao` via `criarTextoWhats`) from `visualizarOs.php` and `editarOs.php` to the `Os` controller.

Co-authored-by: cezargf <25113573+cezargf@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses N+1 query vulnerabilities by moving database counts and editability logic from views to controllers or inline checks. It also refactors WhatsApp notification generation by moving the logic from views to the Os controller. Review feedback identifies a security regression in the os.php view due to a missing permission check, potential bugs when formatting null dates in the Os controller, and suggests further refactoring to reduce logic duplication and adhere to MVC principles.

Comment thread application/views/os/os.php
Comment thread application/controllers/Os.php
Comment thread application/controllers/Os.php
Comment thread application/controllers/Os.php
Comment thread application/views/vendas/vendas.php
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.

1 participant