Files
hub/app/Http/Controllers/Buyer/Crm/MessageController.php
kelly 7e2b3d4ce6 fix: update Buyer CRM controllers to use App\Models\Crm namespace
The Buyer CRM controllers were using incorrect namespace imports from
Modules\Crm\Entities\* which doesn't exist. Updated all controllers to
use the correct App\Models\Crm\* namespace:

- DashboardController: CrmInvoice, CrmQuote, CrmThread
- InboxController: CrmThread
- InvoiceController: CrmInvoice, CrmThread
- QuoteController: CrmQuote
- BrandHubController: CrmThread
- MessageController: CrmChannelMessage, CrmThread
- OrderController: CrmThread (inline)

Also changed CrmMessage to CrmChannelMessage as CrmMessage doesn't exist.
2025-12-05 16:09:48 -07:00

117 lines
3.4 KiB
PHP

<?php
namespace App\Http\Controllers\Buyer\Crm;
use App\Http\Controllers\Controller;
use App\Models\Crm\CrmChannelMessage;
use App\Models\Crm\CrmThread;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class MessageController extends Controller
{
public function store(Request $request, CrmThread $thread)
{
$business = Auth::user()->business;
$user = Auth::user();
// Verify thread belongs to this buyer
if ($thread->buyer_business_id !== $business->id) {
abort(403);
}
$validated = $request->validate([
'body' => 'required|string|max:10000',
'attachments' => 'nullable|array',
'attachments.*' => 'file|max:10240', // 10MB max
]);
// Create message
$message = $thread->messages()->create([
'sender_id' => $user->id,
'sender_type' => 'buyer',
'body' => $validated['body'],
]);
// Handle attachments
if ($request->hasFile('attachments')) {
foreach ($request->file('attachments') as $file) {
$path = $file->store("threads/{$thread->id}/attachments", 'minio');
$message->attachments()->create([
'filename' => $file->getClientOriginalName(),
'path' => $path,
'mime_type' => $file->getMimeType(),
'size' => $file->getSize(),
]);
}
}
// Update thread
$thread->update([
'last_message_at' => now(),
'status' => CrmThread::STATUS_AWAITING_SELLER,
]);
// TODO: Dispatch notification to seller
if ($request->wantsJson()) {
return response()->json([
'success' => true,
'message' => $message->load('sender'),
]);
}
return back()->with('success', 'Message sent.');
}
public function destroy(CrmThread $thread, CrmChannelMessage $message)
{
$business = Auth::user()->business;
$user = Auth::user();
// Verify thread belongs to this buyer
if ($thread->buyer_business_id !== $business->id) {
abort(403);
}
// Can only delete own messages within 5 minutes
if ($message->sender_id !== $user->id || $message->sender_type !== 'buyer') {
abort(403);
}
if ($message->created_at->diffInMinutes(now()) > 5) {
return back()->with('error', 'Messages can only be deleted within 5 minutes of sending.');
}
$message->delete();
return back()->with('success', 'Message deleted.');
}
public function react(Request $request, CrmThread $thread, CrmChannelMessage $message)
{
$business = Auth::user()->business;
$user = Auth::user();
// Verify thread belongs to this buyer
if ($thread->buyer_business_id !== $business->id) {
abort(403);
}
$validated = $request->validate([
'reaction' => 'required|string|in:thumbsup,heart,check,question',
]);
$message->toggleReaction($user->id, $validated['reaction']);
if ($request->wantsJson()) {
return response()->json([
'success' => true,
'reactions' => $message->fresh()->reactions,
]);
}
return back();
}
}