From cbaa43c8809acebcfe23c53ce171fe1014777baa Mon Sep 17 00:00:00 2001 From: Pinga <121483313+getpinga@users.noreply.github.com> Date: Mon, 15 Jan 2024 15:00:13 +0200 Subject: [PATCH] Registrar notification improvements --- automation/messagebroker.php | 2 +- automation/registrar.php | 72 ++++++++++++++++++++++++++++-------- 2 files changed, 58 insertions(+), 16 deletions(-) diff --git a/automation/messagebroker.php b/automation/messagebroker.php index 8e55df7..ef7182f 100644 --- a/automation/messagebroker.php +++ b/automation/messagebroker.php @@ -35,7 +35,7 @@ $logFilePath = '/var/log/namingo/messagebroker.log'; $log = setupLogger($logFilePath, 'Message_Broker'); $log->info('job started.'); -$server->on("request", function (Request $request, Response $response) use ($c) { +$server->on("request", function (Request $request, Response $response) use ($c,$log) { // Parse the received data $data = json_decode($request->rawContent(), true); diff --git a/automation/registrar.php b/automation/registrar.php index c3cf6f9..4a7957c 100644 --- a/automation/registrar.php +++ b/automation/registrar.php @@ -16,8 +16,35 @@ try { $log->error('DB Connection failed: ' . $e->getMessage()); } +$stmt = $pdo->prepare("SELECT value FROM settings WHERE name = :name"); +$stmt->execute(['name' => 'email']); +$row = $stmt->fetch(); +if ($row) { + $supportEmail = $row['value']; +} else { + $supportEmail = 'default-support@example.com'; +} + +$stmt = $pdo->prepare("SELECT value FROM settings WHERE name = :name"); +$stmt->execute(['name' => 'phone']); +$row = $stmt->fetch(); +if ($row) { + $supportPhoneNumber = $row['value']; +} else { + $supportPhoneNumber = '+1.23456789'; +} + +$stmt = $pdo->prepare("SELECT value FROM settings WHERE name = :name"); +$stmt->execute(['name' => 'company_name']); +$row = $stmt->fetch(); +if ($row) { + $registryName = $row['value']; +} else { + $registryName = 'Example Registry LLC'; +} + // Define the query -$sql = "SELECT id, clid, accountBalance, creditThreshold, creditLimit FROM registrar"; +$sql = "SELECT id, clid, name, accountBalance, creditThreshold, creditLimit, email FROM registrar"; try { $stmt = $pdo->query($sql); @@ -25,13 +52,13 @@ try { while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { if ($row['accountBalance'] < $row['creditThreshold']) { // Case 1: accountBalance is less than creditThreshold - sendEmail($row, 'low_balance'); + sendEmail($row, 'low_balance', $log, $supportEmail, $supportPhoneNumber, $registryName); } elseif ($row['accountBalance'] == 0) { // Case 2: accountBalance is 0 - sendEmail($row, 'zero_balance'); + sendEmail($row, 'zero_balance', $log, $supportEmail, $supportPhoneNumber, $registryName); } elseif (($row['accountBalance'] + $row['creditLimit']) < 0) { // Case 3: accountBalance + creditLimit is less than 0 - sendEmail($row, 'over_limit'); + sendEmail($row, 'over_limit', $log, $supportEmail, $supportPhoneNumber, $registryName); } } @@ -43,28 +70,43 @@ try { } // Function to send email -function sendEmail($data, $case) { - // Implement the actual email sending logic here +function sendEmail($data, $case, $log, $supportEmail, $supportPhoneNumber, $registryName) { + $message = "Dear ".$data['name'].",\n\n"; + switch ($case) { case 'low_balance': - $message = "Low balance alert for registrar: " . $data['clid']; + $subject = "Low balance alert for registrar: " . $data['clid']; + $message .= "We are writing to inform you that your account with us currently has a low balance. As of now, your account balance is {$data['accountBalance']}, which is below the minimum credit threshold of {$data['creditThreshold']}.\n\n"; break; case 'zero_balance': - $message = "Zero balance alert for registrar: " . $data['clid']; + $subject = "Zero balance alert for registrar: " . $data['clid']; + $message .= "We have noticed that your account balance with us is currently zero. This means you are unable to use our services until the balance is topped up.\n\n"; break; case 'over_limit': - $message = "Over limit alert for registrar: " . $data['clid']; + $subject = "Over limit alert for registrar: " . $data['clid']; + $message .= "Your account is currently past the credit limit. Immediate action is required to bring your account back into good standing and avoid service disruption.\n\n"; break; default: - $message = "Alert for registrar: " . $data['clid']; + $subject = "Alert for registrar: " . $data['clid']; + $message .= "This is a generic warning for registrar: " . $data['clid']; } + $message .= "Important: To avoid any interruption in services, we recommend that you top up your account balance as soon as possible.\n\n"; + $message .= "How to Top Up:\n"; + $message .= "1. Log in to your account.\n"; + $message .= "2. Navigate to the 'Financials' -> 'Add Deposit' section.\n"; + $message .= "3. Follow the instructions to add funds.\n\n"; + $message .= "If you have any questions or require assistance, please do not hesitate to contact us at $supportEmail or $supportPhoneNumber.\n\n"; + $message .= "Thank you for your prompt attention to this matter.\n\n"; + $message .= "Best regards,\n"; + $message .= "$registryName's Billing Team"; + // Prepare the data array for the cURL request - $data = [ + $to_send = [ 'type' => 'sendmail', 'subject' => $subject, - 'body' => $body, - 'toEmail' => $row['billing_email'], + 'body' => $message, + 'toEmail' => $data['email'], ]; $url = 'http://127.0.0.1:8250'; @@ -72,10 +114,10 @@ function sendEmail($data, $case) { $options = [ CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => 'POST', - CURLOPT_POSTFIELDS => json_encode($data), + CURLOPT_POSTFIELDS => json_encode($to_send), CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', - 'Content-Length: ' . strlen(json_encode($data)) + 'Content-Length: ' . strlen(json_encode($to_send)) ], ];