diff --git a/app/Helper/helpers.php b/app/Helper/helpers.php index 8b3fa84..0872d9e 100644 --- a/app/Helper/helpers.php +++ b/app/Helper/helpers.php @@ -17,7 +17,7 @@ function get_company($key,$value){ function get_current_company_tickets($args = []){ $companyId = getSelectedCompany(); - + if(!$companyId){ return false; } @@ -30,6 +30,8 @@ function get_current_company_tickets($args = []){ $tickets = Ticket::where('to_email', $company->email); + $tickets->orderBy('created_at','desc'); + if(isset($args['type'])){ $tickets->where('type',$args['type']); } diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index b0a4543..9929daf 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -11,6 +11,7 @@ use Illuminate\Foundation\Auth\AuthenticatesUsers; use App\Services\MailgunService; use App\Http\Controllers\Mailgun\MailgunController; +use App\Models\CompanyUser; class LoginController extends Controller { @@ -61,6 +62,18 @@ public function storeLogin(Request $request) ]); if (Auth::attempt($credentials)) { + // dd(Auth::user()); + if(Auth::user()->role_id == 3){ + $company = CompanyUser::where('user_id',Auth::id())->first(); + + if($company){ + + Session::put('selected_company', $company->company_id); + return redirect('/dashboard'); + } + + }else{ + if(!Auth::user()->Company) { return redirect('/company-info'); } else { @@ -68,7 +81,6 @@ public function storeLogin(Request $request) $domain = $company->domain; $mailgunDomain = $this->mailgunService->getDomain($domain); - // dd($mailgunDomain); if($mailgunDomain){ @@ -92,6 +104,10 @@ public function storeLogin(Request $request) } + + } + + } return redirect()->back()->with('error', 'Invalid Credentials'); diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index a5b5086..ec8e76e 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -8,26 +8,25 @@ use App\Models\User; use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\DB; class DashboardController extends Controller { public function dashboard() { - $tickets = get_current_company_tickets(['type' => 'inbox']); return view('index', ['tickets' => $tickets]); } - public function waiting() - { - - $tickets = get_current_company_tickets(['status' => 'waiting']); - return view('waiting', ['tickets' => $tickets]); - } - public function profile() { - $users = User::where('role_id', '!=', 1)->where('id', '!=', Auth::id())->get(); + $company = getSelectedCompany(); + $users = $users = User::where('role_id', '!=', 1) + //->where('id', '!=', Auth::id()) + ->join('company_users', 'users.id', '=', 'company_users.user_id') + ->where('company_users.company_id', $company) + ->select('users.*') + ->get(); return view('profile', ['users' => $users]); } } diff --git a/app/Http/Controllers/InboxController.php b/app/Http/Controllers/InboxController.php index de660a2..f038b00 100644 --- a/app/Http/Controllers/InboxController.php +++ b/app/Http/Controllers/InboxController.php @@ -6,30 +6,37 @@ use App\Models\Timezone; use App\Models\User; use App\Models\Ticket; +use App\Models\Comment; +use App\Models\Response; use App\Models\Language; use App\Models\CompanyMeta; +use App\Models\CompanyUser; +use Carbon\Carbon; +use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\Auth; class InboxController extends Controller { public function get_canned_responses(){ - $companyId = Auth::user()->company->id; + $companyId = getSelectedCompany(); return CompanyMeta::where('company_id', $companyId)->where('key', 'canned_responses')->get(); } public function inboxSetting() { - $companyId = Auth::user()->company->id; + $companyId = getSelectedCompany(); $timezones = Timezone::all(); $languages = Language::all(); $basic_setting = CompanyMeta::where('company_id', $companyId)->where('type', 'Basic Setting')->get(); $canned_response = $this->get_canned_responses(); $spam_handling = CompanyMeta::where('company_id', $companyId)->where('type', 'Spam Handling')->first(); $email_signature = CompanyMeta::where('company_id', $companyId)->where('type', 'Email Signature')->first(); + + return view('inbox-setting', ['timezones' => $timezones, 'basic_setting' => $basic_setting, 'spam_handling' => $spam_handling, - 'email_signature' => $email_signature, 'canned_response' => $canned_response, 'languages' => $languages]); + 'email_signature' => $email_signature, 'canned_response' => $canned_response, 'languages' => $languages, 'company' => get_company('id',$companyId)]); } public function basicSetting(Request $request) @@ -43,7 +50,7 @@ public function basicSetting(Request $request) 'timezone' => ['required'], ]); - $companyId = Auth::user()->company->id; + $companyId = getSelectedCompany(); //Update Company Meta $basic_data = [ @@ -75,7 +82,7 @@ public function emailSignature(Request $request) 'email_signature' => 'required' ]); - $companyId = Auth::user()->company->id; + $companyId = getSelectedCompany(); CompanyMeta::updateOrCreate([ 'key' => 'email_signature', 'value' => $request->email_signature @@ -91,7 +98,7 @@ public function emailSignature(Request $request) public function responseTime(Request $request) { - $companyId = Auth::user()->company->id; + $companyId = getSelectedCompany(); //Update Company Meta $response_data = [ 'monday_start_time' => $request->monday_start_time, @@ -144,7 +151,7 @@ public function cannedResponse(Request $request) 'text' => 'required' ]); - $companyId = Auth::user()->company->id; + $companyId = getSelectedCompany(); // Collect data into an array $canned_data = @@ -183,7 +190,7 @@ public function cannedResponse(Request $request) public function deleteCannedResponse($index) { - $companyId = Auth::user()->company->id; + $companyId = getSelectedCompany(); CompanyMeta::where('company_id', $companyId) ->where('key', 'canned_responses') @@ -210,7 +217,7 @@ public function deleteCannedResponse($index) public function acknowledgementReceipt(Request $request) { - $companyId = Auth::user()->company->id; + $companyId = getSelectedCompany(); //Update Company Meta $acknowledgement_data = [ 'automatic_reply_subject' => $request->automatic_reply_subject, @@ -245,7 +252,7 @@ public function spamHandling(Request $request) 'spam_email' => 'required|email' ]); - $companyId = Auth::user()->company->id; + $companyId = getSelectedCompany(); // Collect data into an array $canned_data = [ @@ -286,7 +293,7 @@ public function spamHandling(Request $request) public function deleteSpamHandling($index) { - $companyId = Auth::user()->company->id; + $companyId = getSelectedCompany(); // Retrieve the existing canned responses $spamMeta = CompanyMeta::where('company_id', $companyId) @@ -339,8 +346,87 @@ public function fetchChatMessages($ticketId) public function fetchActionBox($ticketId) { + $selectedCompany = getSelectedCompany(); $ticket = Ticket::where('id', $ticketId)->with('comments')->first(); + $companyUsers = CompanyUser::where('company_id', $selectedCompany)->with('user')->get(); - return view('partials.action-box', compact('ticket'))->render(); + return view('partials.action-box', compact('ticket','companyUsers'))->render(); + } + + public function storeResponse(Request $request) + { + $this->validate($request, [ + 'message' => 'required' , + 'ticket_id' => 'required' , + ]); + + $ticket_id = $request->ticket_id; + + // Load the HTML content into DOMDocument + $dom = new \DOMDocument(); + libxml_use_internal_errors(true); // Prevents HTML errors from being thrown as exceptions + $dom->loadHTML('' . $request->message); + libxml_clear_errors(); + + // Get all

tags + $paragraphs = $dom->getElementsByTagName('p'); + + // Add classes to each

tag + foreach ($paragraphs as $paragraph) { + $existingClasses = $paragraph->getAttribute('class'); + $paragraph->setAttribute('class', trim($existingClasses . ' user-message bg-light-green-color color-light')); + } + + // Save the modified HTML + $messageWithClasses = $dom->saveHTML($dom->documentElement); + + // create response + $response = createResponse($ticket_id,$messageWithClasses,1); + + $ticket = Ticket::find($ticket_id); + $companyId = Session::get('selected_company'); + $company = get_company('id',$companyId); + //Send mail to mailgun + + $domain = $company->domain; + $from = $company->email; + $to = $ticket->from_email; + $subject = $ticket->subject; + $html = $request->message; + + sendEmailViaMailgun($domain, $from, $to, $subject, $html); + + // Return the updated response and time + return response()->json([ + 'message' => strip_tags($response->message), // Stripping HTML tags + 'created_at' => $response->created_at->format('h:i A') // Formatting time + ]); + } + + public function storeComment(Request $request) + { + $request->validate([ + 'ticket_id' => 'required|exists:tickets,id', + 'comment' => 'required|string', + ]); + + // Assuming authenticated user + $user_id = auth()->id(); + + $comment = new Comment(); + $comment->author = $user_id; + $comment->ticket_id = $request->ticket_id; + $comment->comment = $request->comment; + $comment->save(); + + return $comment; + } + + public function deleteComment($commentId) + { + $comment = Comment::findOrFail($commentId); + $comment->delete(); + + return response()->json(['message' => 'Comment Deleted Successfully']); } } diff --git a/app/Http/Controllers/TicketController.php b/app/Http/Controllers/TicketController.php index 5241da8..6d0f08a 100644 --- a/app/Http/Controllers/TicketController.php +++ b/app/Http/Controllers/TicketController.php @@ -7,65 +7,50 @@ use App\Models\Comment; use App\Models\Response; use App\Models\Company; +use App\Models\CompanyMeta; +use App\Models\Tag; use Carbon\Carbon; use Illuminate\Support\Facades\Session; +use Illuminate\Support\Facades\Auth; class TicketController extends Controller { + public function get_canned_responses(){ + $companyId = getSelectedCompany();; + return CompanyMeta::where('company_id', $companyId)->where('key', 'canned_responses')->get(); + } + public function allTickets() { $tickets = get_current_company_tickets(); return view('all-tickets', ['tickets' => $tickets]); } - public function storeResponse(Request $request) + public function waiting() { - $this->validate($request, [ - 'message' => 'required' , - 'ticket_id' => 'required' , - ]); - - $ticket_id = $request->ticket_id; - - // Load the HTML content into DOMDocument - $dom = new \DOMDocument(); - libxml_use_internal_errors(true); // Prevents HTML errors from being thrown as exceptions - $dom->loadHTML('' . $request->message); - libxml_clear_errors(); + + $tickets = get_current_company_tickets(['status' => 'waiting']); + return view('waiting', ['tickets' => $tickets]); + } - // Get all

tags - $paragraphs = $dom->getElementsByTagName('p'); - - // Add classes to each

tag - foreach ($paragraphs as $paragraph) { - $existingClasses = $paragraph->getAttribute('class'); - $paragraph->setAttribute('class', trim($existingClasses . ' user-message bg-light-green-color color-light')); - } - - // Save the modified HTML - $messageWithClasses = $dom->saveHTML($dom->documentElement); + public function showTicket($id) + { + $tickets = get_current_company_tickets([ + + 'type' => 'inbox', + 'orderby' => 'id', + 'order' => 'desc', + 'with' => 'lastResponse' + + ]); + + $single_ticket = Ticket::find($id); + + + $messages = []; + $canned_response = $this->get_canned_responses(); - // create response - $response = createResponse($ticket_id,$messageWithClasses,1); - - $ticket = Ticket::find($ticket_id); - $companyId = Session::get('selected_company'); - $company = get_company('id',$companyId); - //Send mail to mailgun - - $domain = $company->domain; - $from = $company->email; - $to = $ticket->from_email; - $subject = $ticket->subject; - $html = $request->message; - - sendEmailViaMailgun($domain, $from, $to, $subject, $html); - - // Return the updated response and time - return response()->json([ - 'message' => strip_tags($response->message), // Stripping HTML tags - 'created_at' => $response->created_at->format('h:i A') // Formatting time - ]); + return view('show-ticket', ['tickets' => $tickets, 'single_ticket' => $single_ticket, 'messages' => $messages, 'canned_response' => $canned_response]); } public function updateStatus(Request $request, $ticketId) @@ -82,30 +67,49 @@ public function updateStatus(Request $request, $ticketId) return response()->json(['message' => 'Ticket status updated successfully']); } - public function storeComment(Request $request) + public function updateTicket(Request $request, $ticketId) { - $request->validate([ - 'ticket_id' => 'required|exists:tickets,id', - 'comment' => 'required|string', - ]); - - // Assuming authenticated user - $user_id = auth()->id(); - - $comment = new Comment(); - $comment->author = $user_id; - $comment->ticket_id = $request->ticket_id; - $comment->comment = $request->comment; - $comment->save(); + $ticket = Ticket::find($ticketId); + //Update Ticket + if(isset($request->priority)) { + $ticket->priority = $request->priority; + } + if(isset($request->user_assigned)) { + $ticket->user_assigned = $request->user_assigned; + } + $ticket->save(); - return $comment; + return response()->json(['success' => true, 'message' => 'Ticket Updated successfully!']); } - public function deleteComment($commentId) + public function storeTags(Request $request) { - $comment = Comment::findOrFail($commentId); - $comment->delete(); - - return response()->json(['message' => 'Comment Deleted Successfully']); + $company = getSelectedCompany(); + $tags = json_decode($request->tags); + foreach($tags as $tag) + { + //Update Tags Table + Tag::updateOrCreate([ + 'company_id' => $company, + 'name' => $tag->value + ],[ + 'company_id' => $company, + 'name' => $tag->value, + 'type' => 'inbox' + ]); + + //Update Company Meta Table + CompanyMeta::updateOrCreate([ + 'company_id' => $company, + 'value' => $tag->value + ],[ + 'company_id' => $company, + 'value' => $tag->value, + 'key' => 'tag', + 'type' => 'tags' + ]); + } + + return response()->json(['success' => true, 'message' => 'Tags Added Successfully']); } } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 78128fb..80eca43 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -4,6 +4,7 @@ use Illuminate\Http\Request; use App\Models\User; +use App\Models\CompanyUser; use Illuminate\Support\Facades\Hash; class UserController extends Controller @@ -15,6 +16,8 @@ public function addUser(Request $request) 'email' => ['required', 'email', 'unique:users'], 'password' => ['required', 'string', 'min:8'], ]); + + $company = getSelectedCompany(); $user = new User; $user->role_id = 3; @@ -22,6 +25,15 @@ public function addUser(Request $request) $user->email = $request->email; $user->password = Hash::make($request->password); $user->save(); + + //Update Company User + CompanyUser::updateOrCreate([ + 'user_id' => $user->id + ],[ + 'user_id' => $user->id, + 'company_id' => $company, + 'access' => json_encode($request->access) + ]); return redirect()->back()->with('success', 'User Added Successfully'); } diff --git a/app/Models/CompanyUser.php b/app/Models/CompanyUser.php new file mode 100644 index 0000000..c29abe5 --- /dev/null +++ b/app/Models/CompanyUser.php @@ -0,0 +1,23 @@ +belongsTo(User::class); + } + + public function company() + { + return $this->belongsTo(Company::class); + } +} diff --git a/app/Models/Tag.php b/app/Models/Tag.php new file mode 100644 index 0000000..2872349 --- /dev/null +++ b/app/Models/Tag.php @@ -0,0 +1,13 @@ +id(); + $table->foreignId('user_id')->constrained('users')->onDelete('cascade'); + $table->foreignId('company_id')->constrained('companies')->onDelete('cascade'); + $table->text('access')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('company_users'); + } +}; diff --git a/database/migrations/2024_06_30_040006_create_tags_table.php b/database/migrations/2024_06_30_040006_create_tags_table.php new file mode 100644 index 0000000..6d43db6 --- /dev/null +++ b/database/migrations/2024_06_30_040006_create_tags_table.php @@ -0,0 +1,30 @@ +id(); + $table->foreignId('company_id')->constrained('companies')->onDelete('cascade'); + $table->string('name'); + $table->string('type'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('tags'); + } +}; diff --git a/error_log b/error_log index b27f7d8..da01df4 100644 --- a/error_log +++ b/error_log @@ -35,3 +35,6 @@ [28-Jun-2024 06:04:34 UTC] PHP Warning: Module "fileinfo" is already loaded in Unknown on line 0 [28-Jun-2024 06:18:54 UTC] PHP Warning: Module "fileinfo" is already loaded in Unknown on line 0 [28-Jun-2024 06:20:52 UTC] PHP Warning: Module "fileinfo" is already loaded in Unknown on line 0 +[30-Jun-2024 03:59:52 UTC] PHP Warning: Module "fileinfo" is already loaded in Unknown on line 0 +[30-Jun-2024 04:00:06 UTC] PHP Warning: Module "fileinfo" is already loaded in Unknown on line 0 +[30-Jun-2024 04:30:35 UTC] PHP Warning: Module "fileinfo" is already loaded in Unknown on line 0 diff --git a/resources/views/all-tickets.blade.php b/resources/views/all-tickets.blade.php index 29c6b0c..7a4183d 100644 --- a/resources/views/all-tickets.blade.php +++ b/resources/views/all-tickets.blade.php @@ -4,6 +4,15 @@ @section('content') + +

@@ -26,7 +35,7 @@