101 lines
3.0 KiB
JavaScript
101 lines
3.0 KiB
JavaScript
const mongoose = require('mongoose');
|
|
const ParticipantList = require('../models/ParticipantList');
|
|
const Participant = require('../models/Participant');
|
|
|
|
function normalizeEmail(email) {
|
|
return email != null ? String(email).trim().toLowerCase() : '';
|
|
}
|
|
|
|
function parseAccommodationValue(v) {
|
|
if (v === true) return true;
|
|
if (v === false) return false;
|
|
const s = v == null ? '' : String(v).trim().toLowerCase();
|
|
if (!s) return false;
|
|
return ['yes', 'true', '1', 'y'].includes(s);
|
|
}
|
|
|
|
function buildGateStatus({ hasParticipant, accommodationAssigned }) {
|
|
if (!hasParticipant) return 'NO_PARTICIPANT_ID_PRESENT';
|
|
if (!accommodationAssigned) return 'ACCOMMODATION_NOT_ASSIGNED';
|
|
return 'ACCEPTED';
|
|
}
|
|
|
|
function resolveSubmittedValue({ form, dataObj, candidateKeys }) {
|
|
const entries = Array.isArray(candidateKeys) ? candidateKeys : [candidateKeys];
|
|
const source = dataObj && typeof dataObj === 'object' ? dataObj : {};
|
|
const fields = Array.isArray(form?.fields) ? form.fields : [];
|
|
|
|
for (const key of entries) {
|
|
if (source[key] !== undefined && source[key] !== null && String(source[key]).trim() !== '') {
|
|
return source[key];
|
|
}
|
|
}
|
|
|
|
const fieldMap = new Map();
|
|
for (const field of fields) {
|
|
if (!field?.mapsTo) continue;
|
|
fieldMap.set(String(field.mapsTo).toLowerCase(), field);
|
|
}
|
|
|
|
for (const key of entries) {
|
|
const field = fieldMap.get(String(key).toLowerCase());
|
|
if (!field) continue;
|
|
const fieldKey = field.name || field.label;
|
|
const value = fieldKey ? source[fieldKey] : undefined;
|
|
if (value !== undefined && value !== null && String(value).trim() !== '') {
|
|
return value;
|
|
}
|
|
}
|
|
|
|
return undefined;
|
|
}
|
|
|
|
async function resolveEventFromForm(form) {
|
|
const directId = form?.event;
|
|
if (!directId) return null;
|
|
if (typeof directId === 'object' && directId._id) return String(directId._id);
|
|
if (mongoose.Types.ObjectId.isValid(String(directId))) return String(directId);
|
|
return null;
|
|
}
|
|
|
|
async function getParticipantListForEvent({ eventId, tenantOrganizationId }) {
|
|
if (!eventId || !tenantOrganizationId) return null;
|
|
return ParticipantList.findOne({
|
|
tenantOrganization: tenantOrganizationId,
|
|
event: eventId,
|
|
}).lean();
|
|
}
|
|
|
|
async function isEmailInParticipantList({ list, tenantOrganizationId, email }) {
|
|
if (!list) return false;
|
|
const emailNorm = normalizeEmail(email);
|
|
if (!emailNorm) return false;
|
|
|
|
if (list.type === 'fixed' && Array.isArray(list.participantIds) && list.participantIds.length) {
|
|
const p = await Participant.findOne({
|
|
tenantOrganization: tenantOrganizationId,
|
|
email: emailNorm,
|
|
_id: { $in: list.participantIds },
|
|
}).select('_id').lean();
|
|
return !!p;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
function collectUserEnteredFields({ dataObj }) {
|
|
return dataObj && typeof dataObj === 'object' ? dataObj : {};
|
|
}
|
|
|
|
module.exports = {
|
|
normalizeEmail,
|
|
parseAccommodationValue,
|
|
buildGateStatus,
|
|
resolveSubmittedValue,
|
|
resolveEventFromForm,
|
|
getParticipantListForEvent,
|
|
isEmailInParticipantList,
|
|
collectUserEnteredFields,
|
|
};
|
|
|