Skip to content
Merged
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
11 changes: 11 additions & 0 deletions backend/app/DomainObjects/Enums/ShareCaptionTone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace HiEvents\DomainObjects\Enums;

enum ShareCaptionTone: string
{
case FRIENDLY = 'FRIENDLY';
case PROFESSIONAL = 'PROFESSIONAL';
case ENERGETIC = 'ENERGETIC';
case SHORT = 'SHORT';
}
12 changes: 12 additions & 0 deletions backend/app/DomainObjects/Enums/ShareSuggestionAvailability.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace HiEvents\DomainObjects\Enums;

enum ShareSuggestionAvailability: string
{
case AVAILABLE = 'AVAILABLE';
case NOT_CONFIGURED = 'NOT_CONFIGURED';
case EMAIL_NOT_CONFIRMED = 'EMAIL_NOT_CONFIRMED';
case PAYMENTS_NOT_CONNECTED = 'PAYMENTS_NOT_CONNECTED';
case EVENT_UNDER_REVIEW = 'EVENT_UNDER_REVIEW';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace HiEvents\Exceptions;

use Exception;

class ShareSuggestionGenerationFailedException extends Exception {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace HiEvents\Exceptions;

use Exception;

class ShareSuggestionQuotaExceededException extends Exception {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace HiEvents\Exceptions;

use Exception;

class ShareSuggestionsUnavailableException extends Exception {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace HiEvents\Http\Actions\Events\ShareSuggestions;

use HiEvents\DomainObjects\EventDomainObject;
use HiEvents\Exceptions\ShareSuggestionGenerationFailedException;
use HiEvents\Exceptions\ShareSuggestionQuotaExceededException;
use HiEvents\Exceptions\ShareSuggestionsUnavailableException;
use HiEvents\Http\Actions\BaseAction;
use HiEvents\Http\ResponseCodes;
use HiEvents\Resources\Event\EventShareSuggestionsResource;
use HiEvents\Services\Application\Handlers\Event\ShareSuggestion\GenerateEventShareSuggestionsHandler;
use Illuminate\Http\JsonResponse;

class GenerateEventShareSuggestionsAction extends BaseAction
{
public function __construct(
private readonly GenerateEventShareSuggestionsHandler $handler,
) {}

public function __invoke(int $eventId): JsonResponse
{
$this->isActionAuthorized($eventId, EventDomainObject::class);

try {
$suggestions = $this->handler->handle($eventId, $this->getAuthenticatedAccountId());
} catch (ShareSuggestionsUnavailableException) {
return $this->errorResponse(
__('Caption suggestions are not available for this event.'),
ResponseCodes::HTTP_FORBIDDEN,
);
} catch (ShareSuggestionQuotaExceededException) {
return $this->errorResponse(
__('You have used all of today\'s caption suggestions. Try again tomorrow.'),
ResponseCodes::HTTP_TOO_MANY_REQUESTS,
);
} catch (ShareSuggestionGenerationFailedException $exception) {
report($exception);

return $this->errorResponse(
__('We couldn\'t suggest captions right now. Please try again.'),
ResponseCodes::HTTP_SERVICE_UNAVAILABLE,
);
}

return $this->resourceResponse(EventShareSuggestionsResource::class, $suggestions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace HiEvents\Http\Actions\Events\ShareSuggestions;

use HiEvents\DomainObjects\EventDomainObject;
use HiEvents\Http\Actions\BaseAction;
use HiEvents\Resources\Event\EventShareSuggestionsResource;
use HiEvents\Services\Application\Handlers\Event\ShareSuggestion\GetEventShareSuggestionsHandler;
use Illuminate\Http\JsonResponse;

class GetEventShareSuggestionsAction extends BaseAction
{
public function __construct(
private readonly GetEventShareSuggestionsHandler $handler,
) {}

public function __invoke(int $eventId): JsonResponse
{
$this->isActionAuthorized($eventId, EventDomainObject::class);

return $this->resourceResponse(
EventShareSuggestionsResource::class,
$this->handler->handle($eventId, $this->getAuthenticatedAccountId()),
);
}
}
27 changes: 27 additions & 0 deletions backend/app/Resources/Event/EventShareSuggestionsResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace HiEvents\Resources\Event;

use HiEvents\Resources\BaseResource;
use HiEvents\Services\Domain\Event\ShareSuggestion\DTO\EventShareSuggestionsDTO;
use HiEvents\Services\Domain\Event\ShareSuggestion\DTO\ShareCaptionDTO;

/**
* @mixin EventShareSuggestionsDTO
*/
class EventShareSuggestionsResource extends BaseResource
{
public function toArray($request): array
{
return [
/** @var 'AVAILABLE'|'NOT_CONFIGURED'|'EMAIL_NOT_CONFIRMED'|'PAYMENTS_NOT_CONNECTED'|'EVENT_UNDER_REVIEW' */
'availability' => $this->availability->value,
/** @var array<int, array{tone: 'FRIENDLY'|'PROFESSIONAL'|'ENERGETIC'|'SHORT', text: string}> */
'captions' => array_map(
static fn (ShareCaptionDTO $caption): array => ['tone' => $caption->tone->value, 'text' => $caption->text],
$this->captions,
),
'remaining_generations' => $this->remaining_generations,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace HiEvents\Services\Application\Handlers\Event\ShareSuggestion;

use HiEvents\DomainObjects\Enums\ShareSuggestionAvailability;
use HiEvents\Exceptions\ShareSuggestionGenerationFailedException;
use HiEvents\Exceptions\ShareSuggestionQuotaExceededException;
use HiEvents\Exceptions\ShareSuggestionsUnavailableException;
use HiEvents\Services\Domain\Event\ShareSuggestion\DTO\EventShareSuggestionsDTO;
use HiEvents\Services\Domain\Event\ShareSuggestion\EventShareCaptionService;
use HiEvents\Services\Domain\Event\ShareSuggestion\EventShareSuggestionAvailabilityService;
use HiEvents\Services\Domain\Event\ShareSuggestion\EventShareSuggestionQuotaService;
use HiEvents\Services\Domain\Event\ShareSuggestion\EventShareSuggestionStore;

class GenerateEventShareSuggestionsHandler
{
public function __construct(
private readonly EventShareSuggestionAvailabilityService $availabilityService,
private readonly EventShareSuggestionQuotaService $quotaService,
private readonly EventShareCaptionService $captionService,
private readonly EventShareSuggestionStore $store,
) {}

/**
* @throws ShareSuggestionsUnavailableException
* @throws ShareSuggestionQuotaExceededException
* @throws ShareSuggestionGenerationFailedException
*/
public function handle(int $eventId, int $accountId): EventShareSuggestionsDTO
{
if ($this->availabilityService->resolve($eventId, $accountId) !== ShareSuggestionAvailability::AVAILABLE) {
throw new ShareSuggestionsUnavailableException;
}

if ($this->quotaService->remaining($accountId) === 0) {
throw new ShareSuggestionQuotaExceededException;
}

$captions = $this->captionService->generate($eventId);

$this->quotaService->recordGeneration($accountId);
$this->store->replace($eventId, $captions);

return new EventShareSuggestionsDTO(
availability: ShareSuggestionAvailability::AVAILABLE,
captions: $captions,
remaining_generations: $this->quotaService->remaining($accountId),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace HiEvents\Services\Application\Handlers\Event\ShareSuggestion;

use HiEvents\DomainObjects\Enums\ShareSuggestionAvailability;
use HiEvents\Services\Domain\Event\ShareSuggestion\DTO\EventShareSuggestionsDTO;
use HiEvents\Services\Domain\Event\ShareSuggestion\EventShareSuggestionAvailabilityService;
use HiEvents\Services\Domain\Event\ShareSuggestion\EventShareSuggestionQuotaService;
use HiEvents\Services\Domain\Event\ShareSuggestion\EventShareSuggestionStore;

class GetEventShareSuggestionsHandler
{
public function __construct(
private readonly EventShareSuggestionAvailabilityService $availabilityService,
private readonly EventShareSuggestionQuotaService $quotaService,
private readonly EventShareSuggestionStore $store,
) {}

public function handle(int $eventId, int $accountId): EventShareSuggestionsDTO
{
$availability = $this->availabilityService->resolve($eventId, $accountId);

return new EventShareSuggestionsDTO(
availability: $availability,
captions: $availability === ShareSuggestionAvailability::AVAILABLE ? $this->store->latest($eventId) : [],
remaining_generations: $this->quotaService->remaining($accountId),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace HiEvents\Services\Domain\Event\ShareSuggestion\DTO;

use HiEvents\DataTransferObjects\BaseDataObject;
use HiEvents\DomainObjects\Enums\ShareSuggestionAvailability;

class EventShareSuggestionsDTO extends BaseDataObject
{
/**
* @param ShareCaptionDTO[] $captions
*/
public function __construct(
public readonly ShareSuggestionAvailability $availability,
public readonly array $captions,
public readonly int $remaining_generations,
) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace HiEvents\Services\Domain\Event\ShareSuggestion\DTO;

use HiEvents\DataTransferObjects\BaseDataObject;
use HiEvents\DomainObjects\Enums\ShareCaptionTone;

class ShareCaptionDTO extends BaseDataObject
{
public function __construct(
public readonly ShareCaptionTone $tone,
public readonly string $text,
) {}
}
Loading
Loading