Skip to content

Commit

Permalink
Added Admin button capability to Edit Users
Browse files Browse the repository at this point in the history
  • Loading branch information
ntrehan committed Sep 16, 2024
1 parent f89f90d commit 2cb3604
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 10 deletions.
6 changes: 4 additions & 2 deletions backend/models/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const userSchema = mongoose.Schema({
managedProjects: [{ type: String}], // Which projects managed by user.
//currentProject: { type: String } // no longer need this as we can get it from Project Team Member table
// password: { type: String, required: true }
isActive: { type: Boolean, default: true }
isActive: { type: Boolean, default: true },
isAdmin: {type: Boolean, default: false} // Signifies if a user is an admin or not
});

userSchema.methods.serialize = function () {
Expand Down Expand Up @@ -67,7 +68,8 @@ userSchema.methods.serialize = function () {
githubPublic2FA: this.githubPublic2FA,
availability: this.availability,
managedProjects: this.managedProjects,
isActive: this.isActive
isActive: this.isActive,
isAdmin: this.isAmdin
};
};

Expand Down
66 changes: 66 additions & 0 deletions backend/scripts/project_event_delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const { MongoClient } = require('mongodb');
// Load config variables
const { DATABASE_URL } = require('../config/database.config');

async function deleteProjectsAndEvents() {
// We can pass any db name in the command line
const dbName = process.argv[2];

if (!dbName) {
console.error('Please provide a database name as an argument.');
process.exit(1);
}

// Strip out the existing db name and parameters from the URL
const baseUrl = DATABASE_URL.split('/').slice(0, -1).join('/');

// Construct the new URL by appending the dbName
const url = `${baseUrl}/${dbName}?retryWrites=true&w=majority`;

const client = new MongoClient(url);

try {
// Connect the client to the server
await client.connect();
console.log('Connected to the server');
const db = client.db(dbName);

const projectsCount = await db.collection('projects').countDocuments();
const eventsCount = await db.collection('events').countDocuments();

console.log(`Projects in database: ${projectsCount}`);
console.log(`Events in database: ${eventsCount}`);

// if (projectsCount > 0) {
// // Find all project IDs
// const projectIds = await db.collection('projects').find({}, { projection: { _id: 1 } }).toArray();
// const projectIdsArray = projectIds.map(project => project._id);

// // Delete corresponding events
// const deleteEventsResult = await db.collection('events').deleteMany({ project: { $in: projectIdsArray } });
// console.log(`Deleted ${deleteEventsResult.deletedCount} events.`);

// // Delete all projects
// const deleteProjectsResult = await db.collection('projects').deleteMany({});
// console.log(`Deleted ${deleteProjectsResult.deletedCount} projects.`);
// } else {
// console.log('No projects found in the database.');
// }

// Verification after deletion
const remainingProjects = await db.collection('projects').countDocuments();
const remainingEvents = await db.collection('events').countDocuments();

console.log(`Projects remaining: ${remainingProjects}`);
console.log(`Events remaining: ${remainingEvents}`);
} catch (err) {
console.error(err);
} finally {
// Close the connection to the MongoDB server
await client.close();
console.log('Connection closed');
}
}

// Run the function
deleteProjectsAndEvents();
16 changes: 16 additions & 0 deletions client/src/api/UserApiService.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ class UserApiService {
alert('server not responding. Please try again.');
}
}

async updateUserDbIsAdmin(userToEdit, isAdmin) {
const url = `${this.baseUserUrl}${userToEdit._id}`;
const requestOptions = {
method: 'PATCH',
headers: this.headers,
body: JSON.stringify({ isAdmin }),
};

try {
return await fetch(url, requestOptions);
} catch (err) {
console.error('update is-admin error', err);
alert('server not responding. Please try again.');
}
}
}

export default UserApiService;
24 changes: 19 additions & 5 deletions client/src/components/user-admin/EditUsers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import '../../sass/UserAdmin.scss';
import { FormGroup, FormControlLabel, Switch } from '@mui/material'

// child of UserAdmin. Displays form to update users.
const EditUsers = ({ userToEdit, backToSearch, updateUserDb, projects, updateUserActiveStatus }) => {
const EditUsers = ({ userToEdit, backToSearch, updateUserDb, projects, updateUserActiveStatus, updateUserAdminStatus }) => {
const [userManagedProjects, setUserManagedProjects] = useState([]); // The projects that the selected user is assigned
const [projectValue, setProjectValue] = useState(''); // State and handler for form in EditUsers
const [isActive, setIsActive] = useState(userToEdit.isActive);
const [isAdmin, setIsAdmin] = useState(userToEdit.isAdmin);

// Prepare data for display
const userName = `${userToEdit.name?.firstName} ${userToEdit.name?.lastName}`;
Expand Down Expand Up @@ -64,6 +65,10 @@ const EditUsers = ({ userToEdit, backToSearch, updateUserDb, projects, updateUse
updateUserActiveStatus(userToEdit, !isActive)
}

const handleSetIsAdmin = () => {
setIsAdmin(!isAdmin)
updateUserAdminStatus(userToEdit, !isAdmin)
}
return (
<div className="edit-users">
<div className="ua-row">
Expand All @@ -74,15 +79,24 @@ const EditUsers = ({ userToEdit, backToSearch, updateUserDb, projects, updateUse
<div className="user-display-column-left">Email:</div>
<div className="user-display-column-right">{userEmail}</div>
</div>
<div className="ua-row is-active-flex">
<div className="user-is-active-column-left">Is Active:</div>
<div className="is-active-flex">
<span className="active-status">{isActive.toString()}</span>
<div className="ua-row toggle-flex">
<div className="user-toggle-column-left">Is Active:</div>
<div className="toggle-flex">
<span className="toggle-status">{isActive.toString()}</span>
<FormGroup>
<FormControlLabel control={<Switch checked={isActive} />} onClick={() => handleSetIsActive()} />
</FormGroup>
</div>
</div>
<div className="ua-row toggle-flex">
<div className="user-toggle-column-left">Is Admin:</div>
<div className="toggle-flex">
<span className="toggle-status">{isAdmin.toString()}</span>
<FormGroup>
<FormControlLabel control={<Switch checked={isAdmin} />} onClick={() => handleSetIsAdmin()} />
</FormGroup>
</div>
</div>
<div className="ua-row">
<div className="user-display-column-left">Projects:</div>
<div className="user-display-column-right">
Expand Down
8 changes: 8 additions & 0 deletions client/src/pages/UserAdmin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ const UserAdmin = () => {
}, [userApiService, fetchUsers]
)

const updateUserAdminStatus = useCallback(
async (user, isAdmin) => {
await userApiService.updateUserDbIsAdmin(user, isAdmin);
fetchUsers()
}, [userApiService, fetchUsers]
)

const fetchProjects = useCallback(async () => {
const projectRes = await projectApiService.fetchProjects();
setProjects(projectRes);
Expand Down Expand Up @@ -65,6 +72,7 @@ const UserAdmin = () => {
updateUserDb={updateUserDb}
backToSearch={backToSearch}
updateUserActiveStatus={updateUserActiveStatus}
updateUserAdminStatus={updateUserAdminStatus}
/>
);
}
Expand Down
6 changes: 3 additions & 3 deletions client/src/sass/UserAdmin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@
margin-bottom: 8px;
}

.user-is-active-column-left {
.user-toggle-column-left {
display: flex;
flex-direction: column;
flex-basis: 15%;
}

.active-status {
.toggle-status {
margin-right: 15px;
width: 25px;
}

.is-active-flex {
.toggle-flex {
display: flex;
align-items: center;
}
Expand Down

0 comments on commit 2cb3604

Please sign in to comment.