100 lines
2.7 KiB
JavaScript
100 lines
2.7 KiB
JavaScript
const Form = require('../models/Form');
|
|
|
|
const BUILTIN_FEEDBACK_FORM_NAME = 'Give us your feedback';
|
|
const BUILTIN_RATINGS_FORM_NAME = 'Rate us';
|
|
|
|
/** @deprecated Renamed; kept for lookup/migration of existing documents */
|
|
const LEGACY_BUILTIN_FEEDBACK_FORM_NAME = 'EventBuzz Default Feedback';
|
|
const LEGACY_BUILTIN_RATINGS_FORM_NAME = 'EventBuzz Default Ratings';
|
|
|
|
const ALL_BUILTIN_FEEDBACK_NAMES = [BUILTIN_FEEDBACK_FORM_NAME, LEGACY_BUILTIN_FEEDBACK_FORM_NAME];
|
|
const ALL_BUILTIN_RATINGS_NAMES = [BUILTIN_RATINGS_FORM_NAME, LEGACY_BUILTIN_RATINGS_FORM_NAME];
|
|
|
|
const DEFAULT_FEEDBACK_FIELDS = [
|
|
{
|
|
label: 'Your feedback',
|
|
type: 'text',
|
|
required: true,
|
|
placeholder: 'Share your experience, suggestions, or issues…',
|
|
options: [],
|
|
},
|
|
];
|
|
|
|
const DEFAULT_RATINGS_FIELDS = [
|
|
{
|
|
label: 'How will you rate the speakers?',
|
|
type: 'stars',
|
|
required: true,
|
|
options: [],
|
|
},
|
|
{
|
|
label: 'How will you rate the quality of content?',
|
|
type: 'stars',
|
|
required: true,
|
|
options: [],
|
|
},
|
|
{
|
|
label: 'Probability to recommend to your friends and colleagues',
|
|
type: 'stars',
|
|
required: true,
|
|
options: [],
|
|
},
|
|
];
|
|
|
|
function isBuiltinVisitorFormName(name) {
|
|
const n = name != null ? String(name).trim() : '';
|
|
return [...ALL_BUILTIN_FEEDBACK_NAMES, ...ALL_BUILTIN_RATINGS_NAMES].includes(n);
|
|
}
|
|
|
|
/**
|
|
* EventBuzz built-in feedback/ratings forms (created per event on first use).
|
|
* Session `audienceFeedbackForm` / `audienceRatingsForm` null = use these, not custom portfolio forms.
|
|
*/
|
|
async function ensureBuiltinVisitorForm(event, purpose = 'feedback') {
|
|
const isRatings = purpose === 'ratings';
|
|
const name = isRatings ? BUILTIN_RATINGS_FORM_NAME : BUILTIN_FEEDBACK_FORM_NAME;
|
|
const lookupNames = isRatings ? ALL_BUILTIN_RATINGS_NAMES : ALL_BUILTIN_FEEDBACK_NAMES;
|
|
const fields = isRatings ? DEFAULT_RATINGS_FIELDS : DEFAULT_FEEDBACK_FIELDS;
|
|
const thankYouMessage = isRatings
|
|
? 'Thank you for rating this event!'
|
|
: 'Your response has been recorded.';
|
|
|
|
let form = await Form.findOne({
|
|
event: event._id,
|
|
type: 'feedback',
|
|
name: { $in: lookupNames },
|
|
}).lean();
|
|
|
|
if (form && form.name !== name) {
|
|
await Form.updateOne({ _id: form._id }, { $set: { name } });
|
|
form = { ...form, name };
|
|
}
|
|
|
|
if (!form) {
|
|
form = await Form.create({
|
|
tenantOrganization: event.organization,
|
|
event: event._id,
|
|
name,
|
|
description: '',
|
|
type: 'feedback',
|
|
fields,
|
|
settings: {
|
|
isActive: true,
|
|
showThankYou: true,
|
|
thankYouMessage,
|
|
allowMultiple: true,
|
|
},
|
|
});
|
|
form = form.toObject();
|
|
}
|
|
|
|
return form;
|
|
}
|
|
|
|
module.exports = {
|
|
BUILTIN_FEEDBACK_FORM_NAME,
|
|
BUILTIN_RATINGS_FORM_NAME,
|
|
isBuiltinVisitorFormName,
|
|
ensureBuiltinVisitorForm,
|
|
};
|