Skip to content

Commit

Permalink
feat(Queue) time range and weekend delivery
Browse files Browse the repository at this point in the history
  • Loading branch information
joebordes committed Mar 20, 2023
2 parents c3081fa + ec8f3ce commit 0624db6
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 24 deletions.
35 changes: 35 additions & 0 deletions build/changeSets/2022/addNewFields2SMSqueueTable.php
@@ -0,0 +1,35 @@
<?php
/*************************************************************************************************
* Copyright 2022 Spike, JPL TSolucio, S.L. -- This file is a part of TSOLUCIO coreBOS Customizations.
* Licensed under the vtiger CRM Public License Version 1.1 (the "License"); you may not use this
* file except in compliance with the License. You can redistribute it and/or modify it
* under the terms of the License. JPL TSolucio, S.L. reserves all rights not expressly
* granted by the License. coreBOS distributed by JPL TSolucio S.L. is distributed in
* the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Unless required by
* applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT ANY WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing
* permissions and limitations under the License. You may obtain a copy of the License
* at <http://corebos.org/documentation/doku.php?id=en:devel:vpl11>
*************************************************************************************************/

class addNewFields2SMSqueueTable extends cbupdaterWorker {

public function applyChange() {
if ($this->hasError()) {
$this->sendError();
}
if ($this->isApplied()) {
$this->sendMsg('Changeset ' . get_class($this) . ' already applied!');
} else {
$this->ExecuteQuery('ALTER TABLE `cb_messagequeue` ADD `deliverstarttime` VARCHAR(10) NULL DEFAULT NULL;');
$this->ExecuteQuery('ALTER TABLE `cb_messagequeue` ADD `deliverendtime` VARCHAR(10) NULL DEFAULT NULL;');
$this->ExecuteQuery('ALTER TABLE `cb_messagequeue` ADD `cansendonsaturday` tinyint NOT NULL DEFAULT 1;');
$this->ExecuteQuery('ALTER TABLE `cb_messagequeue` ADD `cansendonsunday` tinyint NOT NULL DEFAULT 1;');
$this->sendMsg('Changeset ' . get_class($this) . ' applied!');
$this->markApplied(false);
}
$this->finishExecution();
}
}
65 changes: 41 additions & 24 deletions include/cbmqtm/cbmqtm_dbdistributor.php
Expand Up @@ -20,7 +20,7 @@

class cbmqtm_dbdistributor extends cbmqtm_manager {
protected static $db = null;
protected $version = '1.0';
protected $version = '2.0';

public static function getInstance() {
self::setDB();
Expand All @@ -32,30 +32,38 @@ private static function setDB() {
static::$db = new PearDatabase();
}

public function sendMessage($channel, $producer, $consumer, $type, $share, $sequence, $expires, $deliverafter, $userid, $information) {
public function sendMessage($channel, $producer, $consumer, $type, $share, $sequence, $expires, $deliverafter, $userid, $information, $deliverRange = array()) {
if ($share != '1:M' && $share != 'P:S') {
$share = '1:M';
}
if ($share == '1:M' || !$this->subscriptionExist($channel, $producer, $consumer)) {
$this->insertMsg($channel, $producer, $consumer, $type, $share, $sequence, $expires, $deliverafter, $userid, $information);
$this->insertMsg($channel, $producer, $consumer, $type, $share, $sequence, $expires, $deliverafter, $userid, $information, $deliverRange);
} else {
self::setDB();
$subrs = static::$db->pquery('select * from cb_mqsubscriptions where channel=?', array($channel));
while ($subscriber = static::$db->fetch_array($subrs)) {
$this->insertMsg($channel, $producer, $subscriber['consumer'], $type, $share, $sequence, $expires, $deliverafter, $userid, $information);
$this->insertMsg($channel, $producer, $subscriber['consumer'], $type, $share, $sequence, $expires, $deliverafter, $userid, $information, $deliverRange);
}
}
}

private function insertMsg($channel, $producer, $consumer, $type, $share, $sequence, $expires, $deliverafter, $userid, $information) {
private function insertMsg($channel, $producer, $consumer, $type, $share, $sequence, $expires, $deliverafter, $userid, $information, $deliverRange) {
$rightnow = time();
if (empty($deliverafter)) {
$deliverafter = 0;
}
if (empty($deliverRange)) {
$deliverRange = array('deliverStartTime'=>'00:00:00', 'deliverEndTime'=>'23:59:59', 'canSendOnSaturday'=>1, 'canSendOnSunday'=>1);
}
$deliverStartTime = (isset($deliverRange['deliverStartTime'])) ? $deliverRange['deliverStartTime'] : '00:00:00';
$deliverEndTime = (isset($deliverRange['deliverEndTime'])) ? $deliverRange['deliverEndTime'] : '23:59:59';
$canSendOnSaturday = (isset($deliverRange['canSendOnSaturday'])) ? $deliverRange['canSendOnSaturday'] : 1;
$canSendOnSunday = (isset($deliverRange['canSendOnSunday'])) ? $deliverRange['canSendOnSunday'] : 1;
self::setDB();
static::$db->pquery('insert into cb_messagequeue
(channel, producer, consumer, type, share, sequence, senton, deliverafter, expires, version, invalid, invalidreason, userid, information)
values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)', array(
static::$db->pquery(
'insert into cb_messagequeue (channel, producer, consumer, type, share, sequence, senton, deliverafter, expires, version, invalid, invalidreason, userid,
information, deliverstarttime, deliverendtime, cansendonsaturday, cansendonsunday) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
array(
'channel' => $channel,
'producer' => $producer,
'consumer' => $consumer,
Expand All @@ -69,14 +77,20 @@ private function insertMsg($channel, $producer, $consumer, $type, $share, $seque
'invalid' => 0,
'invalidreason' => '',
'userid' => $userid,
'information' => $information
));
'information' => $information,
'deliverstarttime' => $deliverStartTime,
'deliverendtime' => $deliverEndTime,
'cansendonsaturday' => $canSendOnSaturday,
'cansendonsunday' => $canSendOnSunday,
)
);
}

public function getMessage($channel, $consumer, $producer = '*', $userid = '*') {
self::setDB();
$sql = 'select * from cb_messagequeue where deliverafter<=? and channel=? and consumer=?';
$params = array(date('Y-m-d H:i:s', time()), $channel, $consumer);
private function getQuery($fields, $channel, $consumer, $producer = '*', $userid = '*') {
$nowtime = date('H:i:s', time());
$params = array(date('Y-m-d H:i:s', time()), $channel, $consumer, $nowtime, $nowtime);
$sql = 'select '.$fields.' from cb_messagequeue where deliverafter<=? and channel=? and consumer=?
and ((deliverstarttime<? or deliverstarttime is null) and (deliverendtime>? or deliverendtime is null))';
if ($producer != '*') {
$sql .= ' and producer=?';
$params[] = $producer;
Expand All @@ -85,6 +99,18 @@ public function getMessage($channel, $consumer, $producer = '*', $userid = '*')
$sql .= ' and userid=?';
$params[] = $userid;
}
$check_day = date('l');
if ($check_day == 'Saturday') {
$sql .= ' and (cansendonsaturday=1 or cansendonsaturday is null)';
} elseif ($check_day == 'Sunday') {
$sql .= ' and (cansendonsunday=1 or cansendonsunday is null)';
}
return [$sql, $params];
}

public function getMessage($channel, $consumer, $producer = '*', $userid = '*') {
self::setDB();
list($sql, $params) = $this->getQuery('*', $channel, $consumer, $producer, $userid);
$sql .= ' order by deliverafter,sequence asc limit 1';
$msgrs = static::$db->pquery($sql, $params);
if ($msgrs && static::$db->num_rows($msgrs)==1) {
Expand Down Expand Up @@ -114,16 +140,7 @@ public function getMessage($channel, $consumer, $producer = '*', $userid = '*')

public function isMessageWaiting($channel, $consumer, $producer = '*', $userid = '*') {
self::setDB();
$sql = 'select count(*) from cb_messagequeue where deliverafter<=? and channel=? and consumer=?';
$params = array(date('Y-m-d H:i:s', time()), $channel, $consumer);
if ($producer != '*') {
$sql .= ' and producer=?';
$params[] = $producer;
}
if ($userid != '*') {
$sql .= ' and userid=?';
$params[] = $userid;
}
list($sql, $params) = $this->getQuery('count(*)', $channel, $consumer, $producer, $userid);
$msgrs = static::$db->pquery($sql, $params);
return ($msgrs && static::$db->query_result($msgrs, 0, 0) > 0);
}
Expand Down
7 changes: 7 additions & 0 deletions modules/cbupdater/cbupdates/2022.xml
Expand Up @@ -307,4 +307,11 @@
<classname>addFIeldWFscheduleMultipleTimeOption</classname>
<systemupdate>true</systemupdate>
</changeSet>
<changeSet>
<author>mohamed-shibeJR</author>
<description>Add new columns to table messagequeue for holding time range of sms</description>
<filename>build/changeSets/2022/addNewFields2SMSqueueTable.php</filename>
<classname>addNewFields2SMSqueueTable</classname>
<systemupdate>true</systemupdate>
</changeSet>
</updatesChangeLog>

0 comments on commit 0624db6

Please sign in to comment.