diff --git a/app/Helper/helpers.php b/app/Helper/helpers.php index 82e0831..8b3fa84 100644 --- a/app/Helper/helpers.php +++ b/app/Helper/helpers.php @@ -83,9 +83,18 @@ function verifyMailgunSignature($token, $timestamp, $signature) if (!function_exists('insertTicket')) { function insertTicket($from_email, $to_email, $subject, $content, $type, $sender_name) { - $check = Ticket::where('subject', $subject) - ->where('from_email',$from_email) - ->where('to_email',$to_email)->first(); + $check = Ticket::where(function ($query) use ($from_email, $to_email) { + $query->where('from_email', $from_email) + ->where('to_email', $to_email); + }) + ->where(function ($query) use ($subject) { + $cleanSubject = trim(str_ireplace('Re:', '', $subject)); // Remove 'Re:' prefix and trim whitespace + $query->where('subject', $cleanSubject) + ->orWhere('subject', 'Re: ' . $cleanSubject); // Consider both with and without 'Re:' + }) + ->first(); + + if(!$check){ diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index 389e49d..b0a4543 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -9,6 +9,8 @@ use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Session; use Illuminate\Foundation\Auth\AuthenticatesUsers; +use App\Services\MailgunService; +use App\Http\Controllers\Mailgun\MailgunController; class LoginController extends Controller { @@ -37,9 +39,13 @@ class LoginController extends Controller * * @return void */ + + protected $mailgunService; + public function __construct() { $this->middleware('guest')->except('logout'); + $this->mailgunService = app(MailgunService::class); } public function login() @@ -59,8 +65,32 @@ public function storeLogin(Request $request) return redirect('/company-info'); } else { $company = Company::where('user_id', Auth::id())->first(); - Session::put('selected_company', $company->id); - return redirect('/dashboard'); + $domain = $company->domain; + + $mailgunDomain = $this->mailgunService->getDomain($domain); + // dd($mailgunDomain); + + if($mailgunDomain){ + + $state = $mailgunDomain->getDomain()->getState(); + + if($state == 'unverified'){ + return redirect()->route('showDomain',$domain); + }elseif($state == 'active'){ + + if(empty($company->internal_email)){ + + $mailgun = new MailgunController(); + $mailgun->createEmail($domain); + } + + Session::put('selected_company', $company->id); + return redirect('/dashboard'); + + } + } + + } } diff --git a/app/Http/Controllers/InboxController.php b/app/Http/Controllers/InboxController.php index 9066531..de660a2 100644 --- a/app/Http/Controllers/InboxController.php +++ b/app/Http/Controllers/InboxController.php @@ -11,14 +11,21 @@ use Illuminate\Support\Facades\Auth; class InboxController extends Controller + { + public function get_canned_responses(){ + $companyId = Auth::user()->company->id; + return CompanyMeta::where('company_id', $companyId)->where('key', 'canned_responses')->get(); + } + + public function inboxSetting() { $companyId = Auth::user()->company->id; $timezones = Timezone::all(); $languages = Language::all(); $basic_setting = CompanyMeta::where('company_id', $companyId)->where('type', 'Basic Setting')->get(); - $canned_response = CompanyMeta::where('company_id', $companyId)->where('type', 'Canned Response')->first(); + $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, @@ -140,33 +147,31 @@ public function cannedResponse(Request $request) $companyId = Auth::user()->company->id; // Collect data into an array - $canned_data = [ + $canned_data = [ 'name' => $request->name, 'text' => $request->text, - ] - ]; + ]; // Retrieve existing canned responses - $existingMeta = CompanyMeta::where('company_id', $companyId) - ->where('key', 'canned_responses') - ->where('type', 'Canned Response') - ->first(); + // $existingMeta = CompanyMeta::where('company_id', $companyId) + // ->where('key', 'canned_responses') + // ->where('type', 'Canned Response') + // ->first(); - // Decode existing JSON data if it exists - if ($existingMeta) { - $existingData = json_decode($existingMeta->value, true); - $canned_data = array_merge($existingData, $canned_data); - } + // // Decode existing JSON data if it exists + // if ($existingMeta) { + // $existingData = json_decode($existingMeta->value, true); + // $canned_data = array_merge($existingData, $canned_data); + // } - // Encode the data array as JSON + // // Encode the data array as JSON $jsonData = json_encode($canned_data); // Update or create the CompanyMeta entry - CompanyMeta::updateOrCreate([ - 'company_id' => $companyId, - 'key' => 'canned_responses', - ], [ + + + CompanyMeta::create([ 'company_id' => $companyId, 'key' => 'canned_responses', 'value' => $jsonData, @@ -180,25 +185,24 @@ public function deleteCannedResponse($index) { $companyId = Auth::user()->company->id; - // Retrieve the existing canned responses - $cannedMeta = CompanyMeta::where('company_id', $companyId) + CompanyMeta::where('company_id', $companyId) ->where('key', 'canned_responses') - ->where('type', 'Canned Response') - ->first(); + ->where('id', $index) + ->delete(); - if ($cannedMeta) { - $canned_data = json_decode($cannedMeta->value, true); + // if ($cannedMeta) { + // $canned_data = json_decode($cannedMeta->value, true); - if (isset($canned_data[$index])) { - unset($canned_data[$index]); + // if (isset($canned_data[$index])) { + // unset($canned_data[$index]); - $canned_data = array_values($canned_data); - $jsonData = json_encode($canned_data); + // $canned_data = array_values($canned_data); + // $jsonData = json_encode($canned_data); - $cannedMeta->value = $jsonData; - $cannedMeta->save(); - } - } + // $cannedMeta->value = $jsonData; + // $cannedMeta->save(); + // } + // } return redirect()->back()->with('success', 'Canned response deleted successfully.'); @@ -320,8 +324,9 @@ public function inbox() $messages = []; + $canned_response = $this->get_canned_responses(); - return view('inbox', ['tickets' => $tickets, 'messages' => $messages]); + return view('inbox', ['tickets' => $tickets, 'messages' => $messages, 'canned_response' => $canned_response]); } public function fetchChatMessages($ticketId) diff --git a/app/Http/Controllers/Mailgun/EmailController.php b/app/Http/Controllers/Mailgun/EmailController.php index 74ef269..51ab80f 100644 --- a/app/Http/Controllers/Mailgun/EmailController.php +++ b/app/Http/Controllers/Mailgun/EmailController.php @@ -35,7 +35,7 @@ public function saveEmail(Request $request){ $company = get_company('email',$to_email); if($company){ - $ticket = insertTicket($data['from_email'], $data['to_email'], $data['subject'], $message,'inbox',$data['from_name'] ); + $ticket = insertTicket($data['from_email'], $company->email, $data['subject'], $message,'inbox',$data['from_name'] ); if($ticket) $response = createResponse($ticket->id,$message); }else{} diff --git a/app/Http/Controllers/Mailgun/MailgunController.php b/app/Http/Controllers/Mailgun/MailgunController.php index 72a5b49..34637a2 100644 --- a/app/Http/Controllers/Mailgun/MailgunController.php +++ b/app/Http/Controllers/Mailgun/MailgunController.php @@ -11,10 +11,18 @@ class MailgunController extends Controller { protected $mailgunService; + protected $cpanelApiService; public function __construct() { $this->mailgunService = app(MailgunService::class); // Resolving through the service container + $this->cpanelApiService = app(CPanelApiService::class); + } + + public function test(){ + $domain = 'test.com'; + $email = "kundesone.$domain@mailgun.kundesone.no"; + dd($this->createEmail($domain,$email)); } public function addDomain($domain) @@ -35,7 +43,7 @@ public function addDomain($domain) $response = $this->mailgunService->addDomain($domain); - return route('showDomain',$domain)->redirect(); + return redirect()->route('showDomain',$domain); }else{ return redirect()->route('showDomain',$domain); @@ -69,19 +77,11 @@ public function verifyDomain(Request $request) if($state == 'unverified'){ return redirect()->route('showDomain',$domain); }elseif($state == 'active'){ - $this->createRoute($request); + //$this->createRoute($request); - $email = "kundesone.$domain@kundesone.no"; - $this->createEmail($domain,$email); + $email = "kundesone.$domain@mailgun.kundesone.no"; + $this->createEmail($domain); - $company = get_company('domain',$domain); - - if($company){ - $company->internal_email = $email; - $company->save(); - } - - return $email; return redirect('/dashboard'); } @@ -96,9 +96,9 @@ public function createRoute(Request $request) return $response; } - public function createEmail($domain,$email){ + public function createEmail($domain){ - + $email = "kundesone.$domain@mailgun.kundesone.no"; $password = Str::random(12); $quota = 0; @@ -108,6 +108,13 @@ public function createEmail($domain,$email){ return false; } + $company = get_company('domain',$domain); + + if($company){ + $company->internal_email = $email; + $company->save(); + } + return $email; } diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index 1cf5f15..025e874 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -17,7 +17,7 @@ class RouteServiceProvider extends ServiceProvider * * @var string */ - public const HOME = '/home'; + public const HOME = '/dashboard'; /** * Define your route model bindings, pattern filters, and other route configuration. diff --git a/public/images/canned.png b/public/images/canned.png new file mode 100644 index 0000000..1a58c97 Binary files /dev/null and b/public/images/canned.png differ diff --git a/public/images/favicon.ico b/public/images/favicon.ico new file mode 100644 index 0000000..f706aac Binary files /dev/null and b/public/images/favicon.ico differ diff --git a/resources/views/auth/login.blade.php b/resources/views/auth/login.blade.php index 0e26fa1..b05a334 100644 --- a/resources/views/auth/login.blade.php +++ b/resources/views/auth/login.blade.php @@ -11,8 +11,8 @@ - - + + diff --git a/resources/views/domains/verify-domain.blade.php b/resources/views/domains/verify-domain.blade.php index 3f644a2..dcab299 100644 --- a/resources/views/domains/verify-domain.blade.php +++ b/resources/views/domains/verify-domain.blade.php @@ -31,11 +31,11 @@
Type: {{ $record->getType() }}
-Value: {{ $record->getValue() }}
+Forward your email to internal email. i.e kundesone.{{ $domain->getDomain()->getName() }}@mailgun.kundesone.no. Make sure you forward only your company email that you added on the time of registration.
+Type: {{ $record->getType() }}
@@ -67,6 +68,29 @@Value: {{ $record->getValue() }}
Type: {{ $record->getType() }}
+Name: {{ $domain->getDomain()->getName() }}
+Value: {{ $record->getValue() }}
+Priority: 10
+DNS Propagation can take upto 48 hours. In this case your domain will not be active.
+ +