Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions backend/src/Api/Sudo/Controller/IssueController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Api\Sudo\Controller;

use App\Api\Sudo\Object\SudoIssueObject;
use App\Entity\Issue;
use App\Entity\Type\IssueStatus;
use App\Service\Issue\IssueService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;

class IssueController extends AbstractController
{
public function __construct(
private IssueService $issueService,
)
{
}

#[Route('/issues', methods: ['GET'])]
public function getIssues(Request $request): JsonResponse
{
$subdomain = $request->query->has('subdomain') ? $request->query->getString('subdomain') : null;
$status = $request->query->has('status')
? IssueStatus::tryFrom($request->query->getString('status'))
: null;
$limit = $request->query->getInt('limit', 50);
$offset = $request->query->getInt('offset', 0);

return new JsonResponse(
array_map(
fn($issue) => new SudoIssueObject($issue),
$this->issueService->getIssuesGlobal($subdomain, $status, $limit, $offset)
)
);
}

#[Route('/issues/{id}', methods: ['GET'])]
public function getIssue(Issue $issue): JsonResponse
{
return new JsonResponse(new SudoIssueObject($issue));
}
}
44 changes: 44 additions & 0 deletions backend/src/Api/Sudo/Controller/NewsletterController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Api\Sudo\Controller;

use App\Api\Sudo\Object\NewsletterObject;
use App\Entity\Newsletter;
use App\Service\Newsletter\NewsletterService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;

class NewsletterController extends AbstractController
{
public function __construct(
private NewsletterService $newsletterService,
)
{
}

#[Route('/newsletters', methods: ['GET'])]
public function getNewsletters(Request $request): JsonResponse
{
$name = $request->query->has('name') ? $request->query->getString('name') : null;
$limit = $request->query->getInt('limit', 50);
$offset = $request->query->getInt('offset', 0);

return new JsonResponse(
array_map(
fn($newsletter) => new NewsletterObject($newsletter),
$this->newsletterService->getNewsletters($name, $limit, $offset)
)
);
}

#[Route('/newsletters/{id}', methods: ['GET'])]
public function getNewsletter(Newsletter $newsletter): JsonResponse
{
return new JsonResponse([
'newsletter' => new NewsletterObject($newsletter),
'stats' => $this->newsletterService->getNewsletterStats($newsletter),
]);
}
}
29 changes: 29 additions & 0 deletions backend/src/Api/Sudo/Object/NewsletterObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Api\Sudo\Object;

use App\Entity\Newsletter;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use SudoObjectFactory


class NewsletterObject
{
public int $id;
public int $created_at;
public string $subdomain;
public string $name;
public int $user_id;
public ?int $organization_id;
public ?string $language_code;
public bool $is_rtl;

public function __construct(Newsletter $newsletter)
{
$this->id = $newsletter->getId();
$this->created_at = $newsletter->getCreatedAt()->getTimestamp();
$this->subdomain = $newsletter->getSubdomain();
$this->name = $newsletter->getName();
$this->user_id = $newsletter->getUserId();
$this->organization_id = $newsletter->getOrganizationId();
$this->language_code = $newsletter->getLanguageCode();
$this->is_rtl = $newsletter->isRtl();
}
}
38 changes: 38 additions & 0 deletions backend/src/Api/Sudo/Object/SudoIssueObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Api\Sudo\Object;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use SudoObjectFactory


use App\Entity\Issue;
use App\Entity\Type\IssueStatus;

class SudoIssueObject
{
public int $id;
public int $created_at;
public string $uuid;
public ?string $subject;
public IssueStatus $status;
public string $newsletter_subdomain;
public int $newsletter_id;
public ?int $scheduled_at;
public ?int $sending_at;
public ?int $sent_at;
public int $total_sendable;
public ?string $error_private;

public function __construct(Issue $issue)
{
$this->id = $issue->getId();
$this->created_at = $issue->getCreatedAt()->getTimestamp();
$this->uuid = $issue->getUuid();
$this->subject = $issue->getSubject();
$this->status = $issue->getStatus();
$this->newsletter_subdomain = $issue->getNewsletter()->getSubdomain();
$this->newsletter_id = $issue->getNewsletter()->getId();
$this->scheduled_at = $issue->getScheduledAt()?->getTimestamp();
$this->sending_at = $issue->getSendingAt()?->getTimestamp();
$this->sent_at = $issue->getSentAt()?->getTimestamp();
$this->total_sendable = $issue->getTotalSendable();
$this->error_private = $issue->getErrorPrivate();
}
}
4 changes: 4 additions & 0 deletions backend/src/Api/Sudo/Resolver/EntityResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Api\Sudo\Resolver;

use App\Entity\Approval;
use App\Entity\Issue;
use App\Entity\Newsletter;
use App\Entity\SubscriberImport;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
Expand All @@ -16,6 +18,8 @@ class EntityResolver implements ValueResolverInterface
public const ENTITIES = [
'approvals' => Approval::class,
'subscriber-imports' => SubscriberImport::class,
'newsletters' => Newsletter::class,
'issues' => Issue::class,
];

public function __construct(
Expand Down
32 changes: 32 additions & 0 deletions backend/src/Service/Issue/IssueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,38 @@ public function updateIssue(Issue $issue, UpdateIssueDto $updates): Issue
return $issue;
}

/**
* @return Issue[]
*/
public function getIssuesGlobal(
?string $subdomain,
?IssueStatus $status,
int $limit,
int $offset,
): array
{
$qb = $this->em->createQueryBuilder()
->select('i')
->from(Issue::class, 'i')
->join('i.newsletter', 'n')
->orderBy('i.id', 'DESC')
->setMaxResults($limit)
->setFirstResult($offset);

if ($subdomain) {
$qb->andWhere('n.subdomain = :subdomain')
->setParameter('subdomain', $subdomain);
}

if ($status) {
$qb->andWhere('i.status = :status')
->setParameter('status', $status);
}

/** @var Issue[] */
return $qb->getQuery()->getResult();
}

/**
* @return ArrayCollection<int, Issue>
*/
Expand Down
38 changes: 37 additions & 1 deletion backend/src/Service/Newsletter/NewsletterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Entity\NewsletterList;
use App\Entity\Newsletter;
use App\Entity\Send;
use App\Entity\SendingProfile;
use App\Entity\Subscriber;
use App\Entity\Type\IssueStatus;
use App\Entity\Type\SendStatus;
Expand Down Expand Up @@ -102,6 +103,25 @@ public function deleteNewsletter(Newsletter $newsletter): void
$this->em->flush();
}

/**
* @return Newsletter[]
*/
public function getNewsletters(?string $name, int $limit, int $offset): array
{
$qb = $this->em->getRepository(Newsletter::class)->createQueryBuilder('n')
->orderBy('n.id', 'DESC')
->setMaxResults($limit)
->setFirstResult($offset);

if ($name) {
$qb->andWhere('LOWER(n.name) LIKE LOWER(:name)')
->setParameter('name', '%' . $name . '%');
}

/** @var Newsletter[] */
return $qb->getQuery()->getResult();
}

public function getNewsletterById(int $id): ?Newsletter
{
return $this->em->getRepository(Newsletter::class)->find($id);
Expand Down Expand Up @@ -164,7 +184,7 @@ public function getNewsletterUser(Newsletter $newsletter, int $userId): User
}

/**
* @return array<string, array{total: int|float, last_30_days: int|float}>
* @return array{subscribers: array{total: int, last_30_days: int}, issues: array{total: int, last_30_days: int}, bounced_rate: array{total: float, last_30_days: float}, complained_rate: array{total: float, last_30_days: float}, lists_count: int, sending_profiles_count: int}
*/
public function getNewsletterStats(Newsletter $newsletter): array
{
Expand Down Expand Up @@ -224,6 +244,20 @@ public function getNewsletterStats(Newsletter $newsletter): array
$bouncedRateLast30d = $totalSendsLast30d > 0 ? round(($bouncedSendsLast30d / $totalSendsLast30d) * 100, 2) : 0.0;
$complainedRateLast30d = $totalSendsLast30d > 0 ? round(($complainedSendsLast30d / $totalSendsLast30d) * 100, 2) : 0.0;

$listsCount = (int) $this->em->getRepository(NewsletterList::class)->createQueryBuilder('l')
->select('COUNT(l.id)')
->where('l.newsletter = :newsletter')
->setParameter('newsletter', $newsletter)
->getQuery()
->getSingleScalarResult();

$sendingProfilesCount = (int) $this->em->getRepository(SendingProfile::class)->createQueryBuilder('sp')
->select('COUNT(sp.id)')
->where('sp.newsletter = :newsletter')
->setParameter('newsletter', $newsletter)
->getQuery()
->getSingleScalarResult();

return [
'subscribers' => [
'total' => $subscribers,
Expand All @@ -241,6 +275,8 @@ public function getNewsletterStats(Newsletter $newsletter): array
'total' => $complainedRate,
'last_30_days' => $complainedRateLast30d,
],
'lists_count' => $listsCount,
'sending_profiles_count' => $sendingProfilesCount,
];
}

Expand Down
51 changes: 51 additions & 0 deletions backend/tests/Api/Sudo/Issue/GetIssueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Tests\Api\Sudo\Issue;

use App\Api\Sudo\Controller\IssueController;
use App\Api\Sudo\Object\SudoIssueObject;
use App\Service\Issue\IssueService;
use App\Tests\Case\WebTestCase;
use App\Tests\Factory\IssueFactory;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(IssueController::class)]
#[CoversClass(IssueService::class)]
#[CoversClass(SudoIssueObject::class)]
class GetIssueTest extends WebTestCase
{
public function test_get_issue(): void
{
$issue = IssueFactory::createOne();

$response = $this->sudoApi(
'GET',
'/issues/' . $issue->getId()
);

$this->assertSame(200, $response->getStatusCode());
$data = $this->getJson();
$this->assertArrayHasKey('id', $data);
$this->assertArrayHasKey('created_at', $data);
$this->assertArrayHasKey('uuid', $data);
$this->assertArrayHasKey('subject', $data);
$this->assertArrayHasKey('status', $data);
$this->assertArrayHasKey('newsletter_subdomain', $data);
$this->assertArrayHasKey('newsletter_id', $data);
$this->assertArrayHasKey('scheduled_at', $data);
$this->assertArrayHasKey('sending_at', $data);
$this->assertArrayHasKey('sent_at', $data);
$this->assertArrayHasKey('total_sendable', $data);
$this->assertArrayHasKey('error_private', $data);
}

public function test_get_issue_not_found(): void
{
$response = $this->sudoApi(
'GET',
'/issues/99999'
);

$this->assertSame(404, $response->getStatusCode());
}
}
Loading