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,88 @@
<?php
namespace App\Services\Account\Activity\Activity;
use App\Services\BaseService;
use App\Models\Contact\Contact;
use App\Models\Account\Activity;
class AttachContactToActivity extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'activity_id' => 'required|integer|exists:activities,id',
'contacts' => 'required|array',
];
}
/**
* Validate all datas to execute the service.
*
* @param array $data
* @return bool
*/
public function validate(array $data): bool
{
parent::validate($data);
Activity::where('account_id', $data['account_id'])
->findOrFail($data['activity_id']);
foreach ($data['contacts'] as $contactId) {
Contact::where('account_id', $data['account_id'])
->findOrFail($contactId);
}
return true;
}
/**
* Attach contacts to an activity.
*
* @param array $data
* @return Activity
*/
public function execute(array $data): Activity
{
$this->validate($data);
/** @var Activity */
$activity = Activity::find($data['activity_id']);
$this->attach($data, $activity);
return $activity;
}
/**
* Create the association.
*
* @param array $data
* @param Activity $activity
* @return void
*/
private function attach(array $data, Activity $activity)
{
$attendees = [];
foreach ($data['contacts'] as $contact) {
$attendees[$contact] = ['account_id' => $activity->account_id];
}
// sync attendees: old contacts will be detached automatically
$changes = $activity->contacts()->sync($attendees);
foreach ($changes as $change) {
// detached, attached, and updated attendees
foreach ($change as $contactId) {
Contact::find($contactId)->calculateActivitiesStatistics();
}
}
}
}

View File

@@ -0,0 +1,127 @@
<?php
namespace App\Services\Account\Activity\Activity;
use App\Services\BaseService;
use App\Models\Contact\Contact;
use App\Models\Account\Activity;
use App\Models\Account\ActivityType;
use App\Models\Journal\JournalEntry;
use App\Models\Instance\Emotion\Emotion;
class CreateActivity extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'activity_type_id' => 'nullable|integer|exists:activity_types,id',
'summary' => 'required|string:255',
'description' => 'nullable|string:1000000',
'happened_at' => 'required|date|date_format:Y-m-d',
'emotions' => 'nullable|array',
'contacts' => 'required|array',
];
}
/**
* Validate all datas to execute the service.
*
* @param array $data
* @return bool
*/
public function validate(array $data): bool
{
parent::validate($data);
if (count($data['contacts']) > 0) {
foreach ($data['contacts'] as $contactId) {
Contact::where('account_id', $data['account_id'])
->findOrFail($contactId);
}
}
if (! empty($data['activity_type_id']) && $data['activity_type_id'] != '') {
ActivityType::where('account_id', $data['account_id'])
->findOrFail($data['activity_type_id']);
}
if (! empty($data['emotions']) && $data['emotions'] != '') {
foreach ($data['emotions'] as $emotionId) {
Emotion::findOrFail($emotionId);
}
}
return true;
}
/**
* Create an activity.
*
* @param array $data
* @return Activity
*/
public function execute(array $data): Activity
{
$this->validate($data);
$activity = $this->create($data);
// Log a journal entry
JournalEntry::add($activity);
// Now we associate the activity with each one of the attendees
app(AttachContactToActivity::class)->execute([
'account_id' => $data['account_id'],
'activity_id' => $activity->id,
'contacts' => $data['contacts'],
]);
return $activity;
}
/**
* Create the activity.
*
* @param array $data
* @return Activity
*/
private function create(array $data): Activity
{
$activity = Activity::create([
'account_id' => $data['account_id'],
'activity_type_id' => $this->nullOrValue($data, 'activity_type_id'),
'summary' => $data['summary'],
'description' => $this->nullOrValue($data, 'description'),
'happened_at' => $data['happened_at'],
]);
if (! empty($data['emotions']) && $data['emotions'] != '') {
$this->addEmotions($data['emotions'], $activity);
}
return $activity;
}
/**
* Add emotions to the activity.
*
* @param array $emotions
* @param Activity $activity
* @return void
*/
private function addEmotions(array $emotions, Activity $activity)
{
$emotionsSync = [];
foreach ($emotions as $emotion) {
$emotionsSync[$emotion] = ['account_id' => $activity->account_id];
}
$activity->emotions()->sync($emotionsSync);
}
}

View File

@@ -0,0 +1,57 @@
<?php
namespace App\Services\Account\Activity\Activity;
use App\Services\BaseService;
use App\Models\Account\Activity;
class DestroyActivity extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'activity_id' => 'required|integer|exists:activities,id',
];
}
/**
* Validate all datas to execute the service.
*
* @param array $data
* @return bool
*/
public function validate(array $data): bool
{
parent::validate($data);
Activity::where('account_id', $data['account_id'])
->findOrFail($data['activity_id']);
return true;
}
/**
* Destroy an activity.
*
* @param array $data
* @return bool
*/
public function execute(array $data): bool
{
$this->validate($data);
$activity = Activity::find($data['activity_id']);
$activity->deleteJournalEntry();
$activity->delete();
return true;
}
}

View File

@@ -0,0 +1,131 @@
<?php
namespace App\Services\Account\Activity\Activity;
use App\Services\BaseService;
use App\Models\Contact\Contact;
use App\Models\Account\Activity;
use App\Models\Account\ActivityType;
use App\Models\Journal\JournalEntry;
use App\Models\Instance\Emotion\Emotion;
class UpdateActivity extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'activity_id' => 'required|integer|exists:activities,id',
'activity_type_id' => 'nullable|integer|exists:activity_types,id',
'summary' => 'required|string:255',
'description' => 'nullable|string:1000000',
'happened_at' => 'required|date|date_format:Y-m-d',
'emotions' => 'nullable|array',
'contacts' => 'required|array',
];
}
/**
* Validate all datas to execute the service.
*
* @param array $data
* @return bool
*/
public function validate(array $data): bool
{
parent::validate($data);
Activity::where('account_id', $data['account_id'])
->findOrFail($data['activity_id']);
foreach ($data['contacts'] as $contactId) {
Contact::where('account_id', $data['account_id'])
->findOrFail($contactId);
}
if (! empty($data['activity_type_id']) && $data['activity_type_id'] != '') {
ActivityType::where('account_id', $data['account_id'])
->findOrFail($data['activity_type_id']);
}
if (! empty($data['emotions']) && $data['emotions'] != '') {
foreach ($data['emotions'] as $emotionId) {
Emotion::findOrFail($emotionId);
}
}
return true;
}
/**
* Update an activity.
*
* @param array $data
* @return Activity
*/
public function execute(array $data): Activity
{
$this->validate($data);
/** @var Activity */
$activity = Activity::find($data['activity_id']);
$this->update($data, $activity);
// Log a journal entry but need to delete the previous one first
$activity->deleteJournalEntry();
JournalEntry::add($activity);
// Now we update the activity with each one of the attendees
app(AttachContactToActivity::class)->execute([
'account_id' => $data['account_id'],
'activity_id' => $data['activity_id'],
'contacts' => $data['contacts'],
]);
return $activity->refresh();
}
/**
* Update the activity.
*
* @param array $data
* @param Activity $activity
* @return void
*/
private function update(array $data, Activity $activity)
{
$activity->update([
'activity_type_id' => $this->nullOrValue($data, 'activity_type_id'),
'summary' => $data['summary'],
'description' => $this->nullOrValue($data, 'description'),
'happened_at' => $data['happened_at'],
]);
if (! empty($data['emotions']) && $data['emotions'] != '') {
$this->updateEmotions($data['emotions'], $activity);
}
}
/**
* Update activity's emotions.
*
* @param array $emotions
* @param Activity $activity
* @return void
*/
private function updateEmotions(array $emotions, Activity $activity)
{
$emotionsSync = [];
foreach ($emotions as $emotion) {
$emotionsSync[$emotion] = ['account_id' => $activity->account_id];
}
$activity->emotions()->sync($emotionsSync);
}
}

View File

@@ -0,0 +1,125 @@
<?php
namespace App\Services\Account\Activity;
use Carbon\Carbon;
use App\Models\Contact\Contact;
use Illuminate\Support\Collection;
use App\Models\Account\ActivityType;
class ActivityStatisticService
{
/**
* Return the activities with the contact in a given timeframe.
*
* @param Contact $contact
* @param Carbon $startDate
* @param Carbon $endDate
* @return Collection
*/
public function activitiesWithContactInTimeRange(Contact $contact, Carbon $startDate, Carbon $endDate)
{
return $contact->activities()
->where('happened_at', '>=', $startDate)
->where('happened_at', '<=', $endDate)
->orderBy('happened_at', 'desc')
->get();
}
/**
* Get the list of number of activities per year in total done with
* the contact.
*
* @param Contact $contact
* @return \Illuminate\Database\Eloquent\Collection<array-key, \App\Models\Account\ActivityStatistic>
*/
public function activitiesPerYearWithContact(Contact $contact)
{
return $contact->activityStatistics()->get();
}
/**
* Get the list of activities per month for a given year.
*
* @param Contact $contact
* @param int $year
* @return Collection
*/
public function activitiesPerMonthForYear(Contact $contact, int $year)
{
$startDate = Carbon::create($year, 1, 1, 0, 0, 0);
$endDate = Carbon::create($year, 12, 31);
$activities = $this->activitiesWithContactInTimeRange($contact, $startDate, $endDate);
$activitiesPerMonth = collect([]);
for ($month = 1; $month < 13; $month++) {
$activitiesInMonth = collect([]);
foreach ($activities as $activity) {
if ($activity->happened_at->month === $month) {
$activitiesInMonth->push($activity);
}
}
$activitiesPerMonth->push([
'month' => $month,
'occurences' => $activitiesInMonth->count(),
'activities' => $activitiesInMonth,
]);
}
$maxActivitiesInAMonth = $activitiesPerMonth->max('occurences');
$activitiesPerMonth->transform(function ($activity) use ($maxActivitiesInAMonth) {
if ($activity['occurences'] != 0) {
$activity['percent'] = ($activity['occurences'] * 100 / $maxActivitiesInAMonth);
} else {
$activity['percent'] = 0;
}
return $activity;
});
return $activitiesPerMonth;
}
/**
* Get the list of unique activity types for activities done with
* a contact in a given timeframe, along with the number of occurences.
*
* @param Contact $contact
* @param Carbon $startDate
* @param Carbon $endDate
* @return Collection
*/
public function uniqueActivityTypesInTimeRange(Contact $contact, Carbon $startDate, Carbon $endDate)
{
$activities = $this->activitiesWithContactInTimeRange($contact, $startDate, $endDate);
// group activities by activity type id
$grouped = $activities->groupBy(function ($item, $key) {
return $item['activity_type_id'];
});
// remove activity type id that are null
$grouped = $grouped->reject(function ($value, $key) {
return $key == '';
});
// calculate how many occurences of unique activity type id
$activities = $grouped->map(function ($item) {
return collect($item)->count();
});
$activityTypes = collect([]);
foreach ($activities as $key => $value) {
$activityTypes->push([
'object' => ActivityType::find($key),
'occurences' => $value,
]);
}
return $activityTypes;
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace App\Services\Account\Activity\ActivityType;
use App\Services\BaseService;
use App\Models\Account\ActivityType;
use App\Models\Account\ActivityTypeCategory;
class CreateActivityType extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'activity_type_category_id' => 'required|integer|exists:activity_type_categories,id',
'name' => 'nullable|string|max:255',
'translation_key' => 'nullable|string|max:255',
];
}
/**
* Create an activity type.
*
* @param array $data
* @return ActivityType
*/
public function execute(array $data): ActivityType
{
$this->validate($data);
ActivityTypeCategory::where('account_id', $data['account_id'])
->findOrFail($data['activity_type_category_id']);
$activityType = ActivityType::create([
'account_id' => $data['account_id'],
'activity_type_category_id' => $data['activity_type_category_id'],
'name' => $this->nullOrValue($data, 'name'),
'translation_key' => $this->nullOrValue($data, 'translation_key'),
]);
return ActivityType::find($activityType->id);
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Services\Account\Activity\ActivityType;
use App\Services\BaseService;
use App\Models\Account\ActivityType;
class DestroyActivityType extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'activity_type_id' => 'required|integer|exists:activity_types,id',
];
}
/**
* Destroy a activity type.
*
* @param array $data
* @return bool
*/
public function execute(array $data): bool
{
$this->validate($data);
$activityType = ActivityType::where('account_id', $data['account_id'])
->findOrFail($data['activity_type_id']);
$activityType->delete();
return true;
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace App\Services\Account\Activity\ActivityType;
use App\Services\BaseService;
use App\Models\Account\ActivityType;
use App\Models\Account\ActivityTypeCategory;
class UpdateActivityType extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'activity_type_category_id' => 'required|integer|exists:activity_type_categories,id',
'activity_type_id' => 'required|integer|exists:activity_types,id',
'name' => 'nullable|string|max:255',
'translation_key' => 'nullable|string|max:255',
];
}
/**
* Update an activity type.
*
* @param array $data
* @return ActivityType
*/
public function execute(array $data): ActivityType
{
$this->validate($data);
ActivityTypeCategory::where('account_id', $data['account_id'])
->findOrFail($data['activity_type_category_id']);
/** @var ActivityType */
$activityType = ActivityType::where('account_id', $data['account_id'])
->findOrFail($data['activity_type_id']);
$activityType->update([
'activity_type_category_id' => $data['activity_type_category_id'],
'name' => $this->nullOrValue($data, 'name'),
'translation_key' => $this->nullOrValue($data, 'translation_key'),
]);
return $activityType;
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace App\Services\Account\Activity\ActivityTypeCategory;
use App\Services\BaseService;
use App\Models\Account\ActivityTypeCategory;
class CreateActivityTypeCategory extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'name' => 'nullable|string|max:255',
'translation_key' => 'nullable|string|max:255',
];
}
/**
* Create an activity type category.
*
* @param array $data
* @return ActivityTypeCategory
*/
public function execute(array $data): ActivityTypeCategory
{
$this->validate($data);
$activityTypeCategory = ActivityTypeCategory::create([
'account_id' => $data['account_id'],
'name' => $this->nullOrValue($data, 'name'),
'translation_key' => $this->nullOrValue($data, 'translation_key'),
]);
return ActivityTypeCategory::find($activityTypeCategory->id);
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Services\Account\Activity\ActivityTypeCategory;
use App\Services\BaseService;
use App\Models\Account\ActivityTypeCategory;
class DestroyActivityTypeCategory extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'activity_type_category_id' => 'required|integer|exists:activity_type_categories,id',
];
}
/**
* Destroy a activity type category.
*
* @param array $data
* @return bool
*/
public function execute(array $data): bool
{
$this->validate($data);
$activityTypeCategory = ActivityTypeCategory::where('account_id', $data['account_id'])
->findOrFail($data['activity_type_category_id']);
$activityTypeCategory->delete();
return true;
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace App\Services\Account\Activity\ActivityTypeCategory;
use App\Services\BaseService;
use App\Models\Account\ActivityTypeCategory;
class UpdateActivityTypeCategory extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'activity_type_category_id' => 'required|integer|exists:activity_type_categories,id',
'name' => 'nullable|string|max:255',
'translation_key' => 'nullable|string|max:255',
];
}
/**
* Update an activity type category.
*
* @param array $data
* @return ActivityTypeCategory
*/
public function execute(array $data): ActivityTypeCategory
{
$this->validate($data);
/** @var ActivityTypeCategory */
$activityTypeCategory = ActivityTypeCategory::where('account_id', $data['account_id'])
->findOrFail($data['activity_type_category_id']);
$activityTypeCategory->update([
'name' => $this->nullOrValue($data, 'name'),
'translation_key' => $this->nullOrValue($data, 'translation_key'),
]);
return $activityTypeCategory;
}
}