From 30445e4aabd821f051adcb456cf8ef29f440f5e3 Mon Sep 17 00:00:00 2001 From: Ajit Kumar Date: Mon, 6 Apr 2026 09:53:58 +0530 Subject: [PATCH 1/7] Add email functionality for user management in admin panel - Implement EmailUsers component to allow sending emails to users based on filters. - Add API endpoints for fetching email recipient counts and sending emails. - Update user entity methods to support filtering users for email sending. - Enhance styles for the email form in the admin panel. - Update home page subtitle for better clarity. --- client/jsconfig.json | 3 +- client/pages/admin/index.js | 88 +++++++++++++++++++++++++++++++++++ client/pages/admin/style.scss | 81 +++++++++++++++++++++++++++++++- client/pages/home/index.js | 2 +- server/apis/admin.js | 29 ++++++++++++ server/entities/user.js | 49 +++++++++++++++++++ 6 files changed, 248 insertions(+), 4 deletions(-) diff --git a/client/jsconfig.json b/client/jsconfig.json index 7fb5d1e..1d97943 100644 --- a/client/jsconfig.json +++ b/client/jsconfig.json @@ -1,9 +1,8 @@ { "compilerOptions": { - "baseUrl": "./", "paths": { "*": [ - "*" + "./src/*" ], } } diff --git a/client/pages/admin/index.js b/client/pages/admin/index.js index 76398ec..4fb1849 100644 --- a/client/pages/admin/index.js +++ b/client/pages/admin/index.js @@ -19,6 +19,7 @@ export default async function Admin() {

Admin Panel

+ ); } @@ -194,3 +195,90 @@ async function deleteUser(id) { alert('Success', 'User deleted successfully'); } } + +function EmailUsers() { + const recipientCount = Reactive(0); + const sendBtn = Ref(); + let filter = 'all'; + let subject = ''; + let message = ''; + + const fetchCount = async (selectedFilter) => { + const res = await fetch(`api/admin/email-recipients-count?filter=${selectedFilter}`); + const json = await res.json(); + recipientCount.value = json.count; + }; + + fetchCount(filter); + + const onFilterChange = (e) => { + filter = e.target.value; + fetchCount(filter); + }; + + const onSend = async () => { + if (!subject.trim() || !message.trim()) { + alert('ERROR', 'Subject and message are required'); + return; + } + const confirmation = await confirm('Confirm', `Send email to ${recipientCount.value} recipient(s)?`); + if (!confirmation) return; + sendBtn.disabled = true; + sendBtn.textContent = 'Sending...'; + try { + const res = await fetch('api/admin/send-email', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ filter, subject, message }), + }); + const json = await res.json(); + if (json.error) { + alert('ERROR', json.error); + } else { + alert('Success', `Email sent to ${json.sent} user(s)`); + } + } catch { + alert('ERROR', 'Failed to send emails'); + } finally { + sendBtn.disabled = false; + sendBtn.textContent = 'Send Email'; + } + }; + + return ( +
+

Email Users

+
+
+ + + {recipientCount} recipient(s) will receive this email +
+ { + subject = e.target.value; + }} + /> +
+ +