From c157e5c3437a7afad2293b559d378d00d08a28a6 Mon Sep 17 00:00:00 2001
From: ievavold <ievavold@wisc.edu>
Date: Wed, 1 May 2019 11:46:29 -0500
Subject: [PATCH] ROENROLL-1701 fix course dup moving from future to active
 term

---
 .../store/effects/course.effects.ts           | 53 ++++++++++---------
 1 file changed, 28 insertions(+), 25 deletions(-)

diff --git a/src/app/degree-planner/store/effects/course.effects.ts b/src/app/degree-planner/store/effects/course.effects.ts
index 990bf19..ab36ab3 100644
--- a/src/app/degree-planner/store/effects/course.effects.ts
+++ b/src/app/degree-planner/store/effects/course.effects.ts
@@ -1,6 +1,6 @@
 import { Injectable } from '@angular/core';
 import { Actions, Effect, ofType } from '@ngrx/effects';
-import { of } from 'rxjs';
+import { of, forkJoin } from 'rxjs';
 import {
   tap,
   map,
@@ -57,40 +57,43 @@ export class CourseEffects {
     flatMap(([action, degreePlan]) => {
       const roadmapId = degreePlan.roadmapId;
       const activeTerms = this.constants.activeTermCodes();
-      const recordId = action.payload.id;
-      const termCode = action.payload.to;
-      const fromTermCode = action.payload.from;
-
-      // When adding to an active term use cart endpoint
-      if (activeTerms.some(term => term.equals(termCode))) {
-        const subjectCode = action.payload.subjectCode;
-        const courseId = action.payload.courseId;
+      const {
+        id: recordId,
+        to: toTermCode,
+        subjectCode,
+        courseId,
+      } = action.payload;
+
+      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,
           courseId,
-          termCode,
+          toTermCode,
         );
-        // If both terms are active terms, we have to remove course
-        if (activeTerms.some(term => term.equals(fromTermCode))) {
-          return addCourseToCart.pipe(
-            mergeMap(() => this.api.removeCourse(roadmapId, recordId)),
-            map(() => action),
-          );
-        } else {
-          return addCourseToCart.pipe(map(() => action));
-        }
+        return forkJoin(moveCourse, validateCart).pipe(map(() => action));
       } else {
-        return this.api
-          .updateCourseTerm(roadmapId, recordId, termCode)
-          .pipe(map(() => action));
+        return moveCourse.pipe(map(() => action));
       }
     }),
 
     map(action => new MoveCourseBetweenTermsSuccess(action.payload)),
 
-    tap(state => {
-      const touchedTerm = state.payload.to.description;
+    tap(action => {
+      const touchedTerm = action.payload.to.description;
       const message = `Course has been moved to ${touchedTerm}`;
       this.snackBar.open(message, undefined, {});
     }),
-- 
GitLab