import { FrameworkUDFunctionsApi } from '~/types/framework/framework.udfunctions' ; import { execute } from '~/tools/tasks' ; import { Biota } from '~/biota' ; import { UDFunctionFromMethod } from '~/factory/api/constructors' ; import { factoryApi } from '~/factory' ; import { query as q , Expr } from 'faunadb' ; import { splitEvery } from '~/helpers' ; export const scaffold = async function ( this ) { const self = this ; const tasks = [ ] ; const UDFs = [ ] ; const loadStep = ( step ) => { if ( typeof step === 'function' ) { let definition = step ( ) ; if ( typeof definition === 'function' ) definition = definition ( ) ; if ( definition instanceof Expr ) { const UDFunctionDefinition = UDFunctionFromMethod ( definition ) ; if ( UDFunctionDefinition && UDFunctionDefinition . name ) { UDFs . push ( UDFunctionDefinition . name ) ; } } else if ( typeof definition === 'object' ) { return loadStep ( definition ) ; } } else if ( typeof step === 'object' ) { return Object . values ( step ) . map ( ( item ) => loadStep ( item ) ) ; } } ; for ( const key of Object . keys ( factoryApi ) ) { loadStep ( factoryApi [ key ] ) ; } const UDFsBatches = splitEvery ( 50 , UDFs ) ; for ( const UDFsBatch of UDFsBatches ) { tasks . push ( { name : `Adding privileges to User for ${ UDFsBatch . length } functions` , task ( ) { return self . role ( 'biota.user' ) . privilege . setMany ( UDFsBatch . map ( ( UDF ) => ( { resource : q . Function ( UDF ) , actions : { call : true } , } ) ) ) ; } , } ) ; } return execute ( tasks , { domain : 'Biota.udfunctions.scaffold' , } ) ; } ;