const { sanitizeText, parseList } = require('./commonValidation'); const SINGLE_ASSIGNMENT_POSITION_RULES = [ { key: 'head-of-department', label: 'Head of the Department', pattern: /^head of (the )?department$/i, }, { key: 'secretary', label: 'Secretary', pattern: /^secretary$/i, }, { key: 'head-of-laboratory', label: 'Head of the Laboratory', pattern: /^head of (the )?laboratory$/i, }, ]; const normalizeFacultyPosition = (value = '') => sanitizeText(value) .toLowerCase() .replace(/\s+/g, ' '); const getSingleAssignmentPositionRule = (position = '') => { const normalized = normalizeFacultyPosition(position); if (!normalized) return null; return SINGLE_ASSIGNMENT_POSITION_RULES.find((rule) => rule.pattern.test(normalized)) || null; }; const getSingleAssignmentPositionKey = (position = '') => getSingleAssignmentPositionRule(position)?.key || ''; const getSingleAssignmentPositionLabel = (position = '') => getSingleAssignmentPositionRule(position)?.label || ''; const getSingleAssignmentPositionRegex = (position = '') => getSingleAssignmentPositionRule(position)?.pattern || null; const normalizeFacultyCreateInput = (body = {}) => ({ name: sanitizeText(body.name), position: sanitizeText(body.position), specializations: parseList(body.specializations, /\r?\n/), email: parseList(body.email, /[,\n]/), scholarProfile: sanitizeText(body.scholarProfile), }); const normalizeFacultyUpdateInput = (body = {}) => { const payload = {}; if (typeof body.name === 'string') payload.name = body.name.trim(); if (typeof body.position === 'string') payload.position = body.position.trim(); if (body.specializations !== undefined) payload.specializations = parseList(body.specializations, /\r?\n/); if (body.email !== undefined) payload.email = parseList(body.email, /[,\n]/); if (typeof body.scholarProfile === 'string') payload.scholarProfile = body.scholarProfile.trim(); return payload; }; module.exports = { normalizeFacultyCreateInput, normalizeFacultyUpdateInput, getSingleAssignmentPositionKey, getSingleAssignmentPositionLabel, getSingleAssignmentPositionRegex, };