Merge branch 'master' into OS-79-assign-ticket-to-staff

This commit is contained in:
ivan 2016-10-07 17:40:55 -03:00
commit 2eed29e910
22 changed files with 278 additions and 60 deletions

View File

@ -89,8 +89,8 @@ class InitSettingsController extends Controller {
'email' => 'staff@opensupports.com',
'password' => Hashing::hashPassword('staff'),
'profilePic' => 'http://i65.tinypic.com/9bep95.jpg',
'level' => 1,
'sharedDepartmentList' => Department::getAllDepartments(),
'level' => 3,
'sharedDepartmentList' => Department::getAll(),
'sharedTicketList' => []
]);
$staff->store();

View File

@ -2,6 +2,9 @@
include 'ticket/create.php';
include 'ticket/comment.php';
include 'ticket/get.php';
include 'ticket/add-custom-response.php';
include 'ticket/edit-custom-response.php';
include 'ticket/get-custom-responses.php';
$ticketControllers = new ControllerGroup();
$ticketControllers->setGroupPath('/ticket');
@ -9,5 +12,8 @@ $ticketControllers->setGroupPath('/ticket');
$ticketControllers->addController(new CreateController);
$ticketControllers->addController(new CommentController);
$ticketControllers->addController(new TicketGetController);
$ticketControllers->addController(new AddCustomResponseController);
$ticketControllers->addController(new EditCustomResponseController);
$ticketControllers->addController(new GetCustomResponsesController);
$ticketControllers->finalize();

View File

@ -0,0 +1,39 @@
<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
class AddCustomResponseController extends Controller {
const PATH = '/add-custom-response';
public function validations() {
return [
'permission' => 'staff_2',
'requestData' => [
'name' => [
'validation' => DataValidator::length(5, 100),
'error' => ERRORS::INVALID_NAME
],
'content' => [
'validation' => DataValidator::length(20, 500),
'error' => ERRORS::INVALID_CONTENT
],
'language' => [
'validation' => DataValidator::validLanguage(),
'error' => ERRORS::INVALID_LANGUAGE
]
]
];
}
public function handler() {
$customResponse = new CustomResponse();
$customResponse->setProperties([
'name' => Controller::request('name'),
'content' => Controller::request('content'),
'language' => Controller::request('language')
]);
$customResponse->store();
Response::respondSuccess();
}
}

View File

@ -0,0 +1,39 @@
<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
class EditCustomResponseController extends Controller {
const PATH = '/edit-custom-response';
public function validations() {
return [
'permission' => 'staff_2',
'requestData' => [
'id' => [
'validation' => DataValidator::dataStoreId('customresponse'),
'error' => ERRORS::INVALID_NAME
]
]
];
}
public function handler() {
$customResponse = CustomResponse::getDataStore(Controller::request('id'));
if (Controller::request('content')) {
$customResponse->content = Controller::request('content');
}
if (Controller::request('language')) {
$customResponse->language = Controller::request('language');
}
if (Controller::request('name')) {
$customResponse->name = Controller::request('name');
}
$customResponse->store();
Response::respondSuccess();
}
}

View File

@ -0,0 +1,20 @@
<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
class GetCustomResponsesController extends Controller {
const PATH = '/get-custom-responses';
public function validations() {
return [
'permission' => 'staff_1',
'requestData' => []
];
}
public function handler() {
$customResponsesList = CustomResponse::getAll();
Response::respondSuccess($customResponsesList->toArray());
}
}

View File

@ -1,19 +1,20 @@
<?php
class ERRORS {
const INVALID_CREDENTIALS = 'User or password is not defined';
const SESSION_EXISTS = 'User is already logged in';
const USER_EXISTS = 'Email already exists';
const NO_PERMISSION = 'You have no permission to access';
const INVALID_TITLE = 'Invalid title';
const INVALID_CONTENT = 'Invalid content';
const INVALID_EMAIL = 'Invalid email';
const INVALID_PASSWORD = 'Invalid password';
const INVALID_NAME = 'Invalid name';
const INVALID_SETTING = 'Invalid setting';
const INVALID_DEPARTMENT = 'Invalid department';
const INVALID_TICKET = 'Invalid ticket';
const INIT_SETTINGS_DONE = 'Settings already initialized';
const INVALID_OLD_PASSWORD = 'Invalid old password';
const INVALID_CAPTCHA = 'Invalid captcha';
const INVALID_CREDENTIALS = 'INVALID_CREDENTIALS';
const SESSION_EXISTS = 'SESSION_EXISTS';
const USER_EXISTS = 'USER_EXISTS';
const NO_PERMISSION = 'NO_PERMISSION';
const INVALID_TITLE = 'INVALID_TITLE';
const INVALID_CONTENT = 'INVALID_CONTENT';
const INVALID_EMAIL = 'INVALID_EMAIL';
const INVALID_PASSWORD = 'INVALID_PASSWORD';
const INVALID_NAME = 'INVALID_NAME';
const INVALID_SETTING = 'INVALID_SETTING';
const INVALID_DEPARTMENT = 'INVALID_DEPARTMENT';
const INVALID_TICKET = 'INVALID_TICKET';
const INIT_SETTINGS_DONE = 'INIT_SETTINGS_DONE';
const INVALID_OLD_PASSWORD = 'INVALID_OLD_PASSWORD';
const INVALID_CAPTCHA = 'INVALID_CAPTCHA';
const INVALID_TICKET_EVENT = 'INVALID_TICKET_EVENT';
const INVALID_LANGUAGE = 'INVALID_LANGUAGE';
}

View File

@ -41,6 +41,7 @@ spl_autoload_register(function ($class) {
include_once 'libs/validations/dataStoreId.php';
include_once 'libs/validations/userEmail.php';
include_once 'libs/validations/captcha.php';
include_once 'libs/validations/validLanguage.php';
include_once 'libs/validations/validTicketNumber.php';
// LOAD CONTROLLERS

View File

@ -38,6 +38,17 @@ class DataStoreList implements IteratorAggregate {
return $beanList;
}
public function toArray() {
$array = [];
foreach($this->list as $item) {
$item->updateBeanProperties();
$array[] = $item->toArray();
}
return $array;
}
private function getIndexInListOf($dataStore) {
foreach ($this->list as $itemIdInList => $item) {

View File

@ -28,6 +28,9 @@ class DataStoreId extends AbstractRule {
case 'department':
$dataStore = \Department::getDataStore($dataStoreId);
break;
case 'customresponse':
$dataStore = \CustomResponse::getDataStore($dataStoreId);
break;
}
return !$dataStore->isNull();
@ -37,7 +40,8 @@ class DataStoreId extends AbstractRule {
return in_array($dataStoreName, [
'user',
'ticket',
'department'
'department',
'customresponse'
]);
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace CustomValidations;
use Respect\Validation\Rules\AbstractRule;
class ValidLanguage extends AbstractRule {
//TODO: Use a list from database instead
private $languages = [
'en',
'es',
'de'
];
public function validate($ticketNumber) {
return in_array($ticketNumber, $this->languages);
}
}

View File

@ -0,0 +1,21 @@
<?php
class CustomResponse extends DataStore {
const TABLE = 'customresponse';
public static function getProps() {
return [
'name',
'language',
'content'
];
}
public function toArray() {
return [
'name' => $this->name,
'language' => $this->language,
'content' => $this->content,
];
}
}

View File

@ -20,6 +20,17 @@ abstract class DataStore {
return RedBean::count(static::TABLE);
}
public static function getAll() {
$beanList = RedBean::findAll(static::TABLE);
$dataStoreList = new DataStoreList();
foreach($beanList as $bean) {
$dataStoreList->add(new static($bean));
}
return $dataStoreList;
}
private static function validateProp($propToValidate) {
$validProp = false;

View File

@ -12,24 +12,13 @@ class Department extends DataStore {
}
public static function getDepartmentNames() {
$departmentsQuantity = RedBean::count(Department::TABLE);
$departmentsList = RedBean::findAll(Department::TABLE);
$departmentsNameList = [];
for ($departmentIndex = 1; $departmentIndex <= $departmentsQuantity; ++$departmentIndex) {
$departmentsNameList[] = Department::getDataStore($departmentIndex)->name;
foreach($departmentsList as $department) {
$departmentsNameList[] = $department->name;
}
return $departmentsNameList;
}
public static function getAllDepartments() {
$departmentsQuantity = RedBean::count(Department::TABLE);
$departmentList = new DataStoreList();
for ($departmentIndex = 1; $departmentIndex <= $departmentsQuantity; ++$departmentIndex) {
$departmentList->add(Department::getDataStore($departmentIndex));
}
return $departmentList;
}
}

View File

@ -21,4 +21,5 @@ require './user/get.rb'
require './ticket/create.rb'
require './ticket/comment.rb'
require './ticket/get.rb'
require './ticket/custom-response.rb'
require './staff/get.rb'

View File

@ -15,7 +15,7 @@ describe '/ticket/comment/' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('You have no permission to access')
(result['message']).should.equal('NO_PERMISSION')
end
it 'should fail if content is too short' do
@ -27,7 +27,7 @@ describe '/ticket/comment/' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid content')
(result['message']).should.equal('INVALID_CONTENT')
end
it 'should fail if content is very long' do
@ -42,7 +42,7 @@ describe '/ticket/comment/' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid content')
(result['message']).should.equal('INVALID_CONTENT')
end
it 'should fail if ticket does not exist' do
@ -54,7 +54,7 @@ describe '/ticket/comment/' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid ticket')
(result['message']).should.equal('INVALID_TICKET')
end
it 'should add comment to ticket' do
@ -86,7 +86,7 @@ describe '/ticket/comment/' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('You have no permission to access')
(result['message']).should.equal('NO_PERMISSION')
end
#it 'should add comment if logged as ticket owner' do

View File

@ -12,7 +12,7 @@ describe '/ticket/create' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('You have no permission to access')
(result['message']).should.equal('NO_PERMISSION')
end
@ -25,7 +25,7 @@ describe '/ticket/create' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid title')
(result['message']).should.equal('INVALID_TITLE')
end
it 'should fail if title is very long' do
@ -37,7 +37,7 @@ describe '/ticket/create' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid title')
(result['message']).should.equal('INVALID_TITLE')
end
it 'should fail if content is too short' do
@ -50,7 +50,7 @@ describe '/ticket/create' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid content')
(result['message']).should.equal('INVALID_CONTENT')
end
it 'should fail if content is very long' do
@ -66,7 +66,7 @@ describe '/ticket/create' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid content')
(result['message']).should.equal('INVALID_CONTENT')
end
@ -80,7 +80,7 @@ describe '/ticket/create' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid department')
(result['message']).should.equal('INVALID_DEPARTMENT')
end

View File

@ -0,0 +1,56 @@
describe 'CustomResponses' do
Scripts.login('staff@opensupports.com', 'staff', true)
describe '/ticket/add-custom-responses/' do
it 'should create custom response' do
result = request('/ticket/add-custom-response', {
name: 'Some common problem',
language: 'en',
content: 'this is the content of a custom response for a common problem',
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
customResponse = $database.getRow('customresponse', 1)
(result['status']).should.equal('success')
(customResponse['name']).should.equal('Some common problem')
(customResponse['content']).should.equal('this is the content of a custom response for a common problem')
(customResponse['language']).should.equal('en')
end
end
describe '/ticket/edit-custom-responses/' do
it 'should edit a custom response' do
result = request('/ticket/edit-custom-response', {
id: 1,
content: 'this is the content of a custom response for a common problem 2',
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
customResponse = $database.getRow('customresponse', 1)
(result['status']).should.equal('success')
(customResponse['name']).should.equal('Some common problem')
(customResponse['content']).should.equal('this is the content of a custom response for a common problem 2')
(customResponse['language']).should.equal('en')
end
end
describe '/ticket/get-custom-responses/' do
it 'should return all custom responses' do
result = request('/ticket/get-custom-responses', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
(result['status']).should.equal('success')
(result['data'].size).should.equal(1)
(result['data'][0]['name']).should.equal('Some common problem')
(result['data'][0]['content']).should.equal('this is the content of a custom response for a common problem 2')
(result['data'][0]['language']).should.equal('en')
end
end
end

View File

@ -17,7 +17,7 @@ describe '/user/edit-email' do
csrf_token: $csrf_token
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid email')
(result['message']).should.equal('INVALID_EMAIL')
result = request('/user/edit-email', {
newEmail: 'newemailjobs.com',
@ -25,7 +25,7 @@ describe '/user/edit-email' do
csrf_token: $csrf_token
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid email')
(result['message']).should.equal('INVALID_EMAIL')
end
it 'should change email' do

View File

@ -18,7 +18,7 @@ describe '/user/edit-password' do
csrf_token: $csrf_token
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid password')
(result['message']).should.equal('INVALID_PASSWORD')
long_text = ''
250.times {long_text << 'a'}
@ -30,7 +30,7 @@ describe '/user/edit-password' do
csrf_token: $csrf_token
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid password')
(result['message']).should.equal('INVALID_PASSWORD')
end
it 'should fail if old password is not same than old password ' do
@ -41,7 +41,7 @@ describe '/user/edit-password' do
csrf_token: $csrf_token
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid old password')
(result['message']).should.equal('INVALID_OLD_PASSWORD')
end
it 'should change password' do

View File

@ -29,7 +29,7 @@ describe '/user/login' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('User is already logged in')
(result['message']).should.equal('SESSION_EXISTS')
end
it 'should login staff member' do

View File

@ -9,7 +9,7 @@ describe '/user/recover-password' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid email')
(result['message']).should.equal('INVALID_EMAIL')
result = request('/user/recover-password', {
email: 'loginos4.com',
@ -17,7 +17,7 @@ describe '/user/recover-password' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid email')
(result['message']).should.equal('INVALID_EMAIL')
end
it 'should fail if password is incorrect' do
@ -47,6 +47,6 @@ describe '/user/recover-password' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('You have no permission to access')
(result['message']).should.equal('NO_PERMISSION')
end
end

View File

@ -23,7 +23,7 @@ describe '/user/signup' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid name')
(result['message']).should.equal('INVALID_NAME')
result = request('/user/signup', {
name: long_text,
@ -32,7 +32,7 @@ describe '/user/signup' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid name')
(result['message']).should.equal('INVALID_NAME')
result = request('/user/signup', {
name: 'tyri0n',
@ -41,7 +41,7 @@ describe '/user/signup' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid name')
(result['message']).should.equal('INVALID_NAME')
end
it 'should fail if email is invalid' do
@ -52,7 +52,7 @@ describe '/user/signup' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid email')
(result['message']).should.equal('INVALID_EMAIL')
result = request('/user/signup', {
name: 'tyrion',
@ -61,7 +61,7 @@ describe '/user/signup' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid email')
(result['message']).should.equal('INVALID_EMAIL')
end
it 'should fail if password is invalid' do
@ -72,7 +72,7 @@ describe '/user/signup' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid password')
(result['message']).should.equal('INVALID_PASSWORD')
long_text = ''
250.times {long_text << 'a'}
@ -84,7 +84,7 @@ describe '/user/signup' do
})
(result['status']).should.equal('fail')
(result['message']).should.equal('Invalid password')
(result['message']).should.equal('INVALID_PASSWORD')
end
end