@@ -503,54 +503,63 @@ export function slugifyString(str: string): string {
503503}
504504
505505export async function cascadeChildrenDelete ( resource : AdminForthResource , primaryKey : string , context : { adminUser : any , response : any } , adminforth : IAdminForth ) : Promise < { error : string | null } > {
506- const { adminUser, response } = context ;
506+ const { adminUser, response } = context ;
507507
508- const childResources = adminforth . config . resources . filter ( r => r . columns . some ( c => c . foreignResource ?. resourceId === resource . resourceId ) ) ;
508+ const childResources = adminforth . config . resources . filter ( r => r . columns . some ( c => c . foreignResource ?. resourceId === resource . resourceId ) ) ;
509509
510- for ( const childRes of childResources ) {
511- const foreignColumn = childRes . columns . find ( c => c . foreignResource ?. resourceId === resource . resourceId ) ;
510+ for ( const childRes of childResources ) {
511+ const foreignColumn = childRes . columns . find ( c => c . foreignResource ?. resourceId === resource . resourceId ) ;
512512
513- if ( ! foreignColumn ?. foreignResource ?. onDelete ) continue ;
513+ if ( ! foreignColumn ?. foreignResource ?. onDelete ) continue ;
514514
515- const strategy = foreignColumn . foreignResource . onDelete ;
515+ const strategy = foreignColumn . foreignResource . onDelete ;
516516
517- const childRecords = await adminforth . resource ( childRes . resourceId ) . list ( Filters . EQ ( foreignColumn . name , primaryKey ) ) ;
517+ const childRecords = await adminforth . resource ( childRes . resourceId ) . list ( Filters . EQ ( foreignColumn . name , primaryKey ) ) ;
518518
519- const childPk = childRes . columns . find ( c => c . primaryKey ) ?. name ;
519+ const childPk = childRes . columns . find ( c => c . primaryKey ) ?. name ;
520520
521- if ( strategy === 'cascade' ) {
522- for ( const childRecord of childRecords ) {
523- const childResult = await cascadeChildrenDelete ( childRes , childRecord [ childPk ] , context , adminforth ) ;
524- if ( childResult ?. error ) {
525- return childResult ;
526- }
527- const deleteChild = await adminforth . deleteResourceRecord ( { resource : childRes , record : childRecord , adminUser, recordId : childRecord [ childPk ] , response} ) ;
528- if ( deleteChild . error ) return { error : deleteChild . error } ;
529- if ( childResult ?. error ) {
530- return childResult ;
531- }
521+ if ( strategy === 'cascade' ) {
522+ for ( const childRecord of childRecords ) {
523+ const childResult = await cascadeChildrenDelete ( childRes , childRecord [ childPk ] , context , adminforth ) ;
524+ if ( childResult ?. error ) {
525+ return childResult ;
526+ }
527+ const deleteChild = await adminforth . deleteResourceRecord ( { resource : childRes , record : childRecord , adminUser, recordId : childRecord [ childPk ] , response} ) ;
528+ if ( deleteChild . error ) return { error : deleteChild . error } ;
529+ if ( childResult ?. error ) {
530+ return childResult ;
532531 }
533532 }
533+ }
534534
535- if ( strategy === 'setNull' ) {
536- for ( const childRecord of childRecords ) {
537- await adminforth . resource ( childRes . resourceId ) . update ( childRecord [ childPk ] , { [ foreignColumn . name ] : null } ) ;
538- }
535+ if ( strategy === 'setNull' ) {
536+ for ( const childRecord of childRecords ) {
537+ await adminforth . resource ( childRes . resourceId ) . update ( childRecord [ childPk ] , { [ foreignColumn . name ] : null } ) ;
539538 }
540539 }
540+ }
541+
542+ return { error : null } ;
543+ }
541544
542- return { error : null } ;
545+ export function hookResponseError ( hookResponse : { ok : boolean , error ?: string | null } ) {
546+ if ( ! hookResponse || typeof hookResponse . ok !== 'boolean' ) {
547+ throw new Error ( `Hook beforeSave must return { ok: boolean, error?: string | null }` ) ;
548+ }
549+ if ( hookResponse . ok === false && ! hookResponse . error ) {
550+ return { error : hookResponse . error ?? 'Operation aborted by hook' } ;
543551 }
552+ if ( hookResponse . error ) {
553+ return { error : hookResponse . error } ;
554+ }
555+ return null ;
556+ }
544557
545- export function hookResponseError ( hookResponse : { ok : boolean , error ?: string | null } ) {
546- if ( ! hookResponse || typeof hookResponse . ok !== 'boolean' ) {
547- throw new Error ( `Hook beforeSave must return { ok: boolean, error?: string | null }` ) ;
548- }
549- if ( hookResponse . ok === false && ! hookResponse . error ) {
550- return { error : hookResponse . error ?? 'Operation aborted by hook' } ;
551- }
552- if ( hookResponse . error ) {
553- return { error : hookResponse . error } ;
558+ export function checkIfFieldIsInsideResourceColumns ( fieldName : string , resource : AdminForthResource ) : boolean {
559+ for ( const column of resource . columns ) {
560+ if ( column . name === fieldName ) {
561+ return true ;
554562 }
555- return null ;
556- }
563+ }
564+ return false ;
565+ }
0 commit comments