From bfe1aaaa79b89673793109f51a9b3ff303baad82 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Thu, 13 Aug 2026 07:22:44 -0700 Subject: [PATCH] Frontend can always just do versions[0] --- .../document-detail.component.spec.ts | 20 +++++++++---------- .../document-detail.component.ts | 11 ++++------ ...ocument-version-dropdown.component.spec.ts | 13 +++++++----- .../document-version-dropdown.component.ts | 14 ++++++------- 4 files changed, 28 insertions(+), 30 deletions(-) diff --git a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts index 17fa6cd1f..ecd53fbeb 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts +++ b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts @@ -467,13 +467,6 @@ describe('DocumentDetailComponent', () => { const docWithVersions = { ...doc, versions: [ - { - id: doc.id, - added: new Date('2024-01-01T00:00:00Z'), - version_label: 'Original', - checksum: 'aaaa', - is_root: true, - }, { id: 10, added: new Date('2024-01-02T00:00:00Z'), @@ -481,6 +474,13 @@ describe('DocumentDetailComponent', () => { checksum: 'bbbb', is_root: false, }, + { + id: doc.id, + added: new Date('2024-01-01T00:00:00Z'), + version_label: 'Original', + checksum: 'aaaa', + is_root: true, + }, ], } as Document @@ -1232,8 +1232,8 @@ describe('DocumentDetailComponent', () => { metadataSpy.mockClear() component.document().versions = [ - { id: doc.id, is_root: true }, { id: 10, is_root: false }, + { id: doc.id, is_root: true }, ] as any jest.spyOn(documentService, 'getPreviewUrl').mockReturnValue('preview-root') jest.spyOn(documentService, 'getThumbUrl').mockReturnValue('thumb-root') @@ -1929,8 +1929,8 @@ describe('DocumentDetailComponent', () => { component.documentId.set(doc.id) component.document.set({ ...doc, versions: [] } as Document) const updatedVersions = [ - { id: doc.id, is_root: true }, { id: 10, is_root: false }, + { id: doc.id, is_root: true }, ] as any const openDoc = { ...doc, versions: [] } as Document jest.spyOn(openDocumentsService, 'getOpenDocument').mockReturnValue(openDoc) @@ -2046,8 +2046,8 @@ describe('DocumentDetailComponent', () => { it('should include version in download and print only for non-latest selected version', () => { initNormally() component.document().versions = [ - { id: doc.id, is_root: true }, { id: 10, is_root: false }, + { id: doc.id, is_root: true }, ] as any const getDownloadUrlSpy = jest diff --git a/src-ui/src/app/components/document-detail/document-detail.component.ts b/src-ui/src/app/components/document-detail/document-detail.component.ts index 4c061169f..225d536cf 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.ts +++ b/src-ui/src/app/components/document-detail/document-detail.component.ts @@ -889,13 +889,9 @@ export class DocumentDetailComponent updateComponent(doc: Document) { this.document.set(doc) - // Default selected version is the newest version + // Default selected version is the newest version, which the API returns first const versions = doc.versions ?? [] - this.selectedVersionId.set( - versions.length - ? Math.max(...versions.map((version) => version.id)) - : doc.id - ) + this.selectedVersionId.set(versions.length ? versions[0].id : doc.id) this.previewLoaded.set(false) this.requiresPassword = false this.updateFormForCustomFields() @@ -1441,7 +1437,8 @@ export class DocumentDetailComponent if (!versions.length || !this.selectedVersionId()) { return null } - const latestVersionId = Math.max(...versions.map((version) => version.id)) + // The API returns versions newest first + const latestVersionId = versions[0].id return this.selectedVersionId() === latestVersionId ? null : this.selectedVersionId() diff --git a/src-ui/src/app/components/document-detail/document-version-dropdown/document-version-dropdown.component.spec.ts b/src-ui/src/app/components/document-detail/document-version-dropdown/document-version-dropdown.component.spec.ts index 9ec9b4e63..f69f967ab 100644 --- a/src-ui/src/app/components/document-detail/document-version-dropdown/document-version-dropdown.component.spec.ts +++ b/src-ui/src/app/components/document-detail/document-version-dropdown/document-version-dropdown.component.spec.ts @@ -234,9 +234,10 @@ describe('DocumentVersionDropdownComponent', () => { }) it('onVersionFileSelected should upload and update versions after websocket success', () => { + // Newest first, as the API returns them const versions: DocumentVersionInfo[] = [ - { id: 3, is_root: true, checksum: 'aaaa' }, { id: 20, is_root: false, checksum: 'cccc' }, + { id: 3, is_root: true, checksum: 'aaaa' }, ] const file = new File(['test'], 'new-version.pdf', { type: 'application/pdf', @@ -348,9 +349,11 @@ describe('DocumentVersionDropdownComponent', () => { } modalService.open.mockReturnValue(modal as any) documentService.mergeDocumentsAsVersions.mockReturnValue(of({} as any)) + // Newest first, as the API returns them. The merged document has a lower id + // than the root, which is the whole point of merging an existing document. const versions: DocumentVersionInfo[] = [ + { id: 2, is_root: false, checksum: 'cccc' }, { id: 3, is_root: true, checksum: 'aaaa' }, - { id: 20, is_root: false, checksum: 'cccc' }, ] documentService.getVersions.mockReturnValue(of({ id: 3, versions } as any)) component.newVersionLabel = ' Imported ' @@ -359,17 +362,17 @@ describe('DocumentVersionDropdownComponent', () => { component.addExistingDocumentAsVersion() expect(modal.componentInstance.rootDocumentID).toEqual(3) - confirmClicked.next(20) + confirmClicked.next(2) expect(documentService.mergeDocumentsAsVersions).toHaveBeenCalledWith( - [3, 20], + [3, 2], 3, 'Imported' ) expect(documentService.updateVersionLabel).not.toHaveBeenCalled() expect(documentService.getVersions).toHaveBeenCalledWith(3) expect(versionsEmitSpy).toHaveBeenCalledWith(versions) - expect(selectedEmitSpy).toHaveBeenCalledWith(20) + expect(selectedEmitSpy).toHaveBeenCalledWith(2) expect(component.newVersionLabel).toEqual('') expect(modal.close).toHaveBeenCalled() expect(toastService.showInfo).toHaveBeenCalled() diff --git a/src-ui/src/app/components/document-detail/document-version-dropdown/document-version-dropdown.component.ts b/src-ui/src/app/components/document-detail/document-version-dropdown/document-version-dropdown.component.ts index 236236948..a46fa972d 100644 --- a/src-ui/src/app/components/document-detail/document-version-dropdown/document-version-dropdown.component.ts +++ b/src-ui/src/app/components/document-detail/document-version-dropdown/document-version-dropdown.component.ts @@ -258,11 +258,10 @@ export class DocumentVersionDropdownComponent implements OnChanges, OnDestroy { .subscribe({ next: (doc) => { if (uploadDocumentId !== this.documentId) return - if (doc?.versions) { + if (doc?.versions?.length) { this.versionsUpdated.emit(doc.versions) - this.versionSelected.emit( - Math.max(...doc.versions.map((version) => version.id)) - ) + // The API returns versions newest first + this.versionSelected.emit(doc.versions[0].id) this.clearVersionUploadStatus() } }, @@ -308,11 +307,10 @@ export class DocumentVersionDropdownComponent implements OnChanges, OnDestroy { ) .subscribe({ next: (document) => { - if (document?.versions) { + if (document?.versions?.length) { this.versionsUpdated.emit(document.versions) - this.versionSelected.emit( - Math.max(...document.versions.map((version) => version.id)) - ) + // The API returns versions newest first + this.versionSelected.emit(document.versions[0].id) } this.newVersionLabel = '' modal.close()