Merged in TicketNumber-Stage-I (pull request #40)

Ticketnumber stage i
This commit is contained in:
Ivan Diaz 2016-08-22 16:52:29 -03:00
commit fb7693d77e
5 changed files with 62 additions and 6 deletions

View File

@ -50,7 +50,6 @@ class CreateController extends Controller {
$ticket = new Ticket();
$ticket->setProperties(array(
'ticketId' => '',
'title' => $this->title,
'content' => $this->content,
'language' => $this->language,

View File

@ -10,4 +10,7 @@ class Hashing {
public static function generateRandomToken() {
return md5(uniqid(rand()));
}
public static function getRandomTicketNumber($min,$max) {
return rand($min,$max);
}
}

View File

@ -16,6 +16,9 @@ abstract class DataStore {
return ($bean) ? new static($bean) : new NullDataStore();
}
public static function count() {
return RedBean::count(static::TABLE);
}
private static function validateProp($propToValidate) {
$validProp = false;

View File

@ -26,18 +26,35 @@ class Ticket extends DataStore {
public static function getTicket($value, $property = 'id') {
return parent::getDataStore($value, $property);
}
public function getDefaultProps() {
return array(
'owner' => null
'owner' => null,
'ticketNumber' => $this->generateUniqueTicketNumber()
);
}
public function store() {
parent::store();
if ($this->author instanceof User) {
$this->author->store();
}
}
}
public function generateUniqueTicketNumber() {
$ticketQuantity = Ticket::count('ticket');
$minValue = 100000;
$maxValue = 999999;
if ($ticketQuantity === 0) {
$ticketNumber = Hashing::getRandomTicketNumber($minValue, $maxValue);
} else {
$firstTicketNumber = Ticket::getTicket(1)->ticketNumber;
$gap = 176611;
$ticketNumber = ($firstTicketNumber - $minValue + $ticketQuantity * $gap) % ($maxValue - $minValue + 1) + $minValue;
}
return $ticketNumber;
}
}

View File

@ -108,8 +108,42 @@ describe '/ticket/create' do
(ticket['closed']).should.equal('0')
(ticket['department_id']).should.equal('1')
(ticket['author_id']).should.equal($csrf_userid)
(ticket['ticket_number'].size).should.equal(6)
ticket_user_relation = $database.getRow('ticket_user','1','ticket_id')
(ticket_user_relation['user_id']).should.equal($csrf_userid)
end
it 'should set correct ticket number' do
result = request('/ticket/create',{
title: 'Winter is coming1',
content: 'The north remembers',
departmentId: 1,
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
result = request('/ticket/create',{
title: 'Winter is coming2',
content: 'The north remembers',
departmentId: 1,
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
result = request('/ticket/create',{
title: 'Winter is coming3',
content: 'The north remembers',
departmentId: 1,
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
ticket0 = $database.getRow('ticket','Winter is coming','title')['ticket_number'].to_i
ticket1 = $database.getRow('ticket','Winter is coming1','title')['ticket_number'].to_i
ticket2 = $database.getRow('ticket','Winter is coming2','title')['ticket_number'].to_i
ticket3 = $database.getRow('ticket','Winter is coming3','title')['ticket_number'].to_i
(ticket1).should.equal((ticket0 - 100000 + 1 * 176611) % 900000 + 100000)
(ticket2).should.equal((ticket0 - 100000 + 2 * 176611) % 900000 + 100000)
(ticket3).should.equal((ticket0 - 100000 + 3 * 176611) % 900000 + 100000)
end
end