1
0
Fork 0
mirror of https://github.com/Oreolek/kangana.git synced 2024-04-27 14:49:21 +03:00

Proper period check

This commit is contained in:
Alexander Yakovlev 2016-11-12 19:27:24 +07:00
parent 3e89cdc8dd
commit d898efd8b4
3 changed files with 51 additions and 10 deletions

View file

@ -139,9 +139,13 @@ class Model_Task extends ORM {
* Get last sent or prepared letter and check if it's time to send another one.
* @return bool
**/
public static function check_period($client_id, $letters, $period)
public static function check_period($client_id, $letters, $period = NULL)
{
$query = DB::select('date')
if (is_null($period) or $period === 0)
{
return TRUE;
}
$query = DB::select(array(DB::expr('COUNT(*)'), 'cnt'))
->from('tasks');
if (is_array($letters))
{
@ -151,15 +155,21 @@ class Model_Task extends ORM {
{
$query = $query->where('letter_id', '=', $letters);
}
$check = NULL;
$query = $query
->and_where('status', 'IN', [self::STATUS_SENT, self::STATUS_PENDING])
->and_where('client_id', '=', $client_id);
$check = $query->execute()->get('cnt');
if ($check === '0') // no letters at all
{
return TRUE;
}
$check = $query
->and_where('status', '=', self::STATUS_SENT)
->or_where('status', '=', self::STATUS_PENDING)
->and_where('client_id', '=', $client_id)
->and_where(DB::expr('DATEDIFF(CURDATE(), `date`)'), '=', $period)
->and_where(DB::expr('DATEDIFF(CURDATE(), `date`)'), '>=', $period)
->execute()
->get('date');
return is_null($check);
->get('cnt');
return ( $check !== '0');
}
/**

View file

@ -48,7 +48,7 @@ class Task_Prepare extends Minion_Task
Model_Task::prepare($client_id, $letter);
}
} else {
echo "Letter won't be prepared because it's too early to send it.";
echo "Letter won't be prepared because it's too early to send it.\n";
}
}

View file

@ -35,6 +35,37 @@ class CoursesTest extends Unittest_Database_TestCase
$this->assertEquals(Model_Task::STATUS_PENDING, $status);
}
/**
* @group Mail
* Tests that letter shouldn't be prepared if it's too early to send it.
**/
function test_prepare_timeout()
{
DB::delete('tasks')
->where('letter_id', '=', 1)
->and_where('client_id','=',1)
->execute();
DB::insert('tasks', array('letter_id', 'client_id', 'date', 'status'))
->values(array(1,1,date('Y-m-d'), Model_Task::STATUS_PENDING))
->execute();
$check = Model_Task::check_period(1, 1, NULL);
$this->assertEquals(TRUE, $check);
$check = Model_Task::check_period(1, 1, 1);
$this->assertEquals(FALSE, $check);
$check = Model_Task::check_period(1, 1, 0);
$this->assertEquals(TRUE, $check);
DB::delete('tasks')
->where('letter_id', '=', 1)
->and_where('client_id','=',1)
->execute();
DB::insert('tasks', array('letter_id', 'client_id', 'date', 'status'))
->values(array(1,1,date('Y-m-d', strtotime("-1 days")), Model_Task::STATUS_PENDING))
->execute();
$check = Model_Task::check_period(1, 1, 1);
$this->assertEquals(TRUE, $check);
}
function test_send_course()
{
$status = DB::select('status')