Merged in OS-117-all-staff-paths (pull request #88)
OS-117 all staff paths
This commit is contained in:
commit
b30c9acc4f
|
@ -536,6 +536,7 @@ module.exports = [
|
|||
pages: 4
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
@ -6,6 +6,10 @@ 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';
|
||||
require_once 'staff/add.php';
|
||||
require_once 'staff/get-all.php';
|
||||
require_once 'staff/delete.php';
|
||||
require_once 'staff/edit.php';
|
||||
|
||||
$systemControllerGroup = new ControllerGroup();
|
||||
$systemControllerGroup->setGroupPath('/staff');
|
||||
|
@ -17,5 +21,9 @@ $systemControllerGroup->addController(new GetTicketStaffController);
|
|||
$systemControllerGroup->addController(new GetNewTicketsStaffController);
|
||||
$systemControllerGroup->addController(new GetAllTicketsStaffController);
|
||||
$systemControllerGroup->addController(new SearchTicketStaffController);
|
||||
$systemControllerGroup->addController(new AddStaffController);
|
||||
$systemControllerGroup->addController(new GetAllStaffController);
|
||||
$systemControllerGroup->addController(new DeleteStaffController);
|
||||
$systemControllerGroup->addController(new EditStaffController);
|
||||
|
||||
$systemControllerGroup->finalize();
|
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
use Respect\Validation\Validator as DataValidator;
|
||||
DataValidator::with('CustomValidations', true);
|
||||
|
||||
class AddStaffController extends Controller {
|
||||
const PATH = '/add';
|
||||
|
||||
private $name;
|
||||
private $email;
|
||||
private $password;
|
||||
private $profilePic;
|
||||
private $level;
|
||||
private $departments;
|
||||
|
||||
|
||||
public function validations() {
|
||||
return [
|
||||
'permission' => 'staff_3',
|
||||
'requestData' => [
|
||||
'name' => [
|
||||
'validation' => DataValidator::length(2, 55)->alpha(),
|
||||
'error' => ERRORS::INVALID_NAME
|
||||
],
|
||||
'email' => [
|
||||
'validation' => DataValidator::email(),
|
||||
'error' => ERRORS::INVALID_EMAIL
|
||||
],
|
||||
'password' => [
|
||||
'validation' => DataValidator::length(5, 200),
|
||||
'error' => ERRORS::INVALID_PASSWORD
|
||||
],
|
||||
'level' => [
|
||||
'validation' => DataValidator::between(1, 3, true),
|
||||
'error' => ERRORS::INVALID_LEVEL
|
||||
]
|
||||
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function handler() {
|
||||
$this->storeRequestData();
|
||||
$staff = new Staff();
|
||||
|
||||
$staffRow = Staff::getDataStore($this->email,'email');
|
||||
|
||||
if($staffRow->isNull()) {
|
||||
$staff->setProperties([
|
||||
'name'=> $this->name,
|
||||
'email' => $this->email,
|
||||
'password'=> Hashing::hashPassword($this->password),
|
||||
'profilePic' => $this->profilePic,
|
||||
'level' => $this->level,
|
||||
'sharedDepartmentList'=> $this->getDepartmentList(),
|
||||
]);
|
||||
|
||||
|
||||
Response::respondSuccess([
|
||||
'id' => $staff->store()
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
Response::respondError(ERRORS::ALREADY_A_STAFF);
|
||||
}
|
||||
|
||||
public function storeRequestData() {
|
||||
$this->name = Controller::request('name');
|
||||
$this->email = Controller::request('email');
|
||||
$this->password = Controller::request('password');
|
||||
$this->profilePic = Controller::request('profilePic');
|
||||
$this->level = Controller::request('level');
|
||||
$this->departments = Controller::request('departments');
|
||||
}
|
||||
|
||||
public function getDepartmentList() {
|
||||
$listDepartments = new DataStoreList();
|
||||
$departmentIds = json_decode($this->departments);
|
||||
|
||||
foreach($departmentIds as $id) {
|
||||
$department = Department::getDataStore($id);
|
||||
$listDepartments->add($department);
|
||||
}
|
||||
|
||||
return $listDepartments;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
use Respect\Validation\Validator as DataValidator;
|
||||
DataValidator::with('CustomValidations', true);
|
||||
|
||||
class DeleteStaffController extends Controller {
|
||||
const PATH = '/delete';
|
||||
|
||||
public function validations() {
|
||||
return [
|
||||
'permission' => 'staff_3',
|
||||
'requestData' => [
|
||||
'staffId' =>[
|
||||
'validation' => DataValidator::dataStoreId('staff'),
|
||||
'error' => ERRORS::INVALID_STAFF
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function handler() {
|
||||
$staffId = Controller::request('staffId');
|
||||
$staff = Staff::getDataStore($staffId);
|
||||
|
||||
foreach($staff->sharedTicketList as $ticket) {
|
||||
$ticket->owner = null;
|
||||
$ticket->true = true;
|
||||
$ticket->store();
|
||||
}
|
||||
|
||||
$staff->delete();
|
||||
Response::respondSuccess();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
use Respect\Validation\Validator as DataValidator;
|
||||
|
||||
class EditStaffController extends Controller {
|
||||
const PATH = '/edit';
|
||||
|
||||
private $staffRow;
|
||||
private $staffId;
|
||||
|
||||
public function validations() {
|
||||
return [
|
||||
'permission' => 'staff_1',
|
||||
'requestData' => []
|
||||
];
|
||||
}
|
||||
|
||||
public function handler() {
|
||||
$this->staffId = Controller::request('staffId');
|
||||
|
||||
if(!$this->staffId) {
|
||||
$this->staffRow = Controller::getLoggedUser();
|
||||
} else if(Controller::isStaffLogged(3)) {
|
||||
$this->staffRow = Staff::getDataStore($this->staffId, 'id');
|
||||
|
||||
if($this->staffRow->isNull()) {
|
||||
Response::respondError(ERRORS::INVALID_STAFF);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
Response::respondError(ERRORS::NO_PERMISSION);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->editInformation();
|
||||
Response::respondSuccess();
|
||||
}
|
||||
|
||||
public function editInformation() {
|
||||
|
||||
if(Controller::request('email')) {
|
||||
$this->staffRow->email = Controller::request('email');
|
||||
}
|
||||
|
||||
if(Controller::request('password')) {
|
||||
$this->staffRow->password = Hashing::hashPassword(Controller::request('password'));
|
||||
}
|
||||
|
||||
if(Controller::request('level') && Controller::isStaffLogged(3)) {
|
||||
$this->staffRow->level = Controller::request('level');
|
||||
}
|
||||
|
||||
if(Controller::request('departments') && Controller::isStaffLogged(3)) {
|
||||
$this->staffRow->sharedDepartmentList = $this->getDepartmentList();
|
||||
}
|
||||
|
||||
$this->staffRow->store();
|
||||
}
|
||||
|
||||
|
||||
public function getDepartmentList() {
|
||||
$listDepartments = new DataStoreList();
|
||||
$departmentIds = json_decode(Controller::request('departments'));
|
||||
|
||||
foreach($departmentIds as $id) {
|
||||
$department = Department::getDataStore($id);
|
||||
$listDepartments->add($department);
|
||||
}
|
||||
|
||||
return $listDepartments;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
use Respect\Validation\Validator as DataValidator;
|
||||
|
||||
|
||||
class GetAllStaffController extends Controller {
|
||||
const PATH ='/get-all';
|
||||
|
||||
public function validations() {
|
||||
return [
|
||||
'permission' => 'staff_3',
|
||||
'requestData' => []
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
public function handler() {
|
||||
$staffs = Staff::getAll();
|
||||
$staffArray = [];
|
||||
|
||||
foreach($staffs as $staff) {
|
||||
$assignedTickets = 0;
|
||||
$closedTickets = 0;
|
||||
|
||||
foreach ($staff->sharedTicketList as $ticket) {
|
||||
if($ticket->closed) $closedTickets++;
|
||||
else $assignedTickets++;
|
||||
}
|
||||
|
||||
$staffArray[] = [
|
||||
'id' => $staff->id,
|
||||
'name' => $staff->name,
|
||||
'email' => $staff->email,
|
||||
'profilePic' => $staff->profilePic,
|
||||
'level' => $staff->level,
|
||||
'departments' => $staff->sharedDepartmentList->toArray(),
|
||||
'assignedTickets' => $assignedTickets,
|
||||
'closedTickets' => $closedTickets,
|
||||
];
|
||||
}
|
||||
|
||||
Response::respondSuccess($staffArray);
|
||||
|
||||
}
|
||||
}
|
|
@ -14,6 +14,14 @@ class GetStaffController extends Controller {
|
|||
|
||||
public function handler() {
|
||||
$user = Controller::getLoggedUser();
|
||||
|
||||
$userId = Controller::request('staffId');
|
||||
$userRow = Staff::getDataStore($userId);
|
||||
|
||||
if($user->level == 3 && !$userRow->isNull()) {
|
||||
$user = $userRow;
|
||||
}
|
||||
|
||||
$parsedDepartmentList = [];
|
||||
$departmentList = $user->sharedDepartmentList;
|
||||
|
||||
|
|
|
@ -26,4 +26,7 @@ class ERRORS {
|
|||
const INVALID_ORDER = 'INVALID_ORDER';
|
||||
const INVALID_USER = 'INVALID_USER';
|
||||
const ALREADY_BANNED = 'ALREADY_BANNED';
|
||||
const INVALID_LEVEL = 'INVALID_LEVEL';
|
||||
const ALREADY_A_STAFF = 'ALREADY_A_STAFF';
|
||||
const INVALID_STAFF = 'INVALID_STAFF';
|
||||
}
|
||||
|
|
|
@ -22,6 +22,9 @@ class DataStoreId extends AbstractRule {
|
|||
case 'user':
|
||||
$dataStore = \User::getUser($dataStoreId);
|
||||
break;
|
||||
case 'staff':
|
||||
$dataStore = \Staff::getUser($dataStoreId);
|
||||
break;
|
||||
case 'ticket':
|
||||
$dataStore = \Ticket::getTicket($dataStoreId);
|
||||
break;
|
||||
|
@ -45,6 +48,7 @@ class DataStoreId extends AbstractRule {
|
|||
private function isDataStoreNameValid($dataStoreName) {
|
||||
return in_array($dataStoreName, [
|
||||
'user',
|
||||
'staff',
|
||||
'ticket',
|
||||
'department',
|
||||
'customresponse',
|
||||
|
|
|
@ -24,4 +24,10 @@ class Department extends DataStore {
|
|||
|
||||
return $departmentsNameList;
|
||||
}
|
||||
public function toArray() {
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name
|
||||
];
|
||||
}
|
||||
}
|
|
@ -30,4 +30,16 @@ class Staff extends DataStore {
|
|||
public static function getUser($value, $property = 'id') {
|
||||
return parent::getDataStore($value, $property);
|
||||
}
|
||||
|
||||
public function toArray() {
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'email' => $this->email,
|
||||
'profilePic' => $this->profilePic,
|
||||
'level' => $this->level,
|
||||
'departments' => $this->sharedDepartmentList->toArray(),
|
||||
'tickets' => $this->sharedTicketList->toArray()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,7 +25,10 @@ require './ticket/custom-response.rb'
|
|||
require './ticket/change-department.rb'
|
||||
require './ticket/close.rb'
|
||||
require './ticket/re-open.rb'
|
||||
require './staff/add.rb'
|
||||
require './staff/get.rb'
|
||||
require './staff/edit.rb'
|
||||
require './staff/delete.rb'
|
||||
require './staff/assign-ticket.rb'
|
||||
require './staff/un-assign-ticket.rb'
|
||||
require './staff/get-tickets.rb'
|
||||
|
@ -39,5 +42,6 @@ require './user/get-user.rb'
|
|||
require './user/ban.rb'
|
||||
require './user/get-users-test.rb'
|
||||
require './user/delete.rb'
|
||||
require './staff/get-all.rb'
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
describe'/staff/add' do
|
||||
request('/user/logout')
|
||||
Scripts.login($staff[:email], $staff[:password], true)
|
||||
|
||||
it 'should add staff member' do
|
||||
result= request('/staff/add', {
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token,
|
||||
name: 'Tyrion Lannister',
|
||||
email: 'tyrion@opensupports.com',
|
||||
password: 'testpassword',
|
||||
level: 2,
|
||||
profilePic: 'http://www.opensupports.com/profilepic.jpg',
|
||||
departments: '[1]'
|
||||
})
|
||||
|
||||
(result['status']).should.equal('success')
|
||||
|
||||
row = $database.getRow('staff', result['data']['id'], 'id')
|
||||
|
||||
(row['name']).should.equal('Tyrion Lannister')
|
||||
(row['email']).should.equal('tyrion@opensupports.com')
|
||||
(row['profile_pic']).should.equal('http://www.opensupports.com/profilepic.jpg')
|
||||
(row['level']).should.equal('2')
|
||||
end
|
||||
it 'should fail if staff member is alrady a staff' do
|
||||
result= request('/staff/add', {
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token,
|
||||
name: 'Tyrion Lannister',
|
||||
email: 'tyrion@opensupports.com',
|
||||
password: 'testpassword',
|
||||
level: 2,
|
||||
profilePic: 'http://www.opensupports.com/profilepic.jpg',
|
||||
departments: '[1]'
|
||||
})
|
||||
|
||||
(result['status']).should.equal('fail')
|
||||
(result['message']).should.equal('ALREADY_A_STAFF')
|
||||
|
||||
end
|
||||
end
|
|
@ -0,0 +1,29 @@
|
|||
describe'/staff/delete' do
|
||||
request('/user/logout')
|
||||
Scripts.login($staff[:email], $staff[:password], true)
|
||||
|
||||
it 'should delete staff member' do
|
||||
result= request('/staff/delete', {
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token,
|
||||
staffId:2
|
||||
})
|
||||
|
||||
(result['status']).should.equal('success')
|
||||
|
||||
row = $database.getRow('staff', 2, 'id')
|
||||
(row).should.equal(nil)
|
||||
|
||||
end
|
||||
it 'should fail delete if staff member is does not exist' do
|
||||
result= request('/staff/delete', {
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token,
|
||||
staffId:2
|
||||
})
|
||||
|
||||
(result['status']).should.equal('fail')
|
||||
(result['message']).should.equal('INVALID_STAFF')
|
||||
|
||||
end
|
||||
end
|
|
@ -0,0 +1,56 @@
|
|||
describe'/staff/edit' do
|
||||
request('/user/logout')
|
||||
Scripts.login($staff[:email], $staff[:password], true)
|
||||
|
||||
it 'should edit another staff member' do
|
||||
result= request('/staff/edit', {
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token,
|
||||
email: 'LittleLannister@opensupports.com',
|
||||
level: 1,
|
||||
departments: '[1, 2]',
|
||||
staffId: 2
|
||||
})
|
||||
|
||||
(result['status']).should.equal('success')
|
||||
|
||||
row = $database.getRow('staff', 2, 'id')
|
||||
|
||||
(row['email']).should.equal('LittleLannister@opensupports.com')
|
||||
(row['level']).should.equal('1')
|
||||
|
||||
rows = $database.getRow('department_staff', 2, 'staff_id')
|
||||
|
||||
(rows['department_id']).should.equal('1')
|
||||
|
||||
end
|
||||
|
||||
it 'should edit staff member ' do
|
||||
request('/staff/add', {
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token,
|
||||
name: 'Arya Stark',
|
||||
password: 'starkpassword',
|
||||
email: 'arya@opensupports.com',
|
||||
level: 2,
|
||||
profilePic: 'http://www.opensupports.com/profilepic.jpg',
|
||||
departments: '[1]'
|
||||
})
|
||||
request('/user/logout')
|
||||
Scripts.login('arya@opensupports.com', 'starkpassword', true)
|
||||
|
||||
result = request('/staff/edit', {
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token,
|
||||
email: 'newwstaff@opensupports.com',
|
||||
})
|
||||
|
||||
(result['status']).should.equal('success')
|
||||
|
||||
row = $database.getRow('staff', $csrf_userid, 'id')
|
||||
|
||||
(row['email']).should.equal('newwstaff@opensupports.com')
|
||||
(row['level']).should.equal('2')
|
||||
|
||||
end
|
||||
end
|
|
@ -0,0 +1,35 @@
|
|||
describe'/staff/get-all' do
|
||||
request('/user/logout')
|
||||
Scripts.login($staff[:email], $staff[:password], true)
|
||||
|
||||
it 'should get all staff member' do
|
||||
result= request('/staff/get-all', {
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token
|
||||
})
|
||||
|
||||
(result['status']).should.equal('success')
|
||||
|
||||
(result['data'][0]['name']).should.equal('Emilia Clarke')
|
||||
(result['data'][0]['email']).should.equal('staff@opensupports.com')
|
||||
(result['data'][0]['profilePic']).should.equal('http://www.opensupports.com/profilepic.jpg')
|
||||
(result['data'][0]['level']).should.equal('3')
|
||||
(result['data'][0]['departments'][0]['id']).should.equal('1')
|
||||
(result['data'][0]['departments'][0]['name']).should.equal('Tech Support')
|
||||
(result['data'][0]['departments'][1]['id']).should.equal('2')
|
||||
(result['data'][0]['departments'][1]['name']).should.equal('Suggestions')
|
||||
(result['data'][0]['departments'][2]['id']).should.equal('3')
|
||||
(result['data'][0]['departments'][2]['name']).should.equal('Sales and Subscriptions')
|
||||
(result['data'][0]['assignedTickets']).should.equal(3)
|
||||
(result['data'][0]['closedTickets']).should.equal(0)
|
||||
|
||||
(result['data'][1]['name']).should.equal('Arya Stark')
|
||||
(result['data'][1]['email']).should.equal('newwstaff@opensupports.com')
|
||||
(result['data'][1]['profilePic']).should.equal('http://www.opensupports.com/profilepic.jpg')
|
||||
(result['data'][1]['level']).should.equal('2')
|
||||
(result['data'][1]['departments'][0]['id']).should.equal('1')
|
||||
(result['data'][1]['departments'][0]['name']).should.equal('Tech Support')
|
||||
(result['data'][1]['assignedTickets']).should.equal(0)
|
||||
(result['data'][1]['closedTickets']).should.equal(0)
|
||||
end
|
||||
end
|
|
@ -11,5 +11,20 @@ describe '/staff/get/' do
|
|||
(result['status']).should.equal('success')
|
||||
(result['data']['name']).should.equal('Emilia Clarke')
|
||||
(result['data']['staff']).should.equal(true)
|
||||
(result['data']['email']).should.equal('staff@opensupports.com')
|
||||
(result['data']['level']).should.equal('3')
|
||||
end
|
||||
it 'should return staff member data with staff Id' do
|
||||
result = request('/staff/get', {
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token,
|
||||
staffId:2
|
||||
})
|
||||
|
||||
(result['status']).should.equal('success')
|
||||
(result['data']['name']).should.equal('Tyrion Lannister')
|
||||
(result['data']['staff']).should.equal(true)
|
||||
(result['data']['email']).should.equal('tyrion@opensupports.com')
|
||||
(result['data']['level']).should.equal('2')
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue