server/api/blocks/blocks.controller.js
/**
 * Add a work block to a specific assignment and have the updated
 * assignment returned. The request body should contain the following:
 * - startTime
 * - endTime
 *
 * @param {Koa context} ctx
 * @returns The updated assignment with the new block added
 *
 * POST /
 */
Add a work block to a specific assignment and have the updated
assignment returned. The request body should contain the following:
- startTime
- endTime

@param {Koa context} ctx
@returns The updated assignment with the new block added

POST
Add a work block to a specific assignment and have the updated assignment returned . The request body should contain the following : - startTime - endTime @param { Koa context } ctx @returns The updated assignment with the new block added POST
async function addWorkBlock (ctx) {
  const { assessmentType, assessmentID } = ctx.params;
  const { startTime, endTime } = ctx.request.body;
  const newBlock = new Block({
    _student: ctx.state.user._id,
    startTime,
    endTime,
    completed: false,
    locked: false,
    notified: false
  });
  try {
    await newBlock.save();
  } catch (e) {
    logger.error(`Failed to save new block for ${ctx.state.user.rcs_id}: ${e}`);
    return ctx.badRequest('There was an error scheduling the work block.');
  }
  let assessment;
  try {
    assessment = await (assessmentType === 'assignment' ? Assignment : Exam)
      .findOne({
        _student: ctx.state.user._id,
        _id: assessmentID
      })
      .populate('_blocks');
  } catch (e) {
    logger.error(
      `Failed to get ${assessmentType} to add new work block for ${
        ctx.state.user.rcs_id
      }: ${e}`
    );
    return ctx.internalServerError(
      `There was an error getting the ${assessmentType}.`
    );
  }
  assessment._blocks.push(newBlock);
  try {
    await assessment.save();
  } catch (e) {
    logger.error(
      `Failed to save ${assessmentType} with new work block for ${
        ctx.state.user.rcs_id
      }: ${e}`
    );
    return ctx.badRequest('There was an error scheduling the work block.');
  }
  logger.info(`Adding work block for ${ctx.state.user.rcs_id}`);
  return ctx.ok({
    ['updated' +
    (assessmentType === 'assignment' ? 'Assignment' : 'Exam')]: assessment
  });
}
async function addWorkBlock ( ctx ) { const { assessmentType , assessmentID } = ctx . params ; const { startTime , endTime } = ctx . request . body ; const newBlock = new Block ( { _student : ctx . state . user . _id , startTime , endTime , completed : false , locked : false , notified : false } ) ; try { await newBlock . save ( ) ; } catch ( e ) { logger . error ( ` ${ ctx . state . user . rcs_id } ${ e } ` ) ; return ctx . badRequest ( 'There was an error scheduling the work block.' ) ; } let assessment ; try { assessment = await ( assessmentType === 'assignment' ? Assignment : Exam ) . findOne ( { _student : ctx . state . user . _id , _id : assessmentID } ) . populate ( '_blocks' ) ; } catch ( e ) { logger . error ( ` ${ assessmentType } ${ ctx . state . user . rcs_id } ${ e } ` ) ; return ctx . internalServerError ( ` ${ assessmentType } ` ) ; } assessment . _blocks . push ( newBlock ) ; try { await assessment . save ( ) ; } catch ( e ) { logger . error ( ` ${ assessmentType } ${ ctx . state . user . rcs_id } ${ e } ` ) ; return ctx . badRequest ( 'There was an error scheduling the work block.' ) ; } logger . info ( ` ${ ctx . state . user . rcs_id } ` ) ; return ctx . ok ( { [ 'updated' + ( assessmentType === 'assignment' ? 'Assignment' : 'Exam' ) ] : assessment } ) ; }
Add a work block to a specific assignment and have the updated
 assignment returned.
Add a work block to a specific assignment and have the updated assignment returned .
// Get assessment // eslint-disable-next-line standard/computed-property-even-spacing