red-ui/libs/red-domain/src/lib/files/file.model.ts
2021-10-29 01:55:38 +03:00

145 lines
5.9 KiB
TypeScript

import { IListable, List } from '@iqser/common-ui';
import { StatusSorter } from '../shared';
import { FileStatus, FileStatuses } from './types';
import { IFile } from './file';
import { FileAttributes, IFileAttributesConfig } from '../file-attributes';
const processingStatuses: List<FileStatus> = [
FileStatuses.REPROCESS,
FileStatuses.FULLREPROCESS,
FileStatuses.OCR_PROCESSING,
FileStatuses.INDEXING,
FileStatuses.PROCESSING,
] as const;
export class File implements IFile, IListable {
readonly added?: string;
readonly allManualRedactionsApplied: boolean;
readonly analysisDuration?: number;
readonly analysisRequired: boolean;
readonly approvalDate?: string;
readonly currentReviewer?: string;
readonly dictionaryVersion?: number;
readonly dossierDictionaryVersion?: number;
readonly dossierId: string;
readonly excluded: boolean;
readonly fileAttributes?: FileAttributes;
readonly fileId: string;
readonly filename: string;
readonly hasAnnotationComments: boolean;
readonly hasHints: boolean;
readonly hasImages: boolean;
readonly hasRedactions: boolean;
readonly hasUpdates: boolean;
readonly lastOCRTime?: string;
readonly lastProcessed?: string;
readonly lastReviewer?: string;
readonly lastUpdated?: string;
readonly lastUploaded?: string;
readonly legalBasisVersion?: number;
readonly numberOfAnalyses?: number;
readonly numberOfPages?: number;
readonly rulesVersion?: number;
readonly status: FileStatus;
readonly uploader?: string;
readonly excludedPages?: number[];
readonly hasSuggestions: boolean;
readonly primaryAttribute?: string;
lastOpened = false;
readonly statusSort: number;
readonly cacheIdentifier?: string;
readonly hintsOnly: boolean;
readonly hasNone: boolean;
readonly isUnassigned: boolean;
readonly isError: boolean;
readonly isProcessing: boolean;
readonly isApproved: boolean;
readonly isPending: boolean;
readonly isUnderReview: boolean;
readonly isUnderApproval: boolean;
readonly canBeApproved: boolean;
readonly canBeOpened: boolean;
readonly isWorkable: boolean;
readonly canBeOCRed: boolean;
constructor(file: IFile, readonly reviewerName: string, fileAttributesConfig?: IFileAttributesConfig) {
this.added = file.added;
this.allManualRedactionsApplied = !!file.allManualRedactionsApplied;
this.analysisDuration = file.analysisDuration;
this.analysisRequired = !!file.analysisRequired && !file.excluded;
this.approvalDate = file.approvalDate;
this.currentReviewer = file.currentReviewer;
this.dictionaryVersion = file.dictionaryVersion;
this.dossierDictionaryVersion = file.dossierDictionaryVersion;
this.dossierId = file.dossierId;
this.excluded = !!file.excluded;
this.fileAttributes = file.fileAttributes;
this.fileId = file.fileId;
this.filename = file.filename;
this.hasAnnotationComments = !!file.hasAnnotationComments;
this.hasHints = !!file.hasHints;
this.hasImages = !!file.hasImages;
this.hasRedactions = !!file.hasRedactions;
this.hasUpdates = !!file.hasUpdates;
this.lastOCRTime = file.lastOCRTime;
this.lastProcessed = file.lastProcessed;
this.lastReviewer = file.lastReviewer;
this.lastUpdated = file.lastUpdated;
this.lastUploaded = file.lastUploaded;
this.legalBasisVersion = file.legalBasisVersion;
this.numberOfAnalyses = file.numberOfAnalyses;
this.status = ['REPROCESS', 'FULLREPROCESS', 'INDEXING'].includes(file.status) ? FileStatuses.PROCESSING : file.status;
this.isError = this.status === FileStatuses.ERROR;
this.numberOfPages = this.isError ? 0 : file.numberOfPages ?? 0;
this.rulesVersion = file.rulesVersion;
this.uploader = file.uploader;
this.excludedPages = file.excludedPages;
this.hasSuggestions = !!file.hasSuggestions;
this.statusSort = StatusSorter[this.status];
if (this.lastUpdated && this.lastOCRTime) {
this.cacheIdentifier = btoa((this.lastUploaded ?? '') + this.lastOCRTime);
}
this.hintsOnly = this.hasHints && !this.hasRedactions;
this.hasNone = !this.hasRedactions && !this.hasHints && !this.hasSuggestions;
this.isUnassigned = !this.currentReviewer;
this.isProcessing = processingStatuses.includes(this.status);
this.isApproved = this.status === FileStatuses.APPROVED;
this.isPending = this.status === FileStatuses.UNPROCESSED;
this.isUnderReview = this.status === FileStatuses.UNDER_REVIEW;
this.isUnderApproval = this.status === FileStatuses.UNDER_APPROVAL;
this.canBeApproved = !this.analysisRequired && !this.hasSuggestions;
this.canBeOpened = !this.isError && !this.isPending;
this.isWorkable = !this.isProcessing && this.canBeOpened;
this.canBeOCRed = !this.excluded && !this.lastOCRTime && ['UNASSIGNED', 'UNDER_REVIEW', 'UNDER_APPROVAL'].includes(this.status);
if (fileAttributesConfig) {
const primary = fileAttributesConfig.fileAttributeConfigs?.find(c => c.primaryAttribute);
if (primary && file.fileAttributes?.attributeIdToValue) {
this.primaryAttribute = file.fileAttributes?.attributeIdToValue[primary.id];
}
if (!this.primaryAttribute) {
// Fallback here
this.primaryAttribute = '-';
}
}
if (!this.fileAttributes || !this.fileAttributes.attributeIdToValue) {
this.fileAttributes = { attributeIdToValue: {} };
}
}
get id(): string {
return this.fileId;
}
get searchKey(): string {
return this.filename;
}
get routerLink(): string | undefined {
return this.canBeOpened ? `/main/dossiers/${this.dossierId}/file/${this.fileId}` : undefined;
}
}