Fix: handle document notes user format api change (#5751)

This commit is contained in:
shamoon 2024-02-13 09:25:35 -08:00 committed by GitHub
parent 907b6d1294
commit d83bbdc50b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 41 additions and 14 deletions

View File

@ -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: [

View File

@ -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)

View File

@ -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)

View File

@ -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
}