Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit b8f369e

Browse files
committed
ii
1 parent abe662b commit b8f369e

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

server/controllers/client.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export const processRegistrations = async (req, res) => {
127127
firstName,
128128
lastName,
129129
email,
130-
position, // This will be used as role
130+
position, // This will be used as role and position
131131
department,
132132
phoneNumber: phone = '',
133133
address = '',
@@ -153,9 +153,9 @@ export const processRegistrations = async (req, res) => {
153153
const saltRounds = 10;
154154
const hashedPassword = await bcryptjs.hash(generatedPassword, saltRounds);
155155

156-
// Normalize fields - use formatDepartment instead of capitalizeFirstLetter
156+
// Normalize fields
157157
const normalizedDepartment = formatDepartment(department);
158-
const normalizedRole = position.trim(); // Using position as role as requested
158+
const normalizedRole = position.trim().toLowerCase(); // Using position as role
159159

160160
// Prepare user data
161161
const userData = {
@@ -165,6 +165,7 @@ export const processRegistrations = async (req, res) => {
165165
email,
166166
password: hashedPassword,
167167
role: normalizedRole,
168+
position: position.trim(), // Set position explicitly
168169
department: normalizedDepartment,
169170
phoneNumber: phone || '0000000000',
170171
username: generateUsername(normalizedRole),
@@ -312,7 +313,7 @@ export const saveUser = async (req, res) => {
312313
const saltRounds = 10;
313314
const hashedPassword = await bcryptjs.hash(generatedPassword, saltRounds);
314315

315-
// Normalize and validate department and role - use formatDepartment instead of capitalizeFirstLetter
316+
// Normalize and validate department and role
316317
const normalizedDepartment = formatDepartment(department);
317318
const normalizedRole = role.toLowerCase().trim();
318319

@@ -324,6 +325,7 @@ export const saveUser = async (req, res) => {
324325
email,
325326
password: hashedPassword, // Store hashed password
326327
role: normalizedRole,
328+
position: role.trim(), // Add position field with the original role value
327329
department: normalizedDepartment,
328330
phoneNumber: phone || '0000000000',
329331
username: generateUsername(normalizedRole),

server/model/User.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,16 @@ const userSchema = new mongoose.Schema(
120120
{ timestamps: true }
121121
);
122122

123+
// Updated pre-save hook to handle both position and role updates
123124
userSchema.pre('save', function (next) {
125+
// If position is modified, update role
124126
if (this.position && this.isModified('position')) {
125127
this.role = this.position.toLowerCase();
126128
}
129+
// If position is missing but role exists, set position from role
130+
else if (!this.position && this.role) {
131+
this.position = this.role;
132+
}
127133
next();
128134
});
129135

0 commit comments

Comments
 (0)