From 421e78a74809f71ef106316113080b39bb340e8e Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Mon, 7 Aug 2023 15:48:00 -0700 Subject: [PATCH] Handle very old date strings in correspondent list --- .../document-detail.component.spec.ts | 2 +- .../correspondent-list.component.spec.ts | 29 +++++++++++++++---- .../correspondent-list.component.ts | 14 ++++++++- .../src/app/data/paperless-correspondent.ts | 2 +- 4 files changed, 39 insertions(+), 8 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 6ca99803c..dee1435b7 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 @@ -668,7 +668,7 @@ describe('DocumentDetailComponent', () => { const object = { id: 22, name: 'Correspondent22', - last_correspondence: new Date(), + last_correspondence: new Date().toISOString(), } as PaperlessCorrespondent const qfSpy = jest.spyOn(documentListViewService, 'quickFilter') component.filterDocuments([object]) diff --git a/src-ui/src/app/components/manage/correspondent-list/correspondent-list.component.spec.ts b/src-ui/src/app/components/manage/correspondent-list/correspondent-list.component.spec.ts index 4f68a609b..d2168d8bd 100644 --- a/src-ui/src/app/components/manage/correspondent-list/correspondent-list.component.spec.ts +++ b/src-ui/src/app/components/manage/correspondent-list/correspondent-list.component.spec.ts @@ -31,8 +31,12 @@ describe('CorrespondentListComponent', () => { ReactiveFormsModule, ], }).compileComponents() - correspondentsService = TestBed.inject(CorrespondentService) + }) + + // Tests are included in management-list.compontent.spec.ts + + it('should use correct delete message', () => { jest.spyOn(correspondentsService, 'listFiltered').mockReturnValue( of({ count: 3, @@ -56,15 +60,30 @@ describe('CorrespondentListComponent', () => { fixture = TestBed.createComponent(CorrespondentListComponent) component = fixture.componentInstance fixture.detectChanges() - }) - // Tests are included in management-list.compontent.spec.ts - - it('should use correct delete message', () => { expect( component.getDeleteMessage({ id: 1, name: 'Correspondent1' }) ).toEqual( 'Do you really want to delete the correspondent "Correspondent1"?' ) }) + + it('should support very old date strings', () => { + jest.spyOn(correspondentsService, 'listFiltered').mockReturnValue( + of({ + count: 1, + all: [1], + results: [ + { + id: 1, + name: 'Correspondent1', + last_correspondence: '1832-12-31T15:32:54-07:52:58', + }, + ], + }) + ) + fixture = TestBed.createComponent(CorrespondentListComponent) + component = fixture.componentInstance + fixture.detectChanges() + }) }) diff --git a/src-ui/src/app/components/manage/correspondent-list/correspondent-list.component.ts b/src-ui/src/app/components/manage/correspondent-list/correspondent-list.component.ts index d75d208ef..06f9d11b1 100644 --- a/src-ui/src/app/components/manage/correspondent-list/correspondent-list.component.ts +++ b/src-ui/src/app/components/manage/correspondent-list/correspondent-list.component.ts @@ -44,7 +44,19 @@ export class CorrespondentListComponent extends ManagementListComponent { - return this.datePipe.transform(c.last_correspondence) + if (c.last_correspondence) { + let date = new Date(c.last_correspondence) + if (date.toString() == 'Invalid Date') { + // very old date strings are unable to be parsed + date = new Date( + c.last_correspondence + ?.toString() + .replace(/-(\d\d):\d\d:\d\d/gm, `-$1:00`) + ) + } + return this.datePipe.transform(date) + } + return '' }, }, ] diff --git a/src-ui/src/app/data/paperless-correspondent.ts b/src-ui/src/app/data/paperless-correspondent.ts index 142efd452..89aa6a694 100644 --- a/src-ui/src/app/data/paperless-correspondent.ts +++ b/src-ui/src/app/data/paperless-correspondent.ts @@ -1,5 +1,5 @@ import { MatchingModel } from './matching-model' export interface PaperlessCorrespondent extends MatchingModel { - last_correspondence?: Date + last_correspondence?: string // Date }