refactor: replace custom CRM with Monica fork
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s

This commit is contained in:
Kroonk
2026-05-22 16:19:55 +02:00
parent 38cc4c09ca
commit a213a1dba0
2215 changed files with 242567 additions and 6015 deletions

View File

@@ -0,0 +1,94 @@
<?php
namespace App\Services\Contact\Call;
use Illuminate\Support\Arr;
use App\Models\Contact\Call;
use App\Services\BaseService;
use App\Models\Contact\Contact;
use App\Models\Instance\Emotion\Emotion;
class CreateCall extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'contact_id' => 'required|integer',
'called_at' => 'required|date',
'content' => 'nullable|string',
'contact_called' => 'nullable|boolean',
'emotions' => 'nullable|array',
];
}
/**
* Create a call.
*
* @param array $data
* @return Call
*/
public function execute(array $data): Call
{
$this->validate($data);
$contact = Contact::where('account_id', $data['account_id'])
->findOrFail($data['contact_id']);
$contact->throwInactive();
// emotions array is left out as they are not attached during this call
$call = Call::create(Arr::except($data, ['emotions']));
$this->updateLastCallInfo($contact, $call);
if (! empty($data['emotions'])) {
if ($data['emotions'] != '') {
$this->addEmotions($data['emotions'], $call);
}
}
return $call;
}
/**
* Add emotions to the call.
*
* @param array $emotions
* @param Call $call
* @return void
*/
private function addEmotions(array $emotions, Call $call)
{
foreach ($emotions as $emotionId) {
$emotion = Emotion::findOrFail($emotionId);
$call->emotions()->syncWithoutDetaching([$emotion->id => [
'account_id' => $call->account_id,
'contact_id' => $call->contact_id,
]]);
}
}
/**
* Update last call information of the contact.
*
* @param Contact $contact
* @param Call $call
* @return void
*/
private function updateLastCallInfo(Contact $contact, Call $call)
{
if (is_null($contact->last_talked_to)) {
$contact->last_talked_to = $call->called_at;
} else {
$contact->last_talked_to = $contact->last_talked_to->max($call->called_at);
}
$contact->save();
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace App\Services\Contact\Call;
use App\Models\Contact\Call;
use App\Services\BaseService;
use App\Models\Contact\Contact;
class DestroyCall extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'call_id' => 'required|integer',
];
}
/**
* Destroy a call.
*
* @param array $data
* @return bool
*/
public function execute(array $data): bool
{
$this->validate($data);
$call = Call::where('account_id', $data['account_id'])
->findOrFail($data['call_id']);
$contact = $call->contact;
$contact->throwInactive();
// delete all associations with emotions
$call->emotions()->sync([]);
$call->delete();
$this->updateLastCallInfo($contact);
return true;
}
/**
* Update last call information of the contact.
*
* @param Contact $contact
* @return void
*/
private function updateLastCallInfo(Contact $contact)
{
// look for all the calls of the contact and take the most recent call
// as the one we just deleted could have been the most recent call
$contact->last_talked_to = optional($contact->calls->first())->called_at;
$contact->save();
}
}

View File

@@ -0,0 +1,102 @@
<?php
namespace App\Services\Contact\Call;
use App\Models\Contact\Call;
use App\Services\BaseService;
use App\Models\Instance\Emotion\Emotion;
class UpdateCall extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'call_id' => 'required|integer|exists:calls,id',
'called_at' => 'required|date',
'content' => 'nullable|string',
'contact_called' => 'nullable|boolean',
'emotions' => 'nullable|array',
];
}
/**
* Update a call.
*
* @param array $data
* @return Call
*/
public function execute(array $data): Call
{
$this->validate($data);
/** @var Call */
$call = Call::where('account_id', $data['account_id'])
->findOrFail($data['call_id']);
$call->contact->throwInactive();
$call->update([
'called_at' => $data['called_at'],
'content' => (empty($data['content']) ? null : $data['content']),
'contact_called' => (empty($data['contact_called']) ? null : $data['contact_called']),
]);
// emotions array is left out as they are not attached during this call
if (! empty($data['emotions'])) {
if ($data['emotions'] != '') {
$this->addEmotions($data['emotions'], $call);
}
}
$this->updateLastCallInfo($call);
return $call;
}
/**
* Add emotions to the call.
*
* @param array $emotions
* @param Call $call
* @return void
*/
private function addEmotions(array $emotions, Call $call)
{
// reset current emotions
$call->emotions()->sync([]);
// saving new emotions
foreach ($emotions as $emotionId) {
$emotion = Emotion::findOrFail($emotionId);
$call->emotions()->syncWithoutDetaching([$emotion->id => [
'account_id' => $call->account_id,
'contact_id' => $call->contact_id,
]]);
}
}
/**
* Update last call information of the contact.
*
* @param Call $call
* @return void
*/
private function updateLastCallInfo(Call $call)
{
/** @var \App\Models\Contact\Contact */
$contact = $call->contact;
if (is_null($contact->last_talked_to)) {
$contact->last_talked_to = $call->called_at;
} else {
$contact->last_talked_to = $contact->last_talked_to->max($call->called_at);
}
$contact->save();
}
}