Ivan - Add Get all tickets and search [skip ci]

This commit is contained in:
ivan 2016-11-04 19:10:32 -03:00
parent de161c8956
commit 83e3672f55
10 changed files with 244 additions and 10 deletions

View File

@ -5,6 +5,7 @@ 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');
@ -15,6 +16,6 @@ $systemControllerGroup->addController(new UnAssignStaffController);
$systemControllerGroup->addController(new GetTicketStaffController);
$systemControllerGroup->addController(new GetNewTicketsStaffController);
$systemControllerGroup->addController(new GetAllTicketsStaffController);
$systemControllerGroup->addController(new SearchTicketStaffController);
$systemControllerGroup->finalize();

View File

@ -0,0 +1,54 @@
<?php
use Respect\Validation\Validator as DataValidator;
class GetAllTicketsStaffController extends Controller {
const PATH = '/get-all-tickets';
public function validations() {
return[
'permission' => '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;
}
}

View File

@ -0,0 +1,74 @@
<?php
use Respect\Validation\Validator as DataValidator;
class SearchTicketStaffController extends Controller {
const PATH = '/search-tickets';
public function validations() {
return[
'permission' => '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;
}
}

View File

@ -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' => [

View File

@ -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';
}

View File

@ -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) {

View File

@ -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;
}

View File

@ -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'

View File

@ -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

View File

@ -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