A couple more fast email buttons
This commit is contained in:
@@ -494,6 +494,302 @@ class ShiftsController {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Admin: Add user to shift
|
||||
async addUserToShift(req, res) {
|
||||
try {
|
||||
const { shiftId } = req.params;
|
||||
const { userEmail } = req.body;
|
||||
|
||||
if (!userEmail) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: 'User email is required'
|
||||
});
|
||||
}
|
||||
|
||||
if (!config.nocodb.shiftsSheetId || !config.nocodb.shiftSignupsSheetId) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
error: 'Shifts not properly configured'
|
||||
});
|
||||
}
|
||||
|
||||
// Get shift details
|
||||
const shift = await nocodbService.getById(config.nocodb.shiftsSheetId, shiftId);
|
||||
if (!shift) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: 'Shift not found'
|
||||
});
|
||||
}
|
||||
|
||||
// Check if user exists
|
||||
const user = await nocodbService.getUserByEmail(userEmail);
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: 'User not found'
|
||||
});
|
||||
}
|
||||
|
||||
// Check if user is already signed up
|
||||
const allSignups = await nocodbService.getAll(config.nocodb.shiftSignupsSheetId);
|
||||
const existingSignup = (allSignups.list || []).find(signup => {
|
||||
return signup['Shift ID'] === parseInt(shiftId) &&
|
||||
signup['User Email'] === userEmail &&
|
||||
signup.Status === 'Confirmed';
|
||||
});
|
||||
|
||||
if (existingSignup) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: 'User is already signed up for this shift'
|
||||
});
|
||||
}
|
||||
|
||||
// Check capacity
|
||||
const confirmedSignups = (allSignups.list || []).filter(signup =>
|
||||
signup['Shift ID'] === parseInt(shiftId) && signup.Status === 'Confirmed'
|
||||
);
|
||||
|
||||
if (confirmedSignups.length >= shift['Max Volunteers']) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: 'Shift is at maximum capacity'
|
||||
});
|
||||
}
|
||||
|
||||
// Create signup
|
||||
const signup = await nocodbService.create(config.nocodb.shiftSignupsSheetId, {
|
||||
'Shift ID': parseInt(shiftId),
|
||||
'Shift Title': shift.Title,
|
||||
'User Email': userEmail,
|
||||
'User Name': user.Name || user.name || userEmail,
|
||||
'Signup Date': new Date().toISOString(),
|
||||
'Status': 'Confirmed'
|
||||
});
|
||||
|
||||
// Update shift volunteer count
|
||||
const newCount = confirmedSignups.length + 1;
|
||||
await nocodbService.update(config.nocodb.shiftsSheetId, shiftId, {
|
||||
'Current Volunteers': newCount,
|
||||
'Status': newCount >= shift['Max Volunteers'] ? 'Full' : 'Open'
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'User successfully added to shift',
|
||||
signup: signup
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
logger.error('Error adding user to shift:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: 'Failed to add user to shift'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Admin: Remove user from shift
|
||||
async removeUserFromShift(req, res) {
|
||||
try {
|
||||
const { shiftId, userId } = req.params;
|
||||
|
||||
if (!config.nocodb.shiftsSheetId || !config.nocodb.shiftSignupsSheetId) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
error: 'Shifts not properly configured'
|
||||
});
|
||||
}
|
||||
|
||||
// Find the signup by user ID (signup record ID)
|
||||
const signup = await nocodbService.getById(config.nocodb.shiftSignupsSheetId, userId);
|
||||
|
||||
if (!signup) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: 'Signup not found'
|
||||
});
|
||||
}
|
||||
|
||||
// Verify the signup belongs to the specified shift
|
||||
if (signup['Shift ID'] !== parseInt(shiftId)) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: 'Signup does not belong to this shift'
|
||||
});
|
||||
}
|
||||
|
||||
// Update signup status to cancelled
|
||||
await nocodbService.update(config.nocodb.shiftSignupsSheetId, userId, {
|
||||
'Status': 'Cancelled'
|
||||
});
|
||||
|
||||
// Update shift volunteer count
|
||||
const allSignups = await nocodbService.getAll(config.nocodb.shiftSignupsSheetId);
|
||||
const confirmedSignups = (allSignups.list || []).filter(s =>
|
||||
s['Shift ID'] === parseInt(shiftId) && s.Status === 'Confirmed'
|
||||
);
|
||||
const newCount = confirmedSignups.length;
|
||||
|
||||
const shift = await nocodbService.getById(config.nocodb.shiftsSheetId, shiftId);
|
||||
await nocodbService.update(config.nocodb.shiftsSheetId, shiftId, {
|
||||
'Current Volunteers': newCount,
|
||||
'Status': newCount >= shift['Max Volunteers'] ? 'Full' : 'Open'
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'User successfully removed from shift'
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
logger.error('Error removing user from shift:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: 'Failed to remove user from shift'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Admin: Email shift details to all volunteers
|
||||
async emailShiftDetails(req, res) {
|
||||
try {
|
||||
const { shiftId } = req.params;
|
||||
|
||||
if (!config.nocodb.shiftsSheetId || !config.nocodb.shiftSignupsSheetId) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
error: 'Shifts not properly configured'
|
||||
});
|
||||
}
|
||||
|
||||
// Get shift details
|
||||
const shift = await nocodbService.getById(config.nocodb.shiftsSheetId, shiftId);
|
||||
if (!shift) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: 'Shift not found'
|
||||
});
|
||||
}
|
||||
|
||||
// Get all confirmed signups for this shift
|
||||
const allSignups = await nocodbService.getAll(config.nocodb.shiftSignupsSheetId);
|
||||
const shiftSignups = (allSignups.list || []).filter(signup =>
|
||||
signup['Shift ID'] === parseInt(shiftId) && signup.Status === 'Confirmed'
|
||||
);
|
||||
|
||||
if (shiftSignups.length === 0) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: 'No volunteers signed up for this shift'
|
||||
});
|
||||
}
|
||||
|
||||
// Import email service
|
||||
const { sendEmail } = require('../services/email');
|
||||
const emailTemplates = require('../services/emailTemplates');
|
||||
const config_app = require('../config');
|
||||
|
||||
// Prepare email template variables
|
||||
const shiftDate = new Date(shift.Date);
|
||||
const baseUrl = config_app.isProduction ?
|
||||
`https://map.${config_app.domain}` :
|
||||
`http://localhost:${config_app.port}`;
|
||||
|
||||
const hasDescription = shift.Description && shift.Description.trim().length > 0;
|
||||
|
||||
const templateVariables = {
|
||||
APP_NAME: 'Volunteer Shift Manager',
|
||||
SHIFT_TITLE: shift.Title,
|
||||
SHIFT_DATE: shiftDate.toLocaleDateString(),
|
||||
SHIFT_START_TIME: shift['Start Time'],
|
||||
SHIFT_END_TIME: shift['End Time'],
|
||||
SHIFT_LOCATION: shift.Location || 'TBD',
|
||||
CURRENT_VOLUNTEERS: shiftSignups.length,
|
||||
MAX_VOLUNTEERS: shift['Max Volunteers'],
|
||||
SHIFT_STATUS: shift.Status || 'Open',
|
||||
SHIFT_STATUS_CLASS: (shift.Status || 'Open').toLowerCase(),
|
||||
SHIFT_DESCRIPTION: shift.Description || '',
|
||||
SHIFT_DESCRIPTION_SECTION: hasDescription ? `ADDITIONAL INFORMATION:\n======================\n${shift.Description}` : '',
|
||||
DESCRIPTION_DISPLAY: hasDescription ? 'block' : 'none',
|
||||
TIMESTAMP: new Date().toLocaleString()
|
||||
};
|
||||
|
||||
// Send emails to all volunteers
|
||||
const emailResults = [];
|
||||
const failedEmails = [];
|
||||
|
||||
for (const signup of shiftSignups) {
|
||||
try {
|
||||
const userVariables = {
|
||||
...templateVariables,
|
||||
USER_NAME: signup['User Name'] || signup['User Email'],
|
||||
USER_EMAIL: signup['User Email']
|
||||
};
|
||||
|
||||
const emailContent = await emailTemplates.render('shift-details', userVariables);
|
||||
|
||||
await sendEmail({
|
||||
to: signup['User Email'],
|
||||
subject: `Shift Details: ${shift.Title} - ${shiftDate.toLocaleDateString()}`,
|
||||
text: emailContent.text,
|
||||
html: emailContent.html
|
||||
});
|
||||
|
||||
emailResults.push({
|
||||
email: signup['User Email'],
|
||||
name: signup['User Name'],
|
||||
success: true
|
||||
});
|
||||
|
||||
logger.info(`Sent shift details email to: ${signup['User Email']}`);
|
||||
} catch (emailError) {
|
||||
logger.error(`Failed to send shift details email to ${signup['User Email']}:`, emailError);
|
||||
failedEmails.push({
|
||||
email: signup['User Email'],
|
||||
name: signup['User Name'],
|
||||
error: emailError.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const successCount = emailResults.length;
|
||||
const failCount = failedEmails.length;
|
||||
|
||||
if (successCount === 0) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
error: 'Failed to send any emails',
|
||||
details: failedEmails
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: `Sent shift details to ${successCount} volunteer${successCount !== 1 ? 's' : ''}${failCount > 0 ? `, ${failCount} failed` : ''}`,
|
||||
results: {
|
||||
successful: emailResults,
|
||||
failed: failedEmails,
|
||||
shift: {
|
||||
id: shiftId,
|
||||
title: shift.Title,
|
||||
date: shiftDate.toLocaleDateString(),
|
||||
volunteers: shiftSignups.length
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
logger.error('Error sending shift details emails:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: 'Failed to send shift details emails'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new ShiftsController();
|
||||
@@ -211,6 +211,127 @@ class UsersController {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async emailAllUsers(req, res) {
|
||||
try {
|
||||
const { subject, content } = req.body;
|
||||
|
||||
if (!subject || !content) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: 'Subject and content are required'
|
||||
});
|
||||
}
|
||||
|
||||
if (!config.nocodb.loginSheetId) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
error: 'Login sheet not configured'
|
||||
});
|
||||
}
|
||||
|
||||
// Get all users
|
||||
const response = await nocodbService.getAll(config.nocodb.loginSheetId, {
|
||||
limit: 1000
|
||||
});
|
||||
|
||||
const users = response.list || [];
|
||||
|
||||
if (users.length === 0) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: 'No users found to email'
|
||||
});
|
||||
}
|
||||
|
||||
// Import email service
|
||||
const { sendEmail } = require('../services/email');
|
||||
const emailTemplates = require('../services/emailTemplates');
|
||||
const config_app = require('../config');
|
||||
|
||||
// Convert rich text content to plain text for the text version
|
||||
const stripHtmlTags = (html) => {
|
||||
return html.replace(/<[^>]*>/g, '').replace(/\s+/g, ' ').trim();
|
||||
};
|
||||
|
||||
// Prepare base template variables
|
||||
const baseTemplateVariables = {
|
||||
APP_NAME: 'CMlite Map - User Broadcast',
|
||||
EMAIL_SUBJECT: subject,
|
||||
EMAIL_CONTENT: content,
|
||||
EMAIL_CONTENT_TEXT: stripHtmlTags(content),
|
||||
SENDER_NAME: req.session.userName || req.session.userEmail || 'Administrator',
|
||||
TIMESTAMP: new Date().toLocaleString()
|
||||
};
|
||||
|
||||
// Send emails to all users
|
||||
const emailResults = [];
|
||||
const failedEmails = [];
|
||||
|
||||
for (const user of users) {
|
||||
try {
|
||||
const userVariables = {
|
||||
...baseTemplateVariables,
|
||||
USER_NAME: user.Name || user.name || user.Email || user.email || 'User',
|
||||
USER_EMAIL: user.Email || user.email
|
||||
};
|
||||
|
||||
const emailContent = await emailTemplates.render('user-broadcast', userVariables);
|
||||
|
||||
await sendEmail({
|
||||
to: user.Email || user.email,
|
||||
subject: subject,
|
||||
text: emailContent.text,
|
||||
html: emailContent.html
|
||||
});
|
||||
|
||||
emailResults.push({
|
||||
email: user.Email || user.email,
|
||||
name: user.Name || user.name || user.Email || user.email,
|
||||
success: true
|
||||
});
|
||||
|
||||
logger.info(`Sent broadcast email to: ${user.Email || user.email}`);
|
||||
} catch (emailError) {
|
||||
logger.error(`Failed to send broadcast email to ${user.Email || user.email}:`, emailError);
|
||||
failedEmails.push({
|
||||
email: user.Email || user.email,
|
||||
name: user.Name || user.name || user.Email || user.email,
|
||||
error: emailError.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const successCount = emailResults.length;
|
||||
const failCount = failedEmails.length;
|
||||
|
||||
if (successCount === 0) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
error: 'Failed to send any emails',
|
||||
details: failedEmails
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: `Sent email to ${successCount} user${successCount !== 1 ? 's' : ''}${failCount > 0 ? `, ${failCount} failed` : ''}`,
|
||||
results: {
|
||||
successful: emailResults,
|
||||
failed: failedEmails,
|
||||
total: users.length,
|
||||
subject: subject
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
logger.error('Error sending broadcast email:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: 'Failed to send broadcast email'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new UsersController();
|
||||
Reference in New Issue
Block a user