Skip to content
Snippets Groups Projects
Forked from an inaccessible project.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
new-audit-options.component.ts 5.76 KiB
import { Component, OnInit, Inject } from '@angular/core';
import { MatDialogRef, MatSnackBar } from '@angular/material';
import { DarsApiService } from '../services/api.service';
import { DegreeProgram, DegreePrograms } from '../models/degree-program';
import { SingleAuditRequest } from '../models/single-audit-request';
import { StudentDegreeProgram } from '../models/student-degree-program';
import { Observable } from 'rxjs';
import { Store, select } from '@ngrx/store';
import { GlobalState } from '@app/core/state';
import * as selectors from '../store/selectors';
import * as darsActions from '../store/actions';
import { DARSState } from '../store/state';
import { MAT_DIALOG_DATA } from '@angular/material';
import {
  FormBuilder,
  FormGroup,
  FormControl,
  AbstractControl,
} from '@angular/forms';
import { HonorsOption } from '../models/honors-option';
import { DegreePlan } from '@app/core/models/degree-plan';
import { CourseBase } from '@app/core/models/course';

@Component({
  selector: 'cse-new-audit-options',
  templateUrl: './new-audit-options.component.html',
  styleUrls: ['./new-audit-options.component.scss'],
})
export class NewAuditOptionsComponent implements OnInit {
  public newAuditForm: FormGroup;
  public degreeProgram: DegreeProgram;
  public degreePrograms: DegreePrograms;
  public programOfStudy: string;
  public studentDegreeProgram: StudentDegreeProgram[];
  public DARSprograms: DegreeProgram[] = [];
  public honorsOptions: HonorsOption[] = [];
  public courses: { termCode: string; courses: CourseBase[] }[];
  public degreePlans$: Observable<DARSState['degreePlans']>;
  public singleAuditRequest$: Observable<SingleAuditRequest>;
  public degreePlans: DegreePlan[];
  public primaryPlan: DegreePlan[];
  public primaryPlanId: string;
  public hasVariableCredits: boolean;
  public creditsRange: (CourseBase & { creditRange: number[] })[];
  public isLoaded: boolean;
  public selectedAuditType: string;
  public termsToInclude: { name: string; id: string; value: string }[];
  get formArray(): AbstractControl | null {
    return this.newAuditForm.get('formArray');
  }

  constructor(
    private fb: FormBuilder,
    private dialogRef: MatDialogRef<NewAuditOptionsComponent>,
    private snackBar: MatSnackBar,
    private api: DarsApiService,
    private store: Store<GlobalState>,
    @Inject(MAT_DIALOG_DATA)
    data: {
      selectedAuditType: string;
    },
  ) {
    this.selectedAuditType = 'declaredMajorAudit';
    this.termsToInclude = [
      {
        name: 'Previous, current, future, and planned terms',
        id: 'future-whatif',
        value: 'future',
      },
      {
        name: 'Previous, current, future',
        id: 'future-degree',
        value: 'future',
      },
      { name: 'Previous, current', id: 'current', value: 'current' },
      { name: 'Previous', id: 'previous', value: 'previous' },
    ];
  }

  ngOnInit() {
    // Get Degree plans
    this.degreePlans$ = this.store.pipe(select(selectors.degreePlans));
    this.degreePlans$.subscribe(plans => {
      this.degreePlans = plans;
      this.primaryPlan = plans.filter(function(plan) {
        return plan.primary === true;
      });
      this.primaryPlanId = this.primaryPlan[0].roadmapId.toString();
    });
    // Get student degree programs
    this.api.getStudentDegreePrograms().subscribe(studentDegreeProgram => {
      this.studentDegreeProgram = studentDegreeProgram;
    });

    this.api.getStaticData().subscribe(degreePrograms => {
      this.degreePrograms = degreePrograms;

      for (const institution of Object.values(degreePrograms)) {
        this.programOfStudy = institution.darsInstitutionCodeDescription;
        this.honorsOptions.push(...institution.honorsOptions);
        this.DARSprograms.push(...institution.programs);
      }
    });
    this.newAuditForm = this.fb.group({
      formArray: this.fb.array([
        this.fb.group({
          auditType: new FormControl(''),
        }),
        this.fb.group({
          darsDegreeProgram: new FormControl(''),
          darsInstitution: new FormControl(''),
          darsInstitutionProgram: new FormControl(''),
        }),
        this.fb.group({
          honors: new FormControl('Keep current status'),
          includeCoursesFrom: new FormControl(
            this.selectedAuditType === 'declaredMajorAudit'
              ? this.termsToInclude[1]
              : this.termsToInclude[0],
          ),
          runAgainst: new FormControl(this.primaryPlanId),
        }),
        this.fb.group({
          credits: new FormControl(''),
        }),
      ]),
    });
  }

  public getCoursesList(roadmapId) {
    this.isLoaded = false;

    this.api.getAllCourses(roadmapId).subscribe(courses => {
      this.courses = courses;
      this.creditsRange = [];
      this.isLoaded = true;
      this.hasVariableCredits = false;

      courses.forEach(term => {
        term.courses.forEach(course => {
          const { creditMin: min, creditMax: max } = course;
          if (min !== undefined && max !== undefined && min < max) {
            this.hasVariableCredits = true;
            const creditRange: number[] = [];
            for (let i = min; i <= max; i++) {
              creditRange.push(i);
            }
            this.creditsRange.push({ ...course, creditRange });
          }
        });
      });
    });
  }

  public runDARSAudit() {
    const {
      auditType,
      programOfStudy,
      academicProgram,
      honors,
      includeCoursesFrom,
      runAgainst,
      credits,
    } = this.newAuditForm.value;

    this.store.dispatch(
      new darsActions.StartSendingAudit({
        darsInstitutionCode: programOfStudy,
        darsDegreeProgramCode: academicProgram,
      }),
    );

    this.dialogRef.close();
    this.snackBar.open(
      'Audit in progress. You will be notified when the audit is ready to be viewed.',
    );
  }
}