Skip to content

Commit

Permalink
Merge pull request #261 from jesusantguerrero/release/beta-8
Browse files Browse the repository at this point in the history
Release/beta 8
  • Loading branch information
jesusantguerrero authored Sep 5, 2023
2 parents 00219e7 + ee8d41e commit 5fcddc0
Show file tree
Hide file tree
Showing 90 changed files with 1,747 additions and 734 deletions.
4 changes: 2 additions & 2 deletions app/Console/Commands/AutomationCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace App\Console\Commands;

use App\Domains\Integration\Models\Automation;
use App\Domains\Integration\Services\LogerAutomationService;
use App\Domains\Automation\Models\Automation;
use App\Domains\Automation\Services\LogerAutomationService;
use Illuminate\Console\Command;

class AutomationCheck extends Command
Expand Down
2 changes: 1 addition & 1 deletion app/Domains/AppCore/Models/Planner.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Domains\AppCore\Models;

use App\Concerns\SupportsDateFrame;
use App\Domains\Integration\Services\LogerAutomationService;
use App\Domains\Automation\Services\LogerAutomationService;
use App\Events\AutomationEvent;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand Down
28 changes: 28 additions & 0 deletions app/Domains/Automation/Concerns/AutomationActionContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Domains\Automation\Concerns;

use App\Domains\Automation\Models\Automation;
use App\Domains\Automation\Models\AutomationTaskAction;

interface AutomationActionContract
{
/**
* Validate and create a new team for the given user.
*
* @param Automation $automation
* @param Google_Calendar_Events $calendarEvents
* @return void
*/
public static function handle(
Automation $automation,
mixed $payload,
AutomationTaskAction $task,
AutomationTaskAction $previousTask,
AutomationTaskAction $trigger
);

public function getName(): string;
public function getDescription(): string;
public function label(): string;
}
16 changes: 16 additions & 0 deletions app/Domains/Automation/Data/AutomationData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace App\Domains\Automation\Data;

use Spatie\LaravelData\Data;

class AutomationData extends Data {

public function __construct(
public int $team_id,
public int $user_id,
public int $service_id,
public int $integration_id
) {

}
}
8 changes: 8 additions & 0 deletions app/Domains/Automation/Enums/AutomationTaskType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace App\Domains\Automation\Enums;

enum AutomationTaskType: string {
const ACTION = 'action';
const TRIGGER = 'trigger';
const COMPONENT = 'component';
}
43 changes: 43 additions & 0 deletions app/Domains/Automation/Helpers/AutomationBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
namespace App\Domains\Automation\Helpers;

use App\Domains\Automation\Concerns\AutomationActionContract;
use App\Domains\Automation\Data\AutomationData;
use App\Domains\Automation\Enums\AutomationTaskType;

class AutomationBuilder {
private array $tasks = [];

public function __construct(public AutomationData $data) {}

public static function make(AutomationData $data): self
{
return new self($data);
}

public function addAction(AutomationActionContract $action, string $type, mixed $config): AutomationBuilder {
$this->tasks[] = [
"name" => $action->getName(),
"entity" => get_class($action),
"action" => $action,
"order" => count($this->tasks),
"task_type" => $type,
"values" => $config['values'] ?? []
];

return $this;
}

public function getSchema() {
return [
... $this->data->toArray(),
"description" => implode(" ", array_map(fn ($task) => $task['action']?->label(), $this->tasks)) ,
"integration_id" => $this->data->integration_id,
"service_id" => $this->data->service_id,
"sentence" => implode(" ", array_map(fn ($task) => $task['action']?->label(), $this->tasks)),
"name" => implode(" ", array_map(fn ($task) => $task['action']?->getName(), $this->tasks)),
"sentence" => "",
"tasks" => $this->tasks
];
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace App\Http\Controllers\Api;
namespace App\Domains\Automation\Http\Controllers;

use App\Domains\Integration\Models\Automation;
use App\Jobs\ProcessGmail;
use App\Libraries\GoogleService;
use App\Domains\Automation\Models\Automation;
use App\Domains\Integration\Services\GoogleService;
use Freesgen\Atmosphere\Http\InertiaController;

class AutomationController extends BaseController
class AutomationController extends InertiaController
{
public function __construct()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Domains\Automation\Http\Controllers;

use App\Domains\Automation\Models\AutomationRecipe;
use App\Http\Controllers\Controller;



class AutomationRecipeController extends Controller
{
public function index() {
return AutomationRecipe::all();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Domains\Automation\Http\Controllers;

use App\Domains\Automation\Models\AutomationService;
use App\Http\Controllers\Controller;

class AutomationServiceController extends Controller
{
public function index() {
return AutomationService::all();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
<?php

namespace App\Domains\Integration\Models;
namespace App\Domains\Automation\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Automation extends Model
{
use HasFactory;
protected $fillable = ['team_id', 'user_id', 'trigger_id', 'integration_id', 'name', 'description', 'entity', 'sentence', 'track'];
protected $fillable = [
'team_id',
'user_id',
"automatable_id",
"automatable_type",
'trigger_id',
'integration_id',
'name',
'description',
'entity',
'sentence',
'track'
];
public function recipe() {
return $this->belongsTo('App\Models\Daily\AutomationRecipe', 'automation_recipe_id', 'id');
}
Expand All @@ -25,15 +37,20 @@ public function saveTasks($tasks) {
AutomationTaskAction::query()->where('automation_id', $this->id)->delete();
foreach ($tasks as $index => $task) {
if (!$task['name']) continue;
$taskSource = AutomationTask::find($task['automation_task_id']);
if (empty($task['entity'])) {
$taskSource = AutomationTask::find($task['automation_task_id']);
$task['entity'] = $taskSource->entity;
$task['task_type'] = $taskSource->task_type;
}

$this->tasks()->create([
"team_id" => $this->team_id,
"user_id" => $this->user_id,
"entity" => $taskSource->entity,
"task_type" => $taskSource->task_type,
"entity" => $task['entity'],
"task_type" => $task['task_type'],
'order' => $index,
"automation_id" => $this->id,
"automation_task_id" => $task['automation_task_id'],
"automation_task_id" => $task['automation_task_id'] ?? null,
"name" => $task['name'],
"values" => json_encode($task['values']),
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Domains\Integration\Models;
namespace App\Domains\Automation\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Domains\Integration\Models;
namespace App\Domains\Automation\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Domains\Integration\Models;
namespace App\Domains\Automation\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Domains\Integration\Models;
namespace App\Domains\Automation\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
<?php

namespace App\Domains\Integration\Services;
namespace App\Domains\Automation\Services;

use App\Domains\Integration\Models\Automation;
use App\Domains\Automation\Models\Automation;
use App\Domains\Automation\Models\AutomationTask;
use App\Domains\Integration\Actions\BHD;
use App\Domains\Integration\Actions\BHDAlert;
use App\Domains\Integration\Actions\BHDNotification;
use App\Domains\Integration\Actions\Entry;
use App\Domains\Integration\Actions\Gmail;
use App\Domains\Integration\Actions\MealPlanAutomation;
use App\Domains\Integration\Actions\OccurrenceCheckAutomation;
use App\Domains\Integration\Models\AutomationTask;
use App\Domains\Transaction\Services\BHDService;
use Illuminate\Support\Facades\DB;
use App\Domains\Integration\Actions\TransactionCreateEntry;

class LogerAutomationService {

Expand All @@ -30,8 +26,7 @@ public static function run(Automation $automation, $eventData = null) {
$previousTask = null;
}
$entity = $task->entity;
$action = $task->name;
$lastData = $entity::$action($automation, $lastData, $task, $previousTask, $trigger);
$lastData = $entity::handle($automation, $lastData, $task, $previousTask, $trigger);
if (!$lastData) {
break;
}
Expand Down Expand Up @@ -145,20 +140,12 @@ public static function services() {
'components' => [
[
'label' => 'Parse Alert',
'name' => 'parseAlert',
'entity' => BHDAlert::class,
'name' => 'parseMessage',
'entity' => BHD::class,
'description' => 'Parse an email alert',
'config' => json_encode(BHDAlert::getSchema()),

],
[
'label' => 'Parse Notification',
'name' => 'parseNotification',
'entity' => BHDNotification::class,
'description' => 'Parse an email alert',
'config' => json_encode(BHDNotification::getSchema()),

]
],
"type" => "internal"

Expand All @@ -167,9 +154,9 @@ public static function services() {
'name' => 'transactions',
'label' => 'Transactions',
'logo' => '/images/transactions.png',
'entity' => Entry::class,
'entity' => TransactionCreateEntry::class,
'description' => 'BHD bank',
'fields' => json_encode(Entry::fieldConfig()),
'fields' => json_encode(TransactionCreateEntry::fieldConfig()),
"triggers" => [
[
'name' => 'transactionCreated',
Expand All @@ -180,7 +167,7 @@ public static function services() {
'field' => [
'title' => 'field',
'type' => 'select',
'options' => [array_keys(Entry::fieldConfig())]
'options' => [array_keys(TransactionCreateEntry::fieldConfig())]
],
'conditionType' => [
'title' => 'Condition',
Expand All @@ -204,9 +191,9 @@ public static function services() {
[
'name' => 'createTransaction',
'label' => 'Create transaction',
'entity' => Entry::class,
'entity' => TransactionCreateEntry::class,
'description' => 'Create a new transaction',
'config' => json_encode(Entry::fieldConfig()),
'config' => json_encode(TransactionCreateEntry::fieldConfig()),
"accepts_config" => true,
],
],
Expand Down
22 changes: 22 additions & 0 deletions app/Domains/Automation/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use App\Domains\Automation\Http\Controllers\AutomationController;
use App\Domains\Automation\Http\Controllers\AutomationRecipeController;
use App\Domains\Automation\Http\Controllers\AutomationServiceController;
use Illuminate\Support\Facades\Route;

/**************************************************************************************
* API Section
***************************************************************************************/


Route::middleware(['auth:sanctum', 'atmosphere.teamed', 'verified'])->prefix('/api')->name('api.')->group(function () {
Route::controller(AutomationController::class)->group(function () {
Route::apiResource('automation', AutomationController::class);
Route::post('/automation/{id}/run', 'run');
Route::post('/automation/run-all', 'runAll');
});

Route::apiResource('/automation-services', AutomationServiceController::class);
Route::apiResource('/automation-recipes', AutomationRecipeController::class);
});
Loading

0 comments on commit 5fcddc0

Please sign in to comment.