Sync Ladill Events to the Mail utility calendar.
Events upsert calendar entries by stable external_ref keys, prefer the owner's linked mailbox, and Meet skips mail sync for events-sourced rooms so linked virtual sessions appear only once on the calendar. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Integrations;
|
||||
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class MailCalendarClient
|
||||
{
|
||||
public function isConfigured(): bool
|
||||
{
|
||||
return filled(config('mail_calendar.api_key')) && filled(config('mail_calendar.api_url'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $payload
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
public function upsertEvent(array $payload): ?array
|
||||
{
|
||||
if (! $this->isConfigured()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = Http::withToken((string) config('mail_calendar.api_key'))
|
||||
->acceptJson()
|
||||
->asJson()
|
||||
->connectTimeout(10)
|
||||
->timeout(15)
|
||||
->post($this->baseUrl().'/calendar/events', $payload);
|
||||
} catch (ConnectionException $e) {
|
||||
Log::warning('Events mail calendar upsert failed', ['error' => $e->getMessage()]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $response->successful()) {
|
||||
Log::warning('Events mail calendar upsert failed', [
|
||||
'status' => $response->status(),
|
||||
'body' => $response->body(),
|
||||
]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = $response->json('data');
|
||||
|
||||
return is_array($data) ? $data : null;
|
||||
}
|
||||
|
||||
public function deleteEvent(int $eventId, string $mailboxEmail, string $externalRef): bool
|
||||
{
|
||||
if (! $this->isConfigured()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = Http::withToken((string) config('mail_calendar.api_key'))
|
||||
->acceptJson()
|
||||
->connectTimeout(10)
|
||||
->timeout(15)
|
||||
->delete($this->baseUrl().'/calendar/events/'.$eventId, [
|
||||
'mailbox_email' => strtolower(trim($mailboxEmail)),
|
||||
'external_ref' => $externalRef,
|
||||
]);
|
||||
} catch (ConnectionException $e) {
|
||||
Log::warning('Events mail calendar delete failed', ['error' => $e->getMessage()]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return $response->successful();
|
||||
}
|
||||
|
||||
protected function baseUrl(): string
|
||||
{
|
||||
return rtrim((string) config('mail_calendar.api_url'), '/');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user