1- import { AdminForthPlugin , AdminForthDataTypes , AdminForthResourcePages } from "adminforth" ;
1+ import { AdminForthPlugin , AdminForthDataTypes , AdminForthResourcePages , Filters } from "adminforth" ;
22import type { IAdminForth , IHttpServer , AdminForthResourceColumn , AdminForthResource , IAdminForthHttpResponse , AdminUser , AdminForthComponentDeclaration } from "adminforth" ;
33import type { PluginOptions } from './types.js' ;
4-
4+
55export default class UserSoftDelete extends AdminForthPlugin {
66 options : PluginOptions ;
7+ allowDisableFunc : Function | null | boolean = null ;
78
89 constructor ( options : PluginOptions ) {
910 super ( options , import . meta. url ) ;
@@ -12,19 +13,14 @@ export default class UserSoftDelete extends AdminForthPlugin {
1213
1314 async modifyResourceConfig ( adminforth : IAdminForth , resourceConfig : AdminForthResource ) {
1415 super . modifyResourceConfig ( adminforth , resourceConfig ) ;
15- let allowDisableFunc ;
1616 if ( this . options . canDeactivate ) {
17- //console.log('Using canDeactivate from plugin options');
18- allowDisableFunc = this . options . canDeactivate ;
17+ this . allowDisableFunc = this . options . canDeactivate ;
1918 } else if ( resourceConfig . options . allowedActions . delete && typeof resourceConfig . options . allowedActions . delete === 'function' ) {
20- //console.log('Using existing delete permission function from resource config');
21- allowDisableFunc = resourceConfig . options . allowedActions . delete ;
19+ this . allowDisableFunc = resourceConfig . options . allowedActions . delete ;
2220 } else if ( resourceConfig . options . allowedActions . delete && typeof resourceConfig . options . allowedActions . delete === 'boolean' ) {
23- //console.log('Using existing delete permission boolean from resource config:', resourceConfig.options.allowedActions.delete);
24- allowDisableFunc = async ( ) => resourceConfig . options . allowedActions . delete ;
21+ this . allowDisableFunc = async ( ) => resourceConfig . options . allowedActions . delete ;
2522 } else {
26- //console.log('No delete permission function found, defaulting to allow all');
27- allowDisableFunc = async ( ) => true ;
23+ this . allowDisableFunc = async ( ) => true ;
2824 }
2925
3026 resourceConfig . options . allowedActions . delete = false ;
@@ -72,7 +68,7 @@ export default class UserSoftDelete extends AdminForthPlugin {
7268 resourceConfig . options . pageInjections . list . threeDotsDropdownItems = [ ] ;
7369 }
7470 ( resourceConfig . options . pageInjections . list . threeDotsDropdownItems as AdminForthComponentDeclaration [ ] ) . push (
75- { file : this . componentPath ( 'DisableButton.vue' ) }
71+ { file : this . componentPath ( 'DisableButton.vue' ) , meta : { pluginInstanceId : this . pluginInstanceId } }
7672 ) ;
7773
7874 // simply modify resourceConfig or adminforth.config. You can get access to plugin options via this.options;
@@ -85,16 +81,44 @@ export default class UserSoftDelete extends AdminForthPlugin {
8581 instanceUniqueRepresentation ( pluginOptions : any ) : string {
8682 // optional method to return unique string representation of plugin instance.
8783 // Needed if plugin can have multiple instances on one resource
88- return `single ` ;
84+ return `user-soft-delete ` ;
8985 }
9086
9187 setupEndpoints ( server : IHttpServer ) {
9288 server . endpoint ( {
9389 method : 'POST' ,
94- path : `/plugin/${ this . pluginInstanceId } /example` ,
95- handler : async ( { body } ) => {
96- const { name } = body ;
97- return { hey : `Hello ${ name } ` } ;
90+ path : `/plugin/${ this . pluginInstanceId } /deactivateUser` ,
91+ handler : async ( { adminUser, body } ) => {
92+ let isAllowedToDeactivate = false ;
93+ if ( typeof this . allowDisableFunc === "function" ) {
94+ isAllowedToDeactivate = await this . allowDisableFunc ( adminUser ) ;
95+ } else if ( typeof this . allowDisableFunc === "boolean" ) {
96+ isAllowedToDeactivate = this . allowDisableFunc ;
97+ }
98+ if ( isAllowedToDeactivate === false ) {
99+ return { ok : false , error : "Not allowed to deactivate user" }
100+ }
101+ const id = body . record ;
102+ const primaryKeyColumn = this . resourceConfig . columns . find ( ( col ) => col . primaryKey ) ;
103+
104+ const oldUser = await this . adminforth
105+ . resource ( this . resourceConfig . resourceId )
106+ . get ( [ Filters . EQ ( primaryKeyColumn . name , id ) ] ) ;
107+
108+ if ( ! oldUser ) {
109+ throw new Error ( `User with id ${ id } not found` ) ;
110+ }
111+
112+ const newUser = { ...oldUser , [ this . options . activeFieldName ] : false } ;
113+
114+ await this . adminforth . updateResourceRecord ( {
115+ resource : this . resourceConfig ,
116+ recordId : id ,
117+ oldRecord : oldUser ,
118+ record : newUser ,
119+ adminUser : adminUser
120+ } )
121+ return { ok : true }
98122 }
99123 } ) ;
100124 }
0 commit comments