Merge branch 'features/feature-talk-notifications-on-agenda' into staging
This commit is contained in:
commit
c1a69f0d73
@ -142,5 +142,8 @@ return [
|
||||
|
||||
//user groups getUserConnectedGroups
|
||||
['name' => 'page#getUserConnectedGroups', 'url' => '/getUserConnectedGroups', 'verb' => 'GET'],
|
||||
|
||||
//testTalk
|
||||
['name' => 'page#testTalk', 'url' => '/testTalk', 'verb' => 'POST'],
|
||||
]
|
||||
];
|
||||
|
||||
@ -5,6 +5,7 @@ namespace OCA\Gestion\Constants;
|
||||
abstract class BddConstant
|
||||
{
|
||||
const DEFAULT_CALENDAR_UUID_FOR_DEVIS = "not-related";
|
||||
const CALENDAR_TABLE_PREFIX = "*PREFIX*";
|
||||
const DEFAULT_TABLE_PREFIX = "*PREFIX*";
|
||||
const DEFAULT_CLIENT_GROUP_NAME = "Nom du groupe";
|
||||
const DEFAULT_ADMIN_ID_NEXTCLOUD = 'admin';
|
||||
}
|
||||
@ -24,6 +24,7 @@ use OCA\Gestion\Service\Certificate\CertificateService;
|
||||
use OCA\Gestion\Service\ExportClientStatisticService;
|
||||
use OCA\Gestion\Service\ExportThanatoStatisticService;
|
||||
use OCA\Gestion\Service\InvoicePdfService;
|
||||
use OCA\Gestion\Service\TalkService;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
date_default_timezone_set('Europe/Paris');
|
||||
@ -64,6 +65,9 @@ class PageController extends Controller {
|
||||
|
||||
private $adminStorage;
|
||||
|
||||
/** @var TalkService */
|
||||
private $talkService;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
@ -80,7 +84,8 @@ class PageController extends Controller {
|
||||
ExportThanatoStatisticService $exportThanatoStatisticService,
|
||||
ExportClientStatisticService $exportClientStatisticService,
|
||||
InvoicePdfService $invoicePdfService,
|
||||
CertificateService $certificateService) {
|
||||
CertificateService $certificateService,
|
||||
TalkService $talkService) {
|
||||
|
||||
parent::__construct($AppName, $request);
|
||||
|
||||
@ -94,6 +99,7 @@ class PageController extends Controller {
|
||||
$this->invoicePdfService = $invoicePdfService;
|
||||
$this->certificateService = $certificateService;
|
||||
$this->defaultImagePath = $this->sharedImagePath.self::DEFAULT_NEXTCLOUD_ADMIN.'/files/.gestion/';
|
||||
$this->talkService = $talkService;
|
||||
//$this->fpdf = $fpdf;
|
||||
|
||||
if ($userSession->isLoggedIn()) {
|
||||
@ -2938,10 +2944,26 @@ class PageController extends Controller {
|
||||
* @param int $defuntId
|
||||
*/
|
||||
|
||||
public function getUserConnectedGroups(){
|
||||
public function getUserConnectedGroups(){
|
||||
try{
|
||||
return json_encode($this->groups);
|
||||
}
|
||||
catch(\OCP\Files\NotFoundException $e) { }
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
* @NoCSRFRequired
|
||||
*/
|
||||
|
||||
public function testTalk(){
|
||||
try{
|
||||
$message = $this->myDb->getDevisTalkRoomMessage(1,$this->idNextcloud);
|
||||
// $message ="FROM API";
|
||||
$this->talkService->sendDevisTalkNotifications($message,$this->idNextcloud);
|
||||
return true;
|
||||
}
|
||||
catch(\OCP\Files\NotFoundException $e) { }
|
||||
}
|
||||
}
|
||||
|
||||
@ -2888,6 +2888,40 @@ class Bdd {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private function getDevisByIdWithCalendarData($devisId,$idNextcloud){
|
||||
$devis = $this->getOneDevis($devisId,$idNextcloud);
|
||||
$devis= json_decode($devis);
|
||||
if(empty($devis)){
|
||||
return null;
|
||||
}
|
||||
$devis = $devis[0];
|
||||
if(isset($devis->numm) && $devis->num == null){
|
||||
return null;
|
||||
}
|
||||
$calendarData = $this->getCalendarDataByCalendarObjectUuid($devis->num);
|
||||
$devisTimeValue = VCalendarHelpers::GetStartAndEndTimeFromVCalendarString($calendarData);
|
||||
$devis->startTime = $devisTimeValue["startTime"];
|
||||
$devis->endTime = $devisTimeValue["endTime"];
|
||||
return $devis;
|
||||
}
|
||||
|
||||
|
||||
public function getDevisTalkRoomMessage($devisId,$idNextcloud){
|
||||
$devis = $this->getDevisByIdWithCalendarData($devisId,$idNextcloud);
|
||||
if($devis == null){
|
||||
return null;
|
||||
}
|
||||
$devisDate = new Datetime($devis->date);
|
||||
$devisDate = $devisDate->format('d/m/Y');
|
||||
|
||||
$message = "NOUVEAUX SOINS : ";
|
||||
$message .= $devis->nom_defunt . ' ';
|
||||
$message .= 'le '.$devisDate. ' ';
|
||||
$message .= 'à '.$devis->startTime. ' ';
|
||||
$message .= 'à '.$devis->lieu. ' '. $devis->adresse_soin;
|
||||
return $message;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
230
gestion/lib/Db/TalkDb.php
Normal file
230
gestion/lib/Db/TalkDb.php
Normal file
@ -0,0 +1,230 @@
|
||||
<?php
|
||||
namespace OCA\Gestion\Db;
|
||||
|
||||
use OCP\IDBConnection;
|
||||
use \Datetime;
|
||||
use OCA\Gestion\Constants\BddConstant;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
class TalkDb {
|
||||
private $talkTablePrefix;
|
||||
|
||||
private const DEVIS_TALK_ROOM_TYPE = 1;
|
||||
private const DEVIS_TALK_ROOM_ACTOR_TYPE = "users";
|
||||
private const DEVIS_TALK_ROOM_VERB_COMMENT = "comment";
|
||||
private const DEVIS_TALK_ROOM_VERB_INITIAL_MESSAGE = "system";
|
||||
private const DEVIS_TALK_ROOM_INITIAL_MESSAGE = '{"message":"conversation_created","parameters":[]}';
|
||||
private const DEVIS_TALK_ROOM_DEFAULT_MESSAGE_TYPE = "chat";
|
||||
private const DEVIS_TALK_ROOM_DEFAULT_PARTICIPANT_TYPE = 1;
|
||||
private $commentsTablePrefix;
|
||||
private IDbConnection $pdo;
|
||||
|
||||
public function __construct(IDbConnection $db) {
|
||||
$this->talkTablePrefix = BddConstant::DEFAULT_TABLE_PREFIX ."talk_";
|
||||
$this->commentsTablePrefix = BddConstant::DEFAULT_TABLE_PREFIX;
|
||||
$this->pdo = $db;
|
||||
}
|
||||
|
||||
private function execSQL($sql, $conditions){
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->execute($conditions);
|
||||
$data = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
$stmt->closeCursor();
|
||||
return json_encode($data);
|
||||
}
|
||||
|
||||
private function execSQLNoData($sql, $conditions){
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->execute($conditions);
|
||||
$stmt->closeCursor();
|
||||
}
|
||||
|
||||
private function execSQLNoJsonReturn($sql, $conditions){
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->execute($conditions);
|
||||
$data = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
$stmt->closeCursor();
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function getDevisTalkRoomByName($name){
|
||||
$sql = "SELECT *
|
||||
FROM ".$this->talkTablePrefix."rooms as room
|
||||
WHERE room.name = ? ORDER BY room.id DESC LIMIT 1;";
|
||||
|
||||
$rooms = $this->execSQLNoJsonReturn(
|
||||
$sql,
|
||||
[$name]);
|
||||
|
||||
if(!empty($rooms)){
|
||||
return $rooms[0];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private function createDevisTalkRoom($name,$token){
|
||||
$dateTime = new Datetime();
|
||||
$dateTime = $dateTime->format(('Y-m-d H:i:s'));
|
||||
$sql = "INSERT INTO `".$this->talkTablePrefix."rooms` (
|
||||
`name`,
|
||||
`token`,
|
||||
`type`,
|
||||
`last_activity`) VALUES (?,?,?,?);";
|
||||
$this->execSQLNoData($sql, array(
|
||||
$name,
|
||||
$token,
|
||||
self::DEVIS_TALK_ROOM_TYPE,
|
||||
$dateTime
|
||||
));
|
||||
return true;
|
||||
}
|
||||
|
||||
private function createDevisTalkRoomAttendeesByActors($actors,$roomId){
|
||||
foreach($actors as $actor){
|
||||
$this->createDevisTalkRoomAttendees($actor,$roomId);
|
||||
}
|
||||
}
|
||||
|
||||
private function createDevisTalkRoomAttendees($idNextCloud,$roomId){
|
||||
$sql = "INSERT INTO `".$this->talkTablePrefix."attendees` (
|
||||
`room_id`,
|
||||
`actor_type`,
|
||||
`actor_id`,
|
||||
`display_name`,
|
||||
`participant_type`) VALUES (?,?,?,?,?);";
|
||||
$this->execSQLNoData($sql, array(
|
||||
$roomId,
|
||||
self::DEVIS_TALK_ROOM_ACTOR_TYPE,
|
||||
$idNextCloud,
|
||||
$idNextCloud,
|
||||
self::DEVIS_TALK_ROOM_DEFAULT_PARTICIPANT_TYPE
|
||||
));
|
||||
}
|
||||
|
||||
private function createDevisTalkRoomMessage($actorId,$message,$verb,$roomId,$reference = null){
|
||||
$datetime = new DateTime();
|
||||
$datetime = $datetime->format(('Y-m-d H:i:s'));
|
||||
$sql = "INSERT INTO `".$this->commentsTablePrefix."comments` (
|
||||
`actor_type`,
|
||||
`actor_id`,
|
||||
`message`,
|
||||
`verb`,
|
||||
`creation_timestamp`,
|
||||
`object_type`,
|
||||
`object_id`,
|
||||
`reference_id`
|
||||
) VALUES (?,?,?,?,?,?,?,?);";
|
||||
$this->execSQLNoData($sql, array(
|
||||
self::DEVIS_TALK_ROOM_ACTOR_TYPE,
|
||||
$actorId,
|
||||
$message,
|
||||
$verb,
|
||||
$datetime,
|
||||
self::DEVIS_TALK_ROOM_DEFAULT_MESSAGE_TYPE,
|
||||
$roomId,
|
||||
$reference
|
||||
));
|
||||
}
|
||||
|
||||
private function getDevisTalkRoomMessageByRoomAndMessage($roomId,$message){
|
||||
$sql = "SELECT *
|
||||
FROM ".$this->commentsTablePrefix."comments as comment
|
||||
WHERE comment.object_id = ? AND
|
||||
comment.message = ?
|
||||
ORDER BY comment.id DESC
|
||||
LIMIT 1;";
|
||||
|
||||
$messages = $this->execSQLNoJsonReturn(
|
||||
$sql,
|
||||
[$roomId,$message]);
|
||||
|
||||
if(!empty($messages)){
|
||||
return $messages[0];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getDevisTalkRoomByNames($names){
|
||||
$sql = "SELECT *
|
||||
FROM ".$this->talkTablePrefix."rooms as room
|
||||
WHERE room.name = ? OR room.name = ? LIMIT 1;";
|
||||
|
||||
$rooms = $this->execSQLNoJsonReturn(
|
||||
$sql,
|
||||
[$names['createdByUser'],$names['createdByAdmin']]);
|
||||
|
||||
if(!empty($rooms)){
|
||||
return $rooms[0];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function createDevisTalkRoomAndReturnDevisTalkRoom($idNextCloud,$token){
|
||||
$roomName = '["'.$idNextCloud.'","'.BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD.'"]';
|
||||
$this->createDevisTalkRoom($roomName,$token);
|
||||
$room = $this->getDevisTalkRoomByName($roomName);
|
||||
$attendees = [
|
||||
$idNextCloud,
|
||||
BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD
|
||||
];
|
||||
$this->createDevisTalkRoomAttendeesByActors($attendees,$room['id']);
|
||||
return $room;
|
||||
}
|
||||
public function setDevisTalkRoomInitialMessageAndReturnMessage($roomId,$idNextCloud){
|
||||
$this->createDevisTalkRoomMessage(
|
||||
$idNextCloud,
|
||||
self::DEVIS_TALK_ROOM_INITIAL_MESSAGE,
|
||||
self::DEVIS_TALK_ROOM_VERB_INITIAL_MESSAGE,
|
||||
$roomId
|
||||
);
|
||||
|
||||
$message = $this->getDevisTalkRoomMessageByRoomAndMessage($roomId,self::DEVIS_TALK_ROOM_INITIAL_MESSAGE);
|
||||
return $message;
|
||||
}
|
||||
|
||||
public function setAttendeeLastReadMessage($roomId,$messageId,$idNextCloud){
|
||||
$sql = "UPDATE ".$this->talkTablePrefix."attendees as attendee
|
||||
SET
|
||||
attendee.last_read_message = ?
|
||||
WHERE attendee.room_id = ? AND
|
||||
attendee.actor_id = ?;";
|
||||
$this->execSQLNoData(
|
||||
$sql,
|
||||
[$messageId,$roomId,$idNextCloud]);
|
||||
}
|
||||
|
||||
public function createDevisTalkRoomMessageAndReturnMessage($roomId,$message){
|
||||
$this->createDevisTalkRoomMessage(
|
||||
BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD,
|
||||
$message,
|
||||
self::DEVIS_TALK_ROOM_VERB_COMMENT,
|
||||
$roomId,
|
||||
Uuid::uuid4()->toString()
|
||||
);
|
||||
$message = $this->getDevisTalkRoomMessageByRoomAndMessage($roomId,$message);
|
||||
return $message;
|
||||
}
|
||||
|
||||
public function updateDevisTalkRoomLastActivity($roomId,$datetime){
|
||||
$datetime = $datetime->format(('Y-m-d H:i:s'));
|
||||
$sql = "UPDATE ".$this->talkTablePrefix."rooms as room
|
||||
SET
|
||||
room.last_activity = ?
|
||||
WHERE room.id = ?;";
|
||||
$this->execSQLNoData(
|
||||
$sql,
|
||||
[$datetime,$roomId]);
|
||||
}
|
||||
|
||||
public function updateRoomLastMessage($roomId,$lastMessageId){
|
||||
$sql = "UPDATE ".$this->talkTablePrefix."rooms as room
|
||||
SET
|
||||
room.last_message = ?
|
||||
WHERE room.id = ?;";
|
||||
$this->execSQLNoData(
|
||||
$sql,
|
||||
[$lastMessageId,$roomId]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -37,11 +37,16 @@ class GestionService {
|
||||
/** @var LoggerInterface */
|
||||
private $logger;
|
||||
|
||||
/** @var TalkService */
|
||||
private $talkService;
|
||||
|
||||
public function __construct(
|
||||
Bdd $gestionBdd,
|
||||
LoggerInterface $logger) {
|
||||
LoggerInterface $logger,
|
||||
TalkService $talkService) {
|
||||
$this->logger = $logger;
|
||||
$this->gestionBdd = $gestionBdd;
|
||||
$this->talkService = $talkService;
|
||||
}
|
||||
|
||||
private function GetCalendarSummaryFromVCalendarString(string $vCalendarString): string
|
||||
@ -134,6 +139,8 @@ class GestionService {
|
||||
$this->gestionBdd->insertDevisArticleFromDevisIdAndArticlesIdArray($devisId, $articleIds);
|
||||
}
|
||||
$this->gestionBdd->createDevisTrajetFromVCalendar($devisId,$userName);
|
||||
$devisTalkMessage = $this->gestionBdd->getDevisTalkRoomMessage($devisId,$userName);
|
||||
$this->talkService->sendDevisTalkNotifications($devisTalkMessage,$userName);
|
||||
}
|
||||
|
||||
private function GetThanatoNameFromVCalendarString($vCalendarString){
|
||||
|
||||
87
gestion/lib/Service/TalkService.php
Normal file
87
gestion/lib/Service/TalkService.php
Normal file
@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* Calendar App
|
||||
*
|
||||
* @copyright 2021 Anna Larch <anna.larch@gmx.net>
|
||||
*
|
||||
* @author Anna Larch <anna.larch@gmx.net>
|
||||
* @author Richard Steinmetz <richard@steinmetz.cloud>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 3 of the License, or any later version.
|
||||
*
|
||||
* This library 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. See the
|
||||
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public
|
||||
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Gestion\Service;
|
||||
|
||||
use DateTime;
|
||||
use OCA\Gestion\Constants\BddConstant;
|
||||
use OCA\Gestion\Db\Bdd;
|
||||
use OCA\Gestion\Db\TalkDb;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
class TalkService {
|
||||
|
||||
/** @var TalkDb */
|
||||
private $talkDb;
|
||||
|
||||
/** @var LoggerInterface */
|
||||
private $logger;
|
||||
|
||||
public function __construct(
|
||||
TalkDb $talkDb,
|
||||
LoggerInterface $logger) {
|
||||
$this->logger = $logger;
|
||||
$this->talkDb = $talkDb;
|
||||
}
|
||||
|
||||
private function getUserDevisTalkRoomNames($idNextCloud){
|
||||
$roomNamesCreatedByUser = '["'.$idNextCloud.'","'.BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD.'"]';
|
||||
$roomNamesCreatedByAdmin = '["'.BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD.'","'.$idNextCloud.'"]';
|
||||
return [
|
||||
"createdByUser" => $roomNamesCreatedByUser,
|
||||
"createdByAdmin" => $roomNamesCreatedByAdmin
|
||||
];
|
||||
}
|
||||
|
||||
private function generateTalkRandomToken(){
|
||||
$length = 8;
|
||||
$characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
||||
$charactersLength = strlen($characters);
|
||||
$randomToken = '';
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$randomToken .= $characters[rand(0, $charactersLength - 1)];
|
||||
}
|
||||
return $randomToken;
|
||||
}
|
||||
|
||||
public function sendDevisTalkNotifications(string $message,string $idNextcloud){
|
||||
if($idNextcloud === BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD){
|
||||
return true;
|
||||
}
|
||||
$roomNames = $this->getUserDevisTalkRoomNames($idNextcloud);
|
||||
$room = $this->talkDb->getDevisTalkRoomByNames($roomNames);
|
||||
if($room == null){
|
||||
$room = $this->talkDb->createDevisTalkRoomAndReturnDevisTalkRoom($idNextcloud,$this->generateTalkRandomToken());
|
||||
$initialMessage = $this->talkDb->setDevisTalkRoomInitialMessageAndReturnMessage($room['id'],$idNextcloud);
|
||||
}
|
||||
$devisMessage = $this->talkDb->createDevisTalkRoomMessageAndReturnMessage($room['id'],$message);
|
||||
$this->talkDb->updateRoomLastMessage($room['id'],$devisMessage['id']);
|
||||
$this->talkDb->setAttendeeLastReadMessage($room['id'],$devisMessage['id'],BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user