@@ -339,45 +339,49 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
339
339
method : 'POST' ,
340
340
path : `/plugin/${ this . pluginInstanceId } /update_fields` ,
341
341
handler : async ( body ) => {
342
- const selectedIds = body . body . selectedIds || [ ] ;
343
- const fieldsToUpdate = body . body . fields || { } ;
344
- const outputImageFields = [ ] ;
345
- if ( this . options . generateImages ) {
346
- for ( const [ key , value ] of Object . entries ( this . options . generateImages ) ) {
347
- outputImageFields . push ( key ) ;
342
+ if ( this . options . isAllowedToSave !== false ) {
343
+ const selectedIds = body . body . selectedIds || [ ] ;
344
+ const fieldsToUpdate = body . body . fields || { } ;
345
+ const outputImageFields = [ ] ;
346
+ if ( this . options . generateImages ) {
347
+ for ( const [ key , value ] of Object . entries ( this . options . generateImages ) ) {
348
+ outputImageFields . push ( key ) ;
349
+ }
348
350
}
349
- }
350
- const primaryKeyColumn = this . resourceConfig . columns . find ( ( col ) => col . primaryKey ) ;
351
- const updates = selectedIds . map ( async ( ID , idx ) => {
352
- const oldRecord = await this . adminforth . resource ( this . resourceConfig . resourceId ) . get ( [ Filters . EQ ( primaryKeyColumn . name , ID ) ] ) ;
353
- for ( const [ key , value ] of Object . entries ( outputImageFields ) ) {
354
- const columnPlugin = this . adminforth . activatedPlugins . find ( p =>
355
- p . resourceConfig ! . resourceId === this . resourceConfig . resourceId &&
356
- p . pluginOptions . pathColumnName === value
357
- ) ;
358
- if ( columnPlugin ) {
359
- if ( columnPlugin . pluginOptions . storageAdapter . objectCanBeAccesedPublicly ( ) ) {
360
- if ( oldRecord [ value ] ) {
361
- // put tag to delete old file
362
- try {
363
- await columnPlugin . pluginOptions . storageAdapter . markKeyForDeletation ( oldRecord [ value ] ) ;
364
- } catch ( e ) {
365
- // file might be e.g. already deleted, so we catch error
366
- console . error ( `Error setting tag to true for object ${ oldRecord [ value ] } . File will not be auto-cleaned up` , e ) ;
351
+ const primaryKeyColumn = this . resourceConfig . columns . find ( ( col ) => col . primaryKey ) ;
352
+ const updates = selectedIds . map ( async ( ID , idx ) => {
353
+ const oldRecord = await this . adminforth . resource ( this . resourceConfig . resourceId ) . get ( [ Filters . EQ ( primaryKeyColumn . name , ID ) ] ) ;
354
+ for ( const [ key , value ] of Object . entries ( outputImageFields ) ) {
355
+ const columnPlugin = this . adminforth . activatedPlugins . find ( p =>
356
+ p . resourceConfig ! . resourceId === this . resourceConfig . resourceId &&
357
+ p . pluginOptions . pathColumnName === value
358
+ ) ;
359
+ if ( columnPlugin ) {
360
+ if ( columnPlugin . pluginOptions . storageAdapter . objectCanBeAccesedPublicly ( ) ) {
361
+ if ( oldRecord [ value ] ) {
362
+ // put tag to delete old file
363
+ try {
364
+ await columnPlugin . pluginOptions . storageAdapter . markKeyForDeletation ( oldRecord [ value ] ) ;
365
+ } catch ( e ) {
366
+ // file might be e.g. already deleted, so we catch error
367
+ console . error ( `Error setting tag to true for object ${ oldRecord [ value ] } . File will not be auto-cleaned up` , e ) ;
368
+ }
369
+ }
370
+ if ( fieldsToUpdate [ idx ] [ key ] !== null ) {
371
+ // remove tag from new file
372
+ // in this case we let it crash if it fails: this is a new file which just was uploaded.
373
+ await columnPlugin . pluginOptions . storageAdapter . markKeyForNotDeletation ( fieldsToUpdate [ idx ] [ value ] ) ;
367
374
}
368
- }
369
- if ( fieldsToUpdate [ idx ] [ key ] !== null ) {
370
- // remove tag from new file
371
- // in this case we let it crash if it fails: this is a new file which just was uploaded.
372
- await columnPlugin . pluginOptions . storageAdapter . markKeyForNotDeletation ( fieldsToUpdate [ idx ] [ value ] ) ;
373
375
}
374
376
}
375
377
}
376
- }
377
- return this . adminforth . resource ( this . resourceConfig . resourceId ) . update ( ID , fieldsToUpdate [ idx ] )
378
- } ) ;
379
- await Promise . all ( updates ) ;
380
- return { ok : true } ;
378
+ return this . adminforth . resource ( this . resourceConfig . resourceId ) . update ( ID , fieldsToUpdate [ idx ] )
379
+ } ) ;
380
+ await Promise . all ( updates ) ;
381
+ return { ok : true } ;
382
+ } else {
383
+ return { ok : false }
384
+ }
381
385
}
382
386
} ) ;
383
387
0 commit comments