server/api/assignments/assignments.controller.js
/**
 * Toggle an assignment's completion status. The assignment ID is passed in the request params.
 *
 * POST /assignments/a/:assignmentID/toggle
 * @param {Koa context} ctx
 * @returns The updated assignment
 */
Toggle an assignment's completion status. The assignment ID is passed in the request params.
POST /assignments/a/:assignmentID/toggle
@param {Koa context} ctx
@returns The updated assignment
Toggle an assignment ' s completion status . The assignment ID is passed in the request params . POST / assignments / a / : assignmentID / toggle @param { Koa context } ctx @returns The updated assignment
async function toggleAssignment (ctx) {
  const assignmentID = ctx.params.assignmentID;
  ctx.state.assignment.completed = !ctx.state.assignment.completed;
  ctx.state.assignment.completedAt = ctx.state.assignment.completed
    ? moment().toDate()
    : null;
  if (ctx.state.assignment.completed) {
    ctx.state.assignment.timeEstimate =
      ctx.state.assignment._blocks
        .filter(b => b.endTime <= ctx.state.assignment.completedAt)
        .reduce((acc, b) => acc + b.duration, 0) / 60; 
  }
  try {
    await ctx.state.assignment.save();
  } catch (e) {
    logger.error(`Failed to toggle assignment with ID ${assignmentID}.`);
    return ctx.badRequest('There was an error toggling the assignment.');
  }
  logger.info(
    `Set assigment ${ctx.state.assignment._id} completion status to ${
      ctx.state.assignment.completed
    } for ${ctx.state.user.rcs_id}.`
  );
  ctx.ok({
    updatedAssignment: ctx.state.assignment
  });
}
async function toggleAssignment ( ctx ) { const assignmentID = ctx . params . assignmentID ; ctx . state . assignment . completed = ! ctx . state . assignment . completed ; ctx . state . assignment . completedAt = ctx . state . assignment . completed ? moment ( ) . toDate ( ) : null ; if ( ctx . state . assignment . completed ) { ctx . state . assignment . timeEstimate = ctx . state . assignment . _blocks . filter ( b => b . endTime <= ctx . state . assignment . completedAt ) . reduce ( ( acc , b ) => acc + b . duration , 0 ) / 60 ; } try { await ctx . state . assignment . save ( ) ; } catch ( e ) { logger . error ( ` ${ assignmentID } ` ) ; return ctx . badRequest ( 'There was an error toggling the assignment.' ) ; } logger . info ( ` ${ ctx . state . assignment . _id } ${ ctx . state . assignment . completed } ${ ctx . state . user . rcs_id } ` ) ; ctx . ok ( { updatedAssignment : ctx . state . assignment } ) ; }
Toggle an assignment's completion status.
Toggle an assignment ' s completion status .
// Toggle completed status // Readjust time estimate if completed // MUST BE IN HOURS