File size: 2,188 Bytes
e8c33fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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,
};