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:
77
app/Services/Contact/Relationship/CreateRelationship.php
Normal file
77
app/Services/Contact/Relationship/CreateRelationship.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Contact\Relationship;
|
||||
|
||||
use App\Services\BaseService;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Relationship\Relationship;
|
||||
use App\Models\Relationship\RelationshipType;
|
||||
|
||||
class CreateRelationship 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_is' => 'required|integer|exists:contacts,id',
|
||||
'of_contact' => 'required|integer|exists:contacts,id',
|
||||
'relationship_type_id' => 'required|integer|exists:relationship_types,id',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a relationship between two contacts.
|
||||
*
|
||||
* @param array $data
|
||||
* @return Relationship
|
||||
*/
|
||||
public function execute(array $data): Relationship
|
||||
{
|
||||
$this->validate($data);
|
||||
|
||||
$contact = Contact::where('account_id', $data['account_id'])
|
||||
->findOrFail($data['contact_is']);
|
||||
|
||||
$contact->throwInactive();
|
||||
|
||||
$otherContact = Contact::where('account_id', $data['account_id'])
|
||||
->findOrFail($data['of_contact']);
|
||||
|
||||
$relationshipType = RelationshipType::where('account_id', $data['account_id'])
|
||||
->findOrFail($data['relationship_type_id']);
|
||||
|
||||
// create the relationship
|
||||
$relationship = $this->setRelationship($contact, $otherContact, $relationshipType);
|
||||
|
||||
$reverseRelationshipType = $relationshipType->reverseRelationshipType();
|
||||
if ($reverseRelationshipType) {
|
||||
// create the reverse relationship
|
||||
$this->setRelationship($otherContact, $contact, $reverseRelationshipType);
|
||||
}
|
||||
|
||||
return $relationship;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a relationship between two contacts.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @param Contact $otherContact
|
||||
* @param RelationshipType $relationshipType
|
||||
* @return Relationship
|
||||
*/
|
||||
public function setRelationship(Contact $contact, Contact $otherContact, RelationshipType $relationshipType): Relationship
|
||||
{
|
||||
return Relationship::create([
|
||||
'account_id' => $relationshipType->account_id,
|
||||
'relationship_type_id' => $relationshipType->id,
|
||||
'contact_is' => $contact->id,
|
||||
'of_contact' => $otherContact->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
90
app/Services/Contact/Relationship/DestroyRelationship.php
Normal file
90
app/Services/Contact/Relationship/DestroyRelationship.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Contact\Relationship;
|
||||
|
||||
use App\Services\BaseService;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Relationship\Relationship;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use App\Services\Contact\Contact\DestroyContact;
|
||||
|
||||
class DestroyRelationship extends BaseService
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the service.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'account_id' => 'required|integer|exists:accounts,id',
|
||||
'relationship_id' => 'required|integer|exists:relationships,id',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy a relationship.
|
||||
*
|
||||
* @param array $data
|
||||
* @return bool
|
||||
*/
|
||||
public function execute(array $data): bool
|
||||
{
|
||||
$this->validate($data);
|
||||
|
||||
$relationship = Relationship::where('account_id', $data['account_id'])
|
||||
->findOrFail($data['relationship_id']);
|
||||
|
||||
$relationship->contactIs->throwInactive();
|
||||
|
||||
$otherContact = $relationship->ofContact;
|
||||
|
||||
$this->deleteRelationship($relationship);
|
||||
|
||||
$this->deletePartialContact($otherContact);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete relationship.
|
||||
*
|
||||
* @param Relationship $relationship
|
||||
*/
|
||||
private function deleteRelationship(Relationship $relationship)
|
||||
{
|
||||
$reverseRelationship = $relationship->reverseRelationship();
|
||||
if ($reverseRelationship) {
|
||||
$reverseRelationship->delete();
|
||||
}
|
||||
|
||||
$relationship->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete partial contact.
|
||||
*
|
||||
* @param Contact $contact
|
||||
*/
|
||||
private function deletePartialContact(Contact $contact)
|
||||
{
|
||||
// the contact is partial - if the relationship is deleted, the partial
|
||||
// contact has no reason to exist anymore
|
||||
if ($contact->is_partial) {
|
||||
$otherRelations = Relationship::where('account_id', $contact->account_id)
|
||||
->where(function (Builder $query) use ($contact) {
|
||||
return $query->where('of_contact', $contact->id)
|
||||
->orWhere('contact_is', $contact->id);
|
||||
})
|
||||
->count();
|
||||
|
||||
if ($otherRelations == 0) {
|
||||
DestroyContact::dispatch([
|
||||
'account_id' => $contact->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
69
app/Services/Contact/Relationship/UpdateRelationship.php
Normal file
69
app/Services/Contact/Relationship/UpdateRelationship.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Contact\Relationship;
|
||||
|
||||
use App\Services\BaseService;
|
||||
use App\Models\Relationship\Relationship;
|
||||
use App\Models\Relationship\RelationshipType;
|
||||
|
||||
class UpdateRelationship extends BaseService
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the service.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'account_id' => 'required|integer|exists:accounts,id',
|
||||
'relationship_id' => 'required|integer|exists:relationships,id',
|
||||
'relationship_type_id' => 'required|integer|exists:relationship_types,id',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a relationship.
|
||||
*
|
||||
* @param array $data
|
||||
* @return Relationship
|
||||
*/
|
||||
public function execute(array $data): Relationship
|
||||
{
|
||||
$this->validate($data);
|
||||
|
||||
$relationship = Relationship::where('account_id', $data['account_id'])
|
||||
->findOrFail($data['relationship_id']);
|
||||
|
||||
$relationship->contactIs->throwInactive();
|
||||
|
||||
$newRelationshipType = RelationshipType::where('account_id', $data['account_id'])
|
||||
->findOrFail($data['relationship_type_id']);
|
||||
|
||||
$reverseRelationship = $relationship->reverseRelationship();
|
||||
if ($reverseRelationship) {
|
||||
$newReverseRelationshipType = $newRelationshipType->reverseRelationshipType();
|
||||
if ($newReverseRelationshipType) {
|
||||
$this->updateRelationship($reverseRelationship, $newReverseRelationshipType);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->updateRelationship($relationship, $newRelationshipType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update one relationship.
|
||||
*
|
||||
* @param Relationship $relationship
|
||||
* @param RelationshipType $relationshipType
|
||||
* @return Relationship
|
||||
*/
|
||||
private function updateRelationship(Relationship $relationship, RelationshipType $relationshipType): Relationship
|
||||
{
|
||||
$relationship->update([
|
||||
'relationship_type_id' => $relationshipType->id,
|
||||
]);
|
||||
|
||||
return $relationship;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user