1
0
Fork 0
mirror of https://github.com/Oreolek/kangana.git synced 2024-05-10 13:08:19 +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. * Get last sent or prepared letter and check if it's time to send another one.
* @return bool * @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'); ->from('tasks');
if (is_array($letters)) if (is_array($letters))
{ {
@ -151,15 +155,21 @@ class Model_Task extends ORM {
{ {
$query = $query->where('letter_id', '=', $letters); $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 $check = $query
->and_where('status', '=', self::STATUS_SENT) ->and_where(DB::expr('DATEDIFF(CURDATE(), `date`)'), '>=', $period)
->or_where('status', '=', self::STATUS_PENDING)
->and_where('client_id', '=', $client_id)
->and_where(DB::expr('DATEDIFF(CURDATE(), `date`)'), '=', $period)
->execute() ->execute()
->get('date'); ->get('cnt');
return is_null($check); return ( $check !== '0');
} }
/** /**

View file

@ -48,7 +48,7 @@ class Task_Prepare extends Minion_Task
Model_Task::prepare($client_id, $letter); Model_Task::prepare($client_id, $letter);
} }
} else { } 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); $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() function test_send_course()
{ {
$status = DB::select('status') $status = DB::select('status')