feat(professors,sms): merge professor with users, add existing user link, professor portal endpoint, and SMS bypass list
This commit is contained in:
@@ -484,44 +484,6 @@ const upsertProfessorAndUser = async (profInput, professorRole, userRole, stats,
|
||||
});
|
||||
}
|
||||
|
||||
let existingProf = null;
|
||||
if (nationalIdCode) {
|
||||
existingProf = await Professor.findOne({ nationalIdCode });
|
||||
}
|
||||
if (!existingProf && phoneNumber) {
|
||||
existingProf = await Professor.findOne({ phoneNumber });
|
||||
}
|
||||
if (!existingProf && fullName) {
|
||||
const allProfs = await Professor.find({});
|
||||
existingProf = allProfs.find((p) => namesMatch(`${p.name} ${p.surname}`, fullName) || namesMatch(p.name, name));
|
||||
}
|
||||
|
||||
let professorDoc;
|
||||
if (existingProf) {
|
||||
if (name) existingProf.name = name;
|
||||
if (surname) existingProf.surname = surname;
|
||||
if (cardNumber) existingProf.cardNumber = cardNumber;
|
||||
if (shabaNumber) existingProf.shabaNumber = shabaNumber;
|
||||
if (email && !existingProf.email) existingProf.email = email;
|
||||
if (profInput.bio && !existingProf.bio) existingProf.bio = profInput.bio;
|
||||
await existingProf.save();
|
||||
professorDoc = existingProf;
|
||||
stats.professorsUpdated += 1;
|
||||
} else {
|
||||
professorDoc = await Professor.create({
|
||||
name: name || fullName,
|
||||
surname: surname || '',
|
||||
nationalIdCode,
|
||||
phoneNumber,
|
||||
cardNumber: cardNumber || undefined,
|
||||
shabaNumber: shabaNumber || undefined,
|
||||
email: email || undefined,
|
||||
bio: profInput.bio || undefined,
|
||||
isActive: true
|
||||
});
|
||||
stats.professorsCreated += 1;
|
||||
}
|
||||
|
||||
const targetRole = professorRole || userRole;
|
||||
let user = await findExistingUser({ nationalIdCode, phoneNumber, name: fullName });
|
||||
if (user) {
|
||||
@@ -529,7 +491,7 @@ const upsertProfessorAndUser = async (profInput, professorRole, userRole, stats,
|
||||
if (cardNumber && !user.cardNumber) user.cardNumber = cardNumber;
|
||||
if (shabaNumber && !user.shabaNumber) user.shabaNumber = shabaNumber;
|
||||
if (email && !user.email) user.email = email;
|
||||
if (targetRole && String(user.role) === String(userRole?._id)) {
|
||||
if (targetRole && String(user.role) !== String(targetRole._id)) {
|
||||
user.role = targetRole._id;
|
||||
}
|
||||
await user.save();
|
||||
@@ -555,6 +517,30 @@ const upsertProfessorAndUser = async (profInput, professorRole, userRole, stats,
|
||||
stats.studentsCreated += 1;
|
||||
}
|
||||
|
||||
let professorDoc = await Professor.findOne({ user: user._id });
|
||||
if (!professorDoc) {
|
||||
professorDoc = await Professor.findOne({
|
||||
$or: [
|
||||
{ nationalIdCode },
|
||||
{ phoneNumber }
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
if (professorDoc) {
|
||||
professorDoc.user = user._id;
|
||||
if (profInput.bio && !professorDoc.bio) professorDoc.bio = profInput.bio;
|
||||
await professorDoc.save();
|
||||
stats.professorsUpdated += 1;
|
||||
} else {
|
||||
professorDoc = await Professor.create({
|
||||
user: user._id,
|
||||
bio: profInput.bio || undefined,
|
||||
isActive: true
|
||||
});
|
||||
stats.professorsCreated += 1;
|
||||
}
|
||||
|
||||
return { professor: professorDoc, user };
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user