Merge pull request #250 from ivandiazwm/master

Allow staff members to create tickets
This commit is contained in:
Ivan Diaz 2018-07-19 22:08:57 -03:00 committed by GitHub
commit ac5b72f35d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 294 additions and 125 deletions

View File

@ -17,6 +17,7 @@ class TicketList extends React.Component {
ticketPath: React.PropTypes.string, ticketPath: React.PropTypes.string,
showDepartmentDropdown: React.PropTypes.bool, showDepartmentDropdown: React.PropTypes.bool,
tickets: React.PropTypes.arrayOf(React.PropTypes.object), tickets: React.PropTypes.arrayOf(React.PropTypes.object),
userId: React.PropTypes.number,
type: React.PropTypes.oneOf([ type: React.PropTypes.oneOf([
'primary', 'primary',
'secondary' 'secondary'
@ -233,7 +234,15 @@ class TicketList extends React.Component {
} }
isTicketUnread(ticket) { isTicketUnread(ticket) {
return (this.props.type === 'primary' && ticket.unread) || (this.props.type === 'secondary' && ticket.unreadStaff); if(this.props.type === 'primary') {
return ticket.unread;
} else if(this.props.type === 'secondary') {
if(ticket.author.id == this.props.userId && ticket.author.staff) {
return ticket.unread;
} else {
return ticket.unreadStaff;
}
}
} }
} }

View File

@ -99,7 +99,7 @@ class TicketViewer extends React.Component {
</div> </div>
<div className="ticket-viewer__info-row-header row"> <div className="ticket-viewer__info-row-header row">
<div className="col-md-4">{i18n('PRIORITY')}</div> <div className="col-md-4">{i18n('PRIORITY')}</div>
<div className="col-md-4">{i18n('OWNED')}</div> <div className="col-md-4">{i18n('OWNER')}</div>
<div className="col-md-4">{i18n('STATUS')}</div> <div className="col-md-4">{i18n('STATUS')}</div>
</div> </div>
<div className="ticket-viewer__info-row-values row"> <div className="ticket-viewer__info-row-values row">
@ -107,9 +107,7 @@ class TicketViewer extends React.Component {
<DropDown className="ticket-viewer__editable-dropdown" items={priorityList} selectedIndex={priorities[ticket.priority]} onChange={this.onPriorityDropdownChanged.bind(this)} /> <DropDown className="ticket-viewer__editable-dropdown" items={priorityList} selectedIndex={priorities[ticket.priority]} onChange={this.onPriorityDropdownChanged.bind(this)} />
</div> </div>
<div className="col-md-4"> <div className="col-md-4">
<Button type={(ticket.owner) ? 'primary' : 'secondary'} size="extra-small" onClick={this.onAssignClick.bind(this)}> {this.renderEditableOwnerNode()}
{i18n(ticket.owner ? 'UN_ASSIGN' : 'ASSIGN_TO_ME')}
</Button>
</div> </div>
<div className="col-md-4"> <div className="col-md-4">
{ticket.closed ? {ticket.closed ?
@ -162,6 +160,23 @@ class TicketViewer extends React.Component {
); );
} }
renderEditableOwnerNode() {
let ownerNode = null;
let {ticket, userId} = this.props;
if (_.isEmpty(ticket.owner) || ticket.owner.id == userId) {
ownerNode = (
<Button type={(ticket.owner) ? 'primary' : 'secondary'} size="extra-small" onClick={this.onAssignClick.bind(this)}>
{i18n(ticket.owner ? 'UN_ASSIGN' : 'ASSIGN_TO_ME')}
</Button>
);
} else {
ownerNode = (this.props.ticket.owner) ? this.props.ticket.owner.name : i18n('NONE')
}
return ownerNode;
}
renderOwnerNode() { renderOwnerNode() {
let ownerNode = null; let ownerNode = null;
@ -371,6 +386,7 @@ class TicketViewer extends React.Component {
export default connect((store) => { export default connect((store) => {
return { return {
userId: store.session.userId,
allowAttachments: store.config['allow-attachments'], allowAttachments: store.config['allow-attachments'],
userSystemEnabled: store.config['user-system-enabled'] userSystemEnabled: store.config['user-system-enabled']
}; };

View File

@ -235,6 +235,7 @@ class StaffEditor extends React.Component {
getTicketListProps() { getTicketListProps() {
return { return {
type: 'secondary', type: 'secondary',
userId: this.props.staffId,
tickets: this.props.tickets, tickets: this.props.tickets,
departments: this.props.departments, departments: this.props.departments,
ticketPath: '/admin/panel/tickets/view-ticket/' ticketPath: '/admin/panel/tickets/view-ticket/'

View File

@ -13,6 +13,7 @@ import Message from 'core-components/message';
class AdminPanelAllTickets extends React.Component { class AdminPanelAllTickets extends React.Component {
static defaultProps = { static defaultProps = {
userId: 0,
departments: [], departments: [],
tickets: [] tickets: []
}; };
@ -40,6 +41,7 @@ class AdminPanelAllTickets extends React.Component {
getTicketListProps() { getTicketListProps() {
return { return {
userId: this.props.userId,
showDepartmentDropdown: false, showDepartmentDropdown: false,
departments: this.props.departments, departments: this.props.departments,
tickets: this.props.tickets, tickets: this.props.tickets,
@ -75,6 +77,7 @@ class AdminPanelAllTickets extends React.Component {
export default connect((store) => { export default connect((store) => {
return { return {
userId: store.session.userId,
departments: store.session.userDepartments, departments: store.session.userDepartments,
tickets: store.adminData.allTickets, tickets: store.adminData.allTickets,
pages: store.adminData.allTicketsPages, pages: store.adminData.allTicketsPages,

View File

@ -5,13 +5,18 @@ import i18n from 'lib-app/i18n';
import AdminDataAction from 'actions/admin-data-actions'; import AdminDataAction from 'actions/admin-data-actions';
import TicketList from 'app-components/ticket-list'; import TicketList from 'app-components/ticket-list';
import ModalContainer from 'app-components/modal-container';
import CreateTicketForm from 'app/main/dashboard/dashboard-create-ticket/create-ticket-form';
import Button from 'core-components/button';
import Icon from 'core-components/icon';
import Header from 'core-components/header'; import Header from 'core-components/header';
import Message from 'core-components/message'; import Message from 'core-components/message';
class AdminPanelMyTickets extends React.Component { class AdminPanelMyTickets extends React.Component {
static defaultProps = { static defaultProps = {
userId: 0,
departments: [], departments: [],
tickets: [] tickets: []
}; };
@ -19,18 +24,24 @@ class AdminPanelMyTickets extends React.Component {
componentDidMount() { componentDidMount() {
this.props.dispatch(AdminDataAction.retrieveMyTickets()); this.props.dispatch(AdminDataAction.retrieveMyTickets());
} }
render() { render() {
return ( return (
<div className="admin-panel-my-tickets"> <div className="admin-panel-my-tickets">
<Header title={i18n('MY_TICKETS')} description={i18n('MY_TICKETS_DESCRIPTION')} /> <Header title={i18n('MY_TICKETS')} description={i18n('MY_TICKETS_DESCRIPTION')} />
{(this.props.error) ? <Message type="error">{i18n('ERROR_RETRIEVING_TICKETS')}</Message> : <TicketList {...this.getProps()}/>} {(this.props.error) ? <Message type="error">{i18n('ERROR_RETRIEVING_TICKETS')}</Message> : <TicketList {...this.getProps()}/>}
<div style={{textAlign: 'right', marginTop: 10}}>
<Button onClick={this.onCreateTicket.bind(this)} type="secondary" size="medium">
<Icon size="sm" name="plus"/> Create ticket
</Button>
</div>
</div> </div>
); );
} }
getProps() { getProps() {
return { return {
userId: this.props.userId,
departments: this.props.departments, departments: this.props.departments,
tickets: this.props.tickets, tickets: this.props.tickets,
type: 'secondary', type: 'secondary',
@ -38,10 +49,27 @@ class AdminPanelMyTickets extends React.Component {
ticketPath: '/admin/panel/tickets/view-ticket/' ticketPath: '/admin/panel/tickets/view-ticket/'
}; };
} }
onCreateTicket() {
ModalContainer.openModal(
<div>
<CreateTicketForm onSuccess={this.onCreateTicketSuccess.bind(this)} />
<div style={{textAlign: 'center'}}>
<Button onClick={ModalContainer.closeModal} type="link">Close</Button>
</div>
</div>
);
}
onCreateTicketSuccess() {
ModalContainer.closeModal();
this.props.dispatch(AdminDataAction.retrieveMyTickets());
}
} }
export default connect((store) => { export default connect((store) => {
return { return {
userId: store.session.userId,
departments: store.session.userDepartments, departments: store.session.userDepartments,
tickets: store.adminData.myTickets, tickets: store.adminData.myTickets,
loading: !store.adminData.myTicketsLoaded, loading: !store.adminData.myTicketsLoaded,

View File

@ -12,6 +12,7 @@ import Message from 'core-components/message';
class AdminPanelNewTickets extends React.Component { class AdminPanelNewTickets extends React.Component {
static defaultProps = { static defaultProps = {
userId: 0,
departments: [], departments: [],
tickets: [] tickets: []
}; };
@ -31,6 +32,7 @@ class AdminPanelNewTickets extends React.Component {
getProps() { getProps() {
return { return {
userId: this.props.userId,
departments: this.props.departments, departments: this.props.departments,
tickets: this.props.tickets, tickets: this.props.tickets,
type: 'secondary', type: 'secondary',
@ -42,6 +44,7 @@ class AdminPanelNewTickets extends React.Component {
export default connect((store) => { export default connect((store) => {
return { return {
userId: store.session.userId,
departments: store.session.userDepartments, departments: store.session.userDepartments,
tickets: store.adminData.newTickets, tickets: store.adminData.newTickets,
loading: !store.adminData.newTicketsLoaded, loading: !store.adminData.newTicketsLoaded,

View File

@ -75,7 +75,10 @@ class AdminPanelViewTicket extends React.Component {
onChange: this.retrieveTicket.bind(this), onChange: this.retrieveTicket.bind(this),
assignmentAllowed: this.props.assignmentAllowed, assignmentAllowed: this.props.assignmentAllowed,
customResponses: this.props.customResponses, customResponses: this.props.customResponses,
editable: this.state.ticket.owner && this.state.ticket.owner.id == SessionStore.getSessionData().userId editable: (
(this.state.ticket.owner && this.state.ticket.owner.id == SessionStore.getSessionData().userId) ||
(this.state.ticket.author && this.state.ticket.author.staff && this.state.ticket.author.id == SessionStore.getSessionData().userId)
)
}; };
} }
@ -94,7 +97,7 @@ class AdminPanelViewTicket extends React.Component {
ticket: result.data ticket: result.data
}); });
if(!this.props.avoidSeen && result.data.unreadStaff) { if(!this.props.avoidSeen) {
API.call({ API.call({
path: '/ticket/seen', path: '/ticket/seen',
data: { data: {
@ -109,7 +112,7 @@ class AdminPanelViewTicket extends React.Component {
loading: false, loading: false,
ticket: {} ticket: {}
}); });
if(this.props.onRetrieveFail) { if(this.props.onRetrieveFail) {
this.props.onRetrieveFail(); this.props.onRetrieveFail();
} }

View File

@ -6,8 +6,6 @@ import history from 'lib-app/history';
import i18n from 'lib-app/i18n'; import i18n from 'lib-app/i18n';
import API from 'lib-app/api-call'; import API from 'lib-app/api-call';
import SessionStore from 'lib-app/session-store'; import SessionStore from 'lib-app/session-store';
import store from 'app/store';
import SessionActions from 'actions/session-actions';
import LanguageSelector from 'app-components/language-selector'; import LanguageSelector from 'app-components/language-selector';
import Captcha from 'app/main/captcha'; import Captcha from 'app/main/captcha';
@ -21,7 +19,8 @@ import Message from 'core-components/message';
class CreateTicketForm extends React.Component { class CreateTicketForm extends React.Component {
static propTypes = { static propTypes = {
userLogged: React.PropTypes.bool userLogged: React.PropTypes.bool,
onSuccess: React.PropTypes.func,
}; };
static defaultProps = { static defaultProps = {
@ -138,14 +137,11 @@ class CreateTicketForm extends React.Component {
this.setState({ this.setState({
loading: false, loading: false,
message: 'success' message: 'success'
}, () => {
if(this.props.onSuccess) {
this.props.onSuccess();
}
}); });
if(this.props.userLogged) {
store.dispatch(SessionActions.getUserData());
setTimeout(() => {history.push('/dashboard')}, 2000);
} else {
setTimeout(() => {history.push('/check-ticket/' + result.data.ticketNumber + '/' + email)}, 1000);
}
} }
onTicketFail() { onTicketFail() {

View File

@ -2,7 +2,9 @@ import React from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
import {connect} from 'react-redux'; import {connect} from 'react-redux';
import SessionActions from 'actions/session-actions';
import CreateTicketForm from 'app/main/dashboard/dashboard-create-ticket/create-ticket-form'; import CreateTicketForm from 'app/main/dashboard/dashboard-create-ticket/create-ticket-form';
import Widget from 'core-components/widget'; import Widget from 'core-components/widget';
class DashboardCreateTicketPage extends React.Component { class DashboardCreateTicketPage extends React.Component {
@ -21,12 +23,23 @@ class DashboardCreateTicketPage extends React.Component {
return ( return (
<div className={this.getClass()}> <div className={this.getClass()}>
<Wrapper> <Wrapper>
<CreateTicketForm userLogged={(this.props.location.pathname !== '/create-ticket')} /> <CreateTicketForm
userLogged={(this.props.location.pathname !== '/create-ticket')}
onSuccess={this.onCreateTicketSuccess.bind(this)}/>
</Wrapper> </Wrapper>
</div> </div>
); );
} }
onCreateTicketSuccess() {
if((this.props.location.pathname !== '/create-ticket')) {
this.props.dispatch(SessionActions.getUserData());
setTimeout(() => {history.push('/dashboard')}, 2000);
} else {
setTimeout(() => {history.push('/check-ticket/' + result.data.ticketNumber + '/' + email)}, 1000);
}
}
getClass() { getClass() {
let classes = { let classes = {
'dashboard-create-ticket-page': true, 'dashboard-create-ticket-page': true,

View File

@ -59,7 +59,7 @@ class AssignStaffController extends Controller {
} else { } else {
$this->user->sharedTicketList->add($this->ticket); $this->user->sharedTicketList->add($this->ticket);
$this->ticket->owner = $this->user; $this->ticket->owner = $this->user;
$this->ticket->unread = true; $this->ticket->unread = !$this->ticket->isAuthor($this->user);
$event = Ticketevent::getEvent(Ticketevent::ASSIGN); $event = Ticketevent::getEvent(Ticketevent::ASSIGN);
$event->setProperties(array( $event->setProperties(array(
'authorStaff' => Controller::getLoggedUser(), 'authorStaff' => Controller::getLoggedUser(),

View File

@ -15,7 +15,7 @@ use Respect\Validation\Validator as DataValidator;
* @apiPermission staff1 * @apiPermission staff1
* *
* @apiUse NO_PERMISSION * @apiUse NO_PERMISSION
* *
* @apiSuccess {[Ticket](#api-Data_Structures-ObjectTicket)[]} data Array of new tickets. * @apiSuccess {[Ticket](#api-Data_Structures-ObjectTicket)[]} data Array of new tickets.
* *
*/ */
@ -41,13 +41,13 @@ class GetNewTicketsStaffController extends Controller {
foreach ($user->sharedDepartmentList as $department) { foreach ($user->sharedDepartmentList as $department) {
$query .= 'department_id=' . $department->id . ' OR '; $query .= 'department_id=' . $department->id . ' OR ';
} }
$query = substr($query,0,-3);
$ownerExists = RedBean::exec('SHOW COLUMNS FROM ticket LIKE \'owner_id\''); $ownerExists = RedBean::exec('SHOW COLUMNS FROM ticket LIKE \'owner_id\'');
if($ownerExists != 0) { if($ownerExists != 0) {
$query .= ') AND owner_id IS NULL'; $query .= 'FALSE) AND owner_id IS NULL';
} else { } else {
$query .= ')'; $query .= 'FALSE)';
} }
$ticketList = Ticket::find($query); $ticketList = Ticket::find($query);

View File

@ -45,12 +45,14 @@ class UnAssignStaffController extends Controller {
$ticket = Ticket::getByTicketNumber($ticketNumber); $ticket = Ticket::getByTicketNumber($ticketNumber);
$owner = $ticket->owner; $owner = $ticket->owner;
if(($owner && $owner->id === $user->id) || $user->level > 2) { if($ticket->isOwner($user) || $user->level > 2) {
$owner->sharedTicketList->remove($ticket); if(!$ticket->isAuthor($user)) {
$owner->store(); $owner->sharedTicketList->remove($ticket);
$owner->store();
}
$ticket->owner = null; $ticket->owner = null;
$ticket->unread = true; $ticket->unread = !$ticket->isAuthor($user);
$event = Ticketevent::getEvent(Ticketevent::UN_ASSIGN); $event = Ticketevent::getEvent(Ticketevent::UN_ASSIGN);
$event->setProperties(array( $event->setProperties(array(

View File

@ -64,7 +64,7 @@ class ChangeDepartmentController extends Controller {
)); ));
$ticket->addEvent($event); $ticket->addEvent($event);
$ticket->department = $department; $ticket->department = $department;
$ticket->unread = true; $ticket->unread = !$ticket->isAuthor($user);
$ticket->store(); $ticket->store();
if(!$user->sharedDepartmentList->includesId($department->id)) { if(!$user->sharedDepartmentList->includesId($department->id)) {

View File

@ -18,9 +18,9 @@ use Respect\Validation\Validator as DataValidator;
* *
* @apiUse NO_PERMISSION * @apiUse NO_PERMISSION
* @apiUse INVALID_TICKET * @apiUse INVALID_TICKET
* @apiUse INVALID_PRIORITY * @apiUse INVALID_PRIORITY
* *
* @apiSuccess {Object} data Empty object * @apiSuccess {Object} data Empty object
* *
*/ */
@ -52,10 +52,10 @@ class ChangePriorityController extends Controller {
if($ticket->owner && $user->id === $ticket->owner->id) { if($ticket->owner && $user->id === $ticket->owner->id) {
$ticket->priority = $priority; $ticket->priority = $priority;
$ticket->unread = true; $ticket->unread = !$ticket->isAuthor($user);
$event = Ticketevent::getEvent(Ticketevent::PRIORITY_CHANGED); $event = Ticketevent::getEvent(Ticketevent::PRIORITY_CHANGED);
$event->setProperties(array( $event->setProperties(array(
'authorStaff' => Controller::getLoggedUser(), 'authorStaff' => Controller::getLoggedUser(),
'content' => $ticket->priority, 'content' => $ticket->priority,
'date' => Date::getCurrentDate() 'date' => Date::getCurrentDate()
)); ));
@ -70,5 +70,3 @@ class ChangePriorityController extends Controller {
} }
} }

View File

@ -62,9 +62,12 @@ class CloseController extends Controller {
public function handler() { public function handler() {
$this->ticket = Ticket::getByTicketNumber(Controller::request('ticketNumber')); $this->ticket = Ticket::getByTicketNumber(Controller::request('ticketNumber'));
if($this->shouldDenyPermission()) { if(
Response::respondError(ERRORS::NO_PERMISSION); (Controller::isUserSystemEnabled() || Controller::isStaffLogged()) &&
return; !$this->ticket->isOwner(Controller::getLoggedUser()) &&
!$this->ticket->isAuthor(Controller::getLoggedUser())
) {
throw new Exception(ERRORS::NO_PERMISSION);
} }
$this->markAsUnread(); $this->markAsUnread();
@ -79,22 +82,9 @@ class CloseController extends Controller {
Response::respondSuccess(); Response::respondSuccess();
} }
private function shouldDenyPermission() {
if(Controller::isStaffLogged()) {
return $this->ticket->owner && $this->ticket->owner->id !== Controller::getLoggedUser()->id;
} else if(Controller::isUserSystemEnabled()) {
return $this->ticket->author->id !== Controller::getLoggedUser()->id;
} else {
return false;
}
}
private function markAsUnread() { private function markAsUnread() {
if(Controller::isStaffLogged()) { $this->ticket->unread = !$this->ticket->isAuthor(Controller::getLoggedUser());
$this->ticket->unread = true; $this->ticket->unreadStaff = !$this->ticket->isOwner(Controller::getLoggedUser());
} else {
$this->ticket->unreadStaff = true;
}
} }
private function addCloseEvent() { private function addCloseEvent() {

View File

@ -65,7 +65,6 @@ class CommentController extends Controller {
'csrf_token' => [ 'csrf_token' => [
'validation' => DataValidator::equals($session->getToken()), 'validation' => DataValidator::equals($session->getToken()),
'error' => ERRORS::INVALID_TOKEN 'error' => ERRORS::INVALID_TOKEN
] ]
] ]
]; ];
@ -73,24 +72,30 @@ class CommentController extends Controller {
} }
public function handler() { public function handler() {
$session = Session::getInstance();
$this->requestData(); $this->requestData();
$ticketAuthor = $this->ticket->authorToArray();
$isAuthor = $this->ticket->isAuthor(Controller::getLoggedUser());
$isOwner = $this->ticket->isOwner(Controller::getLoggedUser());
if ((!Controller::isUserSystemEnabled() && !Controller::isStaffLogged()) || if((Controller::isUserSystemEnabled() || Controller::isStaffLogged()) && !$isOwner && !$isAuthor) {
(!Controller::isStaffLogged() && $session->isLoggedWithId(($this->ticket->author) ? $this->ticket->author->id : 0)) || throw new Exception(ERRORS::NO_PERMISSION);
(Controller::isStaffLogged() && $session->isLoggedWithId(($this->ticket->owner) ? $this->ticket->owner->id : 0))) {
$this->storeComment();
if(Controller::isStaffLogged() || $this->ticket->owner) {
$this->sendMail();
}
Log::createLog('COMMENT', $this->ticket->ticketNumber);
Response::respondSuccess();
} else {
Response::respondError(ERRORS::NO_PERMISSION);
} }
$this->storeComment();
if($isAuthor && $this->ticket->owner) {
$this->sendMail([
'email' => $this->ticket->owner->email,
'name' => $this->ticket->owner->name,
'staff' => true
]);
} else {
$this->sendMail($ticketAuthor);
}
Log::createLog('COMMENT', $this->ticket->ticketNumber);
Response::respondSuccess();
} }
private function requestData() { private function requestData() {
@ -110,7 +115,8 @@ class CommentController extends Controller {
)); ));
if(Controller::isStaffLogged()) { if(Controller::isStaffLogged()) {
$this->ticket->unread = true; $this->ticket->unread = !$this->ticket->isAuthor(Controller::getLoggedUser());
$this->ticket->unreadStaff = !$this->ticket->isOwner(Controller::getLoggedUser());
$comment->authorStaff = Controller::getLoggedUser(); $comment->authorStaff = Controller::getLoggedUser();
} else if(Controller::isUserSystemEnabled()) { } else if(Controller::isUserSystemEnabled()) {
$this->ticket->unreadStaff = true; $this->ticket->unreadStaff = true;
@ -121,20 +127,16 @@ class CommentController extends Controller {
$this->ticket->store(); $this->ticket->store();
} }
private function sendMail() { private function sendMail($recipient) {
$mailSender = MailSender::getInstance(); $mailSender = MailSender::getInstance();
$email = ($this->ticket->author) ? $this->ticket->author->email : $this->ticket->authorEmail; $email = $recipient['email'];
$name = ($this->ticket->author) ? $this->ticket->author->name : $this->ticket->authorName; $name = $recipient['name'];
$isStaff = $recipient['staff'];
if(!Controller::isStaffLogged() && $this->ticket->owner) {
$email = $this->ticket->owner->email;
$name = $this->ticket->owner->name;
}
$url = Setting::getSetting('url')->getValue(); $url = Setting::getSetting('url')->getValue();
if(!Controller::isUserSystemEnabled()) { if(!Controller::isUserSystemEnabled() && !$isStaff) {
$url .= '/check-ticket/' . $this->ticket->ticketNumber; $url .= '/check-ticket/' . $this->ticket->ticketNumber;
$url .= '/' . $email; $url .= '/' . $email;
} }

View File

@ -122,7 +122,6 @@ class CreateController extends Controller {
'title' => $this->title, 'title' => $this->title,
'content' => $this->content, 'content' => $this->content,
'language' => $this->language, 'language' => $this->language,
'author' => $author,
'department' => $department, 'department' => $department,
'file' => ($fileUploader instanceof FileUploader) ? $fileUploader->getFileName() : null, 'file' => ($fileUploader instanceof FileUploader) ? $fileUploader->getFileName() : null,
'date' => Date::getCurrentDate(), 'date' => Date::getCurrentDate(),
@ -130,19 +129,23 @@ class CreateController extends Controller {
'unreadStaff' => true, 'unreadStaff' => true,
'closed' => false, 'closed' => false,
'authorName' => $this->name, 'authorName' => $this->name,
'authorEmail' => $this->email 'authorEmail' => $this->email,
)); ));
if(Controller::isUserSystemEnabled()) { $ticket->setAuthor($author);
if(Controller::isUserSystemEnabled() || Controller::isStaffLogged()) {
$author->sharedTicketList->add($ticket); $author->sharedTicketList->add($ticket);
}
if(Controller::isUserSystemEnabled() && !Controller::isStaffLogged()) {
$author->tickets++; $author->tickets++;
$this->email = $author->email; $this->email = $author->email;
$this->name = $author->name; $this->name = $author->name;
$author->store();
} }
$author->store();
$ticket->store(); $ticket->store();
$this->ticketNumber = $ticket->ticketNumber; $this->ticketNumber = $ticket->ticketNumber;

View File

@ -43,20 +43,20 @@ class SeenController extends Controller {
$ticketnumber = Controller::request('ticketNumber'); $ticketnumber = Controller::request('ticketNumber');
$user = Controller::getLoggedUser(); $user = Controller::getLoggedUser();
$ticket = Ticket::getByTicketNumber($ticketnumber); $ticket = Ticket::getByTicketNumber($ticketnumber);
if (Controller::isStaffLogged() && $ticket->owner && $ticket->owner->id === $user->id) { if(!$ticket->isOwner($user) && !$ticket->isAuthor($user)) {
throw new Exception(ERRORS::NO_PERMISSION);
}
if ($ticket->isOwner($user)) {
$ticket->unreadStaff = false; $ticket->unreadStaff = false;
$ticket->store();
Response::respondSuccess();
return;
} }
if (!Controller::isStaffLogged() && $ticket->author && $user->id === $ticket->author->id) { if ($ticket->isAuthor($user)) {
$ticket->unread = false; $ticket->unread = false;
$ticket->store();
Response::respondSuccess();
return;
} }
Response::respondError(ERRORS::NO_PERMISSION);
$ticket->store();
Response::respondSuccess();
} }
} }

View File

@ -43,6 +43,7 @@ class Ticket extends DataStore {
'closed', 'closed',
'priority', 'priority',
'author', 'author',
'authorStaff',
'owner', 'owner',
'ownTicketeventList', 'ownTicketeventList',
'unreadStaff', 'unreadStaff',
@ -60,6 +61,22 @@ class Ticket extends DataStore {
return Ticket::getTicket($value, 'ticketNumber'); return Ticket::getTicket($value, 'ticketNumber');
} }
public function setAuthor($author) {
if($author instanceof User) {
$this->author = $author;
} else if($author instanceof Staff) {
$this->authorStaff = $author;
}
}
public function getAuthor() {
if($this->author && !$this->author->isNull()) {
return $this->author;
} else {
return $this->authorStaff;
}
}
public function getDefaultProps() { public function getDefaultProps() {
return array( return array(
'priority' => 'low', 'priority' => 'low',
@ -112,18 +129,20 @@ class Ticket extends DataStore {
} }
public function authorToArray() { public function authorToArray() {
$author = $this->author; $author = $this->getAuthor();
if ($author && !$author->isNull()) { if ($author && !$author->isNull()) {
return [ return [
'id' => $author->id, 'id' => $author->id,
'name' => $author->name, 'name' => $author->name,
'staff' => $author instanceof Staff,
'profilePic' => ($author instanceof Staff) ? $author->profilePic : null,
'email' => $author->email 'email' => $author->email
]; ];
} else { } else {
return [ return [
'name' => $this->authorName, 'name' => $this->authorName,
'email' => $this->authorEmail 'email' => $this->authorEmail
]; ];
} }
} }
@ -155,7 +174,7 @@ class Ticket extends DataStore {
]; ];
$author = $ticketEvent->getAuthor(); $author = $ticketEvent->getAuthor();
if(!$author->isNull()) { if($author && !$author->isNull()) {
$event['author'] = [ $event['author'] = [
'id'=> $author->id, 'id'=> $author->id,
'name' => $author->name, 'name' => $author->name,
@ -174,4 +193,13 @@ class Ticket extends DataStore {
public function addEvent(Ticketevent $event) { public function addEvent(Ticketevent $event) {
$this->ownTicketeventList->add($event); $this->ownTicketeventList->add($event);
} }
public function isAuthor($user) {
$ticketAuthor = $this->authorToArray();
return $user->id == $ticketAuthor['id'] && ($user instanceof Staff) == $ticketAuthor['staff'];
}
public function isOwner($user) {
return $this->owner && $user->id == $this->owner->id && ($user instanceof Staff);
}
} }

View File

@ -20,7 +20,7 @@ describe'/staff/get-all' do
(result['data'][0]['departments'][1]['name']).should.equal('Suggestions') (result['data'][0]['departments'][1]['name']).should.equal('Suggestions')
(result['data'][0]['departments'][2]['id']).should.equal('3') (result['data'][0]['departments'][2]['id']).should.equal('3')
(result['data'][0]['departments'][2]['name']).should.equal('Tech support') (result['data'][0]['departments'][2]['name']).should.equal('Tech support')
(result['data'][0]['assignedTickets']).should.equal(3) (result['data'][0]['assignedTickets']).should.equal(4)
(result['data'][0]['closedTickets']).should.equal(0) (result['data'][0]['closedTickets']).should.equal(0)
(result['data'][2]['name']).should.equal('Arya Stark') (result['data'][2]['name']).should.equal('Arya Stark')

View File

@ -10,6 +10,6 @@ describe '/staff/get-new-tickets' do
}) })
(result['status']).should.equal('success') (result['status']).should.equal('success')
(result['data'].size).should.equal(9) (result['data'].size).should.equal(10)
end end
end end

View File

@ -22,6 +22,6 @@ describe '/staff/get-tickets' do
}) })
(result['status']).should.equal('success') (result['status']).should.equal('success')
(result['data'].size).should.equal(2) (result['data'].size).should.equal(3)
end end
end end

View File

@ -23,7 +23,7 @@ describe '/staff/un-assign-ticket' do
(ticket['owner_id']).should.equal(nil) (ticket['owner_id']).should.equal(nil)
(ticket['unread']).should.equal('1') (ticket['unread']).should.equal('1')
staff_ticket = $database.getRow('staff_ticket', 1 , 'id') staff_ticket = $database.getRow('staff_ticket', 1 , 'ticket_id')
(staff_ticket).should.equal(nil) (staff_ticket).should.equal(nil)
end end
@ -75,7 +75,7 @@ describe '/staff/un-assign-ticket' do
(ticket['owner_id']).should.equal(nil) (ticket['owner_id']).should.equal(nil)
(ticket['unread']).should.equal('1') (ticket['unread']).should.equal('1')
staff_ticket = $database.getRow('staff_ticket', 1 , 'id') staff_ticket = $database.getRow('staff_ticket', 1 , 'ticket_id')
(staff_ticket).should.equal(nil) (staff_ticket).should.equal(nil)
end end

View File

@ -92,7 +92,7 @@ describe'system/disable-user-system' do
numberOftickets= $database.query("SELECT * FROM ticket WHERE author_email IS NULL AND author_name IS NULL AND author_id IS NOT NULL" ) numberOftickets= $database.query("SELECT * FROM ticket WHERE author_email IS NULL AND author_name IS NULL AND author_id IS NOT NULL" )
(numberOftickets.num_rows).should.equal(39) (numberOftickets.num_rows).should.equal(40)
end end

View File

@ -2,9 +2,32 @@ describe '/ticket/close' do
request('/user/logout') request('/user/logout')
Scripts.login($staff[:email], $staff[:password], true) Scripts.login($staff[:email], $staff[:password], true)
it 'should close a ticket if everything is okey' do it 'should not close ticket if not assigned' do
ticket = $database.getRow('ticket', 1 , 'id')
request('/staff/un-assign-ticket', {
ticketNumber: ticket['ticket_number'],
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
result = request('/ticket/close', {
ticketNumber: ticket['ticket_number'],
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
(result['status']).should.equal('fail')
end
it 'should close ticket if you have it assigned' do
ticket = $database.getRow('ticket', 1 , 'id') ticket = $database.getRow('ticket', 1 , 'id')
request('/staff/assign-ticket', {
ticketNumber: ticket['ticket_number'],
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
result = request('/ticket/close', { result = request('/ticket/close', {
ticketNumber: ticket['ticket_number'], ticketNumber: ticket['ticket_number'],
csrf_userid: $csrf_userid, csrf_userid: $csrf_userid,
@ -19,28 +42,34 @@ describe '/ticket/close' do
lastLog = $database.getLastRow('log') lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('CLOSE') (lastLog['type']).should.equal('CLOSE')
request('/staff/un-assign-ticket', {
request('/user/logout')
Scripts.createUser('closer@os4.com','closer','Closer')
Scripts.login('closer@os4.com','closer')
Scripts.createTicket('tickettoclose')
ticket = $database.getRow('ticket', 'tickettoclose', 'title')
result = request('/ticket/close', {
ticketNumber: ticket['ticket_number'], ticketNumber: ticket['ticket_number'],
csrf_userid: $csrf_userid, csrf_userid: $csrf_userid,
csrf_token: $csrf_token csrf_token: $csrf_token
}) })
end
(result['status']).should.equal('success') it 'should close ticket if you are the author' do
request('/user/logout')
Scripts.createUser('closer@os4.com','closer','Closer')
Scripts.login('closer@os4.com','closer')
Scripts.createTicket('tickettoclose')
ticket = $database.getRow('ticket', 'tickettoclose', 'title') ticket = $database.getRow('ticket', 'tickettoclose', 'title')
(ticket['closed']).should.equal('1')
lastLog = $database.getLastRow('log') result = request('/ticket/close', {
(lastLog['type']).should.equal('CLOSE') ticketNumber: ticket['ticket_number'],
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
(result['status']).should.equal('success')
ticket = $database.getRow('ticket', 'tickettoclose', 'title')
(ticket['closed']).should.equal('1')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('CLOSE')
end end
end end

View File

@ -78,6 +78,31 @@ describe '/ticket/comment/' do
(lastLog['type']).should.equal('COMMENT') (lastLog['type']).should.equal('COMMENT')
end end
it 'should add comment to ticket created by staff' do
request('/user/logout')
Scripts.login($staff[:email], $staff[:password], true)
result = request('/ticket/comment', {
content: 'some comment content',
ticketNumber: $ticketNumberByStaff,
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
(result['status']).should.equal('success')
ticket = $database.getRow('ticket', $ticketNumberByStaff, 'ticket_number')
comment = $database.getRow('ticketevent', ticket['id'], 'ticket_id')
(comment['content']).should.equal('some comment content')
(comment['type']).should.equal('COMMENT')
(comment['author_staff_id']).should.equal($csrf_userid)
(ticket['unread_staff']).should.equal('1')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('COMMENT')
request('/user/logout')
end
it 'should fail if user is not the author nor owner' do it 'should fail if user is not the author nor owner' do
Scripts.createUser('no_commenter@comment.com', 'no_commenter', 'No Commenter') Scripts.createUser('no_commenter@comment.com', 'no_commenter', 'No Commenter')
Scripts.login('no_commenter@comment.com', 'no_commenter') Scripts.login('no_commenter@comment.com', 'no_commenter')

View File

@ -144,4 +144,24 @@ describe '/ticket/create' do
(ticket2).should.equal((ticket0 - 100000 + 2 * ticket_number_gap) % 900000 + 100000) (ticket2).should.equal((ticket0 - 100000 + 2 * ticket_number_gap) % 900000 + 100000)
(ticket3).should.equal((ticket0 - 100000 + 3 * ticket_number_gap) % 900000 + 100000) (ticket3).should.equal((ticket0 - 100000 + 3 * ticket_number_gap) % 900000 + 100000)
end end
it 'should be able to create a ticket while being staff' do
request('/user/logout')
Scripts.login($staff[:email], $staff[:password], true)
result = request('/ticket/create', {
title: 'created by staff',
content: 'The staff created it',
departmentId: 1,
language: 'en',
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
(result['status']).should.equal('success')
ticket = $database.getRow('ticket', result['data']['ticketNumber'], 'ticket_number')
(ticket['author_id']).should.equal(nil)
(ticket['author_staff_id']).should.equal('1')
$ticketNumberByStaff = result['data']['ticketNumber']
request('/user/logout')
end
end end