Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More reshuffling of scopes in assets API #13227

Merged
merged 1 commit into from
Jun 29, 2023
Merged

More reshuffling of scopes in assets API #13227

merged 1 commit into from
Jun 29, 2023

Conversation

snipe
Copy link
Owner

@snipe snipe commented Jun 29, 2023

Previously, because of the way the filters, searches and scopes were ordered, we'd end up with something like this when searching within the Pending, Deployed, etc meta statuses:

SELECT `assets`.* 
FROM   `assets` 
       LEFT JOIN `users` AS `assets_users` 
              ON `assets_users`.`id` = `assets`.`assigned_to` 
                 AND `assets`.`assigned_type` = 'App\Models\User' 
       LEFT JOIN `locations` AS `assets_locations` 
              ON `assets_locations`.`id` = `assets`.`assigned_to` 
                 AND `assets`.`assigned_type` = 'App\Models\Location' 
       LEFT JOIN `assets` AS `assigned_assets` 
              ON `assigned_assets`.`id` = `assets`.`assigned_to` 
                 AND `assets`.`assigned_type` = 'App\Models\Asset' 
WHERE  ( ( `assets`.`assigned_to` IS NOT NULL 
           AND `assets`.`name` LIKE '%750446712%' 
            OR `assets`.`asset_tag` LIKE '%750446712%' 
            OR `assets`.`serial` LIKE '%750446712%' 
            OR `assets`.`order_number` LIKE '%750446712%' 
            OR `assets`.`purchase_cost` LIKE '%750446712%' 
            OR `assets`.`notes` LIKE '%750446712%' 
            OR `assets`.`created_at` LIKE '%750446712%' 
            OR `assets`.`updated_at` LIKE '%750446712%' 
            OR `assets`.`purchase_date` LIKE '%750446712%' 
            OR `assets`.`expected_checkin` LIKE '%750446712%' 
            OR `assets`.`next_audit_date` LIKE '%750446712%' 
            OR `assets`.`last_audit_date` LIKE '%750446712%' 
            OR `assets`.`asset_eol_date` LIKE '%750446712%' 
            OR `assets`.`_snipeit_imei_1` LIKE '%750446712%' 
            OR `assets`.`_snipeit_phone_number_2` LIKE '%750446712%' 
            OR `assets`.`_snipeit_ram_3` LIKE '%750446712%' 
            OR `assets`.`_snipeit_cpu_4` LIKE '%750446712%' 
            OR `assets`.`_snipeit_mac_address_5` LIKE '%750446712%' 
            OR `assets`.`_snipeit_list_test_6` LIKE '%750446712%' 
            OR EXISTS (SELECT * 
                       FROM   `status_labels` 
                       WHERE  `assets`.`status_id` = `status_labels`.`id` 
                              AND `status_labels`.`name` LIKE '%750446712%' 
                              AND `status_labels`.`deleted_at` IS NULL) 
            OR EXISTS (SELECT * 
                       FROM   `suppliers` 
                       WHERE  `assets`.`supplier_id` = `suppliers`.`id` 
                              AND `suppliers`.`name` LIKE '%750446712%' 
                              AND `suppliers`.`deleted_at` IS NULL) 
            OR EXISTS (SELECT * 
                       FROM   `companies` 
                       WHERE  `assets`.`company_id` = `companies`.`id` 
                              AND `companies`.`name` LIKE '%750446712%') 
            OR EXISTS (SELECT * 
                       FROM   `locations` 
                       WHERE  `assets`.`rtd_location_id` = `locations`.`id` 
                              AND `locations`.`name` LIKE '%750446712%' 
                              AND `locations`.`deleted_at` IS NULL) 
            OR EXISTS (SELECT * 
                       FROM   `locations` 
                       WHERE  `assets`.`location_id` = `locations`.`id` 
                              AND `locations`.`name` LIKE '%750446712%' 
                              AND `locations`.`deleted_at` IS NULL) 
            OR EXISTS (SELECT * 
                       FROM   `models` 
                       WHERE  `assets`.`model_id` = `models`.`id` 
                              AND ( `models`.`name` LIKE '%750446712%' 
                                     OR `models`.`model_number` LIKE 
                                        '%750446712%' 
                                     OR `models`.`eol` LIKE '%750446712%' )) 
            OR EXISTS (SELECT * 
                       FROM   `models` 
                       WHERE  `assets`.`model_id` = `models`.`id` 
                              AND EXISTS (SELECT * 
                                          FROM   `categories` 
                                          WHERE  `models`.`category_id` = 
                                                 `categories`.`id` 
                                                 AND `categories`.`name` LIKE 
                                                     '%750446712%' 
                                                 AND `categories`.`deleted_at` 
                                                     IS NULL) 
                      ) 
            OR EXISTS (SELECT * 
                       FROM   `models` 
                       WHERE  `assets`.`model_id` = `models`.`id` 
                              AND EXISTS (SELECT * 
                                          FROM   `manufacturers` 
                                          WHERE  `models`.`manufacturer_id` = 
                                                 `manufacturers`.`id` 
                                                 AND `manufacturers`.`name` LIKE 
                                                     '%750446712%' 
                                                 AND 
                                         `manufacturers`.`deleted_at` IS 
                                         NULL 
                                         )) 
            OR `assets_users`.`first_name` LIKE '%750446712%' 
            OR `assets_users`.`last_name` LIKE '%750446712%' 
            OR `assets_users`.`username` LIKE '%750446712%' ) 
          OR ( ( Concat(assets_users.first_name, "", assets_users.last_name) 
                 LIKE 
                       '%750446712%' ) 
                OR `assets_locations`.`name` LIKE '%750446712%' 
                OR `assigned_assets`.`name` LIKE '%750446712%' ) ) 
       AND `assets`.`deleted_at` IS NULL 
ORDER  BY `image` ASC 
LIMIT  20 offset 0 

This line here:

WHERE  ( ( `assets`.`assigned_to` IS NOT NULL 
           AND `assets`.`name` LIKE '%750446712%' 
            OR `assets`.`asset_tag` LIKE '%750446712%' 

was making that query too broad and including out of scope results.

@shortcut-integration
Copy link

@what-the-diff
Copy link

what-the-diff bot commented Jun 29, 2023

PR Summary

  • Enhanced Filtering Mechanism in Assets
    The AssetsController.php file now has improved functionality that allows for a finer, more specific search within the assets, using specific filters or search inputs. This change will make locating assets smoother and more efficient.

  • Improved Asset Ordering and Sorting
    The logic behind ordering and sorting assets has been updated in the AssetsController.php file. This means assets can now be organized in a better way, thereby improving navigability and user experience.

  • Updated Offset and Limit Values Setting
    The AssetsController.php also saw changes in how offset and limit values are set up. This adjustment will bring further improvement in the way data is handled and displayed, ensuring better performance.

@spencerrlongg
Copy link
Collaborator

Been playing with it and haven't been able to recreate an out of scope search in develop or in the new branch, so I guess that's good? I think I'd need a specific example in which it was broken to verify that it's not anymore - but the code makes sense to me I think.

Search might be something I dive into relatively soon as I can see that having an effect on mobile.

@snipe snipe merged commit 8be3a69 into develop Jun 29, 2023
3 checks passed
@snipe snipe deleted the bug/sc-23433 branch June 29, 2023 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants