From ac80f1e545af34318d7c68cd573e15498c8c1423 Mon Sep 17 00:00:00 2001
From: Mark Ferguson <mferguso@nrao.edu>
Date: Wed, 30 Oct 2024 14:02:40 -0600
Subject: [PATCH] Wired up ability to save scheduler comments to TAC per the
 behavior outlined in STT-2116

---
 .../comments-for-tac.component.html           |  2 +-
 .../comments-for-tac.component.ts             | 50 +++++++++++++++++--
 2 files changed, 48 insertions(+), 4 deletions(-)

diff --git a/frontend/src/app/reviews/review-solicitation/prioritization-tools/comments-for-tac/comments-for-tac.component.html b/frontend/src/app/reviews/review-solicitation/prioritization-tools/comments-for-tac/comments-for-tac.component.html
index 786a0e32c..baed2a06c 100644
--- a/frontend/src/app/reviews/review-solicitation/prioritization-tools/comments-for-tac/comments-for-tac.component.html
+++ b/frontend/src/app/reviews/review-solicitation/prioritization-tools/comments-for-tac/comments-for-tac.component.html
@@ -19,5 +19,5 @@
 -->
 <form [formGroup]="commentForm" id="commentForm" class="py-2">
   <textarea id="comments" formControlName="comments" class="form-control" rows="12"></textarea>
-  <button type="button" class="btn btn-primary mt-2" [disabled]="isAVReadOnly()">Save</button>
+  <button type="button" class="btn btn-primary mt-2" [disabled]="isAVReadOnly()" (click)="saveCommentsForTAC()">Save</button>
 </form>
diff --git a/frontend/src/app/reviews/review-solicitation/prioritization-tools/comments-for-tac/comments-for-tac.component.ts b/frontend/src/app/reviews/review-solicitation/prioritization-tools/comments-for-tac/comments-for-tac.component.ts
index f653faf74..d185a497d 100644
--- a/frontend/src/app/reviews/review-solicitation/prioritization-tools/comments-for-tac/comments-for-tac.component.ts
+++ b/frontend/src/app/reviews/review-solicitation/prioritization-tools/comments-for-tac/comments-for-tac.component.ts
@@ -17,11 +17,15 @@
  * along with TTAT.  If not, see <https://www.gnu.org/licenses/>.
  *
  */
-import {ChangeDetectionStrategy, Component, effect, Signal} from '@angular/core';
+import {ChangeDetectionStrategy, Component, computed, effect, Signal} from '@angular/core';
 import {AsyncPipe, NgClass, NgFor, NgIf} from '@angular/common';
 import {FontAwesomeModule} from "@fortawesome/angular-fontawesome";
 import {FormControl, FormGroup, ReactiveFormsModule} from "@angular/forms";
 import {SelectionService} from "../../../../services/selection.service";
+import {AllocationDisposition} from "../../../../model/allocation-disposition";
+import {Proposal} from "../../../../model/proposal";
+import {AllocationDispositionService} from "../../../../services/allocation-disposition.service";
+import {faSave} from "@fortawesome/free-solid-svg-icons";
 
 @Component({
   selector: 'app-comments-for-tac',
@@ -40,16 +44,37 @@ import {SelectionService} from "../../../../services/selection.service";
 })
 export class CommentsForTacComponent {
 
-  isAVReadOnly: Signal<boolean> = this.selectedService.isAllocationVersionReadOnly;
+  isAVReadOnly: Signal<boolean> = this.selectionService.isAllocationVersionReadOnly;
+  selectedProposal: Signal<Proposal | undefined> = this.selectionService.selectedProposal;
+
+  allocationDispositions: Signal<Array<AllocationDisposition>> = computed(() => {
+    const allocationDispositions: Array<AllocationDisposition> = [];
+    const versionAllocationDispositions: Array<AllocationDisposition> = this.selectionService.allocationVersionAllocationDispositions();
+    const selectedProposal: Proposal | undefined = this.selectedProposal();
+    if (selectedProposal && versionAllocationDispositions.length > 0) {
+      for (const ad of versionAllocationDispositions) {
+        if (ad.allocationRequest.proposalCopyId == selectedProposal.observatoryCopy!.proposalCopyId) {
+          allocationDispositions.push(ad);
+        }
+      }
+    }
+    return allocationDispositions;
+  });
 
   commentForm = new FormGroup({
     comments: new FormControl<string|null>(null)
   });
 
-  constructor(private selectedService: SelectionService) {
+  constructor(
+    private selectionService: SelectionService,
+    private allocationDispositionService: AllocationDispositionService,
+  ) {
     /** disable the form if the av is readonly */
     effect(() => {
       const isReadOnly: boolean = this.isAVReadOnly();
+      const ads: Array<AllocationDisposition> = this.allocationDispositions();
+      const comment: string|null = ads.length > 0 ? ads[0].schedulerInternalCommentsToTac : null;
+      this.comments.setValue(comment ? comment: '');
       if (isReadOnly) {
         this.commentForm.disable();
       } else {
@@ -57,4 +82,23 @@ export class CommentsForTacComponent {
       }
     });
   }
+
+  saveCommentsForTAC(): void {
+    const ads: Array<AllocationDisposition> = this.allocationDispositions();
+    ads.forEach((ad: AllocationDisposition) => {
+      this.allocationDispositionService.update(
+        ad.state,
+        this.comments.value,
+        ad.allocationDispositionId
+      );
+      ad.schedulerInternalCommentsToTac = this.comments.value;
+    });
+  }
+
+  /** form getters */
+  get comments(): FormControl<string> {
+    return this.commentForm.get('comments') as FormControl<string>;
+  }
+
+  protected readonly faSave = faSave;
 }
-- 
GitLab