add more controllers and fix tests
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Email;
|
||||
|
||||
use App\Http\Controllers\Api\BaseApiController;
|
||||
use App\Http\Requests\Email\EmailSendRequest;
|
||||
use App\Http\Resources\Email\EmailSenderResource;
|
||||
use App\Services\Email\EmailAttachmentService;
|
||||
use App\Services\Email\EmailDispatchService;
|
||||
use App\Services\Email\EmailSenderOptionsService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class EmailController extends BaseApiController
|
||||
{
|
||||
public function __construct(
|
||||
private EmailDispatchService $dispatch,
|
||||
private EmailSenderOptionsService $senderOptions,
|
||||
private EmailAttachmentService $attachments
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function senders(): JsonResponse
|
||||
{
|
||||
$senders = $this->senderOptions->listSenders();
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
'senders' => EmailSenderResource::collection($senders),
|
||||
]);
|
||||
}
|
||||
|
||||
public function send(EmailSendRequest $request): JsonResponse
|
||||
{
|
||||
$payload = $request->validated();
|
||||
|
||||
$attachments = $this->attachments->normalize($request->file('attachments', []));
|
||||
|
||||
$ok = $this->dispatch->send(
|
||||
$payload['recipient'],
|
||||
$payload['subject'],
|
||||
$payload['html_message'],
|
||||
$payload['profile'] ?? null,
|
||||
$payload['reply_to_email'] ?? null,
|
||||
$payload['reply_to_name'] ?? null,
|
||||
$attachments
|
||||
);
|
||||
|
||||
if (!$ok) {
|
||||
return response()->json([
|
||||
'ok' => false,
|
||||
'message' => 'Email failed to send.',
|
||||
], 500);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
'message' => 'Email sent successfully.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Email;
|
||||
|
||||
use App\Http\Controllers\Api\BaseApiController;
|
||||
use App\Http\Requests\Email\EmailExtractorCompareRequest;
|
||||
use App\Http\Resources\Email\EmailExtractorCompareResource;
|
||||
use App\Http\Resources\Email\EmailExtractorEmailsResource;
|
||||
use App\Services\Email\EmailExtractorService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class EmailExtractorController extends BaseApiController
|
||||
{
|
||||
public function __construct(private EmailExtractorService $service)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function emails(): JsonResponse
|
||||
{
|
||||
$emails = $this->service->listEmails();
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
'emails' => new EmailExtractorEmailsResource($emails),
|
||||
]);
|
||||
}
|
||||
|
||||
public function compare(EmailExtractorCompareRequest $request): JsonResponse
|
||||
{
|
||||
$csvEmails = [];
|
||||
|
||||
$file = $request->file('file');
|
||||
if ($file && $file->isValid()) {
|
||||
$contents = file_get_contents($file->getRealPath() ?: $file->getPathname());
|
||||
$csvEmails = $this->service->extractEmailsFromText($contents);
|
||||
}
|
||||
|
||||
if (empty($csvEmails)) {
|
||||
$csvEmails = $request->input('csv_emails', []);
|
||||
}
|
||||
|
||||
if (empty($csvEmails)) {
|
||||
return response()->json([
|
||||
'ok' => false,
|
||||
'message' => 'Provide a CSV file or csv_emails array.',
|
||||
], 422);
|
||||
}
|
||||
|
||||
$result = $this->service->compare($csvEmails);
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
'comparison' => new EmailExtractorCompareResource($result),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user