Skip to content
Snippets Groups Projects
Commit c157e5c3 authored by Isaac Evavold's avatar Isaac Evavold Committed by Isaac Evavold
Browse files

ROENROLL-1701 fix course dup moving from future to active term

parent 8e49ee38
No related branches found
No related tags found
No related merge requests found
Pipeline #37149 passed
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects'; import { Actions, Effect, ofType } from '@ngrx/effects';
import { of } from 'rxjs'; import { of, forkJoin } from 'rxjs';
import { import {
tap, tap,
map, map,
...@@ -57,40 +57,43 @@ export class CourseEffects { ...@@ -57,40 +57,43 @@ export class CourseEffects {
flatMap(([action, degreePlan]) => { flatMap(([action, degreePlan]) => {
const roadmapId = degreePlan.roadmapId; const roadmapId = degreePlan.roadmapId;
const activeTerms = this.constants.activeTermCodes(); const activeTerms = this.constants.activeTermCodes();
const recordId = action.payload.id; const {
const termCode = action.payload.to; id: recordId,
const fromTermCode = action.payload.from; to: toTermCode,
subjectCode,
// When adding to an active term use cart endpoint courseId,
if (activeTerms.some(term => term.equals(termCode))) { } = action.payload;
const subjectCode = action.payload.subjectCode;
const courseId = action.payload.courseId; const toActiveTerm = activeTerms.some(t => t.equals(toTermCode));
const moveCourse = this.api.updateCourseTerm(
roadmapId,
recordId,
toTermCode,
);
const addCourseToCart = this.api.addCourseToCart( if (toActiveTerm) {
/**
* The `updateCourseTerm` API won't force cart validation which we want
* if we're adding a course to the cart. Calling the `addCourseToCart`
* API when moving a cart to an active term will trigger the cart
* validation.
*/
const validateCart = this.api.addCourseToCart(
subjectCode, subjectCode,
courseId, courseId,
termCode, toTermCode,
); );
// If both terms are active terms, we have to remove course return forkJoin(moveCourse, validateCart).pipe(map(() => action));
if (activeTerms.some(term => term.equals(fromTermCode))) {
return addCourseToCart.pipe(
mergeMap(() => this.api.removeCourse(roadmapId, recordId)),
map(() => action),
);
} else {
return addCourseToCart.pipe(map(() => action));
}
} else { } else {
return this.api return moveCourse.pipe(map(() => action));
.updateCourseTerm(roadmapId, recordId, termCode)
.pipe(map(() => action));
} }
}), }),
map(action => new MoveCourseBetweenTermsSuccess(action.payload)), map(action => new MoveCourseBetweenTermsSuccess(action.payload)),
tap(state => { tap(action => {
const touchedTerm = state.payload.to.description; const touchedTerm = action.payload.to.description;
const message = `Course has been moved to ${touchedTerm}`; const message = `Course has been moved to ${touchedTerm}`;
this.snackBar.open(message, undefined, {}); this.snackBar.open(message, undefined, {});
}), }),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment