From d83bbdc50bacf4e73ca5f2d02ed68a5fd03c90c3 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Tue, 13 Feb 2024 09:25:35 -0800 Subject: [PATCH] Fix: handle document notes user format api change (#5751) --- .../document-detail.component.spec.ts | 4 +- .../document-notes.component.spec.ts | 45 ++++++++++++++----- .../document-notes.component.ts | 3 +- src-ui/src/app/data/document-note.ts | 3 +- 4 files changed, 41 insertions(+), 14 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 a30588970..e0da11a3e 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 @@ -95,12 +95,12 @@ const doc: Document = { { created: new Date(), note: 'note 1', - user: 1, + user: { id: 1, username: 'user1' }, }, { created: new Date(), note: 'note 2', - user: 2, + user: { id: 2, username: 'user2' }, }, ], custom_fields: [ diff --git a/src-ui/src/app/components/document-notes/document-notes.component.spec.ts b/src-ui/src/app/components/document-notes/document-notes.component.spec.ts index 6957f1c74..4472d5ca5 100644 --- a/src-ui/src/app/components/document-notes/document-notes.component.spec.ts +++ b/src-ui/src/app/components/document-notes/document-notes.component.spec.ts @@ -19,22 +19,32 @@ const notes: DocumentNote[] = [ { id: 23, note: 'Note 23', - user: 1, + user: { + id: 1, + username: 'user1', + first_name: 'User1', + last_name: 'Lastname1', + }, }, { id: 24, note: 'Note 24', - user: 1, + user: { + id: 1, + username: 'user1', + first_name: 'User1', + last_name: 'Lastname1', + }, }, { id: 25, note: 'Note 25', - user: 2, + user: { id: 2, username: 'user2' }, }, { id: 30, note: 'Note 30', - user: 3, + user: { id: 3, username: 'user3' }, }, ] @@ -123,11 +133,24 @@ describe('DocumentNotesComponent', () => { }) it('should handle note user display in all situations', () => { - expect(component.displayName({ id: 1, user: 1 })).toEqual( - 'User1 Lastname1 (user1)' - ) - expect(component.displayName({ id: 1, user: 2 })).toEqual('user2') - expect(component.displayName({ id: 1, user: 4 })).toEqual('') + expect( + component.displayName({ + id: 1, + user: { + id: 1, + username: 'user1', + first_name: 'User1', + last_name: 'Lastname1', + }, + }) + ).toEqual('User1 Lastname1 (user1)') + expect( + component.displayName({ id: 1, user: { id: 2, username: 'user2' } }) + ).toEqual('user2') + expect(component.displayName({ id: 1, user: 2 } as any)).toEqual('user2') + expect( + component.displayName({ id: 1, user: { id: 4, username: 'user4' } }) + ).toEqual('') expect(component.displayName({ id: 1 })).toEqual('') }) @@ -146,7 +169,9 @@ describe('DocumentNotesComponent', () => { expect(addSpy).toHaveBeenCalledWith(12, note) expect(toastsSpy).toHaveBeenCalled() - addSpy.mockReturnValueOnce(of([...notes, { id: 31, note, user: 1 }])) + addSpy.mockReturnValueOnce( + of([...notes, { id: 31, note, user: { id: 1 } }]) + ) addButton.triggerEventHandler('click') fixture.detectChanges() expect(fixture.debugElement.nativeElement.textContent).toContain(note) diff --git a/src-ui/src/app/components/document-notes/document-notes.component.ts b/src-ui/src/app/components/document-notes/document-notes.component.ts index 45e1469d8..e35f08e7f 100644 --- a/src-ui/src/app/components/document-notes/document-notes.component.ts +++ b/src-ui/src/app/components/document-notes/document-notes.component.ts @@ -84,7 +84,8 @@ export class DocumentNotesComponent extends ComponentWithPermissions { displayName(note: DocumentNote): string { if (!note.user) return '' - const user = this.users?.find((u) => u.id === note.user) + const user_id = typeof note.user === 'number' ? note.user : note.user.id + const user = this.users?.find((u) => u.id === user_id) if (!user) return '' const nameComponents = [] if (user.first_name) nameComponents.push(user.first_name) diff --git a/src-ui/src/app/data/document-note.ts b/src-ui/src/app/data/document-note.ts index bc33df60c..e34a10c19 100644 --- a/src-ui/src/app/data/document-note.ts +++ b/src-ui/src/app/data/document-note.ts @@ -1,7 +1,8 @@ import { ObjectWithId } from './object-with-id' +import { User } from './user' export interface DocumentNote extends ObjectWithId { created?: Date note?: string - user?: number // User + user?: User }