mirror of
https://github.com/opensupports/opensupports.git
synced 2025-07-28 00:04:31 +02:00
Merged in OS-91-92-custom-responses (pull request #57)
OS-91 92 custom responses
This commit is contained in:
commit
aabc1bdba4
@ -89,8 +89,8 @@ class InitSettingsController extends Controller {
|
|||||||
'email' => 'staff@opensupports.com',
|
'email' => 'staff@opensupports.com',
|
||||||
'password' => Hashing::hashPassword('staff'),
|
'password' => Hashing::hashPassword('staff'),
|
||||||
'profilePic' => 'http://i65.tinypic.com/9bep95.jpg',
|
'profilePic' => 'http://i65.tinypic.com/9bep95.jpg',
|
||||||
'level' => 1,
|
'level' => 3,
|
||||||
'sharedDepartmentList' => Department::getAllDepartments(),
|
'sharedDepartmentList' => Department::getAll(),
|
||||||
'sharedTicketList' => []
|
'sharedTicketList' => []
|
||||||
]);
|
]);
|
||||||
$staff->store();
|
$staff->store();
|
||||||
|
@ -2,6 +2,9 @@
|
|||||||
include 'ticket/create.php';
|
include 'ticket/create.php';
|
||||||
include 'ticket/comment.php';
|
include 'ticket/comment.php';
|
||||||
include 'ticket/get.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 = new ControllerGroup();
|
||||||
$ticketControllers->setGroupPath('/ticket');
|
$ticketControllers->setGroupPath('/ticket');
|
||||||
@ -9,5 +12,8 @@ $ticketControllers->setGroupPath('/ticket');
|
|||||||
$ticketControllers->addController(new CreateController);
|
$ticketControllers->addController(new CreateController);
|
||||||
$ticketControllers->addController(new CommentController);
|
$ticketControllers->addController(new CommentController);
|
||||||
$ticketControllers->addController(new TicketGetController);
|
$ticketControllers->addController(new TicketGetController);
|
||||||
|
$ticketControllers->addController(new AddCustomResponseController);
|
||||||
|
$ticketControllers->addController(new EditCustomResponseController);
|
||||||
|
$ticketControllers->addController(new GetCustomResponsesController);
|
||||||
|
|
||||||
$ticketControllers->finalize();
|
$ticketControllers->finalize();
|
39
server/controllers/ticket/add-custom-response.php
Normal file
39
server/controllers/ticket/add-custom-response.php
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
39
server/controllers/ticket/edit-custom-response.php
Normal file
39
server/controllers/ticket/edit-custom-response.php
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
20
server/controllers/ticket/get-custom-responses.php
Normal file
20
server/controllers/ticket/get-custom-responses.php
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
@ -16,4 +16,5 @@ class ERRORS {
|
|||||||
const INVALID_OLD_PASSWORD = 'Invalid old password';
|
const INVALID_OLD_PASSWORD = 'Invalid old password';
|
||||||
const INVALID_CAPTCHA = 'Invalid captcha';
|
const INVALID_CAPTCHA = 'Invalid captcha';
|
||||||
const INVALID_TICKET_EVENT = 'INVALID_TICKET_EVENT';
|
const INVALID_TICKET_EVENT = 'INVALID_TICKET_EVENT';
|
||||||
|
const INVALID_LANGUAGE = 'INVALID_LANGUAGE';
|
||||||
}
|
}
|
||||||
|
@ -41,6 +41,7 @@ spl_autoload_register(function ($class) {
|
|||||||
include_once 'libs/validations/dataStoreId.php';
|
include_once 'libs/validations/dataStoreId.php';
|
||||||
include_once 'libs/validations/userEmail.php';
|
include_once 'libs/validations/userEmail.php';
|
||||||
include_once 'libs/validations/captcha.php';
|
include_once 'libs/validations/captcha.php';
|
||||||
|
include_once 'libs/validations/validLanguage.php';
|
||||||
include_once 'libs/validations/validTicketNumber.php';
|
include_once 'libs/validations/validTicketNumber.php';
|
||||||
|
|
||||||
// LOAD CONTROLLERS
|
// LOAD CONTROLLERS
|
||||||
|
@ -39,6 +39,17 @@ class DataStoreList implements IteratorAggregate {
|
|||||||
return $beanList;
|
return $beanList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function toArray() {
|
||||||
|
$array = [];
|
||||||
|
|
||||||
|
foreach($this->list as $item) {
|
||||||
|
$item->updateBeanProperties();
|
||||||
|
$array[] = $item->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
private function getIndexInListOf($dataStore) {
|
private function getIndexInListOf($dataStore) {
|
||||||
foreach ($this->list as $itemIdInList => $item) {
|
foreach ($this->list as $itemIdInList => $item) {
|
||||||
if ($item->id === $dataStore->id) {
|
if ($item->id === $dataStore->id) {
|
||||||
|
@ -28,6 +28,9 @@ class DataStoreId extends AbstractRule {
|
|||||||
case 'department':
|
case 'department':
|
||||||
$dataStore = \Department::getDataStore($dataStoreId);
|
$dataStore = \Department::getDataStore($dataStoreId);
|
||||||
break;
|
break;
|
||||||
|
case 'customresponse':
|
||||||
|
$dataStore = \CustomResponse::getDataStore($dataStoreId);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return !$dataStore->isNull();
|
return !$dataStore->isNull();
|
||||||
@ -37,7 +40,8 @@ class DataStoreId extends AbstractRule {
|
|||||||
return in_array($dataStoreName, [
|
return in_array($dataStoreName, [
|
||||||
'user',
|
'user',
|
||||||
'ticket',
|
'ticket',
|
||||||
'department'
|
'department',
|
||||||
|
'customresponse'
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
19
server/libs/validations/validLanguage.php
Normal file
19
server/libs/validations/validLanguage.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
21
server/models/CustomResponse.php
Normal file
21
server/models/CustomResponse.php
Normal 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,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -20,6 +20,17 @@ abstract class DataStore {
|
|||||||
return RedBean::count(static::TABLE);
|
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) {
|
private static function validateProp($propToValidate) {
|
||||||
$validProp = false;
|
$validProp = false;
|
||||||
|
|
||||||
|
@ -12,24 +12,13 @@ class Department extends DataStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static function getDepartmentNames() {
|
public static function getDepartmentNames() {
|
||||||
$departmentsQuantity = RedBean::count(Department::TABLE);
|
$departmentsList = RedBean::findAll(Department::TABLE);
|
||||||
$departmentsNameList = [];
|
$departmentsNameList = [];
|
||||||
|
|
||||||
for ($departmentIndex = 1; $departmentIndex <= $departmentsQuantity; ++$departmentIndex) {
|
foreach($departmentsList as $department) {
|
||||||
$departmentsNameList[] = Department::getDataStore($departmentIndex)->name;
|
$departmentsNameList[] = $department->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $departmentsNameList;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -21,4 +21,5 @@ require './user/get.rb'
|
|||||||
require './ticket/create.rb'
|
require './ticket/create.rb'
|
||||||
require './ticket/comment.rb'
|
require './ticket/comment.rb'
|
||||||
require './ticket/get.rb'
|
require './ticket/get.rb'
|
||||||
|
require './ticket/custom-response.rb'
|
||||||
require './staff/get.rb'
|
require './staff/get.rb'
|
||||||
|
56
tests/ticket/custom-response.rb
Normal file
56
tests/ticket/custom-response.rb
Normal 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
|
Loading…
x
Reference in New Issue
Block a user