CRM/backend/app/Services/LegacyCallNoteBackfillServi...

41 خطوط
1.3 KiB
PHP

<?php
namespace App\Services;
use App\Models\Call;
use App\Models\Note;
class LegacyCallNoteBackfillService
{
public function run(): int
{
$created = 0;
Call::query()
->whereNotNull('notes')
->where('notes', '<>', '')
->orderBy('id')
->chunkById(100, function ($calls) use (&$created): void {
foreach ($calls as $call) {
$note = Note::firstOrCreate(
['source_key' => "legacy_call:{$call->id}"],
[
'notable_type' => Call::class,
'notable_id' => $call->id,
'user_id' => $call->user_id,
'content' => $call->notes,
'type' => 'call_summary',
'visibility' => 'team',
'is_pinned' => false,
'created_at' => $call->updated_at ?? $call->created_at ?? now(),
'updated_at' => $call->updated_at ?? now(),
]
);
$created += $note->wasRecentlyCreated ? 1 : 0;
}
});
return $created;
}
}