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>
82 lines
2.3 KiB
PHP
82 lines
2.3 KiB
PHP
<?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'), '/');
|
|
}
|
|
}
|