server/api/assignments/assignments.controller.js
/**
 * Create an assignment given the assignment properies in the request body.
 * Request body:
 *  - title, description, dueDate, course_crn, time_estimate, priority
 *
 * POST /assignments
 * @param {Koa context} ctx
 * @returns The created assignment
 */
Create an assignment given the assignment properies in the request body.
Request body:
- title, description, dueDate, course_crn, time_estimate, priority

POST /assignments
@param {Koa context} ctx
@returns The created assignment
Create an assignment given the assignment properies in the request body . Request body : - title description dueDate course_crn time_estimate priority POST / assignments @param { Koa context } ctx @returns The created assignment
async function createAssignment (ctx) {
  const body = ctx.request.body;
  const due = moment(body.dueDate);
  if (
    !due.isBetween(ctx.session.currentTerm.start, ctx.session.currentTerm.end)
  ) {
    logger.error(
      `${
        ctx.state.user.rcs_id
      } tried to add assignment outside of current semester.`
    );
    return ctx.badRequest(
      'You cannot add an assignment due outisde of this semester.'
    );
  }
  const assignmentData = {
    _student: ctx.state.user._id,
    title: body.title,
    description: body.description,
    dueDate: due.toDate(),
    courseCRN: body.courseCRN,
    timeEstimate: body.timeEstimate,
    timeRemaining: body.timeEstimate,
    priority: parseInt(body.priority, 10),
    isRecurring: body.isRecurring
  };
  const newAssignment = new Assignment(assignmentData);
  let recurringAssignments = [];
  try {
    await newAssignment.save();
    if (body.isRecurring) {
      logger.info('Creating recurring assignments...');
      for (let dayIndex of body.recurringDays) {
        const nextFirst = moment(due).add(1, 'day');
        while (nextFirst.day() !== dayIndex) {
          nextFirst.add(1, 'day');
        }
        while (nextFirst.isBefore(ctx.session.currentTerm.classesEnd)) {
          const recurringAssignment = new Assignment({
            ...assignmentData,
            recurringOriginal: newAssignment._id,
            dueDate: nextFirst
          });
          await recurringAssignment.save();
          recurringAssignments.push(recurringAssignment);
          nextFirst.add(1, 'week');
        }
      }
    }
  } catch (e) {
    const errMap = {
      title: 'title',
      description: 'description',
      dueDate: 'due_date',
      course: 'course_id',
      timeEstimate: 'time_estimate',
      timeRemaining: 'time_estimate',
      priority: 'priority'
    };
    const errors = [];
    for (const key in e.errors) {
      errors.push(errMap[key]);
    }
    logger.error(
      `Failed to create new assignment for ${ctx.state.user.rcs_id}: ${e}`
    );
    return ctx.badRequest({
      errors
    });
  }
  logger.info(
    `Created new assigment '${newAssignment.title}' for ${
      ctx.state.user.rcs_id
    }`
  );
  ctx.created({
    createdAssignment: newAssignment,
    recurringAssignments
  });
}
async function createAssignment ( ctx ) { const body = ctx . request . body ; const due = moment ( body . dueDate ) ; if ( ! due . isBetween ( ctx . session . currentTerm . start , ctx . session . currentTerm . end ) ) { logger . error ( ` ${ ctx . state . user . rcs_id } ` ) ; return ctx . badRequest ( 'You cannot add an assignment due outisde of this semester.' ) ; } const assignmentData = { _student : ctx . state . user . _id , title : body . title , description : body . description , dueDate : due . toDate ( ) , courseCRN : body . courseCRN , timeEstimate : body . timeEstimate , timeRemaining : body . timeEstimate , priority : parseInt ( body . priority , 10 ) , isRecurring : body . isRecurring } ; const newAssignment = new Assignment ( assignmentData ) ; let recurringAssignments = [ ] ; try { await newAssignment . save ( ) ; if ( body . isRecurring ) { logger . info ( 'Creating recurring assignments...' ) ; for ( let dayIndex of body . recurringDays ) { const nextFirst = moment ( due ) . add ( 1 , 'day' ) ; while ( nextFirst . day ( ) !== dayIndex ) { nextFirst . add ( 1 , 'day' ) ; } while ( nextFirst . isBefore ( ctx . session . currentTerm . classesEnd ) ) { const recurringAssignment = new Assignment ( { ... assignmentData , recurringOriginal : newAssignment . _id , dueDate : nextFirst } ) ; await recurringAssignment . save ( ) ; recurringAssignments . push ( recurringAssignment ) ; nextFirst . add ( 1 , 'week' ) ; } } } } catch ( e ) { const errMap = { title : 'title' , description : 'description' , dueDate : 'due_date' , course : 'course_id' , timeEstimate : 'time_estimate' , timeRemaining : 'time_estimate' , priority : 'priority' } ; const errors = [ ] ; for ( const key in e . errors ) { errors . push ( errMap [ key ] ) ; } logger . error ( ` ${ ctx . state . user . rcs_id } ${ e } ` ) ; return ctx . badRequest ( { errors } ) ; } logger . info ( ` ${ newAssignment . title } ${ ctx . state . user . rcs_id } ` ) ; ctx . created ( { createdAssignment : newAssignment , recurringAssignments } ) ; }
Create an assignment given the assignment properies in the request body.
Create an assignment given the assignment properies in the request body .
// Limit to this semester // AUTO WORK-BLOCK ALLOCATION // const openSchedule = compileWeeklyOpenSchedule( //   ctx.session.currentTerm, //   ctx.state.user // ); // // Create work blocks of max size 60 minutes (TODO: Make customizable) // // Loop through days from now until assignment due date // const daysUntilDue = due.diff(new Date(), 'days'); // const today = moment().day(); // console.log(daysUntilDue); // let minutesLeft = body.timeEstimate * 60; // for (let d = 0; d < Math.abs(10); d++) { //   if (minutesLeft === 0) break; //   const day = (d + today) % 7; // Get day of the week //   const openBlocksThatDay = openSchedule.filter(p => p.day === day); //   // Loop through open blocks //   for (let p of openBlocksThatDay) { //     const duration = Math.min(p.duration, 60); // Limit to 60 minutes //     const startTime = moment(p.start, 'HH:mm', true).add(d, 'days'); //     const endTime = moment(startTime).add(duration, 'minutes'); //     const newBlock = new Block({ //       _student: ctx.state.user._id, //       startTime, //       endTime //     }); //     newBlock.save(); //     newAssignment._blocks.push(newBlock); //     minutesLeft -= duration; //     if (minutesLeft === 0) break; //   } // } // -------------------------- // Create all assignments every week up until end of classes in current semester // For other days of the week // For day of week of actual assignment // mapping schema fields to form fields