diff --git a/server/controllers/system/init-settings.php b/server/controllers/system/init-settings.php index e4d81831..8b6050de 100644 --- a/server/controllers/system/init-settings.php +++ b/server/controllers/system/init-settings.php @@ -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(); diff --git a/server/controllers/ticket.php b/server/controllers/ticket.php index 726ed42c..5a93e7bc 100644 --- a/server/controllers/ticket.php +++ b/server/controllers/ticket.php @@ -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(); \ No newline at end of file diff --git a/server/controllers/ticket/add-custom-response.php b/server/controllers/ticket/add-custom-response.php new file mode 100644 index 00000000..4ba7b3fe --- /dev/null +++ b/server/controllers/ticket/add-custom-response.php @@ -0,0 +1,39 @@ + '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(); + } +} \ No newline at end of file diff --git a/server/controllers/ticket/edit-custom-response.php b/server/controllers/ticket/edit-custom-response.php new file mode 100644 index 00000000..c030f2de --- /dev/null +++ b/server/controllers/ticket/edit-custom-response.php @@ -0,0 +1,39 @@ + '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(); + } +} \ No newline at end of file diff --git a/server/controllers/ticket/get-custom-responses.php b/server/controllers/ticket/get-custom-responses.php new file mode 100644 index 00000000..7c2872cf --- /dev/null +++ b/server/controllers/ticket/get-custom-responses.php @@ -0,0 +1,20 @@ + 'staff_1', + 'requestData' => [] + ]; + } + + public function handler() { + $customResponsesList = CustomResponse::getAll(); + + Response::respondSuccess($customResponsesList->toArray()); + } +} \ No newline at end of file diff --git a/server/data/ERRORS.php b/server/data/ERRORS.php index d173ccfa..a4ef80d2 100644 --- a/server/data/ERRORS.php +++ b/server/data/ERRORS.php @@ -1,19 +1,20 @@ list as $item) { + $item->updateBeanProperties(); + $array[] = $item->toArray(); + } + + return $array; + } private function getIndexInListOf($dataStore) { foreach ($this->list as $itemIdInList => $item) { diff --git a/server/libs/validations/dataStoreId.php b/server/libs/validations/dataStoreId.php index 4bd16725..182d6774 100644 --- a/server/libs/validations/dataStoreId.php +++ b/server/libs/validations/dataStoreId.php @@ -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' ]); } } \ No newline at end of file diff --git a/server/libs/validations/validLanguage.php b/server/libs/validations/validLanguage.php new file mode 100644 index 00000000..4578f830 --- /dev/null +++ b/server/libs/validations/validLanguage.php @@ -0,0 +1,19 @@ +languages); + } +} \ No newline at end of file diff --git a/server/models/CustomResponse.php b/server/models/CustomResponse.php new file mode 100644 index 00000000..5bcd8731 --- /dev/null +++ b/server/models/CustomResponse.php @@ -0,0 +1,21 @@ + $this->name, + 'language' => $this->language, + 'content' => $this->content, + ]; + } +} \ No newline at end of file diff --git a/server/models/DataStore.php b/server/models/DataStore.php index c8296b12..614c9b5e 100644 --- a/server/models/DataStore.php +++ b/server/models/DataStore.php @@ -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; diff --git a/server/models/Department.php b/server/models/Department.php index f2c93407..10628b65 100644 --- a/server/models/Department.php +++ b/server/models/Department.php @@ -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; - } } \ No newline at end of file diff --git a/tests/init.rb b/tests/init.rb index db0b6d11..a664dc8b 100644 --- a/tests/init.rb +++ b/tests/init.rb @@ -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' diff --git a/tests/ticket/comment.rb b/tests/ticket/comment.rb index 422c2244..c05790cb 100644 --- a/tests/ticket/comment.rb +++ b/tests/ticket/comment.rb @@ -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 diff --git a/tests/ticket/create.rb b/tests/ticket/create.rb index 9bde8203..d670f03d 100644 --- a/tests/ticket/create.rb +++ b/tests/ticket/create.rb @@ -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 diff --git a/tests/ticket/custom-response.rb b/tests/ticket/custom-response.rb new file mode 100644 index 00000000..b3c645c2 --- /dev/null +++ b/tests/ticket/custom-response.rb @@ -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 \ No newline at end of file diff --git a/tests/user/edit-email.rb b/tests/user/edit-email.rb index 63e4b1d4..b322f0c2 100644 --- a/tests/user/edit-email.rb +++ b/tests/user/edit-email.rb @@ -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 diff --git a/tests/user/edit-password.rb b/tests/user/edit-password.rb index 88aeb72e..d9767e1b 100644 --- a/tests/user/edit-password.rb +++ b/tests/user/edit-password.rb @@ -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 diff --git a/tests/user/login.rb b/tests/user/login.rb index 870cbc1f..0dc60c4a 100644 --- a/tests/user/login.rb +++ b/tests/user/login.rb @@ -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 diff --git a/tests/user/recover-password.rb b/tests/user/recover-password.rb index a072f74a..168b2fcb 100644 --- a/tests/user/recover-password.rb +++ b/tests/user/recover-password.rb @@ -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 diff --git a/tests/user/signup.rb b/tests/user/signup.rb index bebf131d..b3e0978b 100644 --- a/tests/user/signup.rb +++ b/tests/user/signup.rb @@ -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