Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MK-3137-premium-prompts-workflow #2608

Draft
wants to merge 14 commits into
base: development
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function handle(): void
]);

//now rules worklfow actions
info('Now lets setup the workflow actions');
info('Now lets setup the workflow actions. Please use the space bar to select.');

$actions = multiselect(
label: 'What actions should be assigned?',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use Kanvas\Social\Messages\Jobs\CreateMessageFromReceiverJob;
use Kanvas\Social\Messages\Workflows\Activities\GenerateMessageTagsActivity;
use Kanvas\Users\Workflows\Activities\AssignToDefaultCompanyActivity;
use Kanvas\Social\Messages\Workflows\Activities\SetForReviewActivity;
use Kanvas\Workflow\Rules\Models\Action;

class KanvasWorkflowSynActionCommand extends Command
Expand Down Expand Up @@ -99,6 +100,7 @@ public function handle(): void
SyncOrderWithIPlusActivities::class,
SyncPeopleWithIPlusActivities::class,
SyncZohoAgentFromReceiverJob::class,
SetForReviewActivity::class,
];

$createdActions = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
use Kanvas\Social\MessagesTypes\DataTransferObject\MessageTypeInput;
use Kanvas\Social\MessagesTypes\Repositories\MessagesTypesRepository;
use Kanvas\SystemModules\Models\SystemModules;
use Kanvas\Social\Messages\Actions\SetForReviewAction;
use Kanvas\Notifications\Jobs\SendEmailToUserJob;
use Kanvas\Users\Models\Users;

class MessageManagementMutation
{
Expand Down Expand Up @@ -80,6 +83,11 @@ public function create(mixed $root, array $request): Message
);
$message = $action->execute();

// $setMessageForReview = (new SetForReviewAction($message))->execute();

// print_r($setMessageForReview);
// die();

if (! key_exists('distribution', $messageData)) {
return $message;
}
Expand Down Expand Up @@ -206,4 +214,34 @@ public function recoverMessage(mixed $root, array $request): Message

return $message;
}

public function reviewPendingMessage(mixed $root, array $request): Message
{
$request = $request['input'];
$message = Message::getById((int)$request['message_id'], app(Apps::class));
if (! $request['is_reviewed']) {
SendEmailToUserJob::dispatch(
$message->user,
"Your post has been declined",
[
"body" => "Your post has been declined for the following reasons: {$request['declined_reason']}"
]
);

return $message;
}

// $message->setUnlock();
$message->setPublic();

SendEmailToUserJob::dispatch(
$message->user,
"Your post has been approved",
[
"body" => "Your post {$message->message['title']} has been approved!"
]
);

return $message;
}
}
10 changes: 10 additions & 0 deletions graphql/schemas/Social/message.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ input DistributionInput {
followers: [ID!]!
}

input ReviewPendingMessageInput {
message_id: Int!
is_reviewed: Int!
declined_reason: String
}

extend type Mutation @guard {
createMessage(input: MessageInput!): Message
@field(
Expand Down Expand Up @@ -158,6 +164,10 @@ extend type Mutation @guard {
@field(
resolver: "App\\GraphQL\\Social\\Mutations\\Messages\\MessageManagementMutation@attachFileToMessage"
)
reviewPendingMessage(input: ReviewPendingMessageInput): Message!
@field(
resolver: "App\\GraphQL\\Social\\Mutations\\Messages\\MessageManagementMutation@reviewPendingMessage"
)
}

extend type Query @guard {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Kanvas\Social\Messages\Workflows\Activities;

use Baka\Contracts\AppInterface;
use Kanvas\Apps\Models\Apps;
use Kanvas\Social\Messages\Models\Message;
use Illuminate\Database\Eloquent\Model;
use Kanvas\Notifications\Jobs\SendEmailToUserJob;
use Kanvas\Users\Models\Users;
use Kanvas\Users\Repositories\UsersRepository;
use Illuminate\Support\Facades\Log;
use Kanvas\Workflow\KanvasActivity;
use Kanvas\Workflow\Contracts\WorkflowActivityInterface;

class SetForReviewActivity extends KanvasActivity implements WorkflowActivityInterface
{
public function execute(Model $entity, AppInterface $app, array $params): array
{
$messageData = $entity->message;
//validate if message has for_review=true,is_reviewed=false and is_public=1
if ($messageData['for_review'] && ! $messageData['is_reviewed'] && $entity->is_public) {
// $entity->setLock();

$reviewersList = explode(',', getenv('PROMPT_REVIEWERS_EMAILS'));//Later be gotta implement user groups
//send email to reviewers
$reviewers = UsersRepository::findUsersByArray(
$reviewersList,
$app,
);

foreach ($reviewers as $reviewer) {
SendEmailToUserJob::dispatch(
$reviewer,
'Prompt pending for review',
[
"body" => "{$reviewer->firstname} the prompt titled: {$entity->message['title']} is in need for review"
]
);
}
}
return ["Done"];
}
}
50 changes: 50 additions & 0 deletions src/Kanvas/Notifications/Jobs/SendEmailToUserJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Kanvas\Notifications\Jobs;

use Baka\Contracts\AppInterface;
use Baka\Traits\KanvasJobsTrait;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Kanvas\Users\Models\Users;
use Kanvas\Notifications\Templates\Blank;
use Illuminate\Support\Facades\Notification;

class SendEmailToUserJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
use KanvasJobsTrait;

public function __construct(
protected Users $user,
protected string $subject,
protected array $body = [],
protected string $emailTemplateName = 'Default',
) {
}

/**
* Send message notifications to all followers of a user.
*/
public function handle(): void
{
$notification = new Blank(
$this->emailTemplateName,
$this->body,
['mail'],
$this->user
);
$notification->setSubject($this->subject);
Notification::route('mail', $this->user->email)->notify($notification);
Log::info("Email sent to user {$this->user->email}");
}
}
Loading