diff --git a/server/controllers/staff.php b/server/controllers/staff.php index 1dccf0d1..aafedcbd 100644 --- a/server/controllers/staff.php +++ b/server/controllers/staff.php @@ -4,6 +4,8 @@ require_once 'staff/assign-ticket.php'; require_once 'staff/un-assign-ticket.php'; require_once 'staff/get-tickets.php'; require_once 'staff/get-new-tickets.php'; +require_once 'staff/get-all-tickets.php'; +require_once 'staff/search-tickets.php'; $systemControllerGroup = new ControllerGroup(); $systemControllerGroup->setGroupPath('/staff'); @@ -13,5 +15,7 @@ $systemControllerGroup->addController(new AssignStaffController); $systemControllerGroup->addController(new UnAssignStaffController); $systemControllerGroup->addController(new GetTicketStaffController); $systemControllerGroup->addController(new GetNewTicketsStaffController); +$systemControllerGroup->addController(new GetAllTicketsStaffController); +$systemControllerGroup->addController(new SearchTicketStaffController); $systemControllerGroup->finalize(); \ No newline at end of file diff --git a/server/controllers/staff/get-all-tickets.php b/server/controllers/staff/get-all-tickets.php new file mode 100644 index 00000000..be7641a2 --- /dev/null +++ b/server/controllers/staff/get-all-tickets.php @@ -0,0 +1,54 @@ + 'staff_1', + 'requestData' => [ + 'page' => [ + 'validation' => DataValidator::numeric(), + 'error' => ERRORS::INVALID_PAGE + ] + ] + ]; + } + + public function handler() { + + Response::respondSuccess([ + 'tickets' => $this->getTicketList()->toArray(), + 'pages' => $this->getTotalPages() + ]); + } + + private function getTicketList() { + $page = Controller::request('page'); + + $query = $this->getStaffDepartmentsQueryFilter(); + $query .= 'ORDER BY id DESC LIMIT 10 OFFSET ' . (($page-1)*10); + + return Ticket::find($query); + } + + private function getTotalPages() { + $query = $this->getStaffDepartmentsQueryFilter(); + + return ceil(Ticket::count($query) / 10); + } + + private function getStaffDepartmentsQueryFilter() { + $user = Controller::getLoggedUser(); + + $query = ' ('; + foreach ($user->sharedDepartmentList as $department) { + $query .= 'department_id=' . $department->id . ' OR '; + } + $query = substr($query,0,-3); + $query .= ') '; + + return $query; + } +} \ No newline at end of file diff --git a/server/controllers/staff/search-tickets.php b/server/controllers/staff/search-tickets.php new file mode 100644 index 00000000..e7af0189 --- /dev/null +++ b/server/controllers/staff/search-tickets.php @@ -0,0 +1,74 @@ + 'staff_1', + 'requestData' => [ + 'query' => [ + 'validation' => DataValidator::alpha(), + 'error' => ERRORS::INVALID_QUERY + ], + 'page' => [ + 'validation' => DataValidator::numeric(), + 'error' => ERRORS::INVALID_PAGE + ] + ] + ]; + } + + public function handler() { + Response::respondSuccess([ + 'tickets' => $this->getTicketList()->toArray(), + 'pages' => $this->getTotalPages() + ]); + } + + private function getTicketList() { + $query = $this->getSearchQuery(); + + return Ticket::find($query, [ + Controller::request('query') . '%', + '%' . Controller::request('query') . '%', + Controller::request('query') . '%' + ]); + } + + private function getSearchQuery() { + $page = Controller::request('page'); + + $query = " (title LIKE ? OR title LIKE ?) AND "; + $query .= $this->getStaffDepartmentsQueryFilter(); + $query .= "ORDER BY CASE WHEN (title LIKE ?) THEN 1 ELSE 2 END ASC LIMIT 10 OFFSET " . (($page-1)*10); + + return $query; + } + + private function getTotalPages() { + $query = " (title LIKE ? OR title LIKE ?) AND "; + $query .= $this->getStaffDepartmentsQueryFilter(); + + $ticketQuantity = Ticket::count($query, [ + Controller::request('query') . '%', + '%' . Controller::request('query') . '%' + ]); + + return ceil($ticketQuantity / 10); + } + + private function getStaffDepartmentsQueryFilter() { + $user = Controller::getLoggedUser(); + + $query = ' ('; + foreach ($user->sharedDepartmentList as $department) { + $query .= 'department_id=' . $department->id . ' OR '; + } + $query = substr($query, 0, -3); + $query .= ') '; + + return $query; + } +} \ No newline at end of file diff --git a/server/controllers/ticket/create.php b/server/controllers/ticket/create.php index 4de927f9..8cd3be5e 100644 --- a/server/controllers/ticket/create.php +++ b/server/controllers/ticket/create.php @@ -16,7 +16,7 @@ class CreateController extends Controller { 'permission' => 'user', 'requestData' => [ 'title' => [ - 'validation' => DataValidator::length(3, 30), + 'validation' => DataValidator::length(10, 200), 'error' => ERRORS::INVALID_TITLE ], 'content' => [ diff --git a/server/data/ERRORS.php b/server/data/ERRORS.php index c1478b8e..90f3b0d5 100644 --- a/server/data/ERRORS.php +++ b/server/data/ERRORS.php @@ -19,4 +19,6 @@ class ERRORS { const INVALID_LANGUAGE = 'INVALID_LANGUAGE'; const TICKET_ALREADY_ASSIGNED = 'TICKET_ALREADY_ASSIGNED'; const INVALID_PRIORITY = 'INVALID_PRIORITY'; + const INVALID_PAGE = 'INVALID_PAGE'; + const INVALID_QUERY = 'INVALID_QUERY'; } diff --git a/server/models/DataStore.php b/server/models/DataStore.php index 41b19bf8..d73abfcd 100644 --- a/server/models/DataStore.php +++ b/server/models/DataStore.php @@ -16,8 +16,9 @@ abstract class DataStore { return ($bean) ? new static($bean) : new NullDataStore(); } - public static function count() { - return RedBean::count(static::TABLE); + + public static function count($addSQL = '', $bindings = array()) { + return RedBean::count(static::TABLE, $addSQL, $bindings); } public static function getAll() { @@ -30,10 +31,10 @@ abstract class DataStore { return $dataStoreList; } - public static function find($query) { - $beanList = RedBean::find(static::TABLE,$query); + public static function find($query = '', $matches = []) { + $beanList = RedBean::find(static::TABLE, $query, $matches); - return DataStoreList::getList(ucfirst(static::TABLE),$beanList); + return DataStoreList::getList(ucfirst(static::TABLE), $beanList); } private static function validateProp($propToValidate) { diff --git a/server/models/Ticket.php b/server/models/Ticket.php index 74f166d2..aa44c289 100644 --- a/server/models/Ticket.php +++ b/server/models/Ticket.php @@ -45,7 +45,7 @@ class Ticket extends DataStore { } public function generateUniqueTicketNumber() { - $ticketQuantity = Ticket::count('ticket'); + $ticketQuantity = Ticket::count(); $minValue = 100000; $maxValue = 999999; @@ -53,7 +53,7 @@ class Ticket extends DataStore { $ticketNumber = Hashing::getRandomTicketNumber($minValue, $maxValue); } else { $firstTicketNumber = Ticket::getTicket(1)->ticketNumber; - $gap = 176611; + $gap = 176611; //TODO: USE RANDOM PRIME INSTEAD $ticketNumber = ($firstTicketNumber - $minValue + $ticketQuantity * $gap) % ($maxValue - $minValue + 1) + $minValue; } diff --git a/tests/init.rb b/tests/init.rb index 1ae95b85..23122e39 100644 --- a/tests/init.rb +++ b/tests/init.rb @@ -31,5 +31,6 @@ require './staff/un-assign-ticket.rb' require './staff/get-tickets.rb' require './ticket/change-priority.rb' require './staff/get-new-tickets.rb' +require './staff/get-all-tickets.rb' diff --git a/tests/staff/get-all-tickets.rb b/tests/staff/get-all-tickets.rb new file mode 100644 index 00000000..fdedc865 --- /dev/null +++ b/tests/staff/get-all-tickets.rb @@ -0,0 +1,98 @@ +describe 'Retrieve all tickets' do + describe '/staff/get-all-tickets' do + Scripts.login('login@os4.com', 'loginpass') + + def createTicket(title) + request('/ticket/create',{ + title: title, + content: 'The north remembers', + departmentId: 1, + csrf_userid: $csrf_userid, + csrf_token: $csrf_token + }) + end + + it 'should return last tickets tickets' do + createTicket('Integer sit amet tellus cursus') + createTicket('consequat tortor sed') + createTicket('Fusce lacinia felis quis molestie pellentesque') + createTicket('Aliquam fringilla dapibus lacus') + createTicket('Aenean enim orci') + createTicket('luctus in sagittis non') + createTicket('consectetur at velit') + createTicket('Etiam et maximus quam') + createTicket('Donec facilisis pelleipsumntesque feugiat') + createTicket('Cras gravida bibendum vehicula') + createTicket('Fusce venenatis iaculis commodo') + createTicket('quis vulputate lectus feugiat eu') + createTicket('ipsum Aenean maximus quis leo et eleifend') + createTicket('In vel ex semper nisl sollicitudin') + createTicket('volutpat vel nec enim') + createTicket('Ut semper viverra nulla') + createTicket('Duis consequat nec metus a vestibulum') + createTicket('Vestibulum porta justo id sem bibendum lacinia') + createTicket('Phasellus erat ipsum') + createTicket('imperdiet vel auctor sed') + createTicket('placerat id velit') + createTicket('Quisque egestas ipsum') + + request('/user/logout') + Scripts.login($staff[:email], $staff[:password], true) + response = request('/staff/get-all-tickets', { + page: 1, + csrf_userid: $csrf_userid, + csrf_token: $csrf_token + }) + + (response['status']).should.equal('success') + (response['data']['pages']).should.equal(4) + (response['data']['tickets'].size).should.equal(10) + (response['data']['tickets'][0]['title']).should.equal('Quisque egestas ipsum') + (response['data']['tickets'][1]['title']).should.equal('placerat id velit') + (response['data']['tickets'][2]['title']).should.equal('imperdiet vel auctor sed') + (response['data']['tickets'][3]['title']).should.equal('Phasellus erat ipsum') + (response['data']['tickets'][4]['title']).should.equal('Vestibulum porta justo id sem bibendum lacinia') + (response['data']['tickets'][5]['title']).should.equal('Duis consequat nec metus a vestibulum') + (response['data']['tickets'][6]['title']).should.equal('Ut semper viverra nulla') + (response['data']['tickets'][7]['title']).should.equal('volutpat vel nec enim') + (response['data']['tickets'][8]['title']).should.equal('In vel ex semper nisl sollicitudin') + (response['data']['tickets'][9]['title']).should.equal('ipsum Aenean maximus quis leo et eleifend') + end + + it 'should work with pagination' do + response = request('/staff/get-all-tickets', { + page: 2, + csrf_userid: $csrf_userid, + csrf_token: $csrf_token + }) + + (response['status']).should.equal('success') + (response['data']['pages']).should.equal(4) + (response['data']['tickets'].size).should.equal(10) + (response['data']['tickets'][0]['title']).should.equal('quis vulputate lectus feugiat eu') + (response['data']['tickets'][1]['title']).should.equal('Fusce venenatis iaculis commodo') + (response['data']['tickets'][2]['title']).should.equal('Cras gravida bibendum vehicula') + (response['data']['tickets'][3]['title']).should.equal('Donec facilisis pelleipsumntesque feugiat') + (response['data']['tickets'][4]['title']).should.equal('Etiam et maximus quam') + (response['data']['tickets'][5]['title']).should.equal('consectetur at velit') + (response['data']['tickets'][6]['title']).should.equal('luctus in sagittis non') + (response['data']['tickets'][7]['title']).should.equal('Aenean enim orci') + (response['data']['tickets'][8]['title']).should.equal('Aliquam fringilla dapibus lacus') + (response['data']['tickets'][9]['title']).should.equal('Fusce lacinia felis quis molestie pellentesque') + end + end + + describe '/staff/search-tickets' do + response = request('/staff/search-tickets', { + query: 'ipsum', + page: 1, + csrf_userid: $csrf_userid, + csrf_token: $csrf_token + }) + + (response['status']).should.equal('success') + (response['data']['pages']).should.equal(4) + (response['data']['tickets'].size).should.equal(10) + (response['data']['tickets'][0]['title']).should.equal('ipsum Aenean maximus quis leo et eleifend') + end +end \ No newline at end of file diff --git a/tests/ticket/create.rb b/tests/ticket/create.rb index d670f03d..b1a56d8e 100644 --- a/tests/ticket/create.rb +++ b/tests/ticket/create.rb @@ -29,8 +29,11 @@ describe '/ticket/create' do end it 'should fail if title is very long' do + long_text = '' + 300.times {long_text << 'a'} + result = request('/ticket/create',{ - title: 'I WISH I WAS THE MONSTER YOU THINK I AM. -Tyrion', + title: long_text, departmentId: 1, csrf_userid: $csrf_userid, csrf_token: $csrf_token