diff --git a/server/controllers/ticket.php b/server/controllers/ticket.php index 89296842..d95c777c 100644 --- a/server/controllers/ticket.php +++ b/server/controllers/ticket.php @@ -7,6 +7,8 @@ include 'ticket/delete-custom-response.php'; include 'ticket/edit-custom-response.php'; include 'ticket/get-custom-responses.php'; include 'ticket/change-department.php'; +include 'ticket/close.php'; +include 'ticket/re-open.php'; $ticketControllers = new ControllerGroup(); $ticketControllers->setGroupPath('/ticket'); @@ -19,5 +21,6 @@ $ticketControllers->addController(new DeleteCustomResponseController); $ticketControllers->addController(new EditCustomResponseController); $ticketControllers->addController(new GetCustomResponsesController); $ticketControllers->addController(new ChangeDepartmentController); - +$ticketControllers->addController(new CloseController); +$ticketControllers->addController(new ReOpenController); $ticketControllers->finalize(); \ No newline at end of file diff --git a/server/controllers/ticket/close.php b/server/controllers/ticket/close.php new file mode 100644 index 00000000..5e0b04f5 --- /dev/null +++ b/server/controllers/ticket/close.php @@ -0,0 +1,39 @@ + 'user', + 'requestData' => [ + 'ticketNumber' => [ + 'validation' => DataValidator::validTicketNumber(), + 'error' => ERRORS::INVALID_TICKET + ] + ] + ]; + } + + public function handler() { + $ticketNumber = Controller::request('ticketNumber'); + $ticket = Ticket::getByTicketNumber($ticketNumber); + $user = Controller::getLoggedUser(); + + if(!Controller::isStaffLogged() && $ticket->author->id !== $user->id){ + Response::respondError(ERRORS::NO_PERMISSION); + return; + } + + if(Controller::isStaffLogged() && $ticket->owner && $ticket->owner->id !== $user->id){ + Response::respondError(ERRORS::NO_PERMISSION); + return; + } + + $ticket->closed = true; + $ticket->store(); + Response::respondSuccess(); + } +} \ No newline at end of file diff --git a/server/controllers/ticket/re-open.php b/server/controllers/ticket/re-open.php new file mode 100644 index 00000000..0ee2caae --- /dev/null +++ b/server/controllers/ticket/re-open.php @@ -0,0 +1,38 @@ + 'user', + 'requestData' => [ + 'ticketNumber' => [ + 'validation' => DataValidator::validTicketNumber(), + 'error' => ERRORS::INVALID_TICKET + ] + ] + ]; + } + + public function handler() { + $ticketNumber = Controller::request('ticketNumber'); + $ticket = Ticket::getByTicketNumber($ticketNumber); + $user = Controller::getLoggedUser(); + + if(!Controller::isStaffLogged() && $ticket->author->id !== $user->id){ + Response::respondError(ERRORS::NO_PERMISSION); + return; + } + + if(Controller::isStaffLogged() && $ticket->owner && $ticket->owner->id !== $user->id){ + Response::respondError(ERRORS::NO_PERMISSION); + return; + } + + $ticket->closed = false; + $ticket->store(); + Response::respondSuccess(); + } +} \ No newline at end of file diff --git a/tests/init.rb b/tests/init.rb index 3b931eeb..c4da8c53 100644 --- a/tests/init.rb +++ b/tests/init.rb @@ -22,7 +22,11 @@ require './ticket/create.rb' require './ticket/comment.rb' require './ticket/get.rb' require './ticket/custom-response.rb' +require './ticket/change-department.rb' +require './ticket/close.rb' +require './ticket/re-open.rb' require './staff/get.rb' require './staff/assign-ticket.rb' require './staff/un-assign-ticket.rb' require './staff/get-tickets.rb' + diff --git a/tests/ticket/change-department.rb b/tests/ticket/change-department.rb index ca003236..e7e96b78 100644 --- a/tests/ticket/change-department.rb +++ b/tests/ticket/change-department.rb @@ -5,10 +5,11 @@ describe '/ticket/change-department' do #TODO: Create tests it 'should change department if everything is okey' do + ticket = $database.getRow('ticket', 1 , 'id') result = request('/ticket/change-department', { - ticketNumber:1, - departmentId:2, + ticketNumber: ticket['ticket_number'], + departmentId: 2, csrf_userid: $csrf_userid, csrf_token: $csrf_token }) @@ -16,6 +17,6 @@ describe '/ticket/change-department' do (result['status']).should.equal('success') ticket = $database.getRow('ticket', 1 , 'id') - (ticket['department']).should.equal('2') + (ticket['department_id']).should.equal('2') end end \ No newline at end of file diff --git a/tests/ticket/close.rb b/tests/ticket/close.rb new file mode 100644 index 00000000..ab305ab1 --- /dev/null +++ b/tests/ticket/close.rb @@ -0,0 +1,22 @@ +describe '/ticket/close' do + request('/user/logout') + Scripts.login($staff[:email], $staff[:password], true) + + #TODO: DO THINGS + + it 'should close a ticket if everything is okey' do + ticket = $database.getRow('ticket', 1 , 'id') + + result = request('/ticket/close', { + ticketNumber: ticket['ticket_number'], + csrf_userid: $csrf_userid, + csrf_token: $csrf_token + }) + + (result['status']).should.equal('success') + + ticket = $database.getRow('ticket', 1 , 'id') + (ticket['closed']).should.equal('1') + + end +end \ No newline at end of file diff --git a/tests/ticket/re-open.rb b/tests/ticket/re-open.rb new file mode 100644 index 00000000..2cacfbbd --- /dev/null +++ b/tests/ticket/re-open.rb @@ -0,0 +1,22 @@ +describe '/ticket/re-open' do + request('/user/logout') + Scripts.login($staff[:email], $staff[:password], true) + + #TODO: DO THINGS + + it 'should re open a ticket if everything is okey' do + ticket = $database.getRow('ticket', 1 , 'id') + + result = request('/ticket/re-open', { + ticketNumber: ticket['ticket_number'], + csrf_userid: $csrf_userid, + csrf_token: $csrf_token + }) + + (result['status']).should.equal('success') + + ticket = $database.getRow('ticket', 1 , 'id') + (ticket['closed']).should.equal('0') + + end +end \ No newline at end of file