Skip to content

Commit

Permalink
Merge pull request #1473 from bakaphp/feat-task-list
Browse files Browse the repository at this point in the history
feat: add lead engagement task
  • Loading branch information
kaioken authored Jun 8, 2024
2 parents 1e04439 + 436f8f5 commit 5b0abd0
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace App\GraphQL\ActionEngine\Builders\Engagements;

use GraphQL\Type\Definition\ResolveInfo;
use Illuminate\Database\Eloquent\Builder;
use Kanvas\ActionEngine\Tasks\Models\TaskListItem;
use Kanvas\Apps\Models\Apps;
use Kanvas\Guild\Leads\Models\Lead;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;

class TaskEngagementBuilder
{
public function getLeadTaskItems(
mixed $root,
array $args,
GraphQLContext $context,
ResolveInfo $resolveInfo
): Builder {
$company = auth()->user()->getCurrentCompany();
$user = auth()->user();
$app = app(Apps::class);

$lead = Lead::getByIdFromCompanyApp($args['lead_id'], $company, $app);
$leadId = $lead->getId();

return TaskListItem::leftJoin('company_task_engagement_items', function ($join) use ($lead) {
$join->on('company_task_list_items.id', '=', 'company_task_engagement_items.task_list_item_id')
->where('company_task_engagement_items.lead_id', '=', $lead->getId());
})
->select(
'company_task_list_items.*',
'company_task_engagement_items.lead_id',
'company_task_engagement_items.status',
'company_task_engagement_items.engagement_start_id',
'company_task_engagement_items.engagement_end_id',
'company_task_engagement_items.created_at',
'company_task_engagement_items.updated_at'
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('company_task_engagement_items', function (Blueprint $table) {
$table->bigInteger('task_list_item_id')->index();
$table->bigInteger('lead_id')->index();
$table->bigInteger('companies_id')->index();
$table->bigInteger('apps_id')->index();
$table->bigInteger('users_id')->index();
$table->enum('status', ['pending', 'in_progress', 'completed'])->default('pending')->comment('pending, in_progress, completed')->index();
$table->bigInteger('engagement_start_id')->index();
$table->bigInteger('engagement_end_id')->index();
$table->json('config');
$table->timestamps();
$table->tinyInteger('is_deleted')->default(0)->index();

$table->primary(['task_list_item_id', 'lead_id']);
$table->index(['task_list_item_id', 'lead_id', 'companies_id'], 'task_list_company_index');
$table->index(['task_list_item_id', 'lead_id', 'apps_id'], 'task_list_apps_index');
$table->index(['task_list_item_id', 'lead_id', 'users_id'], 'task_list_users_index');
$table->index(['task_list_item_id', 'lead_id', 'engagement_start_id'], 'task_list_engagement_start_index');
$table->index(['task_list_item_id', 'lead_id', 'engagement_end_id'], 'task_list_engagement_end_index');
$table->index(['task_list_item_id', 'lead_id', 'apps_id', 'companies_id'], 'task_list_apps_company_index');
$table->index(['task_list_item_id', 'lead_id', 'status'], 'task_list_status_index');
$table->index(['task_list_item_id', 'lead_id', 'is_deleted'], 'task_list_deleted_index');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('company_task_engagement_items');
}
};
11 changes: 11 additions & 0 deletions graphql/schemas/ActionEngine/engagement.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type Engagement {
id: ID!
uuid: String!
user: User! @belongsTo
company_action: CompanyAction! @belongsTo
message: Message! @belongsTo
lead: Lead! @belongsTo
people: People @belongsTo
entity_uuid: String!
slug: String!
}
16 changes: 13 additions & 3 deletions graphql/schemas/ActionEngine/task.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,21 @@ type TaskListItem {
weight: Float!
}

type LeadTaskEngagementItem {
name: String!
action: CompanyAction! @belongsTo
status: String
config: Mixed
engagement_start: Engagement @hasOne(relation: "engagementStart")
engagement_end: Engagement @hasOne(relation: "engagementEnd")
created_at: DateTime!
updated_at: DateTime!
}

extend type Query @guard {
leadTaskList (lead_id: ID!): [TaskList!]!
leadTaskItems(lead_id: ID!): [LeadTaskEngagementItem!]!
@paginate(
model: "Kanvas\\ActionEngine\\Tasks\\Models\\TaskList"
scopes: ["fromApp", "fromCompany"]
builder: "App\\GraphQL\\ActionEngine\\Builders\\Engagements\\TaskEngagementBuilder@getLeadTaskItems"
defaultCount: 25
)
}
60 changes: 60 additions & 0 deletions src/Domains/ActionEngine/Tasks/Models/TaskEngagementItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Kanvas\ActionEngine\Tasks\Models;

use Baka\Casts\Json;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Kanvas\ActionEngine\Engagements\Models\Engagement;
use Kanvas\ActionEngine\Models\BaseModel;
use Kanvas\Guild\Leads\Models\Lead;

/**
* Class TaskEngagementItem.
*
* @property int $task_list_item_id
* @property int $lead_id
* @property int $companies_id
* @property int $apps_id
* @property string $status
* @property int $engagement_start_id
* @property int $engagement_end_id
* @property string $config
*/
class TaskEngagementItem extends BaseModel
{
protected $table = 'company_task_engagement_items';
protected $guarded = [];

protected $casts = [
'config' => Json::class,
];

public function item(): BelongsTo
{
return $this->belongsTo(TaskListItem::class, 'task_list_item_id');
}

public function lead(): BelongsTo
{
return $this->belongsTo(Lead::class, 'lead_id');
}

/**
* temp relationship to engagement will only work on LeadTaskEngagementItem
*/
public function engagementStart(): HasOne
{
return $this->hasOne(Engagement::class, 'id', 'engagement_start_id');
}

/**
* temp relationship to engagement will only work on LeadTaskEngagementItem
*/
public function engagementEnd(): HasOne
{
return $this->hasOne(Engagement::class, 'id', 'engagement_end_id');
}
}
7 changes: 7 additions & 0 deletions src/Domains/ActionEngine/Tasks/Models/TaskListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Baka\Casts\Json;
use Baka\Traits\UuidTrait;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Kanvas\ActionEngine\Actions\Models\CompanyAction;
use Kanvas\ActionEngine\Engagements\Models\Engagement;
use Kanvas\ActionEngine\Models\BaseModel;

/**
Expand Down Expand Up @@ -41,4 +43,9 @@ public function action(): BelongsTo
{
return $this->belongsTo(CompanyAction::class, 'companies_action_id');
}

public function engagementStart(): HasOne
{
return $this->hasOne(Engagement::class, 'id', 'engagement_start_id');
}
}
95 changes: 95 additions & 0 deletions tests/GraphQL/ActionEngine/CompanyTaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

declare(strict_types=1);

namespace Tests\GraphQL\ActionEngine;

use Tests\TestCase;

class CompanyTaskTest extends TestCase
{
protected function createLeadAndGetResponse(array $input = [])
{
$user = auth()->user();
$branch = $user->getCurrentBranch();
$title = fake()->title();

if (empty($input)) {
$input = [
'branch_id' => $branch->getId(),
'title' => $title,
'pipeline_stage_id' => 0,
'people' => [
'firstname' => fake()->firstName(),
'lastname' => fake()->lastName(),
'contacts' => [
[
'value' => fake()->email(),
'contacts_types_id' => 1,
'weight' => 0,
],
],
'address' => [
[
'address' => fake()->address(),
'city' => fake()->city(),
'state' => fake()->state(),
'country' => fake()->country(),
'zip' => fake()->postcode(),
],
],
],
'custom_fields' => [],
'files' => [
[
'url' => 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf',
'name' => 'dummy.pdf',
],
],
];
}

return $this->graphQL('
mutation($input: LeadInput!) {
createLead(input: $input) {
id
uuid
people {
id
},
systemModule{
id
}
}
}
', [
'input' => $input,
])->json();
}

public function testGetLeadTaskEngagement()
{
$lead = $this->createLeadAndGetResponse();
$leadId = $lead['data']['createLead']['id'];

$this->graphQL('
query leadTasks($lead_id: ID!) {
leadTaskItems(lead_id: $lead_id) {
data {
name
action {
name
}
status
config
engagement_start {
uuid
}
}
}
}
', [
'lead_id' => $leadId, // Passing the lead ID to the GraphQL query
])->assertOk();
}
}

0 comments on commit 5b0abd0

Please sign in to comment.