SHARE
TWEET

Untitled

a guest Sep 17th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { ActivatedRouteSnapshot, Resolve } from '@angular/router';
  3. import { Observable } from 'rxjs';
  4. import { map, tap } from 'rxjs/operators';
  5.  
  6. import { AssessmentService } from '../services/assessment.service';
  7. import { MinifiedUserModel } from '@shared/models';
  8. import { HelpersService } from '@shared/services';
  9. import { AssessmentExtended } from '../models/assessment-extended.model';
  10. import { AssessmentReviewStatuses } from '../../../enums';
  11. import { AssessmentUsers } from '../models/assessment-users.model';
  12. import { CounterStatuses } from '../models';
  13.  
  14. @Injectable({
  15.   providedIn: 'root'
  16. })
  17.  
  18. export class AssessmentViewResolver implements Resolve<Observable<AssessmentExtended>> {
  19.   assessmentReviewStatuses = AssessmentReviewStatuses;
  20.   assessmentStatuses = {
  21.     notStarted: 0,
  22.     inProgress: 0,
  23.     done: 0,
  24.     rejected: 0
  25.   };
  26.   reviewers = 0;
  27.  
  28.   constructor(private readonly assessmentService: AssessmentService,
  29.               private readonly helperService: HelpersService) {}
  30.  
  31.   resolve(activeSnapshot: ActivatedRouteSnapshot): Observable<AssessmentExtended> {
  32.     const id = +activeSnapshot.params.id;
  33.  
  34.     const users: MinifiedUserModel[] = this.helperService.resolveRouteParam(activeSnapshot, 'minifiedUsers');
  35.  
  36.     return this.assessmentService.getAssessmentView(id).pipe(
  37.       map(assessment => ({
  38.         ...assessment,
  39.         author: users.find((user: MinifiedUserModel) => user.id === assessment.authorId),
  40.         revieweesList: assessment.reviewees.map(userObj => {
  41.           return {
  42.             user: users.find((userItem: MinifiedUserModel) => userItem.id === userObj.userId),
  43.             reviewers: userObj.reviewers.map(reviewerItem => {
  44.               const reviewer = users.find((userItem: MinifiedUserModel) =>
  45.                   userItem.id === (reviewerItem as any as MinifiedUserReviewer).userId) as MinifiedUserModelWithStatus;
  46.  
  47.               const review = assessment.reviews.find((reviewItem: MinifiedUserReview) =>
  48.                 reviewer && (reviewerItem as any as MinifiedUserReviewer).revieweeId === reviewItem.revieweeId
  49.                 && (reviewerItem as any as MinifiedUserReviewer).id === reviewItem.reviewerId);
  50.  
  51.               return {...reviewer, ...{statusReview: review.status}};
  52.             })
  53.           };
  54.         })
  55.       } as any as AssessmentExtended)),
  56.       tap((assessment: AssessmentExtended) => {
  57.         assessment.revieweesList.map((reviewee: AssessmentUsers, i: number) => {
  58.           assessment.revieweesList[i] = this.countPercentages(reviewee);
  59.  
  60.           return assessment;
  61.         });
  62.       }),
  63.       map(assessment => ({
  64.         ...assessment,
  65.         statusesTotalSum: this.countTotalPercentages(this.assessmentStatuses, this.reviewers)
  66.       } as any as AssessmentExtended))
  67.     );
  68.   }
  69.  
  70.   private countPercentages(reviewee: AssessmentUsers): AssessmentUsers {
  71.     this.reviewers += reviewee.reviewers.length;
  72.     reviewee.statuses = {
  73.       notStarted: 0,
  74.       inProgress: 0,
  75.       done: 0,
  76.       rejected: 0
  77.     };
  78.     for (const reviewer of reviewee.reviewers) {
  79.       switch (reviewer.statusReview) {
  80.         case this.assessmentReviewStatuses.notStarted:
  81.           reviewee.statuses.notStarted++;
  82.           this.assessmentStatuses.notStarted++;
  83.           break;
  84.         case this.assessmentReviewStatuses.inProgress:
  85.           reviewee.statuses.inProgress++;
  86.           this.assessmentStatuses.inProgress++;
  87.           break;
  88.         case this.assessmentReviewStatuses.done:
  89.           reviewee.statuses.done++;
  90.           this.assessmentStatuses.done++;
  91.           break;
  92.         default:
  93.           reviewee.statuses.rejected++;
  94.           this.assessmentStatuses.rejected++;
  95.       }
  96.     }
  97.  
  98.     reviewee.statuses = this.countTotalPercentages(reviewee.statuses, reviewee.reviewers.length);
  99.  
  100.     return reviewee;
  101.   }
  102.  
  103.   private countTotalPercentages(statuses: CounterStatuses, lengthValue: number) {
  104.     const transformedArr = this.helperService.transformPercentages(
  105.       [this.helperService.roundNumbers(statuses.notStarted, lengthValue),
  106.         this.helperService.roundNumbers(statuses.inProgress, lengthValue),
  107.         this.helperService.roundNumbers(statuses.done, lengthValue),
  108.         this.helperService.roundNumbers(statuses.rejected, lengthValue)
  109.       ]);
  110.  
  111.     Object.keys(statuses).forEach((key: string, index: number) => {
  112.       ((statuses as any)[key]) = transformedArr.find((_value: number, idx: number) => index === idx);
  113.     });
  114.  
  115.     return statuses;
  116.   }
  117. }
  118.  
  119. export interface MinifiedUserModelWithStatus extends MinifiedUserModel {
  120.   statusReview: number;
  121. }
  122.  
  123. export interface MinifiedUserReviewer {
  124.   id: number;
  125.   revieweeId: number;
  126.   userId: number;
  127. }
  128.  
  129. export interface MinifiedUserReview {
  130.   id: number;
  131.   revieweeId: number;
  132.   reviewerId: number;
  133.   status: number;
  134.   assessmentId: number;
  135.   answer: [];
  136. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top