Add phone reliability features

This commit is contained in:
osaajani 2024-10-26 18:02:11 +02:00
parent 44b855dd48
commit 52c849e043
22 changed files with 837 additions and 188 deletions

View file

@ -1034,6 +1034,15 @@ namespace controllers\publics;
return $this->json($return);
}
if ($phone['status'] === \models\Phone::STATUS_DISABLED)
{
$return['error'] = self::ERROR_CODES['CANNOT_UPDATE'];
$return['message'] = self::ERROR_MESSAGES['CANNOT_UPDATE'] . 'Phone have been manually disabled, you need to re-enable it manually.';
$this->auto_http_code(false);
return $this->json($return);
}
// If user have activated phone limits, check if RaspiSMS phone limit have already been reached
$limit_reached = false;
if ((int) ($this->user['settings']['phone_limit'] ?? false))
@ -1073,6 +1082,42 @@ namespace controllers\publics;
return $this->json($return);
}
/**
* Manually disable/enable phones
* @param int id : id of phone we want to update status
* @param string $_POST['new_status'] : New status of the phone, either 'disabled' or 'available'
* @param $csrf : CSRF token
*/
public function post_change_phone_status ($id)
{
$new_status = $_POST['status'] ?? '';
if (!in_array($new_status, [\models\Phone::STATUS_AVAILABLE, \models\Phone::STATUS_DISABLED]))
{
$return['error'] = self::ERROR_CODES['INVALID_PARAMETER'];
$return['message'] = self::ERROR_MESSAGES['INVALID_PARAMETER'] . ' "status" must be "disabled" or "available".';
$this->auto_http_code(false);
return $this->json($return);
}
$phone = $this->internal_phone->get_for_user($this->user['id'], $id);
if (!$phone)
{
$return['error'] = self::ERROR_CODES['CANNOT_UPDATE'];
$return['message'] = self::ERROR_MESSAGES['CANNOT_UPDATE'];
$this->auto_http_code(false);
return $this->json($return);
}
$status_update = $this->internal_phone->update_status($id, $new_status);
$return['response'] = $new_status;
$this->auto_http_code(true);
return $this->json($return);
}
/**
* Return statistics about status of sended sms for a period by phone

View file

@ -535,6 +535,16 @@ class Phone extends \descartes\Controller
foreach ($ids as $id)
{
$phone = $this->internal_phone->get_for_user($id_user, $id);
if (!$phone)
{
continue;
}
if ($phone['status'] === \models\Phone::STATUS_DISABLED)
{
\FlashMessage\FlashMessage::push('error', 'Certains téléphones ont été désactivés manuellements, vous devez les réactiver manuellement.');
continue;
}
// If user have activated phone limits, check if RaspiSMS phone limit have already been reached
$limit_reached = false;
@ -581,6 +591,48 @@ class Phone extends \descartes\Controller
return $this->redirect(\descartes\Router::url('Phone', 'list'));
}
/**
* Manually disable/enable phones
* @param array int $_GET['ids'] : ids of phones we want to update status
* @param string $new_status : New status of the phone, either 'disabled' or 'available'
* @param $csrf : CSRF token
*/
public function change_status ($new_status, $csrf)
{
if (!$this->verify_csrf($csrf))
{
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('Phone', 'add'));
}
if (!in_array($new_status, [\models\Phone::STATUS_AVAILABLE, \models\Phone::STATUS_DISABLED]))
{
\FlashMessage\FlashMessage::push('danger', 'Seul les status disponibles et désactivés peuvent être définis manuellement.');
return $this->redirect(\descartes\Router::url('Phone', 'add'));
}
$ids = $_GET['ids'] ?? [];
$id_user = $_SESSION['user']['id'];
foreach ($ids as $id)
{
$phone = $this->internal_phone->get_for_user($id_user, $id);
if (!$phone)
{
continue;
}
$status_update = $this->internal_phone->update_status($id, $new_status);
}
\FlashMessage\FlashMessage::push('success', 'Les status des téléphones ont bien été mis à jour manuellement.');
return $this->redirect(\descartes\Router::url('Phone', 'list'));
}
/**
* Return a list of phones as a JSON array
*/