freescout-helpdesk / freescout

FreeScout — Free self-hosted help desk & shared mailbox (Zendesk / Help Scout alternative)
https://freescout.net
GNU Affero General Public License v3.0
2.67k stars 458 forks source link

Assignment to existing ticket fails #4012

Closed SmokinB closed 2 weeks ago

SmokinB commented 2 weeks ago

Hello,

We use FreeScout for customer support for our Amazon seller account (SellerCentral)

We have the case that every response from Amazon customers to an existing ticket opens a new ticket.

The assignment to the existing ticket does not seem to work here. Any ideas?

Thank you... (:

Best regards, Benjamin

SmokinB commented 2 weeks ago

For someone who has the same problem with Amazon Messages, this fixes our requirement:

        /**
         * Merge new threads from Amazon customers
         * 
         */
        \Eventy::addFilter('conversation.created_by_customer', function ($conversation, $thread, $customer) {

            if (!empty($thread) && $thread->from && strpos($thread->from, '@marketplace.amazon.de') !== false) {

                $mailbox = $conversation->mailbox;

                $prev_conversations = $mailbox->conversations()
                    ->where('customer_id', $customer->id)
                    ->where('id', '<>', $conversation->id)
                    ->where('status', '!=', Conversation::STATUS_SPAM)
                    ->where('state', Conversation::STATE_PUBLISHED)
                    //->limit(100)
                    ->orderBy('created_at', 'desc')
                    ->get();

                foreach ($prev_conversations as $prev_conversation) {

                    // Move all threads from old to new conversation.
                    foreach ($prev_conversation->threads as $thread) {
                        $thread->conversation_id = $conversation->id;
                        $thread->setMeta(Thread::META_PREV_CONVERSATION, $prev_conversation->id);
                        $thread->save();
                    }

                    if ($prev_conversation->has_attachments && !$conversation->has_attachments) {
                        $conversation->has_attachments = true;
                        $conversation->save();
                    }

                    // Move star mark.
                    $mailbox_star_folders = Folder::where('mailbox_id', $prev_conversation->mailbox_id)
                        ->where('type', Folder::TYPE_STARRED)
                        ->get();

                    $conv_star_folder_ids = ConversationFolder::select('folder_id')
                        ->whereIn('folder_id', $mailbox_star_folders->pluck('id'))
                        ->where('conversation_id', $prev_conversation->id)
                        ->pluck('folder_id');

                    foreach ($conv_star_folder_ids as $conv_star_folder_id) {
                        $folder = $mailbox_star_folders->find($conv_star_folder_id);
                        if ($folder->user) {
                            $conversation->star($folder->user);
                            $prev_conversation->unstar($folder->user);
                        }
                    }

                    // Delete old conversation.
                    $prev_conversation->delete();

                    // Update counters.
                    $mailbox->updateFoldersCounters();
                    if ($conversation->mailbox_id != $prev_conversation->mailbox_id) {
                        $prev_conversation->mailbox->updateFoldersCounters();
                    }
                }
            }

            return $conversation;
        }, 20, 3);