Skip to content

Commit

Permalink
Merge pull request #926 from bakaphp/channels-leads
Browse files Browse the repository at this point in the history
refactor: lead social channel
  • Loading branch information
FredPeal committed Feb 13, 2024
2 parents c1b4134 + b3dfec1 commit 2495093
Show file tree
Hide file tree
Showing 14 changed files with 242 additions and 3 deletions.
15 changes: 15 additions & 0 deletions app/GraphQL/Social/Builders/Messages/MessageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,19 @@ public function getAll(
*/
return Message::fromApp();
}

public function getChannelMessages(
mixed $root,
array $args,
GraphQLContext $context,
ResolveInfo $resolveInfo
): Builder {
return Message::fromApp()->whereHas('channels', function ($query) use ($args) {
$query->where('channels.uuid', $args['channel_uuid']);
})
->when(! auth()->user()->isAdmin(), function ($query) {
$query->where('companies_id', auth()->user()->currentCompanyId());
})
->select('messages.*');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Kanvas\Social\Channels\Repositories\ChannelRepository;
use Kanvas\SystemModules\Repositories\SystemModulesRepository;
use Kanvas\Users\Models\Users;
use Baka\Support\Str;

class ChannelsManagementMutation
{
Expand All @@ -26,6 +27,7 @@ public function createChannel(mixed $rootValue, array $request): Channel
description: $request['input']['description'],
entity_id: $request['input']['entity_id'],
entity_namespace: $systemModule->uuid,
slug: $request['input']['slug'] ?? Str::slug($request['input']['name'])
);

$createChannel = new CreateChannelAction($channelDto);
Expand Down
27 changes: 27 additions & 0 deletions database/migrations/Social/2024_02_12_172534_uuid_channel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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::table('channels', function (Blueprint $table) {
$table->uuid('uuid')->after('id');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('channels', function (Blueprint $table) {
$table->dropColumn('uuid');
});
}
};
19 changes: 19 additions & 0 deletions database/seeders/SystemModuleSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Kanvas\Guild\Leads\Models\Lead;

class SystemModuleSeeder extends Seeder
{
Expand Down Expand Up @@ -195,6 +196,24 @@ public function run()
'mobile_component_type' => null,
'mobile_navigation_type' => null,
'mobile_tab_index' => '0',
],
[
'name' => 'Leads',
'slug' => 'leads',
'model_name' => Lead::class,
'uuid' => (string) Str::uuid(),
'apps_id' => '1',
'parents_id' => '0',
'menu_order' => null,
'show' => '0',
'use_elastic' => '0',
'browse_fields' => '[{"name":"name","title":"Name","sortField":"name","filterable":true,"searchable":true},{"name":"email","title":"Email","sortField":"email","filterable":true,"searchable":true},{"name":"phone","title":"Phone","sortField":"phone","filterable":true,"searchable":true},{"name":"status","title":"Status","sortField":"status","filterable":true,"searchable":true},{"name":"created_at","title":"Created At","sortField":"created_at","filterable":true,"searchable":true}]',
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => null,
'is_deleted' => '0',
'mobile_component_type' => null,
'mobile_navigation_type' => null,
'mobile_tab_index' => '0',
]
);
}
Expand Down
19 changes: 17 additions & 2 deletions graphql/schemas/Guild/lead.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ type Lead {
pipeline: LeadPipeline @belongsTo
stage: LeadPipelineStage @belongsTo(relation: "stage")
participants: [LeadsParticipants!]! @hasMany
channels: [SocialChannel]! @hasMany
files: [Filesystem!]!
@paginate(
defaultCount: 25
builder: "App\\GraphQL\\Ecosystem\\Queries\\Filesystem\\FilesystemQuery@getFileByGraphType"
)
followers: [User!]!
followers: [User!]!
@paginate(
defaultCount: 25
builder: "App\\GraphQL\\Social\\Builders\\Follows\\FollowBuilder@getEntityFollowers"
Expand Down Expand Up @@ -180,7 +181,21 @@ extend type Query @guard {
"status"
]
)
orderBy: _ @orderBy(columns: ["id", "created_at", "updated_at", "title", "status", "pipeline_stage_id", "firstname", "lastname", "email", "phone"])
orderBy: _
@orderBy(
columns: [
"id"
"created_at"
"updated_at"
"title"
"status"
"pipeline_stage_id"
"firstname"
"lastname"
"email"
"phone"
]
)
): [Lead!]!
@paginate(
model: "Kanvas\\Guild\\Leads\\Models\\Lead"
Expand Down
1 change: 1 addition & 0 deletions graphql/schemas/Social/channels.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extend type User {
type SocialChannel {
id: ID!
name: String!
uuid: String
slug: String
description: String!
entity_namespace: String!
Expand Down
26 changes: 26 additions & 0 deletions graphql/schemas/Social/message.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,30 @@ extend type Query @guard {
builder: "App\\GraphQL\\Social\\Builders\\Messages\\MessageBuilder@getAll"
scopes: ["fromApp"]
)

channelMessages(
channel_uuid: String!
where: _
@whereConditions(
columns: [
"id"
"parent_id"
"parent_unique_id"
"companies_id"
"uuid"
"message_types_id"
"message"
"reactions_count"
"comments_count"
"total_liked"
"total_saved"
"total_shared"
]
)
): [Message!]!
@paginate(
defaultCount: 25
builder: "App\\GraphQL\\Social\\Builders\\Messages\\MessageBuilder@getChannelMessages"
scopes: ["fromApp"]
)
}
7 changes: 7 additions & 0 deletions src/Domains/Guild/Leads/Models/Lead.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Baka\Users\Contracts\UserInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Kanvas\Apps\Models\Apps;
use Kanvas\Guild\Customers\Models\People;
Expand All @@ -19,6 +20,7 @@
use Kanvas\Guild\Organizations\Models\Organization;
use Kanvas\Guild\Pipelines\Models\Pipeline;
use Kanvas\Guild\Pipelines\Models\PipelineStage;
use Kanvas\Social\Channels\Models\Channel;
use Kanvas\Social\Follows\Traits\FollowersTrait;
use Kanvas\Users\Models\Users;
use Kanvas\Workflow\Traits\CanUseWorkflow;
Expand Down Expand Up @@ -105,6 +107,11 @@ public function owner(): BelongsTo
return $this->belongsTo(Users::class, 'leads_owner_id', 'id');
}

public function socialChannels(): HasMany
{
return $this->hasMany(Channel::class, 'entity_id', 'id')->where('entity_namespace', self::class);
}

public function receiver(): BelongsTo
{
return $this->belongsTo(LeadReceiver::class, 'leads_receivers_id', 'id');
Expand Down
19 changes: 19 additions & 0 deletions src/Domains/Guild/Leads/Observers/LeadObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
namespace Kanvas\Guild\Leads\Observers;

use Baka\Support\Str;
use Kanvas\Apps\Models\Apps;
use Kanvas\Guild\Customers\Repositories\PeoplesRepository;
use Kanvas\Guild\Leads\Models\Lead;
use Kanvas\Guild\Leads\Models\LeadReceiver;
use Kanvas\Guild\Leads\Models\LeadStatus;
use Kanvas\Guild\Pipelines\Models\Pipeline;
use Kanvas\Social\Channels\Actions\CreateChannelAction;
use Kanvas\Social\Channels\DataTransferObject\Channel;
use Kanvas\Workflow\Enums\WorkflowEnum;

class LeadObserver
Expand Down Expand Up @@ -64,6 +67,22 @@ public function creating(Lead $lead): void
public function created(Lead $lead): void
{
$lead->fireWorkflow(WorkflowEnum::CREATED->value);
if ($lead->user) {
(
new CreateChannelAction(
new Channel(
app(Apps::class),
$lead->company,
$lead->user,
(string)$lead->id,
Lead::class,
'Default Channel',
$lead->description ?? '',
$lead->uuid->toString()
)
)
)->execute();
}
}

public function updated(Lead $lead): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function execute(): Channel
'apps_id' => $this->channelDto->apps->id,
'companies_id' => $this->channelDto->companies->id,
'name' => $this->channelDto->name,
'slug' => Str::slug($this->channelDto->name),
'slug' => $this->channelDto->slug ?? Str::slug($this->channelDto->name),
'description' => $this->channelDto->description,
'entity_id' => $this->channelDto->entity_id,
'entity_namespace' => $this->channelDto->entity_namespace,
Expand Down
1 change: 1 addition & 0 deletions src/Domains/Social/Channels/DataTransferObject/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __construct(
public string $entity_namespace,
public string $name = '',
public string $description = '',
public ?string $slug = null
) {
}
}
3 changes: 3 additions & 0 deletions src/Domains/Social/Channels/Models/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Kanvas\Social\Channels\Models;

use Baka\Traits\UuidTrait;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Kanvas\Social\Messages\Models\Message;
Expand All @@ -24,6 +25,8 @@
*/
class Channel extends BaseModel
{
use UuidTrait;

protected $table = 'channels';

protected $guarded = [];
Expand Down
9 changes: 9 additions & 0 deletions src/Domains/Social/Messages/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Kanvas\Apps\Models\Apps;
use Kanvas\Social\Channels\Models\Channel;
use Kanvas\Social\Messages\Factories\MessageFactory;
use Kanvas\Social\MessagesComments\Models\MessageComment;
use Kanvas\Social\MessagesTypes\Models\MessageType;
Expand Down Expand Up @@ -85,6 +86,14 @@ public function topics(): BelongsToMany
->where('entity_namespace', self::class);
}

/**
* The roles that belong to the Message
*/
public function channels(): BelongsToMany
{
return $this->belongsToMany(Channel::class, 'channel_messages', 'messages_id', 'channel_id');
}

/**
* messageType
*/
Expand Down
Loading

0 comments on commit 2495093

Please sign in to comment.