99 flat ,
1010} from 'adminjs' ;
1111
12- import { MISSING_USER_ID_ERROR } from './errors' ;
1312import { ADMINJS_LOGGER_DEFAULT_RESOURCE_ID } from './constants' ;
13+ import { MISSING_USER_ID_ERROR } from './errors' ;
1414import { CreateLogActionParams , LoggerFeatureOptions } from './types' ;
1515import { difference } from './utils/difference' ;
1616import { getLogPropertyName } from './utils/get-log-property-name' ;
@@ -42,16 +42,12 @@ export const rememberInitialRecord: Before = async (
4242 return request ;
4343} ;
4444
45- const getRecordTitle = modifiedRecord => {
46- switch ( typeof modifiedRecord . title ) {
47- case 'string' :
48- return modifiedRecord . title ;
49- case 'function' :
50- return modifiedRecord . title ( ) ;
51- }
52- return (
53- Object . values ( modifiedRecord ) . find ( v => typeof v === 'string' ) ?? 'No title'
54- ) ;
45+ const getRecordTitle = ( modifiedRecord , currentAdmin ) => {
46+ const recordJson =
47+ typeof modifiedRecord . toJSON === 'function'
48+ ? modifiedRecord . toJSON ( currentAdmin )
49+ : modifiedRecord ;
50+ return recordJson ?. title ?? 'No title' ;
5551} ;
5652
5753export const createLogAction =
@@ -184,10 +180,11 @@ const createPersistLogAction =
184180 : flat . flatten < object , object > (
185181 JSON . parse ( JSON . stringify ( modifiedRecord ?. params ?? { } ) )
186182 ) ;
187-
188183 const logParams = {
189- [ getLogPropertyName ( 'recordTitle' , propertiesMapping ) ] :
190- getRecordTitle ( modifiedRecord ) ,
184+ [ getLogPropertyName ( 'recordTitle' , propertiesMapping ) ] : getRecordTitle (
185+ modifiedRecord ,
186+ currentAdmin
187+ ) ,
191188 [ getLogPropertyName ( 'resource' , propertiesMapping ) ] : params . resourceId ,
192189 [ getLogPropertyName ( 'action' , propertiesMapping ) ] : params . action ,
193190 [ getLogPropertyName ( 'recordId' , propertiesMapping ) ] :
@@ -204,11 +201,10 @@ const createPersistLogAction =
204201 )
205202 ) ,
206203 } ;
207-
208204 await Log . create ( logParams ) ;
209205 } catch ( e ) {
210206 /* The action should not fail nor display a message to the end-user
211- but we must log the error in server's console for developers */
207+ but we must log the error in server's console for developers */
212208 console . error ( e ) ;
213209 }
214210 } ;
0 commit comments