Created
March 12, 2024 20:17
-
-
Save prondubuisi/28f34445ac8906a203f4622895e67ba7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Crm; | |
use App\Http\Response; | |
use App\Http\Controllers\Controller; | |
use App\Http\Requests\Crm\GardenerBulkMessageRequest; | |
use App\Http\Resources\Crm\GardenerBulkMessagingResource; | |
use App\Repositories\GardenerBulkMessageRepository as MessageRepository; | |
use App\Repositories\GetStreamChannelURLRepository as ChannelRepository; | |
use App\Http\Requests\Crm\GetGardenerBulkMessageRequest; | |
use App\Jobs\SendGetstreamMessage; | |
use App\Jobs\CreateGardenerMessageDeliveryLog; | |
use Carbon\Carbon; | |
class GardenerBulkMessagingController extends Controller | |
{ | |
protected MessageRepository $messageRepository; | |
protected ChannelRepository $channelRepository; | |
public function __construct( | |
MessageRepository $messageRepository, | |
ChannelRepository $channelRepository | |
) { | |
$this->messageRepository = $messageRepository; | |
$this->channelRepository = $channelRepository; | |
} | |
/** | |
* create gardener bulk message record | |
* @param GardenerBulkMessageRequest $request | |
* @return Response | |
**/ | |
public function createGardenerBulKMessage(GardenerBulkMessageRequest $request) | |
{ | |
$messageDetails = $request->validated(); | |
$savedBulKMessages = $this->messageRepository->createGardenerBulkMessage($messageDetails); | |
$queuedMessageDetails = $this->queueGardenerBulkMessage($savedBulKMessages, $messageDetails); | |
$this->processGardenerUnsentBulkMessages($savedBulKMessages->id, $queuedMessageDetails['missing_message_details']); | |
$response = [ | |
'stored_messages' => GardenerBulkMessagingResource::make($savedBulKMessages), | |
'unproccessed_messages_user_ids' => getUserNameAndIdByIds($queuedMessageDetails['missing_message_details']), | |
]; | |
return Response::send(true, 200, "Gardener bulk message created successfully", $response); | |
} | |
/** | |
* Queue already saved gardener bulk message | |
* @param \App\GardenerBulkMessage $savedBulKMessages | |
* @param array $messageDetails | |
* @return Response | |
**/ | |
public function queueGardenerBulkMessage($savedBulkMessages, $messageDetails) | |
{ | |
$originalMessage = $messageDetails['message']; | |
//messaging details for users with correct get stream channel URLs and User Ids | |
$verifiedMessagingDetails = $this->channelRepository->getUserGardenerMessagingDetails($savedBulkMessages->targeted_customers); | |
foreach ($verifiedMessagingDetails['message_details'] as $messagingDetail) { | |
$messagingDetail['location'] = getXEdenLocationHeader(); | |
$messagingDetail['bulk_message_id'] = $savedBulkMessages->id; | |
$modifiedMessage = $this->replaceMessagePlaceholders($originalMessage, $messagingDetail); | |
SendGetstreamMessage::dispatch($modifiedMessage, $messagingDetail); | |
} | |
return $verifiedMessagingDetails; | |
} | |
/** | |
* Replace place holders within messages | |
* @param string $message | |
* @param array $messagingDetail | |
* @return string $message | |
**/ | |
public function replaceMessagePlaceholders($message, $messagingDetail) | |
{ | |
$userName = $messagingDetail['user_name']; | |
$userFirstAndLastName = getCustomerFirstAndLastName($messagingDetail['user_id']); | |
$userFirstName = $userFirstAndLastName['first_name']; | |
$userLastName = $userFirstAndLastName['last_name']; | |
$tags = ['@fullname','@firstname', '@lastname' ]; | |
foreach($tags as $tag) { | |
switch($tag){ | |
case '@fullname': | |
$message = $this->replaceMessageTag($tag, $userName, $message); | |
break; | |
case '@firstname': | |
$message = $this->replaceMessageTag($tag, $userFirstName, $message); | |
break; | |
case '@lastname': | |
$message = $this->replaceMessageTag($tag, $userLastName, $message); | |
break; | |
} | |
} | |
return $message; | |
} | |
/** | |
* Replace tag with user full name, first name or last name | |
* in message | |
* @param string $tag | |
* @param string $replacement | |
* @param string $message | |
* @return string $message | |
**/ | |
public function replaceMessageTag($tag, $replacement, $message) | |
{ | |
$message = str_replace($tag, $replacement, $message); | |
logger('tag', (array)$tag); | |
logger('message name', (array)$replacement); | |
logger('message', (array)$message); | |
return $message; | |
} | |
/** | |
* Get gardener bulk message using request | |
* @param GetGardenerBulkMessageRequest $request | |
* @return Response | |
**/ | |
public function getGardenerBulkMessage(GetGardenerBulkMessageRequest $request) | |
{ | |
$bulkMessages = $this->messageRepository->getGardenerBulkMessage($request->validated()); | |
return Response::sendWithCollection( | |
true, | |
200, | |
'Gardener bulk message retrieved successfully', | |
GardenerBulkMessagingResource::collection($bulkMessages)->response()->getData(), | |
); | |
} | |
/** | |
* Queue unsent saved gardener bulk message | |
* @param $bulkMessageId | |
* @param array $receiverIds | |
* @return bool | |
**/ | |
public function processGardenerUnsentBulkMessages($bulkMessageId, $receiverIds) | |
{ | |
if (!count($receiverIds)) { | |
return; | |
} | |
foreach ($receiverIds as $receiverId) { | |
$deliveryDetails = [ | |
'receiver_id' => $receiverId, | |
'bulk_gardener_message_id' => $bulkMessageId, | |
'is_delivered' => 2, | |
'delivery_response' => 'missing-getstream-integration', | |
'domicile_location' => getXEdenLocationHeader(), | |
]; | |
CreateGardenerMessageDeliveryLog::dispatch($deliveryDetails); | |
} | |
return true; | |
} | |
/** | |
* Send notification to engineering team if bulk message is | |
* pending for more than 15 minutes | |
* @param | |
* @return | |
**/ | |
public function sendNotificationForForeverPendingMessages() | |
{ | |
$messages = $this->messageRepository->getBulkMessagesForToday(); | |
$location = getXEdenLocationHeader(); | |
$timeNow = getDateTimeFromLocation(Carbon::now(), $location); | |
$pendingMessagesArray = []; | |
foreach($messages as $message) { | |
$messageStatus = $message->status(); | |
$timeDifference = Carbon::parse($timeNow)->diffInMinutes(Carbon::parse($message->created_at)); | |
if ($messageStatus != 'delivered' && $timeDifference > 15) { | |
$pendingMessagesArray[] = $message->id; | |
} | |
} | |
if(! empty($pendingMessagesArray)) { | |
return $this->sendPendingMessageSlackNotification($pendingMessagesArray, $location); | |
} | |
} | |
/** | |
* Send slack notification for pending bulk messages | |
* @param array $pendingMessagesArray | |
* @param string $location | |
* @return | |
**/ | |
public function sendPendingMessageSlackNotification($pendingMessagesArray, $location) | |
{ | |
$channel = config('services.slack.prod-webhook'); | |
$backendEngineersId = config('eden.slack.group_ids.backend_engineers'); | |
$backendEngineersTag = "<!subteam^{$backendEngineersId}>"; | |
$crmLink = getCrmFrontendLinkViaEnvironment() . "communications/bulk-messages"; | |
$message = "Hello {$backendEngineersTag}, the following bulk messages with ID(s) " . | |
implode(', ', $pendingMessagesArray) . " in {$location} have been pending for more than 15 minutes. \n\n" . | |
"Please check this <$crmLink|link> for more details."; | |
sendSlackMessage($channel, $message); | |
return Response::send(true, 200, "Pending bulk messaging notification sent sucessfully"); | |
} | |
/** | |
* Get gardener bulk message using request | |
* @param GetGardenerBulkMessageRequest $request | |
* @return Response | |
**/ | |
public function getGardenerBulkMessageWithRecipientsData(GetGardenerBulkMessageRequest $request) | |
{ | |
$bulkMessages = $this->messageRepository->getGardenerBulkMessage($request->validated()); | |
$bulkMessages->load('recipients'); | |
return Response::sendWithCollection( | |
true, | |
200, | |
'Gardener bulk message retrieved successfully', | |
GardenerBulkMessagingResource::collection($bulkMessages)->response()->getData(), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment