server/api/students/students.controller.js
/**
 * Edit a student's properties iff the logged in user is an admin.
 *
 * Request body should be object of updates, e.g.:
 * { grad_year: 2022, name: { first: 'Foo', last: 'bar' } }
 *
 * @param {Koa context} ctx
 * @returns The updated student
 */
Edit a student's properties iff the logged in user is an admin.

@param {Koa context} ctx
@returns The updated student
Edit a student ' s properties iff the logged in user is an admin . @param { Koa context } ctx @returns The updated student
async function editStudent (ctx) {
  if (!ctx.state.user.admin) {
    return ctx.forbidden('You are not an administrator!');
  }
  const studentID = ctx.params.studentID;
  let student = await Student.findById(studentID);
  if (!student) {
    logger.error(
      `Failed to find student ${studentID} for admin ${ctx.state.user.rcs_id}`
    );
    return ctx.notFound(`Student ${studentID} not found.`);
  }
  delete ctx.request.body._id; 
  delete ctx.request.body.rcs_id; 
  student.set(ctx.request.body);
  try {
    await student.save();
  } catch (e) {
    logger.error(
      `Failed to update student ${studentID} for admin ${
        ctx.state.user.rcs_id
      }: ${e}`
    );
    return ctx.badRequest('There was an error updating the student.');
  }
  ctx.ok({
    updatedStudent: student
  });
}
async function editStudent ( ctx ) { if ( ! ctx . state . user . admin ) { return ctx . forbidden ( 'You are not an administrator!' ) ; } const studentID = ctx . params . studentID ; let student = await Student . findById ( studentID ) ; if ( ! student ) { logger . error ( ` ${ studentID } ${ ctx . state . user . rcs_id } ` ) ; return ctx . notFound ( ` ${ studentID } ` ) ; } delete ctx . request . body . _id ; delete ctx . request . body . rcs_id ; student . set ( ctx . request . body ) ; try { await student . save ( ) ; } catch ( e ) { logger . error ( ` ${ studentID } ${ ctx . state . user . rcs_id } ${ e } ` ) ; return ctx . badRequest ( 'There was an error updating the student.' ) ; } ctx . ok ( { updatedStudent : student } ) ; }
Edit a student's properties iff the logged in user is an admin.
Edit a student ' s properties iff the logged in user is an admin .
// Cannot change a user's _id! // Cannot change a user's rcs_id!