refactor: replace custom CRM with Monica fork
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s
This commit is contained in:
94
app/Services/Contact/Call/CreateCall.php
Normal file
94
app/Services/Contact/Call/CreateCall.php
Normal 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();
|
||||
}
|
||||
}
|
||||
64
app/Services/Contact/Call/DestroyCall.php
Normal file
64
app/Services/Contact/Call/DestroyCall.php
Normal 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();
|
||||
}
|
||||
}
|
||||
102
app/Services/Contact/Call/UpdateCall.php
Normal file
102
app/Services/Contact/Call/UpdateCall.php
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user