1- import { AdminForthPlugin , AdminForthDataTypes , AdminForthResourcePages , Filters } from "adminforth" ;
1+ import { AdminForthPlugin , parseBody , AdminForthDataTypes , AdminForthResourcePages , Filters } from "adminforth" ;
22import type { IAdminForth , IHttpServer , AdminForthResourceColumn , AdminForthResource , IAdminForthHttpResponse , AdminUser , AdminForthComponentDeclaration } from "adminforth" ;
33import type { PluginOptions } from './types.js' ;
44import { error } from "console" ;
5-
5+ import { z } from "zod" ;
6+
7+ const deactivateUserBodySchema = z . object ( {
8+ record : z . record ( z . string ( ) , z . unknown ( ) ) ,
9+ } ) . strict ( ) ;
10+
611export default class UserSoftDelete extends AdminForthPlugin {
712 options : PluginOptions ;
813 allowDisableFunc : Function | null | boolean = null ;
@@ -108,7 +113,12 @@ export default class UserSoftDelete extends AdminForthPlugin {
108113 server . endpoint ( {
109114 method : 'POST' ,
110115 path : `/plugin/${ this . pluginInstanceId } /deactivateUser` ,
111- handler : async ( { adminUser, body } ) => {
116+ handler : async ( { adminUser, body, response } ) => {
117+ console . log ( `UserSoftDelete: deactivateUser endpoint called by ${ adminUser . dbUser [ this . resourceConfig . columns . find ( ( col ) => col . primaryKey ) . name ] } ` ) ;
118+ const parsed = parseBody ( deactivateUserBodySchema , body , response ) ;
119+ console . log ( `UserSoftDelete: parsed body: ${ JSON . stringify ( parsed ) } ` ) ;
120+ if ( 'error' in parsed ) return parsed . error ;
121+ const data = parsed . data ;
112122 let isAllowedToDeactivate = false ;
113123 if ( typeof this . allowDisableFunc === "function" ) {
114124 isAllowedToDeactivate = await this . allowDisableFunc ( adminUser ) ;
@@ -118,10 +128,10 @@ export default class UserSoftDelete extends AdminForthPlugin {
118128 }
119129
120130 const primaryKeyColumn = this . resourceConfig . columns . find ( ( col ) => col . primaryKey ) ;
121- if ( ! body . record ) {
131+ if ( ! data . record ) {
122132 return { ok : false , error : "No record provided" } ;
123133 }
124- const id = body . record [ primaryKeyColumn . name ] ;
134+ const id = data . record [ primaryKeyColumn . name ] ;
125135 if ( id === undefined || id === null ) {
126136 return { ok : false , error : "No record id provided" } ;
127137 }
0 commit comments