mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-04-30 21:36:27 +02:00
add support for phone status during sms sending
This commit is contained in:
parent
38d350dfc2
commit
22e5149193
2 changed files with 68 additions and 52 deletions
|
@ -625,8 +625,9 @@ use Monolog\Logger;
|
||||||
$phones_subset = $users_mms_phones[$id_user] ?: $phones_subset;
|
$phones_subset = $users_mms_phones[$id_user] ?: $phones_subset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Keep only phones with remaining volume and available status
|
||||||
$remaining_volume_phones = array_filter($phones_subset, function ($phone) {
|
$remaining_volume_phones = array_filter($phones_subset, function ($phone) {
|
||||||
return $phone['remaining_volume'] > 0;
|
return $phone['remaining_volume'] > 0 && $phone['status'] == \models\Phone::STATUS_AVAILABLE;
|
||||||
});
|
});
|
||||||
$phones_subset = $remaining_volume_phones ?: $phones_subset;
|
$phones_subset = $remaining_volume_phones ?: $phones_subset;
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
|
|
||||||
namespace controllers\internals;
|
namespace controllers\internals;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
class Sended extends StandardController
|
class Sended extends StandardController
|
||||||
{
|
{
|
||||||
protected $model;
|
protected $model;
|
||||||
|
@ -275,71 +277,84 @@ namespace controllers\internals;
|
||||||
$text .= "\n" . join(' - ', $media_urls);
|
$text .= "\n" . join(' - ', $media_urls);
|
||||||
}
|
}
|
||||||
|
|
||||||
//If we reached our max quota, do not send the message
|
try
|
||||||
$internal_quota = new Quota($this->bdd);
|
|
||||||
$nb_credits = $internal_quota::compute_credits_for_message($text); //Calculate how much credit the message require
|
|
||||||
if (!$internal_quota->has_enough_credit($id_user, $nb_credits))
|
|
||||||
{
|
{
|
||||||
$return['error'] = true;
|
//If we reached our max quota, do not send the message
|
||||||
$return['error_message'] = 'Not enough credit to send message.';
|
$internal_quota = new Quota($this->bdd);
|
||||||
}
|
$nb_credits = $internal_quota::compute_credits_for_message($text); //Calculate how much credit the message require
|
||||||
|
if (!$internal_quota->has_enough_credit($id_user, $nb_credits))
|
||||||
|
{
|
||||||
|
throw new Exception('Not enough credit to send message.');
|
||||||
|
}
|
||||||
|
|
||||||
//If we reached limit for this phone, do not send the message
|
// If this phone status indicate it is not available
|
||||||
$internal_phone = new Phone($this->bdd);
|
$internal_phone = new Phone($this->bdd);
|
||||||
$internal_sended = new Sended($this->bdd);
|
$phone = $internal_phone->get_for_user($id_user, $id_phone);
|
||||||
$limits = $internal_phone->get_limits($id_phone);
|
if (!$phone || $phone['status'] != \models\Phone::STATUS_AVAILABLE)
|
||||||
|
{
|
||||||
|
throw new Exception('Invalid phone status : ' . $phone['status']);
|
||||||
|
}
|
||||||
|
|
||||||
$remaining_volume = PHP_INT_MAX;
|
//If we reached limit for this phone, do not send the message
|
||||||
foreach ($limits as $limit)
|
$limits = $internal_phone->get_limits($id_phone);
|
||||||
{
|
|
||||||
$startpoint = new \DateTime($limit['startpoint']);
|
|
||||||
$consumed = $internal_sended->count_since_for_phone_and_user($id_user, $id_phone, $startpoint);
|
|
||||||
$remaining_volume = min(($limit['volume'] - $consumed), $remaining_volume);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($remaining_volume < 1)
|
$remaining_volume = PHP_INT_MAX;
|
||||||
{
|
foreach ($limits as $limit)
|
||||||
$return['error'] = true;
|
{
|
||||||
$return['error_message'] = 'Phone send limit have been reached.';
|
$startpoint = new \DateTime($limit['startpoint']);
|
||||||
}
|
$consumed = $this->count_since_for_phone_and_user($id_user, $id_phone, $startpoint);
|
||||||
|
$remaining_volume = min(($limit['volume'] - $consumed), $remaining_volume);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($remaining_volume < 1)
|
||||||
|
{
|
||||||
|
throw new Exception('Phone send limit have been reached.');
|
||||||
|
}
|
||||||
|
|
||||||
$uid = uniqid();
|
|
||||||
if (!$return['error'])
|
|
||||||
{
|
|
||||||
$response = $adapter->send($destination, $text, $flash, $mms, $media_uris);
|
$response = $adapter->send($destination, $text, $flash, $mms, $media_uris);
|
||||||
$uid = $response['uid'] ?? $uid;
|
|
||||||
|
|
||||||
if ($response['error'])
|
if ($response['error'])
|
||||||
{
|
{
|
||||||
$return['error'] = true;
|
throw new Exception($response['error_message']);
|
||||||
$return['error_message'] = $response['error_message'];
|
|
||||||
}
|
|
||||||
else // If send with success, consume credit
|
|
||||||
{
|
|
||||||
$internal_quota->consume_credit($id_user, $nb_credits);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$uid = $response['uid'];
|
||||||
|
$status = \models\Sended::STATUS_UNKNOWN;
|
||||||
|
|
||||||
|
// If send with success, consume credit
|
||||||
|
$internal_quota->consume_credit($id_user, $nb_credits);
|
||||||
}
|
}
|
||||||
|
catch (Exception $e)
|
||||||
|
{
|
||||||
|
$return['error'] = true;
|
||||||
|
$return['error_message'] = $e->getMessage();
|
||||||
|
|
||||||
// If we fail to send or not, we will always save message as sended, only the status will change.
|
$uid = uniqid();
|
||||||
$status = $return['error'] ? \models\Sended::STATUS_FAILED : \models\Sended::STATUS_UNKNOWN;
|
$status = \models\Sended::STATUS_FAILED;
|
||||||
$sended_id = $this->create($id_user, $id_phone, $at, $text, $destination, $uid, $adapter->meta_classname(), $flash, $mms, $medias, $originating_scheduled, $status);
|
|
||||||
|
|
||||||
$webhook_body = [
|
return $return;
|
||||||
'id' => $sended_id,
|
}
|
||||||
'at' => $at,
|
finally
|
||||||
'status' => $status,
|
{
|
||||||
'text' => $text,
|
$sended_id = $this->create($id_user, $id_phone, $at, $text, $destination, $uid, $adapter->meta_classname(), $flash, $mms, $medias, $originating_scheduled, $status);
|
||||||
'destination' => $destination,
|
|
||||||
'origin' => $id_phone,
|
|
||||||
'mms' => $mms,
|
|
||||||
'medias' => $medias,
|
|
||||||
'originating_scheduled' => $originating_scheduled,
|
|
||||||
];
|
|
||||||
|
|
||||||
$internal_webhook = new Webhook($this->bdd);
|
$webhook_body = [
|
||||||
$internal_webhook->trigger($id_user, \models\Webhook::TYPE_SEND_SMS, $webhook_body);
|
'id' => $sended_id,
|
||||||
|
'at' => $at,
|
||||||
|
'status' => $status,
|
||||||
|
'text' => $text,
|
||||||
|
'destination' => $destination,
|
||||||
|
'origin' => $id_phone,
|
||||||
|
'mms' => $mms,
|
||||||
|
'medias' => $medias,
|
||||||
|
'originating_scheduled' => $originating_scheduled,
|
||||||
|
];
|
||||||
|
|
||||||
return $return;
|
$internal_webhook = new Webhook($this->bdd);
|
||||||
|
$internal_webhook->trigger($id_user, \models\Webhook::TYPE_SEND_SMS, $webhook_body);
|
||||||
|
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue