Merged in OS-83-(Backend)-Ticket-events-architecture (pull request #54)

OS-83 (backend) ticket events architecture
This commit is contained in:
Ivan Diaz 2016-09-29 16:53:01 -03:00
commit 9fba1562ea
8 changed files with 113 additions and 39 deletions

View File

@ -44,14 +44,19 @@ class CommentController extends Controller {
}
private function storeComment() {
$comment = new Comment();
$comment = Ticketevent::getEvent(Ticketevent::COMMENT);
$comment->setProperties(array(
'content' => $this->content,
'author' => Controller::getLoggedUser(),
'date' => Date::getCurrentDate()
));
$this->ticket->ownCommentList->add($comment);
if(Controller::isStaffLogged()) {
$comment->authorUser = Controller::getLoggedUser();
} else {
$comment->authorUser = Controller::getLoggedUser();
}
$this->ticket->addEvent($comment);
$this->ticket->store();
}
}

View File

@ -15,4 +15,5 @@ class ERRORS {
const INIT_SETTINGS_DONE = 'Settings already initialized';
const INVALID_OLD_PASSWORD = 'Invalid old password';
const INVALID_CAPTCHA = 'Invalid captcha';
const INVALID_TICKET_EVENT = 'INVALID_TICKET_EVENT';
}

View File

@ -1,14 +0,0 @@
<?php
class Comment extends DataStore {
const TABLE = 'comment';
public static function getProps() {
return array(
'content',
'file',
'author',
'date'
);
}
}

View File

@ -17,7 +17,7 @@ class Ticket extends DataStore {
'closed',
'author',
'owner',
'ownCommentList'
'ownTicketeventList'
);
}
@ -39,6 +39,7 @@ class Ticket extends DataStore {
public function store() {
parent::store();
}
public function generateUniqueTicketNumber() {
$ticketQuantity = Ticket::count('ticket');
$minValue = 100000;
@ -57,8 +58,6 @@ class Ticket extends DataStore {
}
public function toArray() {
$author = $this->author;
return [
'ticketNumber' => $this->ticketNumber,
'title' => $this->title,
@ -74,7 +73,7 @@ class Ticket extends DataStore {
'closed' => !!$this->closed,
'author' => $this->authorToArray(),
'owner' => $this->ownerToArray(),
'comments' => $this->commentsToArray()
'events' => $this->eventsToArray()
];
}
@ -106,23 +105,35 @@ class Ticket extends DataStore {
}
}
public function commentsToArray() {
$comments = [];
public function eventsToArray() {
$events = [];
foreach ($this->ownCommentList as $comment) {
$comments[] = [
'content'=> $comment->content,
'author' => [
'id'=> 15,
'name' => $comment->author->name,
'email' => $comment->author->email,
'staff' => $comment->author->staff
],
'date'=> $comment->date,
'file'=> $comment->file
foreach ($this->ownTicketeventList as $ticketEvent) {
$event = [
'type' => $ticketEvent->type,
'content'=> $ticketEvent->content,
'author' => [],
'date'=> $ticketEvent->date,
'file'=> $ticketEvent->file
];
$author = $ticketEvent->getAuthor();
if(!$author->isNull()) {
$event['author'] = [
'id'=> $author->id,
'name' => $author->name,
'email' =>$author->email,
'staff' => $author instanceof Staff
];
}
$events[] = $event;
}
return $comments;
return $events;
}
public function addEvent(Ticketevent $event) {
$this->ownTicketeventList->add($event);
}
}

View File

@ -0,0 +1,60 @@
<?php
class Ticketevent extends DataStore {
const TABLE = 'ticketevent';
const COMMENT = 'COMMENT';
const ASSIGN = 'ASSIGN';
const UNASSIGN = 'UNASSIGN';
const CLOSE = 'CLOSE';
const REOPEN = 'REOPEN';
const DEPARTMENT_CHANGED = 'DEPARTMENT_CHANGED';
const PRIORITY_CHANGED = 'PRIORITY_CHANGED';
private static function getEventTypes() {
return [
'COMMENT',
'UNASSIGN',
'CLOSE',
'REOPEN',
'DEPARTMENT_CHANGED',
'PRIORITY_CHANGED',
];
}
public static function getEvent($type) {
if (!in_array($type, Ticketevent::getEventTypes())) {
return new NullDataStore();
}
$ticketEvent = new Ticketevent();
$ticketEvent->setProperties([
'type' => $type
]);
return $ticketEvent;
}
public function getProps() {
return [
'type',
'content',
'file',
'authorUser',
'authorStaff',
'date'
];
}
public function getAuthor() {
if($this->authorUser) {
return $this->authorUser;
}
if($this->authorStaff) {
return $this->authorStaff;
}
return new NullDataStore();
}
}

View File

@ -68,9 +68,10 @@ describe '/ticket/comment/' do
(result['status']).should.equal('success')
ticket = $database.getRow('ticket', @ticketNumber, 'ticket_number')
comment = $database.getRow('comment', ticket['id'], 'ticket_id')
comment = $database.getRow('ticketevent', ticket['id'], 'ticket_id')
(comment['content']).should.equal('some comment content')
(comment['author_id']).should.equal($csrf_userid)
(comment['type']).should.equal('COMMENT')
(comment['author_user_id']).should.equal($csrf_userid)
end
it 'should fail if user is not the author nor owner' do

View File

@ -15,6 +15,13 @@ describe '/ticket/get/' do
csrf_token: $csrf_token
})
@ticketNumber = result['data']['ticketNumber']
request('/ticket/comment', {
ticketNumber: @ticketNumber,
content: 'some valid comment made',
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
end
it 'should fail if ticketNumber is invalid' do
@ -46,6 +53,7 @@ describe '/ticket/get/' do
result = Scripts.login('cersei@os4.com', 'cersei')
$csrf_userid = result['userId']
$csrf_token = result['token']
result = request('/ticket/get', {
ticketNumber: @ticketNumber,
csrf_userid: $csrf_userid,
@ -68,6 +76,8 @@ describe '/ticket/get/' do
(result['data']['author']['name']).should.equal('Cersei Lannister')
(result['data']['author']['email']).should.equal('cersei@os4.com')
(result['data']['owner']).should.equal([])
(result['data']['comments']).should.equal([])
(result['data']['events'].size).should.equal(1)
(result['data']['events'][0]['type']).should.equal('COMMENT')
(result['data']['events'][0]['content']).should.equal('some valid comment made')
end
end

View File

@ -52,6 +52,6 @@ describe '/user/get' do
(ticketFromUser['author']['name']).should.equal('User Get')
(ticketFromUser['author']['email']).should.equal('user_get@os4.com')
(ticketFromUser['owner']).should.equal([])
(ticketFromUser['comments']).should.equal([])
(ticketFromUser['events']).should.equal([])
end
end