@@ -7,7 +7,7 @@ import type {
77import dayjs from 'dayjs'
88import utc from 'dayjs/plugin/utc.js' ;
99
10- import { AdminForthPlugin , AllowedActionsEnum , AdminForthSortDirections , AdminForthDataTypes , HttpExtra , ActionCheckSource , Filters , afLogger , } from "adminforth" ;
10+ import { AdminForthPlugin , AllowedActionsEnum , AdminForthSortDirections , AdminForthDataTypes , HttpExtra , ActionCheckSource , Filters , afLogger , encodeRecordId , isCompositePrimaryKey , primaryKeyColumnNames , } from "adminforth" ;
1111import { PluginOptions } from "./types.js" ;
1212
1313dayjs . extend ( utc ) ;
@@ -95,8 +95,8 @@ export default class AuditLogPlugin extends AdminForthPlugin {
9595 return { ok : true } ;
9696 }
9797
98- const recordIdFieldName = resource . columns . find ( ( c ) => c . primaryKey === true ) ?. name ;
99- const recordId = data ?. [ recordIdFieldName ] || oldRecord ?. [ recordIdFieldName ] ;
98+ // record id is built from all primary key columns, so resources with composite primary key are supported
99+ const recordId = encodeRecordId ( resource , { ... ( oldRecord || { } ) , ... ( data || { } ) } ) ;
100100 const connector = this . adminforth . connectors [ resource . dataSource ] ;
101101
102102 const newRecord = action == AllowedActionsEnum . delete ? { } : ( await connector . getRecordByPrimaryKey ( resource , recordId ) ) || { } ;
@@ -266,6 +266,8 @@ export default class AuditLogPlugin extends AdminForthPlugin {
266266 auditLogResourceId : this . auditLogResource ,
267267 resourceColumns : this . options . resourceColumns ,
268268 pkName : resource . columns . find ( ( c ) => c . primaryKey ) ?. name || 'id' ,
269+ // all primary key columns, so component can build record id of composite primary key resource
270+ pkNames : primaryKeyColumnNames ( resource ) ,
269271 title : 'Edit History'
270272 }
271273 }
@@ -331,6 +333,8 @@ export default class AuditLogPlugin extends AdminForthPlugin {
331333 auditLogResourceId : this . auditLogResource ,
332334 resourceColumns : this . options . resourceColumns ,
333335 pkName : resource . columns . find ( ( c ) => c . primaryKey ) ?. name || 'id' ,
336+ // all primary key columns, so component can build record id of composite primary key resource
337+ pkNames : primaryKeyColumnNames ( resource ) ,
334338 title : 'Edit History'
335339 }
336340 } ) ;
@@ -353,6 +357,12 @@ export default class AuditLogPlugin extends AdminForthPlugin {
353357 const recordIdColumn = resourceConfig . columns . find ( ( c ) => c . name === this . options . resourceColumns . resourceRecordIdColumnName ) ;
354358 if ( ! recordIdColumn . foreignResource ) {
355359 for ( const resource of existingResources ) {
360+ // core does not support foreignResource pointing to resource with composite primary key yet,
361+ // so such resources are still logged, but their record id is not clickable
362+ const targetResource = this . adminforth . config . resources . find ( ( r ) => r . resourceId === resource . value ) ;
363+ if ( targetResource && isCompositePrimaryKey ( targetResource ) ) {
364+ continue ;
365+ }
356366 if ( ! recordIdColumn . foreignResource ?. polymorphicResources ) {
357367 recordIdColumn . foreignResource = {
358368 polymorphicResources : [ ] ,
0 commit comments