message: Use ProgressController for all progresses
diff: @@ -2,14 +2,40 @@ import {
  BadRequestError, Body, CurrentUser, Get, JsonController, Param, Post, Put,
  UseBefore
  } from 'routing-controllers';
  +import * as moment from 'moment';
  import passportJwtMiddleware from '../security/passportJwtMiddleware';
  import {Progress} from '../models/Progress';
  import {IProgress} from '../../../shared/models/IProgress';
  import {IUser} from '../../../shared/models/IUser';
  +import {Unit} from '../models/units/Unit';
  +import {CodeKataUnit} from '../../../app/webFrontend/src/app/models/CodeKataUnit';
  +import {CodeKataProgress} from '../models/CodeKataProgress';
  @JsonController('/progress')
  @UseBefore(passportJwtMiddleware)
  export class ProgressController {
  + private static async getUnit(unitId: string) {
  + return (await Unit.findById(unitId)).toObject();
  + }
  +
  + private static checkDeadline(unit: any) {
  + if (unit.deadline && moment(unit.deadline).isBefore()) {
  + throw new BadRequestError('Past deadline, no further update possible');
  + }
  + }
  +
  + private static getProgressClassForType(type: string) {
  + let progressClass = null;
  + switch (type) {
  + case 'code-kata':
  + progressClass = CodeKataProgress;
  + break;
  + default:
  + progressClass = Progress;
  + }
  +
  + return progressClass;
  + }
  @Get('/units/:id')
  getUnitProgress(@Param('id') id: string) {
  @@ -30,21 +56,29 @@ export class ProgressController {
  }
  @Post('/')
  - createProgress(@Body() data: any, @CurrentUser() currentUser?: IUser) {
  + async createProgress(@Body() data: any, @CurrentUser() currentUser?: IUser) {
  // discard invalid requests
  if (!data.course || !data.unit || !currentUser) {
  throw new BadRequestError('progress need fields course, user and unit');
  }
  + const unit: any = await ProgressController.getUnit(data.unit);
  + ProgressController.checkDeadline(unit);
  data.user = currentUser;
  - return new Progress(data).save()
  - .then((progress) => progress.toObject());
  + const progressClass = ProgressController.getProgressClassForType(data.type);
  + const progress = await new progressClass(data).save();
  +
  + return progress.toObject();
  }
  @Put('/:id')
  - updateCodeKataUnit(@Param('id') id: string, @Body() unit: IProgress) {
  - return Progress.findByIdAndUpdate(id, unit)
  - .then(u => u.toObject());
  + async updateProgress(@Param('id') id: string, @Body() data: any) {
  + const unit: any = await ProgressController.getUnit(data.unit);
  + ProgressController.checkDeadline(unit);
  + const progressClass = ProgressController.getProgressClassForType(data.type);
  + const updatedProgress = await progressClass.findByIdAndUpdate(id, data);
  +
  + return updatedProgress.toObject();
  }
  }
  