Add notion of priority to phones

This commit is contained in:
osaajani 2023-02-05 23:11:58 +01:00
parent 6353d5115b
commit 69619d0bef
7 changed files with 85 additions and 6 deletions

View File

@ -137,15 +137,17 @@ namespace controllers\internals;
* @param string $name : The name of the phone * @param string $name : The name of the phone
* @param string $adapter : The adapter to use the phone * @param string $adapter : The adapter to use the phone
* @param string json $adapter_data : A JSON string representing adapter's data (for example credentials for an api) * @param string json $adapter_data : A JSON string representing adapter's data (for example credentials for an api)
* @param int $priority : Priority with which to use phone to send SMS. Default 0.
* @param array $limits : An array of limits for this phone. Each limit must be an array with a key volume and a key startpoint * @param array $limits : An array of limits for this phone. Each limit must be an array with a key volume and a key startpoint
* *
* @return bool|int : false on error, new id on success * @return bool|int : false on error, new id on success
*/ */
public function create(int $id_user, string $name, string $adapter, string $adapter_data, array $limits = []) public function create(int $id_user, string $name, string $adapter, string $adapter_data, int $priority = 0, array $limits = [])
{ {
$phone = [ $phone = [
'id_user' => $id_user, 'id_user' => $id_user,
'name' => $name, 'name' => $name,
'priority' => $priority,
'adapter' => $adapter, 'adapter' => $adapter,
'adapter_data' => $adapter_data, 'adapter_data' => $adapter_data,
]; ];
@ -185,17 +187,19 @@ namespace controllers\internals;
* @param string $name : The name of the phone * @param string $name : The name of the phone
* @param string $adapter : The adapter to use the phone * @param string $adapter : The adapter to use the phone
* @param string json $adapter_data : A JSON string representing adapter's data (for example credentials for an api) * @param string json $adapter_data : A JSON string representing adapter's data (for example credentials for an api)
* @param int $priority : Priority with which to use phone to send SMS. Default 0.
* @param array $limits : An array of limits for this phone. Each limit must be an array with a key volume and a key startpoint * @param array $limits : An array of limits for this phone. Each limit must be an array with a key volume and a key startpoint
* *
* @return bool : false on error, true on success * @return bool : false on error, true on success
*/ */
public function update_for_user(int $id_user, int $id, string $name, string $adapter, string $adapter_data, array $limits = []): bool public function update_for_user(int $id_user, int $id, string $name, string $adapter, string $adapter_data, int $priority = 0, array $limits = []): bool
{ {
$phone = [ $phone = [
'id_user' => $id_user, 'id_user' => $id_user,
'name' => $name, 'name' => $name,
'adapter' => $adapter, 'adapter' => $adapter,
'adapter_data' => $adapter_data, 'adapter_data' => $adapter_data,
'priority' => $priority,
]; ];
//Use transaction to garanty atomicity //Use transaction to garanty atomicity

View File

@ -506,6 +506,7 @@ namespace controllers\publics;
* @param string $_POST['name'] : Phone name * @param string $_POST['name'] : Phone name
* @param string $_POST['adapter'] : Phone adapter * @param string $_POST['adapter'] : Phone adapter
* @param array $_POST['adapter_data'] : Phone adapter data * @param array $_POST['adapter_data'] : Phone adapter data
* @param int $priority : Priority with which to use phone to send SMS. Default 0.
* @param ?array $_POST['limits'] : Array of limits in number of SMS for a period to be applied to this phone. * @param ?array $_POST['limits'] : Array of limits in number of SMS for a period to be applied to this phone.
* *
* @return int : id phone the new phone on success * @return int : id phone the new phone on success
@ -517,6 +518,8 @@ namespace controllers\publics;
$name = $_POST['name'] ?? false; $name = $_POST['name'] ?? false;
$adapter = $_POST['adapter'] ?? false; $adapter = $_POST['adapter'] ?? false;
$adapter_data = !empty($_POST['adapter_data']) ? $_POST['adapter_data'] : []; $adapter_data = !empty($_POST['adapter_data']) ? $_POST['adapter_data'] : [];
$priority = $_POST['priority'] ?? 0;
$priority = max(((int) $priority), 0);
$limits = $_POST['limits'] ?? []; $limits = $_POST['limits'] ?? [];
$limits = is_array($limits) ? $limits : [$limits]; $limits = is_array($limits) ? $limits : [$limits];
@ -660,7 +663,7 @@ namespace controllers\publics;
return $this->json($return); return $this->json($return);
} }
$phone_id = $this->internal_phone->create($this->user['id'], $name, $adapter, $adapter_data, $limits); $phone_id = $this->internal_phone->create($this->user['id'], $name, $adapter, $adapter_data, $priority, $limits);
if (false === $phone_id) if (false === $phone_id)
{ {
$return['error'] = self::ERROR_CODES['CANNOT_CREATE']; $return['error'] = self::ERROR_CODES['CANNOT_CREATE'];
@ -683,6 +686,7 @@ namespace controllers\publics;
* @param string (optionnal) $_POST['name'] : New phone name * @param string (optionnal) $_POST['name'] : New phone name
* @param string (optionnal) $_POST['adapter'] : New phone adapter * @param string (optionnal) $_POST['adapter'] : New phone adapter
* @param array (optionnal) $_POST['adapter_data'] : New phone adapter data * @param array (optionnal) $_POST['adapter_data'] : New phone adapter data
* @param int $priority : Priority with which to use phone to send SMS. Default 0.
* *
* @return int : id phone the new phone on success * @return int : id phone the new phone on success
*/ */
@ -703,6 +707,8 @@ namespace controllers\publics;
$limits = $this->internal_phone->get_limits(($phone['id'])); $limits = $this->internal_phone->get_limits(($phone['id']));
$name = $_POST['name'] ?? $phone['name']; $name = $_POST['name'] ?? $phone['name'];
$priority = $_POST['priority'] ?? $phone['priority'];
$priority = max(((int) $priority), 0);
$adapter = $_POST['adapter'] ?? $phone['adapter']; $adapter = $_POST['adapter'] ?? $phone['adapter'];
$adapter_data = !empty($_POST['adapter_data']) ? $_POST['adapter_data'] : json_decode($phone['adapter_data']); $adapter_data = !empty($_POST['adapter_data']) ? $_POST['adapter_data'] : json_decode($phone['adapter_data']);
$adapter_data = is_array($adapter_data) ? $adapter_data : [$adapter_data]; $adapter_data = is_array($adapter_data) ? $adapter_data : [$adapter_data];
@ -842,7 +848,7 @@ namespace controllers\publics;
return $this->json($return); return $this->json($return);
} }
$success = $this->internal_phone->update_for_user($this->user['id'], $phone['id'], $name, $adapter, $adapter_data_json, $limits); $success = $this->internal_phone->update_for_user($this->user['id'], $phone['id'], $name, $adapter, $adapter_data_json, $priority, $limits);
if (!$success) if (!$success)
{ {
$return['error'] = self::ERROR_CODES['CANNOT_UPDATE']; $return['error'] = self::ERROR_CODES['CANNOT_UPDATE'];

View File

@ -138,6 +138,7 @@ class Phone extends \descartes\Controller
* @param string $_POST['adapter'] : Phone adapter * @param string $_POST['adapter'] : Phone adapter
* @param ?array $_POST['adapter_data'] : Phone adapter data * @param ?array $_POST['adapter_data'] : Phone adapter data
* @param ?array $_POST['limits'] : Array of limits in number of SMS for a period to be applied to this phone. * @param ?array $_POST['limits'] : Array of limits in number of SMS for a period to be applied to this phone.
* @param int $_POST['priority'] : Priority with which to use phone to send SMS. Default 0.
*/ */
public function create($csrf) public function create($csrf)
{ {
@ -150,6 +151,8 @@ class Phone extends \descartes\Controller
$id_user = $_SESSION['user']['id']; $id_user = $_SESSION['user']['id'];
$name = $_POST['name'] ?? false; $name = $_POST['name'] ?? false;
$priority = $_POST['priority'] ?? 0;
$priority = max(((int) $priority), 0);
$adapter = $_POST['adapter'] ?? false; $adapter = $_POST['adapter'] ?? false;
$adapter_data = !empty($_POST['adapter_data']) ? $_POST['adapter_data'] : []; $adapter_data = !empty($_POST['adapter_data']) ? $_POST['adapter_data'] : [];
$limits = $_POST['limits'] ?? []; $limits = $_POST['limits'] ?? [];
@ -281,7 +284,7 @@ class Phone extends \descartes\Controller
return $this->redirect(\descartes\Router::url('Phone', 'add')); return $this->redirect(\descartes\Router::url('Phone', 'add'));
} }
$success = $this->internal_phone->create($id_user, $name, $adapter, $adapter_data, $limits); $success = $this->internal_phone->create($id_user, $name, $adapter, $adapter_data, $priority, $limits);
if (!$success) if (!$success)
{ {
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer ce téléphone.'); \FlashMessage\FlashMessage::push('danger', 'Impossible de créer ce téléphone.');
@ -346,6 +349,7 @@ class Phone extends \descartes\Controller
* @param string $_POST['phones']['id']['adapter'] : Phone adapter * @param string $_POST['phones']['id']['adapter'] : Phone adapter
* @param ?array $_POST['phones']['id']['adapter_data'] : Phone adapter data * @param ?array $_POST['phones']['id']['adapter_data'] : Phone adapter data
* @param ?array $_POST['phones']['id']['limits'] : Array of limits in number of SMS for a period to be applied to this phone. * @param ?array $_POST['phones']['id']['limits'] : Array of limits in number of SMS for a period to be applied to this phone.
* @param int $_POST['phones']['id']['priority'] : Priority with which to use phone to send SMS. Default 0.
*/ */
public function update($csrf) public function update($csrf)
{ {
@ -367,6 +371,8 @@ class Phone extends \descartes\Controller
foreach ($_POST['phones'] as $id_phone => $phone) foreach ($_POST['phones'] as $id_phone => $phone)
{ {
$name = $phone['name'] ?? false; $name = $phone['name'] ?? false;
$priority = $phone['priority'] ?? 0;
$priority = max(((int) $priority), 0);
$adapter = $phone['adapter'] ?? false; $adapter = $phone['adapter'] ?? false;
$adapter_data = !empty($phone['adapter_data']) ? $phone['adapter_data'] : []; $adapter_data = !empty($phone['adapter_data']) ? $phone['adapter_data'] : [];
$limits = $phone['limits'] ?? []; $limits = $phone['limits'] ?? [];
@ -484,7 +490,7 @@ class Phone extends \descartes\Controller
continue; continue;
} }
$success = $this->internal_phone->update_for_user($id_user, $id_phone, $name, $adapter, $adapter_data, $limits); $success = $this->internal_phone->update_for_user($id_user, $id_phone, $name, $adapter, $adapter_data, $priority, $limits);
if (!$success) if (!$success)
{ {
continue; continue;

View File

@ -0,0 +1,43 @@
<?php
use Phinx\Migration\AbstractMigration;
class AddPhonePriority extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* addCustomColumn
* renameColumn
* addIndex
* addForeignKey
*
* Any other destructive changes will result in an error when trying to
* rollback the migration.
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('phone');
$table->addColumn('priority', 'integer', [
'null' => false,
'default' => 0,
'comment' => 'Priority with which the phone will be used. The higher the more prioritary.',
'after' => 'name'
])
->update();
}
}

View File

@ -47,6 +47,15 @@
<input required="required" name="name" class="form-control" placeholder="Nom du téléphone" value="<?php $this->s($_SESSION['previous_http_post']['name'] ?? '') ?>"> <input required="required" name="name" class="form-control" placeholder="Nom du téléphone" value="<?php $this->s($_SESSION['previous_http_post']['name'] ?? '') ?>">
</div> </div>
</div> </div>
<div class="form-group">
<label>Priorité d'utilisation du téléphone</label>
<p class="italic small help">
Lors de l'envoi de SMS sans téléphone spécifié, les téléphones avec la plus haute priorité seront utilisés en premier.
</p>
<div class="form-group">
<input required="required" name="priority" class="form-control" type="number" min="0" placeholder="Priorité d'utilisation" value="<?php $this->s($_SESSION['previous_http_post']['priority'] ?? 0) ?>">
</div>
</div>
<div class="form-group"> <div class="form-group">
<label>Type de téléphone</label> <label>Type de téléphone</label>
<p class="italic small help" id="description-adapter-general"> <p class="italic small help" id="description-adapter-general">

View File

@ -49,6 +49,15 @@
<input required="required" name="phones[<?php $this->s($phone['id']); ?>][name]" class="form-control" placeholder="Nom du téléphone" value="<?php $this->s($phone['name']); ?>"> <input required="required" name="phones[<?php $this->s($phone['id']); ?>][name]" class="form-control" placeholder="Nom du téléphone" value="<?php $this->s($phone['name']); ?>">
</div> </div>
</div> </div>
<div class="form-group">
<label>Priorité d'utilisation du téléphone</label>
<p class="italic small help">
Lors de l'envoi de SMS sans téléphone spécifié, les téléphones avec la plus haute priorité seront utilisés en premier.
</p>
<div class="form-group">
<input required="required" name="phones[<?php $this->s($phone['id']); ?>][priority]" class="form-control" type="number" min="0" placeholder="Priorité d'utilisation" value="<?php $this->s($phone['priority']) ?>">
</div>
</div>
<div class="form-group"> <div class="form-group">
<label>Type de téléphone</label> <label>Type de téléphone</label>
<p class="italic small help description-adapter-general"> <p class="italic small help description-adapter-general">

View File

@ -41,6 +41,7 @@
<tr> <tr>
<th>ID</th> <th>ID</th>
<th>Nom</th> <th>Nom</th>
<th>Priorité</th>
<th>Type de téléphone</th> <th>Type de téléphone</th>
<th>Callbacks</th> <th>Callbacks</th>
<th>Limites</th> <th>Limites</th>
@ -90,6 +91,7 @@ jQuery(document).ready(function ()
"columns" : [ "columns" : [
{data: 'id', render: jQuery.fn.dataTable.render.text()}, {data: 'id', render: jQuery.fn.dataTable.render.text()},
{data: 'name', render: jQuery.fn.dataTable.render.text()}, {data: 'name', render: jQuery.fn.dataTable.render.text()},
{data: 'priority', render: jQuery.fn.dataTable.render.text()},
{data: 'adapter', render: jQuery.fn.dataTable.render.text()}, {data: 'adapter', render: jQuery.fn.dataTable.render.text()},
{ {
data: '_', data: '_',