wip:mail-pjs

This commit is contained in:
Narindra ezway 2025-03-19 18:21:59 +03:00
parent 2e177f815d
commit 124a5ac324
7 changed files with 265 additions and 78 deletions

View File

@ -22,7 +22,6 @@ use OCP\AppFramework\Db\DoesNotExistException;
class EmailBoxController extends Controller { class EmailBoxController extends Controller {
private ?string $currentUserId; private ?string $currentUserId;
private IMailManager $mailManager; private IMailManager $mailManager;
private IUserPreferences $preferences; private IUserPreferences $preferences;
@ -71,7 +70,7 @@ class EmailBoxController extends Controller {
*/ */
public function loadEmailboxMessage(): JSONResponse { public function loadEmailboxMessage(): JSONResponse {
//TODO //TODO
$mailboxId = 1; $mailboxId = 9;
$lastMessageTimestamp = null; $lastMessageTimestamp = null;
$init = false; $init = false;
@ -99,7 +98,6 @@ class EmailBoxController extends Controller {
return new JSONResponse([], Http::STATUS_FORBIDDEN); return new JSONResponse([], Http::STATUS_FORBIDDEN);
} }
$order = $this->preferences->getPreference($this->currentUserId, 'sort-order', 'newest') === 'newest' ? 'DESC': 'ASC'; $order = $this->preferences->getPreference($this->currentUserId, 'sort-order', 'newest') === 'newest' ? 'DESC': 'ASC';
return new JSONResponse( return new JSONResponse(
$this->mailSearch->findMessages( $this->mailSearch->findMessages(

View File

@ -100,11 +100,10 @@
Revenir dans la listes des emails Revenir dans la listes des emails
</NcActionButton> </NcActionButton>
<div class="image-list-attachement " v-if="!loadingDataEmailBox && mailAttachments.length > 0" v-for="attachment in mailAttachments"> <div class="image-list-attachement " v-if="!loadingDataEmailBox && mailAttachments.length > 0" v-for="attachment in mailAttachments">
<div class="image-item-attachement " @click="pickEmailAttachement(attachment)"> <div v-if="attachment.isImage" class="image-item-attachement " @click="pickEmailAttachement(attachment)">
<img :src="attachment.downloadUrl" :alt="attachment.fileName "> <img :src="attachment.downloadUrl" :alt="attachment.fileName ">
<label>{{ attachment.fileName }}</label> <label>{{ attachment.fileName }}</label>
</div> </div>
</div> </div>
<template v-if="!mailAttachments.length && !loadingDataEmailBox" > <template v-if="!mailAttachments.length && !loadingDataEmailBox" >
<p> Pas d'Attachements</p> <p> Pas d'Attachements</p>

View File

@ -205,6 +205,8 @@ return [
['name' => 'page#getUserConnectedGroups', 'url' => '/getUserConnectedGroups', 'verb' => 'GET'], ['name' => 'page#getUserConnectedGroups', 'url' => '/getUserConnectedGroups', 'verb' => 'GET'],
['name' => 'provider#moveAttachmentFile', 'url' => '/test-move', 'verb' => 'GET'],
['name' => 'provider#sendEmailAttachment', 'url' => '/test-send-mail-attach', 'verb' => 'GET'],
] ]

View File

@ -1,29 +1,34 @@
<?php <?php
namespace OCA\Gestion\Controller; namespace OCA\Gestion\Controller;
use Exception; use Exception;
use OCA\Gestion\Service\ConfigurationService; use OCP\IConfig;
use OCA\Gestion\Service\NavigationService;
use OCA\Gestion\Service\Provider\ProviderService;
use OCA\Gestion\Service\Provider\Statistic\ProviderStatisticService;
use Psr\Log\LoggerInterface;
defined("TAB1") or define("TAB1", "\t");
use OCP\IGroupManager;
use OCP\IRequest; use OCP\IRequest;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\Mail\IMailer; use OCP\Mail\IMailer;
use OCP\Files\IRootFolder; use OCP\IGroupManager;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\DataResponse; defined("TAB1") or define("TAB1", "\t");
use OCP\AppFramework\Controller;
use OCA\Gestion\Db\Bdd;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IConfig; use OCA\Gestion\Db\Bdd;
use OC\Files\Filesystem;
use OCP\Files\IRootFolder;
use Psr\Log\LoggerInterface;
use OCP\AppFramework\Controller;
use OCA\Gestion\Service\GestionService;
use OCP\AppFramework\Http\DataResponse;
use OCA\Gestion\Service\NavigationService;
use OCP\AppFramework\Http\TemplateResponse;
use OCA\Gestion\Service\ConfigurationService;
use OCA\Gestion\Service\Provider\ProviderService;
use OCA\Gestion\Service\Provider\Statistic\ProviderStatisticService;
date_default_timezone_set('Europe/Paris'); date_default_timezone_set('Europe/Paris');
class ProviderController extends Controller { class ProviderController extends Controller
{
private $idNextcloud; private $idNextcloud;
private $urlGenerator; private $urlGenerator;
private $mailer; private $mailer;
@ -38,31 +43,36 @@ class ProviderController extends Controller {
/** @var \OCA\Gestion\Service\NavigationService */ /** @var \OCA\Gestion\Service\NavigationService */
private $navigationService; private $navigationService;
private $providerService; private $providerService;
private $providerStatisticService; private $providerStatisticService;
private $logger; private $logger;
//Gestion service
private $gestionService;
/** /**
* Constructor * Constructor
*/ */
public function __construct($AppName, public function __construct(
IRequest $request, $AppName,
$UserId, IRequest $request,
Bdd $myDb, $UserId,
IRootFolder $rootFolder, Bdd $myDb,
IURLGenerator $urlGenerator, IRootFolder $rootFolder,
IMailer $mailer, IURLGenerator $urlGenerator,
Iconfig $config, IMailer $mailer,
IUserSession $userSession, Iconfig $config,
IGroupManager $groupManager, IUserSession $userSession,
NavigationService $navigationService, IGroupManager $groupManager,
ConfigurationService $configurationService, NavigationService $navigationService,
LoggerInterface $logger, ConfigurationService $configurationService,
ProviderService $providerService, LoggerInterface $logger,
ProviderStatisticService $providerStatisticService ProviderService $providerService,
){ ProviderStatisticService $providerStatisticService,
GestionService $gestionService
) {
parent::__construct($AppName, $request); parent::__construct($AppName, $request);
@ -74,41 +84,43 @@ class ProviderController extends Controller {
$this->navigationService = $navigationService; $this->navigationService = $navigationService;
$this->configurationService = $configurationService; $this->configurationService = $configurationService;
$this->logger = $logger; $this->logger = $logger;
$this->providerService = $providerService; $this->providerService = $providerService;
$this->providerStatisticService = $providerStatisticService; $this->providerStatisticService = $providerStatisticService;
$this->gestionService = $gestionService;
if ($userSession->isLoggedIn()) { if ($userSession->isLoggedIn()) {
$this->user = $userSession->getUser(); $this->user = $userSession->getUser();
} }
if ($this->user != null) { if ($this->user != null) {
$groups = $groupManager->getUserGroups($this->user); $groups = $groupManager->getUserGroups($this->user);
$this->groups = []; $this->groups = [];
foreach ($groups as $group) { foreach ($groups as $group) {
$this->groups[] = $group->getGID(); $this->groups[] = $group->getGID();
} }
} }
try{ try {
$this->storage = $rootFolder->getUserFolder($this->idNextcloud); $this->storage = $rootFolder->getUserFolder($this->idNextcloud);
}catch(\OC\User\NoUserException $e){ } catch (\OC\User\NoUserException $e) {
} }
} }
/** /**
* @NoAdminRequired * @NoAdminRequired
* @NoCSRFRequired * @NoCSRFRequired
*/ */
public function provider() { public function provider()
{
return new TemplateResponse('gestion', 'provider', array('groups' => $this->groups, 'user' => $this->user, 'path' => $this->idNextcloud, 'url' => $this->navigationService->getNavigationLink())); return new TemplateResponse('gestion', 'provider', array('groups' => $this->groups, 'user' => $this->user, 'path' => $this->idNextcloud, 'url' => $this->navigationService->getNavigationLink()));
} }
/** /**
* @NoAdminRequired * @NoAdminRequired
* @NoCSRFRequired * @NoCSRFRequired
*/ */
public function getProviders() { public function getProviders()
{
$providers = $this->providerService->getProvidersAsArray(); $providers = $this->providerService->getProvidersAsArray();
return json_encode($providers); return json_encode($providers);
} }
@ -116,13 +128,13 @@ class ProviderController extends Controller {
/** /**
* @NoAdminRequired * @NoAdminRequired
* @NoCSRFRequired * @NoCSRFRequired
*/ */
public function createDefaultProvider() { public function createDefaultProvider()
try{ {
try {
$this->providerService->createDefaultProvider($this->idNextcloud); $this->providerService->createDefaultProvider($this->idNextcloud);
return true; return true;
} } catch (Exception $e) {
catch(Exception $e){
return null; return null;
} }
} }
@ -130,14 +142,84 @@ class ProviderController extends Controller {
/** /**
* @NoAdminRequired * @NoAdminRequired
* @NoCSRFRequired * @NoCSRFRequired
*/ */
public function exportProvidersStatistic(array $providerIds,$year){ public function exportProvidersStatistic(array $providerIds, $year)
try{ {
$filenames = $this->providerStatisticService->exportProvidersStatisticByYear($providerIds,$year,$this->idNextcloud); try {
$filenames = $this->providerStatisticService->exportProvidersStatisticByYear($providerIds, $year, $this->idNextcloud);
return $filenames; return $filenames;
} } catch (Exception $e) {
catch(Exception $e){
return []; return [];
} }
} }
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function moveAttachmentFile()
{
$tex = "BEGIN:VCALENDAR
PRODID:-//IDN nextcloud.com//Calendar app 4.7.16//EN
CALSCALE:GREGORIAN
VERSION:2.0
BEGIN:VEVENT
CREATED:20250319T070401Z
DTSTAMP:20250319T070538Z
LAST-MODIFIED:20250319T070538Z
SEQUENCE:2
UID:dc3ebd49-74c6-490a-afd2-2b61ef47c6f3
DTSTART;TZID=Africa/Nairobi:20250319T110001
DTEND;TZID=Africa/Nairobi:20250319T120001
STATUS:CONFIRMED
SUMMARY:test 5
LOCATION:2
DESCRIPTION:SOINS
TRANSP:TRANSPARENT
ATTACH;FMTTYPE=image/png;FILENAME=/sign.png;X-NC-FILE-ID=1426;X-NC-HAS-PREV
IEW=true:/f/1426
CLIENT:1
EMBALMER:
END:VEVENT
BEGIN:VTIMEZONE
TZID:Africa/Nairobi
BEGIN:STANDARD
TZOFFSETFROM:+0300
TZOFFSETTO:+0300
TZNAME:EAT
DTSTART:19700101T000000
END:STANDARD
END:VTIMEZONE
END:VCALENDAR";
$this->gestionService->moveCalendarAttachmentFile($tex, '/f');
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function sendEmailAttachment()
{
$this->storage->getFullPath("/");
$attachments = [
['file_id' => 1518, 'name' => 'Capture.JPG']
];
$to = "jonathan.goulet@nextcloud.com";
$subject = "Piece jointe";
$body = "<p>Bonjour.</p> <p>Vous trouverez en pièce jointe les fichiers votre défunt.</p>";
$message = $this->mailer->createMessage();
$message->setSubject($subject);
$message->setTo(recipients: [$to]);
foreach ($attachments as $attachment) {
$path = Filesystem::getPath($attachment['file_id']);
$content = $this->mailer->createAttachment(Filesystem::file_get_contents($path), $attachment['name'], 'image/png');
$message->attach($content);
}
$message->setHtmlBody($body);
$this->mailer->send($message);
return true;
}
} }

View File

@ -0,0 +1,12 @@
<?php
namespace OCA\Gestion\Helpers;
class FolderHelpers
{
public static function GetDefuntFolder(string $clientEntreprise,string $defuntNom){
$clientRacineFolder = 'CLIENTS/'.strtoupper($clientEntreprise).'/';
$defuntFolder = $clientRacineFolder.'DEFUNTS/'.strtoupper($defuntNom).'/';
return $defuntFolder;
}
}

View File

@ -61,4 +61,21 @@ class VCalendarHelpers
} }
return $vCalendarData; return $vCalendarData;
} }
public static function hasAttachment(string $vCalendarString) : bool{
return str_contains($vCalendarString, 'ATTACH;FMTTYPE');
}
public static function extractAttachments(string $vCalendarString): array {
$attachments = [];
preg_match_all('/ATTACH;FMTTYPE=([^;]+);FILENAME=([^;]+);X-NC-FILE-ID=([^;]+);/', $vCalendarString, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$attachments[] = [
'type' =>$match[1], // Extrait 'image' de 'image/png'
'name' => trim($match[2], '/'),
'file_id' => (int) $match[3],
];
}
return $attachments;
}
} }

View File

@ -26,18 +26,22 @@ declare(strict_types=1);
namespace OCA\Gestion\Service; namespace OCA\Gestion\Service;
use OCP\DB\Exception;
use OCP\IUserSession;
use OCP\Mail\IMailer;
use OCA\Gestion\Db\Bdd;
use OC\Files\Filesystem;
use OCP\Files\IRootFolder;
use OCA\Gestion\Db\OrderBdd;
use Psr\Log\LoggerInterface;
use OCA\Gestion\Constants\BddConstant; use OCA\Gestion\Constants\BddConstant;
use OCA\Gestion\Constants\DevisMentionConstant; use OCA\Gestion\Helpers\FolderHelpers;
use OCA\Gestion\Helpers\VCalendarHelpers;
use OCA\Gestion\Constants\OrderStatusConstant; use OCA\Gestion\Constants\OrderStatusConstant;
use OCA\Gestion\Constants\ThanatoTypeConstant; use OCA\Gestion\Constants\ThanatoTypeConstant;
use OCA\Gestion\Db\Bdd;
use OCA\Gestion\Db\OrderBdd;
use OCA\Gestion\Service\Devis\Pdf\DevisPdfService;
use OCA\Gestion\Service\Order\OrderPdfService; use OCA\Gestion\Service\Order\OrderPdfService;
use OCP\DB\Exception; use OCA\Gestion\Constants\DevisMentionConstant;
use Psr\Log\LoggerInterface; use OCA\Gestion\Service\Devis\Pdf\DevisPdfService;
use OCA\Gestion\Helpers\VCalendarHelpers;
use OCP\IUserSession;
class GestionService { class GestionService {
/** @var Bdd */ /** @var Bdd */
@ -58,6 +62,11 @@ class GestionService {
private string $userConnectedUuid; private string $userConnectedUuid;
private $userConnectedStorage;
private $mailer;
public function __construct( public function __construct(
Bdd $gestionBdd, Bdd $gestionBdd,
OrderBdd $orderBdd, OrderBdd $orderBdd,
@ -65,7 +74,10 @@ class GestionService {
OrderPdfService $orderPdfService, OrderPdfService $orderPdfService,
DevisPdfService $devisPdfService, DevisPdfService $devisPdfService,
TalkService $talkService, TalkService $talkService,
IUserSession $userSession IUserSession $userSession,
IRootFolder $rootFolder,
IMailer $mailer,
) { ) {
$this->orderBdd = $orderBdd; $this->orderBdd = $orderBdd;
$this->logger = $logger; $this->logger = $logger;
@ -75,6 +87,9 @@ class GestionService {
$this->talkService = $talkService; $this->talkService = $talkService;
$this->userSession = $userSession; $this->userSession = $userSession;
$this->userConnectedUuid = $userSession->getUser()?->getUID() ?? BddConstant::DEFAULT_ADMIN_APP_ID_NEXTCLOUD; $this->userConnectedUuid = $userSession->getUser()?->getUID() ?? BddConstant::DEFAULT_ADMIN_APP_ID_NEXTCLOUD;
$this->userConnectedStorage = $rootFolder->getUserFolder($this->userConnectedUuid);
$this->mailer = $mailer;
} }
private function GetCalendarSummaryFromVCalendarString(string $vCalendarString): string private function GetCalendarSummaryFromVCalendarString(string $vCalendarString): string
@ -184,6 +199,8 @@ class GestionService {
$calendarUuid = $this->GetCalendarUuidFromVCalendarString($vCalendarString); $calendarUuid = $this->GetCalendarUuidFromVCalendarString($vCalendarString);
$userName = $this->GetThanatoNameFromVCalendarString($vCalendarString); $userName = $this->GetThanatoNameFromVCalendarString($vCalendarString);
$devisAlreadyCreated = $this->IsDevisAlreadyCreated($clientId,$locationId,$thanatoId,$calendarSummary,$calendarUuid); $devisAlreadyCreated = $this->IsDevisAlreadyCreated($clientId,$locationId,$thanatoId,$calendarSummary,$calendarUuid);
if($devisAlreadyCreated){ if($devisAlreadyCreated){
return; return;
} }
@ -210,6 +227,20 @@ class GestionService {
$devisTalkMessage = $this->gestionBdd->getDevisTalkRoomMessage($devisId,$userName); $devisTalkMessage = $this->gestionBdd->getDevisTalkRoomMessage($devisId,$userName);
$this->talkService->sendDevisTalkNotifications($devisTalkMessage,$userName,$this->userConnectedUuid); $this->talkService->sendDevisTalkNotifications($devisTalkMessage,$userName,$this->userConnectedUuid);
$this->devisPdfService->generateDevisPdfByDevisId($devisId,$this->userConnectedUuid); $this->devisPdfService->generateDevisPdfByDevisId($devisId,$this->userConnectedUuid);
//Move calendar attachment file to defunt folder
if (VCalendarHelpers::hasAttachment($vCalendarString)) {
$devis = $this->gestionBdd->getDevisByDevisId($devisId);
if($devis != null && $devis["client_entreprise"] != null){
$destinationFolderAttachment = FolderHelpers::GetDefuntFolder($devis["client_entreprise"],$devis["defunt_nom"]);
$attachments = VCalendarHelpers::extractAttachments($vCalendarString);
$this->moveCalendarAttachmentFile($attachments,$destinationFolderAttachment);
if ($thanatoIsSubcontractor) {
$this->sendEmailAttachment($devis["defunt_nom"],$attachments);
}
}
}
$this->gestionBdd->createDevisTrajetFromVCalendar($devisId,$userName); $this->gestionBdd->createDevisTrajetFromVCalendar($devisId,$userName);
} }
catch(Exception $e){ catch(Exception $e){
@ -217,6 +248,14 @@ class GestionService {
} }
} }
public function moveCalendarAttachmentFile(array $attachments ,string $destinationFolder ){
$this->userConnectedStorage->getFullPath("/");
foreach ($attachments as $attachment) {
$path = Filesystem::getPath($attachment['file_id']);
Filesystem::copy($path, $destinationFolder . $attachment['name']);
}
}
private function GetThanatoNameFromVCalendarString($vCalendarString){ private function GetThanatoNameFromVCalendarString($vCalendarString){
$thanatoName = null; $thanatoName = null;
$thanatoNames = $this->GetAttendeesNameFromVCalendarString($vCalendarString); $thanatoNames = $this->GetAttendeesNameFromVCalendarString($vCalendarString);
@ -329,12 +368,50 @@ class GestionService {
$userName = $this->GetThanatoNameFromVCalendarString($vCalendarString); $userName = $this->GetThanatoNameFromVCalendarString($vCalendarString);
$devisTalkMessage = $this->gestionBdd->getDevisTalkRoomMessage($devis['id'],$userName); $devisTalkMessage = $this->gestionBdd->getDevisTalkRoomMessage($devis['id'],$userName);
$this->talkService->sendDevisTalkNotifications($devisTalkMessage,$userName,$this->userConnectedUuid); $this->talkService->sendDevisTalkNotifications($devisTalkMessage,$userName,$this->userConnectedUuid);
$this->devisPdfService->generateDevisPdfByDevisId($devis['id'],$this->userConnectedUuid);
if (VCalendarHelpers::hasAttachment($vCalendarString)) {
$thanato = $this->gestionBdd->getThanatoByThanatoId($devis['id_thanato']);
$thanatoIsSubcontractor = $thanato["fk_thanato_type_key"] === ThanatoTypeConstant::THANATO_TYPE_SUBCONTRACTOR;
$devis = $this->gestionBdd->getDevisByDevisId($devis['id']);
if($devis != null && $devis["client_entreprise"] != null){
$destinationFolderAttachment = FolderHelpers::GetDefuntFolder($devis["client_entreprise"],$devis["defunt_nom"]);
$attachments = VCalendarHelpers::extractAttachments($vCalendarString);
$this->moveCalendarAttachmentFile($attachments , $destinationFolderAttachment);
if ($thanatoIsSubcontractor) {
$this->sendEmailAttachment($devis["defunt_nom"],$attachments);
}
}
}
} }
$this->devisPdfService->generateDevisPdfByDevisId($devis['id'],$this->userConnectedUuid);
return true; return true;
} }
catch(Exception $e){ catch(Exception $e){
$this->logger->debug("error creating devis"); $this->logger->debug("error creating devis");
} }
} }
public function sendEmailAttachment($defunt_nom ,$attachments = []){
$this->userConnectedStorage->getFullPath("/");
$to = "jonathan.goulet@nextcloud.com";
$subject = "Piece jointe";
$body = "<p>Bonjour.</p> <p>Vous trouverez en pièce jointe les documents concernant de « $defunt_nom ».</p>";
$message = $this->mailer->createMessage();
$message->setSubject($subject);
$message->setTo(recipients: [$to]);
foreach ($attachments as $attachment) {
$path = Filesystem::getPath($attachment['file_id']);
$content = $this->mailer->createAttachment( Filesystem::file_get_contents($path),$attachment['name'],'image/png');
$message->attach($content);
}
$message->setHtmlBody( $body);
$this->mailer->send($message);
}
} }