|
| 1 | +import { |
| 2 | + AsyncPipe, |
| 3 | + NgIf, |
| 4 | +} from '@angular/common'; |
| 5 | +import { |
| 6 | + Component, |
| 7 | + Input, |
| 8 | + OnInit, |
| 9 | +} from '@angular/core'; |
| 10 | +import { TranslateModule } from '@ngx-translate/core'; |
| 11 | +import { |
| 12 | + differenceInDays, |
| 13 | + differenceInMilliseconds, |
| 14 | + parseISO, |
| 15 | +} from 'date-fns'; |
| 16 | +import { |
| 17 | + BehaviorSubject, |
| 18 | + Observable, |
| 19 | + switchMap, |
| 20 | +} from 'rxjs'; |
| 21 | +import { |
| 22 | + map, |
| 23 | + take, |
| 24 | +} from 'rxjs/operators'; |
| 25 | + |
| 26 | +import { AuthorizationDataService } from '../../../../core/data/feature-authorization/authorization-data.service'; |
| 27 | +import { FeatureID } from '../../../../core/data/feature-authorization/feature-id'; |
| 28 | +import { ItemDataService } from '../../../../core/data/item-data.service'; |
| 29 | +import { Item } from '../../../../core/shared/item.model'; |
| 30 | +import { getFirstCompletedRemoteData } from '../../../../core/shared/operators'; |
| 31 | +import { hasValue } from '../../../empty.util'; |
| 32 | + |
| 33 | +@Component({ |
| 34 | + selector: 'ds-in-workflow-statistics', |
| 35 | + templateUrl: './in-workflow-statistics.component.html', |
| 36 | + standalone: true, |
| 37 | + styleUrls: ['./in-workflow-statistics.component.scss'], |
| 38 | + imports: [ |
| 39 | + TranslateModule, |
| 40 | + AsyncPipe, |
| 41 | + NgIf, |
| 42 | + ], |
| 43 | +}) |
| 44 | +export class InWorkflowStatisticsComponent implements OnInit { |
| 45 | + |
| 46 | + @Input() item: Item; |
| 47 | + |
| 48 | + canViewInWorkflowSinceStatistics$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false); |
| 49 | + canViewInWorkflowForDate$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false); |
| 50 | + canViewInWorkflowSinceDate$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false); |
| 51 | + inWorkflowFor$: BehaviorSubject<string> = new BehaviorSubject<string>(null); |
| 52 | + inWorkflowSince$: BehaviorSubject<string> = new BehaviorSubject<string>(null); |
| 53 | + |
| 54 | + constructor( |
| 55 | + private authorizationService: AuthorizationDataService, |
| 56 | + private itemDataService: ItemDataService, |
| 57 | + ) { |
| 58 | + } |
| 59 | + |
| 60 | + ngOnInit(): void { |
| 61 | + this.canViewInWorkflowSinceStatistics().pipe(take(1)) |
| 62 | + .subscribe((result: boolean) => { |
| 63 | + if (result) { |
| 64 | + this.initWorkflowDates(); |
| 65 | + } |
| 66 | + this.canViewInWorkflowSinceStatistics$.next(result); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + public canViewInWorkflowSinceStatistics(): Observable<boolean> { |
| 71 | + if (hasValue(this.item?.self)) { |
| 72 | + return this.isUserAuthorizedToViewItemInWorkflow(this.item.self); |
| 73 | + } else { |
| 74 | + // If self link has no value we fetch again the item from the rest. |
| 75 | + // Since at this stage the item has most likely already been fetched we will get it from the cache |
| 76 | + return this.itemDataService.findById(this.item.id).pipe( |
| 77 | + getFirstCompletedRemoteData(), |
| 78 | + map(data => data.payload.self), |
| 79 | + switchMap(selfLink => this.isUserAuthorizedToViewItemInWorkflow(selfLink)), |
| 80 | + ); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public getDateForArchivedItem(itemStartDate: string, dateAccessioned: string) { |
| 85 | + const itemStartDateConverted: Date = parseISO(itemStartDate); |
| 86 | + const dateAccessionedConverted: Date = parseISO(dateAccessioned); |
| 87 | + const days: number = Math.max(0, Math.floor(differenceInDays(dateAccessionedConverted, itemStartDateConverted))); |
| 88 | + const remainingMilliseconds: number = differenceInMilliseconds(dateAccessionedConverted, itemStartDateConverted) - days * 24 * 60 * 60 * 1000; |
| 89 | + const hours: number = Math.max(0, Math.floor(remainingMilliseconds / (60 * 60 * 1000))); |
| 90 | + return `${days} d ${hours} h`; |
| 91 | + } |
| 92 | + |
| 93 | + public getDateForItem(itemStartDate: string) { |
| 94 | + const itemStartDateConverted: Date = parseISO(itemStartDate); |
| 95 | + const days: number = Math.max(0, Math.floor(differenceInDays(Date.now(), itemStartDateConverted))); |
| 96 | + const remainingMilliseconds: number = differenceInMilliseconds(Date.now(), itemStartDateConverted) - days * 24 * 60 * 60 * 1000; |
| 97 | + const hours: number = Math.max(0, Math.floor(remainingMilliseconds / (60 * 60 * 1000))); |
| 98 | + return `${days} d ${hours} h`; |
| 99 | + } |
| 100 | + |
| 101 | + initWorkflowDates(): void { |
| 102 | + if (this.item) { |
| 103 | + if (this.item.isArchived) { |
| 104 | + if (this.item.hasMetadata('dspace.workflow.startDateTime') && this.item.hasMetadata('dc.date.accessioned')) { |
| 105 | + this.canViewInWorkflowForDate$.next(true); |
| 106 | + this.inWorkflowFor$.next( |
| 107 | + this.getDateForArchivedItem(this.item.firstMetadataValue('dspace.workflow.startDateTime'), this.item.firstMetadataValue('dc.date.accessioned')), |
| 108 | + ); |
| 109 | + } |
| 110 | + } else if (this.item.hasMetadata('dspace.workflow.startDateTime')) { |
| 111 | + this.canViewInWorkflowSinceDate$.next(true); |
| 112 | + this.inWorkflowSince$.next( |
| 113 | + this.getDateForItem(this.item.firstMetadataValue('dspace.workflow.startDateTime')), |
| 114 | + ); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private isUserAuthorizedToViewItemInWorkflow(selfLink: string): Observable<boolean> { |
| 120 | + return this.authorizationService.isAuthorized(FeatureID.CanViewInWorkflowSinceStatistics, selfLink); |
| 121 | + } |
| 122 | +} |
0 commit comments