From 46ca19c2cca6b698daea47d11368f424728776e9 Mon Sep 17 00:00:00 2001 From: Guillermo Giuliana Date: Thu, 23 Jan 2020 22:26:32 -0300 Subject: [PATCH 1/2] add owner filter to ticketSearch BE --- server/controllers/ticket/search.php | 27 +++++++++++- server/data/ERRORS.php | 41 +++++++++++++++++++ server/libs/validations/validOwnersId.php | 21 ++++++++++ .../tests/controllers/ticket/searchTest.php | 23 +++++++++++ 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 server/libs/validations/validOwnersId.php diff --git a/server/controllers/ticket/search.php b/server/controllers/ticket/search.php index 1f9c37f6..f9f987c5 100644 --- a/server/controllers/ticket/search.php +++ b/server/controllers/ticket/search.php @@ -27,6 +27,7 @@ DataValidator::with('CustomValidations', true); * @apiParam {String} query A string to find into a ticket to make a custom search. * @apiParam {Number} page The number of the page of the tickets. * @apiParam {Object} orderBy A object {value, asc}with string and boolean to make a especific order of the search. + * @apiParam {Number[]} owners The ids of the owners to make a custom search. * * @apiUse NO_PERMISSION * @apiUse INVALID_TAG_FILTER @@ -86,6 +87,10 @@ class SearchController extends Controller { 'validation' => DataValidator::oneOf(DataValidator::validAuthorsId(),DataValidator::nullType()), 'error' => ERRORS::INVALID_AUTHOR_FILTER ], + 'owners' => [ + 'validation' => DataValidator::oneOf(DataValidator::validOwnersId(),DataValidator::nullType()), + 'error' => ERRORS::INVALID_OWNER_FILTER + ], 'assigned' => [ 'validation' => DataValidator::oneOf(DataValidator::in(['0','1']),DataValidator::nullType()), 'error' => ERRORS::INVALID_ASSIGNED_FILTER @@ -107,6 +112,7 @@ class SearchController extends Controller { 'dateRange' => json_decode(Controller::request('dateRange')), 'departments' => json_decode(Controller::request('departments')), 'authors' => json_decode(Controller::request('authors'),true), + 'owners' => json_decode(Controller::request('owners')), 'assigned' => Controller::request('assigned'), 'query' => Controller::request('query'), 'orderBy' => json_decode(Controller::request('orderBy'),true), @@ -177,6 +183,7 @@ class SearchController extends Controller { $this->setDepartmentFilter($inputs['departments'],$inputs['allowedDepartments'], $inputs['staffId'], $filters); } if(array_key_exists('authors',$inputs)) $this->setAuthorFilter($inputs['authors'], $filters); + if(array_key_exists('owners',$inputs)) $this->setOwnerFilter($inputs['owners'], $filters); if(array_key_exists('query',$inputs)) $this->setStringFilter($inputs['query'], $filters); if($filters != "") $filters = " WHERE " . $filters; } @@ -289,9 +296,27 @@ class SearchController extends Controller { $filters .= "ticket.author_id = " . $author['id']; } } + $filters .= ")"; + } + } + + private function setOwnerFilter($owners, &$filters){ + if($owners){ + $first = TRUE; + if ($filters != "") $filters .= " and "; + + foreach($owners as $owner){ + + if($first){ + $filters .= "("; + $first = FALSE; + } else { + $filters .= " or "; + } + $filters .= "ticket.owner_id = " . $owner; + } $filters .= ")"; - } } diff --git a/server/data/ERRORS.php b/server/data/ERRORS.php index 34338349..d970ad21 100755 --- a/server/data/ERRORS.php +++ b/server/data/ERRORS.php @@ -91,6 +91,46 @@ * @apiDefine INVALID_QUERY * @apiError {String} INVALID_QUERY The query is invalid. */ +/** + * @apiDefine INVALID_TAG_FILTER + * @apiError {String} INVALID_TAG_FILTER The tag filter is invalid. + */ +/** + * @apiDefine INVALID_CLOSED_FILTER + * @apiError {String} INVALID_CLOSED_FILTER The closed filter is invalid. + */ +/** + * @apiDefine INVALID_UNREAD_STAFF_FILTER + * @apiError {String} INVALID_UNREAD_STAFF_FILTER The unread-staff filter is invalid. + */ +/** + * @apiDefine INVALID_PRIORITY_FILTER + * @apiError {String} INVALID_PRIORITY_FILTER The priority filter is invalid. + */ +/** + * @apiDefine INVALID_DATE_RANGE_FILTER + * @apiError {String} INVALID_DATE_RANGE_FILTER The date-range filter is invalid. + */ +/** + * @apiDefine INVALID_DEPARTMENT_FILTER + * @apiError {String} INVALID_DEPARTMENT_FILTER The department filter is invalid. + */ +/** + * @apiDefine INVALID_AUTHOR_FILTER + * @apiError {String} INVALID_AUTHOR_FILTER The author filter is invalid. + */ +/** + * @apiDefine INVALID_OWNER_FILTER + * @apiError {String} INVALID_OWNER_FILTER The owner filter is invalid. + */ +/** + * @apiDefine INVALID_ASSIGNED_FILTER + * @apiError {String} INVALID_ASSIGNED_FILTER The assigned filter is invalid. + */ +/** + * @apiDefine INVALID_ORDER_BY + * @apiError {String} INVALID_ORDER_BY The order-by is invalid. + */ /** * @apiDefine INVALID_TOPIC * @apiError {String} INVALID_TOPIC The topic is invalid. @@ -288,6 +328,7 @@ class ERRORS { const INVALID_DATE_RANGE_FILTER = 'INVALID_DATE_RANGE_FILTER'; const INVALID_DEPARTMENT_FILTER = 'INVALID_DEPARTMENT_FILTER'; const INVALID_AUTHOR_FILTER = 'INVALID_AUTHOR_FILTER'; + const INVALID_OWNER_FILTER = 'INVALID_OWNER_FILTER'; const INVALID_ASSIGNED_FILTER = 'INVALID_ASSIGNED_FILTER'; const INVALID_ORDER_BY = 'INVALID_ORDER_BY'; const INVALID_TOPIC = 'INVALID_TOPIC'; diff --git a/server/libs/validations/validOwnersId.php b/server/libs/validations/validOwnersId.php new file mode 100644 index 00000000..4c2e8425 --- /dev/null +++ b/server/libs/validations/validOwnersId.php @@ -0,0 +1,21 @@ +isNull()) return false; + } + return true; + } + return false; + } +} \ No newline at end of file diff --git a/server/tests/controllers/ticket/searchTest.php b/server/tests/controllers/ticket/searchTest.php index 35e7f574..a49b3fb3 100644 --- a/server/tests/controllers/ticket/searchTest.php +++ b/server/tests/controllers/ticket/searchTest.php @@ -169,6 +169,29 @@ class SearchControllerTest extends TestCase { ); } + public function testOwnerFilter() { + $this->assertEquals( + $this->searchController->getSQLQuery([ + 'owners' => [1,2] + ]), + 'FROM (ticket LEFT JOIN tag_ticket ON tag_ticket.ticket_id = ticket.id LEFT JOIN ticketevent ON ticketevent.ticket_id = ticket.id) WHERE (ticket.owner_id = 1 or ticket.owner_id = 2) GROUP BY ticket.id' + ); + + $this->assertEquals( + $this->searchController->getSQLQuery([ + 'owners' => [6,1,9] + ]), + 'FROM (ticket LEFT JOIN tag_ticket ON tag_ticket.ticket_id = ticket.id LEFT JOIN ticketevent ON ticketevent.ticket_id = ticket.id) WHERE (ticket.owner_id = 6 or ticket.owner_id = 1 or ticket.owner_id = 9) GROUP BY ticket.id' + ); + + $this->assertEquals( + $this->searchController->getSQLQuery([ + 'owners' => [] + ]), + 'FROM (ticket LEFT JOIN tag_ticket ON tag_ticket.ticket_id = ticket.id LEFT JOIN ticketevent ON ticketevent.ticket_id = ticket.id) GROUP BY ticket.id' + ); + } + public function testDepartmentsFilter() { $this->assertEquals( $this->searchController->getSQLQuery([ From f4b3ea2a6546c7f6129a97cc23e7afb316bd8b76 Mon Sep 17 00:00:00 2001 From: Guillermo Giuliana Date: Fri, 24 Jan 2020 15:18:05 -0300 Subject: [PATCH 2/2] seach valid staff to validownersid --- server/libs/validations/validOwnersId.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/server/libs/validations/validOwnersId.php b/server/libs/validations/validOwnersId.php index 4c2e8425..df59f19e 100644 --- a/server/libs/validations/validOwnersId.php +++ b/server/libs/validations/validOwnersId.php @@ -7,15 +7,13 @@ use Respect\Validation\Rules\AbstractRule; class ValidOwnersId extends AbstractRule { public function validate($owners) { - if(is_array(json_decode($owners))){ - foreach (json_decode($owners) as $owner) { - - $author = \Ticket::getDataStore($owner, "owner_id"); - - if($author->isNull()) return false; - } - return true; - } - return false; + if(is_array(json_decode($owners))){ + foreach (json_decode($owners) as $owner) { + $author = \Staff::getDataStore($owner); + if($author->isNull()) return false; + } + return true; + } + return false; } } \ No newline at end of file