@@ -11,7 +11,6 @@ import { interpretResource, ActionCheckSource } from "adminforth";
1111
1212export default class ForeignInlineListPlugin extends AdminForthPlugin {
1313 foreignResource : AdminForthResource ;
14- copyOfForeignResource : AdminForthResource ;
1514 options : PluginOptions ;
1615 adminforth : IAdminForth ;
1716
@@ -32,19 +31,76 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
3231 method : 'POST' ,
3332 path : `/plugin/${ this . pluginInstanceId } /get_resource` ,
3433 handler : async ( { body, adminUser } ) => {
35- const { allowedActions } = await interpretResource ( adminUser , this . copyOfForeignResource , { } , ActionCheckSource . DisplayButtons , this . adminforth ) ;
34+ const resource = this . adminforth . config . resources . find ( ( res ) => this . options . foreignResourceId === res . resourceId ) ;
35+ if ( ! resource ) {
36+ return { error : `Resource ${ this . options . foreignResourceId } not found` } ;
37+ }
38+ // exclude "plugins" key
39+ const resourceCopy = clone ( { ...resource , plugins : undefined } ) ;
40+
41+ if ( this . options . modifyTableResourceConfig ) {
42+ this . options . modifyTableResourceConfig ( resourceCopy ) ;
43+ }
44+
45+ const { allowedActions } = await interpretResource ( adminUser , resourceCopy , { } , ActionCheckSource . DisplayButtons , this . adminforth ) ;
3646
3747 return {
3848 resource : {
39- ...this . copyOfForeignResource ,
49+ ...resourceCopy ,
4050 options : {
41- ...this . copyOfForeignResource . options ,
51+ ...resourceCopy . options ,
4252 allowedActions,
4353 } ,
4454 }
4555 } ;
4656 }
4757 } ) ;
58+ server . endpoint ( {
59+ method : 'POST' ,
60+ path : `/plugin/${ this . pluginInstanceId } /start_bulk_action` ,
61+ handler : async ( { body, adminUser, tr } ) => {
62+ const { resourceId, actionId, recordIds } = body ;
63+ const resource = this . adminforth . config . resources . find ( ( res ) => res . resourceId == resourceId ) ;
64+ if ( ! resource ) {
65+ return { error : await tr ( `Resource {resourceId} not found` , 'errors' , { resourceId } ) } ;
66+ }
67+
68+ const resourceCopy = JSON . parse ( JSON . stringify ( { ...resource , plugins : undefined } ) ) ;
69+
70+
71+ if ( this . options . modifyTableResourceConfig ) {
72+ this . options . modifyTableResourceConfig ( resourceCopy ) ;
73+ }
74+
75+ const { allowedActions } = await interpretResource (
76+ adminUser ,
77+ resourceCopy ,
78+ { requestBody : body } ,
79+ ActionCheckSource . BulkActionRequest ,
80+ this . adminforth
81+ ) ;
82+
83+ const action = resourceCopy . options . bulkActions . find ( ( act ) => act . id == actionId ) ;
84+ if ( ! action ) {
85+ return { error : await tr ( `Action {actionId} not found` , 'errors' , { actionId } ) } ;
86+ }
87+
88+ if ( action . allowed ) {
89+ const execAllowed = await action . allowed ( { adminUser, resourceCopy, selectedIds : recordIds , allowedActions } ) ;
90+ if ( ! execAllowed ) {
91+ return { error : await tr ( `Action "{actionId}" not allowed` , 'errors' , { actionId : action . label } ) } ;
92+ }
93+ }
94+ const response = await action . action ( { selectedIds : recordIds , adminUser, resourceCopy, tr} ) ;
95+
96+ return {
97+ actionId,
98+ recordIds,
99+ resourceId,
100+ ...response
101+ }
102+ }
103+ } )
48104 server . endpoint ( {
49105 method : 'POST' ,
50106 path : `/plugin/${ this . pluginInstanceId } /get_default_filters` ,
0 commit comments