GWM : Email functions for all creation and rejection

This commit is contained in:
Gowtham M 2025-09-09 17:01:26 +05:30
parent a7ae8cde31
commit 03d800583f
7 changed files with 172 additions and 59 deletions

View File

@ -62,10 +62,7 @@ class AgentAuthController extends ResourceController
if ($update) { if ($update) {
$subject = 'Nhance user verification - OTP'; $EmailResult = trigger_email('login_otp',['email'=>$email, 'name'=>$agentData['name'], 'otp'=>$otp, ]);
$mail_content = $otp . ' is your verification code for Nhance.';
$EmailResult = send_login_otp( $email , $subject , $mail_content);
if ($EmailResult['status'] == 'success') { if ($EmailResult['status'] == 'success') {
$result = ['agent_verification' => true, 'message' => "Verified Successfully"]; $result = ['agent_verification' => true, 'message' => "Verified Successfully"];

View File

@ -159,7 +159,9 @@ class EnquiryController extends ResourceController
'created_by' => $data['created_by'] 'created_by' => $data['created_by']
]; ];
$this->enquiryModel->insert($insertData); $enquiryId = $this->enquiryModel->insert($insertData , true);
trigger_email('enquiry_created', ['enquiry_id' => $enquiryId]);
return $this->respond(['status' => 'success', 'code' => 200, 'message' => 'Enquiry created successfully'], 200); return $this->respond(['status' => 'success', 'code' => 200, 'message' => 'Enquiry created successfully'], 200);
@ -264,6 +266,8 @@ class EnquiryController extends ResourceController
$this->enquiryModel->update($id, $updateData); $this->enquiryModel->update($id, $updateData);
trigger_email('enquiry_assigned', ['enquiry_id' => $id]);
return $this->respond(['status' => 'success', 'code' => 200, 'message' => 'Enquiry updated successfully'], 200); return $this->respond(['status' => 'success', 'code' => 200, 'message' => 'Enquiry updated successfully'], 200);

View File

@ -133,6 +133,8 @@ class PolicyController extends ResourceController
} }
} }
trigger_email('policy_created', ['policy_id' => $policyId]);
return $this->respond(['status' => 'success', 'code' => 200, 'data' => []], 200); return $this->respond(['status' => 'success', 'code' => 200, 'data' => []], 200);
} catch (\Exception $e) { } catch (\Exception $e) {

View File

@ -79,7 +79,9 @@ class QuotationController extends ResourceController
'manager_id' => $data['manager_id'] 'manager_id' => $data['manager_id']
]; ];
$this->QuotationModel->insert($insertData); $id = $this->QuotationModel->insert($insertData, true);
trigger_email('quotation_created', ['quotation_id' => $id]);
//update enquiry status //update enquiry status
$enquiryData = $this->EnquiryModel->where('id',$data['enquiry_id'])->where('status','Awaiting Quotation')->first(); $enquiryData = $this->EnquiryModel->where('id',$data['enquiry_id'])->where('status','Awaiting Quotation')->first();
@ -172,6 +174,13 @@ class QuotationController extends ResourceController
} }
} }
if( $newStatus == 'Accepted')
{
trigger_email('quotation_accepted', ['quotation_id' => $id]);
}else{
trigger_email('quotation_rejected', ['quotation_id' => $id]);
}
return $this->respond(['status' => 'success', 'code' => 200, 'data' => 'Updated Successfully'], 200); return $this->respond(['status' => 'success', 'code' => 200, 'data' => 'Updated Successfully'], 200);

View File

@ -62,10 +62,7 @@ class StaffAuthController extends ResourceController
if ($update) { if ($update) {
$subject = 'Nhance user verification - OTP'; $EmailResult = trigger_email('login_otp',['email'=>$email, 'name'=>$StaffData['name'], 'otp'=>$otp, ]);
$mail_content = $otp . ' is your verification code for Nhance.';
$EmailResult = send_login_otp( $email , $subject , $mail_content);
if ($EmailResult['status'] == 'success') { if ($EmailResult['status'] == 'success') {
$result = ['Staff_verification' => true, 'message' => "Verified Successfully"]; $result = ['Staff_verification' => true, 'message' => "Verified Successfully"];

View File

@ -178,11 +178,11 @@ class StaffController extends ResourceController
public function downloadManagerIncentiveFile() public function downloadManagerIncentiveFile()
{ {
try { try {
$file_name = $this->request->getGet('file_name'); $id = $this->request->getGet('id');
// External API URL // External API URL
$url = "https://venbait.in/nhance/dev/user/download-incentive-file/" . urlencode($file_name); $url = "https://venbait.in/nhance/dev/user/download-incentive-file/" . urlencode($id);
// Use CURL request to fetch the file // Use CURL request to fetch the file
$client = \Config\Services::curlrequest(); $client = \Config\Services::curlrequest();
@ -192,7 +192,7 @@ class StaffController extends ResourceController
// Pass the file contents back as download // Pass the file contents back as download
return $this->response return $this->response
->setHeader('Content-Type', $response->getHeaderLine('Content-Type')) ->setHeader('Content-Type', $response->getHeaderLine('Content-Type'))
->setHeader('Content-Disposition', 'attachment; filename="' . basename($file_name) . '"') ->setHeader('Content-Disposition', 'attachment; id="' . basename($id) . '"')
->setBody($response->getBody()); ->setBody($response->getBody());
} else { } else {
return $this->respond([ return $this->respond([

View File

@ -1,63 +1,167 @@
<?php <?php
use App\Models\EnquiryModel;
use App\Models\QuotationModel;
use App\Models\PolicyModel;
use App\Models\AgentModel; use App\Models\AgentModel;
use App\Models\StaffModel;
if (!function_exists('send_login_otp')) {
function send_login_otp( $to_email , $subject , $message) if (!function_exists('trigger_email')) {
/**
* Trigger an email based on predefined actions
*
* @param string $action
* @param array $data // pass extra data needed for subject/body
* @return array
*/
function trigger_email(string $action, array $data = [])
{ {
try { try {
// Set up ZeptoMail API request
$endpoint = "https://api.zeptomail.com/v1.1/email";
$apiKey = env('ZOHO_API_KEY');
$sender = env('ZOHO_SENDER_MAIL');
$payload = [ $enquiryModel = new EnquiryModel();
"from" => [ $quotationModel = new QuotationModel();
"address" => $sender, $policyModel = new PolicyModel();
"name" => "Nhance-Partner" $agentModel = new AgentModel();
], $staffModel = new StaffModel();
"to" => [
["email_address" => ["address" => $to_email]] $to_email = '';
], $subject = '';
"subject" => $subject, $message = '';
"htmlbody" => $message,
]; switch ($action) {
case 'login_otp':
$to_email = $data['email'];
$subject = "Your Login OTP";
$message = "<p>Hello {$data['name']},</p><p>Your OTP is <b>{$data['otp']}</b></p>";
break;
case 'enquiry_created':
$enquiryData = $enquiryModel->where(['id' => $data['enquiry_id'] ])->first();
$agentData = $agentModel->where(['id' => $enquiryData['agent_id'] ])->first();
$staffData = $staffModel->where(['id' => $enquiryData['manager_id'] , 'role_id' => 1 ])->first();
$to_email = $staffData['email'];
$subject = "New Enquiry Created";
$message = "<p>A new enquiry has been created by {$agentData['name']}.</p>";
break;
case 'enquiry_assigned':
$enquiryData = $enquiryModel->where(['id' => $data['enquiry_id'] ])->first();
$staffData = $staffModel->where(['id' => $enquiryData['assigned_to'] , 'role_id' => 2 ])->first();
$to_email = $staffData['email'];
$subject = "Enquiry Assigned";
$message = "<p>Enquiry #{$data['enquiry_id']} assigned to {$staffData['name']}.</p>";
break;
case 'quotation_created':
$quotationData = $quotationModel->where(['id' => $data['quotation_id'] ])->first();
$agentData = $agentModel->where(['id' => $quotationData['agent_id'] ])->first();
$to_email = $agentData['email'];
$subject = "Quotation Created";
$message = "<p>A new quotation #{$data['quotation_id']} has been created for you.</p>";
break;
case 'quotation_accepted':
$quotationData = $quotationModel->where(['id' => $data['quotation_id'] ])->first();
$staffData = $staffModel->where(['id' => $quotationData['assigned_to'] , 'role_id' => 2 ])->first();
$to_email = $staffData['email']; // assigned staff
$subject = "Quotation Accepted";
$message = "<p>Quotation #{$data['quotation_id']} was accepted.</p>";
break;
case 'quotation_rejected':
$quotationData = $quotationModel->where(['id' => $data['quotation_id'] ])->first();
$staffData = $staffModel->where(['id' => $quotationData['assigned_to'] , 'role_id' => 2 ])->first();
$to_email = $staffData['email']; // assigned staff
$subject = "Quotation Rejected";
$message = "<p>Quotation #{$data['quotation_id']} was rejected.</p>";
break;
case 'policy_created':
$policyData = $policyModel->where(['id' => $data['policy_id'] ])->first();
$agentData = $agentModel->where(['id' => $policyData['agent_id'] ])->first();
$to_email = $agentData['email'];
$subject = "Policy Created";
$message = "<p>Policy #{$policyData['policy_number']} has been created successfully.</p>";
break;
default:
log_message('error', "[EMAIL ERROR] Unknown action '{$action}'");
return ['status' => 'failed', 'code' => 400, 'message' => 'Unknown email action'];
}
return send_email($to_email, $subject, $message);
} catch (\Exception $e) {
log_message('error', "[EMAIL ERROR] Exception: " . $e->getMessage());
return ['status' => 'failed', 'code' => 500, 'message' => $e->getMessage()];
}
}
}
if (!function_exists('send_email')) {
function send_email( $to_email , $subject , $message)
{
try {
// Set up ZeptoMail API request
$endpoint = "https://api.zeptomail.com/v1.1/email";
$apiKey = env('ZOHO_API_KEY');
$sender = env('ZOHO_SENDER_MAIL');
$payload = [
"from" => [
"address" => $sender,
"name" => "Nhance-Partner"
],
"to" => [
["email_address" => ["address" => $to_email]]
],
"subject" => $subject,
"htmlbody" => $message,
];
$headers = [ $headers = [
"Content-Type: application/json", "Content-Type: application/json",
"Authorization: Zoho-enczapikey " . $apiKey "Authorization: Zoho-enczapikey " . $apiKey
]; ];
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint); curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch); $response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) { if (curl_errno($ch)) {
$error_msg = curl_error($ch); $error_msg = curl_error($ch);
log_message('error', "[EMAIL ERROR] Curl failed: {$error_msg}"); log_message('error', "[EMAIL ERROR] Curl failed: {$error_msg}");
return ['status' => 'failed', 'code' => 500, 'message' => 'Curl error', 'error' => $error_msg]; return ['status' => 'failed', 'code' => 500, 'message' => 'Curl error', 'error' => $error_msg];
} }
curl_close($ch); curl_close($ch);
$resp = json_decode($response, true); $resp = json_decode($response, true);
if (isset($resp['data'][0]['message']) && $resp['data'][0]['message'] == 'Email request received') { if (isset($resp['data'][0]['message']) && $resp['data'][0]['message'] == 'Email request received') {
log_message('debug', 'OTP Send Success to agent , email = ' . $to_email); log_message('debug', 'OTP Send Success to agent , email = ' . $to_email);
return ['status' => 'success', 'code' => 200, 'message' => 'Send Success']; return ['status' => 'success', 'code' => 200, 'message' => 'Send Success'];
} else { } else {
log_message('error', "[OTP Send FAILED] , email = {$to_email}, HTTP: {$httpCode}, Response: {$response}"); log_message('error', "[OTP Send FAILED] , email = {$to_email}, HTTP: {$httpCode}, Response: {$response}");
return ['status' => 'failed', 'code' => 500, 'message' => 'Curl error', 'error' => $response]; return ['status' => 'failed', 'code' => 500, 'message' => 'Curl error', 'error' => $response];
} }
} catch (\Exception $e) { } catch (\Exception $e) {