File size: 16,278 Bytes
9a92a42 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 | import { Request, Response } from 'express';
import Form from '../model/form.model';
import FormResponse from '../model/formResponse.model';
import Event from '../model/event.model';
import User from '../model/user.model';
import crypto from 'crypto';
import QRCode from 'qrcode';
import { logActivity } from '../util/activity.util';
import { ActivityAction, ActivityCategory } from '../model/userActivity.model';
// Generate unique short code for form
const generateShortCode = async (): Promise<string> => {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let code: string;
let exists = true;
while (exists) {
code = 'FORM-';
for (let i = 0; i < 6; i++) {
code += chars.charAt(Math.floor(Math.random() * chars.length));
}
const result = await Form.exists({ shortCode: code });
exists = result !== null;
// exists = await Form.exists({ shortCode: code });
}
return code!;
};
// ==================== CREATE FORM ====================
export const createForm = async (req: Request, res: Response) => {
console.log('createForm controller called');
try {
const userId = req.userId;
console.log('User ID:', userId);
const user = await User.findById(userId);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
// Allow trainers, organizations, and admins
const allowedRoles = ['trainer', 'organization', 'admin'];
if (!allowedRoles.includes(user.role)) {
console.log(`User role ${user.role} not authorized`);
return res.status(403).json({ error: 'Unauthorized to create forms' });
}
// Check verification for trainers
if (user.role === 'trainer' && user.organizationVerificationStatus !== 'verified') {
console.log('Trainer not verified');
return res.status(403).json({ error: 'Only verified trainers can create forms' });
}
const {
title,
description,
eventId,
fields,
accessType,
restrictToAttendees,
oneResponsePerUser,
showResponseSummary,
allowEditing
} = req.body;
console.log('Request body:', req.body);
// Validate event if provided
if (eventId) {
const event = await Event.findById(eventId);
if (!event) {
return res.status(404).json({ error: 'Event not found' });
}
// Check if user is creator of event
if (event.createdBy.toString() !== userId) {
// Admins might be allowed to create forms for any event, but let's keep it strict for now unless requested
if (user.role !== 'admin') {
return res.status(403).json({ error: 'You can only create forms for your own events' });
}
}
}
// Determine organization
let organizationId = user.organization;
if (user.role === 'organization') {
organizationId = user._id;
}
// For admins, organizationId might be undefined, which is now allowed
// Generate unique identifiers
console.log('Generating short code...');
const shortCode = await generateShortCode();
console.log('Short code generated:', shortCode);
const shareToken = crypto.randomBytes(32).toString('hex');
// Create form
const form = new Form({
title,
description,
event: eventId || undefined,
organization: organizationId,
createdBy: userId,
fields,
accessType: accessType || 'verified_users',
restrictToAttendees: restrictToAttendees || false,
oneResponsePerUser: oneResponsePerUser !== false,
shortCode,
shareToken,
showResponseSummary: showResponseSummary || false,
allowEditing: allowEditing || false,
status: 'draft'
});
console.log('Saving form...');
await form.save();
console.log('Form saved.');
// Generate QR code
console.log('Generating QR code...');
const formUrl = `${process.env.FRONTEND_URL}/forms/${form._id}?token=${shareToken}`;
const qrCodeDataUrl = await QRCode.toDataURL(formUrl);
console.log('QR code generated.');
form.qrCode = qrCodeDataUrl;
await form.save();
// Log activity
const activityMetadata: any = {
formId: form._id
};
if (eventId && form.event) {
activityMetadata.eventId = form.event;
}
await logActivity({
userId: user._id, // Use user._id instead of userId to ensure it's defined
action: ActivityAction.FORM_CREATE,
category: ActivityCategory.FORM,
description: `Created new form: ${form.title}`,
metadata: activityMetadata,
success: true
});
res.status(201).json({
message: 'Form created successfully',
form: {
id: form._id,
title: form.title,
shortCode: form.shortCode,
shareUrl: formUrl,
qrCode: qrCodeDataUrl
}
});
} catch (error: any) {
console.error('Create form error:', error);
res.status(500).json({ error: 'Failed to create form' });
}
};
// ==================== UPDATE FORM ====================
export const updateForm = async (req: Request, res: Response) => {
try {
const userId = req.userId;
const { formId } = req.params;
const user = await User.findById(userId);
if (!user) return res.status(404).json({ error: 'User not found' });
const form = await Form.findById(formId);
if (!form) {
return res.status(404).json({ error: 'Form not found' });
}
// Check ownership or admin
if (form.createdBy.toString() !== userId && user.role !== 'admin') {
return res.status(403).json({ error: 'You can only update your own forms' });
}
const {
title,
description,
fields,
accessType,
restrictToAttendees,
oneResponsePerUser,
acceptingResponses,
showResponseSummary,
allowEditing,
status
} = req.body;
// Update fields
if (title) form.title = title;
if (description !== undefined) form.description = description;
if (fields) form.fields = fields;
if (accessType) form.accessType = accessType;
if (restrictToAttendees !== undefined) form.restrictToAttendees = restrictToAttendees;
if (oneResponsePerUser !== undefined) form.oneResponsePerUser = oneResponsePerUser;
if (acceptingResponses !== undefined) form.acceptingResponses = acceptingResponses;
if (showResponseSummary !== undefined) form.showResponseSummary = showResponseSummary;
if (allowEditing !== undefined) form.allowEditing = allowEditing;
if (status) form.status = status;
await form.save();
res.json({
message: 'Form updated successfully',
form
});
} catch (error: any) {
console.error('Update form error:', error);
res.status(500).json({ error: 'Failed to update form' });
}
};
// ==================== GET FORM DETAILS ====================
export const getFormDetails = async (req: Request, res: Response) => {
try {
const userId = req.userId;
const { formId } = req.params;
const { token } = req.query;
const form = await Form.findById(formId)
.populate('createdBy', 'username email')
.populate('organization', 'username')
.populate('event', 'title code startDate');
if (!form) {
return res.status(404).json({ error: 'Form not found' });
}
// Verify access token for non-creators
if (form.createdBy._id.toString() !== userId && token !== form.shareToken) {
return res.status(403).json({ error: 'Invalid access token' });
}
// Check if user already submitted (if oneResponsePerUser is true)
let hasSubmitted = false;
if (form.oneResponsePerUser && userId) {
const existingResponse = await FormResponse.findOne({
form: formId,
respondent: userId
});
hasSubmitted = !!existingResponse;
}
res.json({
form,
hasSubmitted,
canEdit: form.allowEditing && hasSubmitted
});
} catch (error: any) {
console.error('Get form details error:', error);
res.status(500).json({ error: 'Failed to fetch form details' });
}
};
// ==================== SUBMIT FORM RESPONSE ====================
export const submitFormResponse = async (req: Request, res: Response) => {
try {
const userId = req.userId;
const { formId } = req.params;
const { responses } = req.body;
const user = await User.findById(userId);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
const form = await Form.findById(formId).populate('event');
if (!form) {
return res.status(404).json({ error: 'Form not found' });
}
// Check if form is accepting responses
if (!form.acceptingResponses || form.status === 'closed') {
return res.status(400).json({ error: 'Form is not accepting responses' });
}
// Access control checks
if (form.accessType === 'verified_users') {
// Allow any logged in user (which authMiddleware ensures)
// If we want to restrict to "verified" users only (e.g. verified trainers/orgs), we can add checks here.
// But user said "all users should be allowed to fill forms".
// So we just rely on them being logged in.
}
// Check if restricted to event attendees
if (form.restrictToAttendees && form.event) {
const event = form.event as any;
const isAttendee = event.participants.some(
(p: any) => p.user.toString() === userId
);
if (!isAttendee) {
return res.status(403).json({ error: 'Only event attendees can submit this form' });
}
}
// Check if user already submitted (if oneResponsePerUser is true)
if (form.oneResponsePerUser) {
const existingResponse = await FormResponse.findOne({
form: formId,
respondent: userId
});
if (existingResponse) {
if (!form.allowEditing) {
return res.status(400).json({ error: 'You have already submitted this form' });
}
// Update existing response
existingResponse.responses = responses;
existingResponse.lastEditedAt = new Date();
await existingResponse.save();
return res.json({
message: 'Response updated successfully',
response: existingResponse
});
}
}
// Validate required fields
const requiredFields = form.fields.filter(f => f.required);
const responseFieldIds = responses.map((r: any) => r.fieldId);
for (const field of requiredFields) {
if (!responseFieldIds.includes(field.id)) {
return res.status(400).json({
error: `Field "${field.label}" is required`
});
}
}
// Create new response
const formResponse = new FormResponse({
form: formId,
event: form.event || undefined,
respondent: userId,
responses,
ipAddress: req.ip,
userAgent: req.get('user-agent'),
submittedAt: new Date(),
isComplete: true
});
await formResponse.save();
// Increment response count
form.responseCount += 1;
await form.save();
res.status(201).json({
message: 'Response submitted successfully',
response: formResponse
});
} catch (error: any) {
console.error('Submit form response error:', error);
// Handle duplicate response error
if (error.code === 11000) {
return res.status(400).json({ error: 'You have already submitted this form' });
}
res.status(500).json({ error: 'Failed to submit response' });
}
};
// ==================== GET FORM RESPONSES ====================
export const getFormResponses = async (req: Request, res: Response) => {
try {
const userId = req.userId;
const { formId } = req.params;
const form = await Form.findById(formId);
if (!form) {
return res.status(404).json({ error: 'Form not found' });
}
// Only form creator can view responses
if (form.createdBy.toString() !== userId) {
return res.status(403).json({ error: 'You can only view responses to your own forms' });
}
const responses = await FormResponse.find({ form: formId })
.populate('respondent', 'username email role')
.sort({ submittedAt: -1 });
res.json({
form: {
title: form.title,
responseCount: form.responseCount
},
responses
});
} catch (error: any) {
console.error('Get form responses error:', error);
res.status(500).json({ error: 'Failed to fetch responses' });
}
};
// ==================== GET MY FORMS ====================
export const getMyForms = async (req: Request, res: Response) => {
try {
const userId = req.userId;
const { eventId } = req.query;
const query: any = { createdBy: userId };
if (eventId) {
query.event = eventId;
}
const forms = await Form.find(query)
.populate('event', 'title code')
.sort({ createdAt: -1 });
res.json({ forms });
} catch (error: any) {
console.error('Get my forms error:', error);
res.status(500).json({ error: 'Failed to fetch forms' });
}
};
// ==================== DELETE FORM ====================
export const deleteForm = async (req: Request, res: Response) => {
try {
const userId = req.userId;
const { formId } = req.params;
const user = await User.findById(userId);
if (!user) return res.status(404).json({ error: 'User not found' });
const form = await Form.findById(formId);
if (!form) {
return res.status(404).json({ error: 'Form not found' });
}
// Check ownership or admin
if (form.createdBy.toString() !== userId && user.role !== 'admin') {
return res.status(403).json({ error: 'You can only delete your own forms' });
}
// Delete all responses
await FormResponse.deleteMany({ form: formId });
// Delete form
await form.deleteOne();
res.json({ message: 'Form deleted successfully' });
} catch (error: any) {
console.error('Delete form error:', error);
res.status(500).json({ error: 'Failed to delete form' });
}
};
// ==================== GET MY RESPONSE ====================
export const getMyResponse = async (req: Request, res: Response) => {
try {
const userId = req.userId;
const { formId } = req.params;
const response = await FormResponse.findOne({
form: formId,
respondent: userId
}).populate('form', 'title fields allowEditing');
if (!response) {
return res.status(404).json({ error: 'No response found' });
}
res.json({ response });
} catch (error: any) {
console.error('Get my response error:', error);
res.status(500).json({ error: 'Failed to fetch response' });
}
};
// ==================== EXPORT RESPONSES TO CSV ====================
export const exportResponsesToCSV = async (req: Request, res: Response) => {
try {
const userId = req.userId;
const { formId } = req.params;
const form = await Form.findById(formId);
if (!form) {
return res.status(404).json({ error: 'Form not found' });
}
// Only form creator can export
if (form.createdBy.toString() !== userId) {
return res.status(403).json({ error: 'Unauthorized' });
}
const responses = await FormResponse.find({ form: formId })
.populate('respondent', 'username email')
.sort({ submittedAt: 1 });
// Build CSV
const headers = ['Timestamp', 'Respondent', 'Email'];
form.fields.forEach(field => headers.push(field.label));
const rows = responses.map(response => {
const row = [
new Date(response.submittedAt).toISOString(),
(response.respondent as any).username,
(response.respondent as any).email
];
form.fields.forEach(field => {
const fieldResponse = response.responses.find(r => r.fieldId === field.id);
row.push(fieldResponse ? JSON.stringify(fieldResponse.value) : '');
});
return row;
});
const csv = [headers, ...rows].map(row => row.join(',')).join('\n');
res.setHeader('Content-Type', 'text/csv');
res.setHeader('Content-Disposition', `attachment; filename="${form.title}-responses.csv"`);
res.send(csv);
} catch (error: any) {
console.error('Export CSV error:', error);
res.status(500).json({ error: 'Failed to export responses' });
}
}; |