Easy_solution/src/Service/OrganizationsService.php

172 lines
7.2 KiB
PHP

<?php
namespace App\Service;
use App\Entity\Apps;
use App\Entity\Organizations;
use App\Entity\Roles;
use App\Entity\UserOrganizatonApp;
use App\Entity\UsersOrganizations;
use App\Repository\UsersOrganizationsRepository;
use App\Service\LoggerService;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
class OrganizationsService
{
private string $logoDirectory;
public function __construct(
string $logoDirectory, private readonly AwsService $awsService,
private readonly EntityManagerInterface $entityManager,
private readonly UsersOrganizationsRepository $uoRepository,
private readonly NotificationService $notificationService,
private readonly LoggerInterface $emailNotificationLogger, private readonly LoggerService $loggerService,
)
{
$this->logoDirectory = $logoDirectory;
}
public function handleLogo(Organizations $organization, $logoFile): void
{
$extension = $logoFile->guessExtension();
$customFilename = $organization->getName() . '_' . date('dmyHis') . "." . $extension;
try {
$this->awsService->PutDocObj($_ENV['S3_PORTAL_BUCKET'], $logoFile, $customFilename, $extension, 'logo/');
$this->loggerService->logAWSAction('Upload organization logo', [
'organization_id' => $organization->getId(),
'filename' => $customFilename,
'bucket' => $_ENV['S3_PORTAL_BUCKET'],
]);
$organization->setLogoUrl('logo/' . $customFilename);
} catch (FileException $e) {
$this->loggerService->logError('Failed to upload organization logo to S3', [
'organization_id' => $organization->getId(),
'error' => $e->getMessage(),
'bucket' => $_ENV['S3_PORTAL_BUCKET'],
]);
throw new FileException('Failed to upload logo to S3: ' . $e->getMessage());
}
}
/**
* Merge all apps with org apps and add a "hasAccess" flag.
*
* @param array $appsAll
* @param array $apps
* @return array
*/
public function appsAccess(array $appsAll, array $apps): array
{
// Build a quick lookup of app IDs the org has access to
$orgAppIds = array_map(static fn(Apps $app) => $app->getId(), $apps);
$result = [];
foreach ($appsAll as $app) {
$result[] = [
'entity' => $app, // Keep the full entity for Twig
'hasAccess' => in_array($app->getId(), $orgAppIds, true),
];
}
return $result;
}
public function notifyOrganizationAdmins(array $data, string $type): void
{
$roleAdmin = $this->entityManager->getRepository(Roles::class)->findOneBy(['name' => 'ADMIN']);
$adminUOs = $this->uoRepository->findBy(['organization' => $data['organization'], 'isActive' => true]);
foreach ($adminUOs as $adminUO) {
$uoa = $this->entityManager->getRepository(UserOrganizatonApp::class)
->findOneBy([
'userOrganization' => $adminUO,
'role' => $roleAdmin,
'isActive' => true
]);
switch ($type) {
case 'USER_ACCEPTED':
if ($uoa && $adminUO->getUsers()->getId() !== $data['user']->getId() ) {
$newUser = $data['user'];
$this->notificationService->notifyUserAcceptedInvite(
$adminUO->getUsers(),
$newUser,
$data['organization']
);
$this->loggerService->logAdminNotified([
'admin_user_id' =>$adminUO->getUsers()->getId(),
'target_user_id' => $newUser->getId(),
'organization_id' => $data['organization']->getId(),'case' =>$type]);
}
break;
case 'USER_INVITED':
if ($uoa) {
$invitedUser = $data['user'];
$this->notificationService->notifyUserInvited(
$adminUO->getUsers(),
$invitedUser,
$data['organization']
);
$this->loggerService->logAdminNotified([
'admin_user_id' =>$adminUO->getUsers()->getId(),
'target_user_id' => $invitedUser->getId(),
'organization_id' => $data['organization']->getId(),'case' =>$type]);
}
break;
case 'USER_DEACTIVATED':
if ($uoa && $adminUO->getUsers()->getId() !== $data['user']->getId() ) {
$removedUser = $data['user'];
$this->notificationService->notifyUserDeactivated(
$adminUO->getUsers(),
$removedUser,
$data['organization']
);
$this->loggerService->logAdminNotified([
'admin_user_id' =>$adminUO->getUsers()->getId(),
'target_user_id' => $removedUser->getId(),
'organization_id' => $data['organization']->getId(),'case' =>$type]);
}
break;
case 'USER_DELETED':
if ($uoa && $adminUO->getUsers()->getId() !== $data['user']->getId() ) {
$removedUser = $data['user'];
$this->notificationService->notifyUserDeleted(
$adminUO->getUsers(),
$removedUser,
$data['organization']
);
$this->loggerService->logAdminNotified([
'admin_user_id' =>$adminUO->getUsers()->getId(),
'target_user_id' => $removedUser->getId(),
'organization_id' => $data['organization']->getId(),'case' =>$type]);
}
break;
case 'USER_ACTIVATED':
if ($uoa && $adminUO->getUsers()->getId() !== $data['user']->getId() ) {
$activatedUser = $data['user'];
$this->notificationService->notifyUserActivated(
$adminUO->getUsers(),
$activatedUser,
$data['organization']
);
$this->loggerService->logAdminNotified([
'admin_user_id' =>$adminUO->getUsers()->getId(),
'target_user_id' => $activatedUser->getId(),
'organization_id' => $data['organization']->getId(),'case' =>$type]);
}
break;
}
}
}
}