UNPKG

flexbiz-server

Version:

Flexible Server

7 lines (6 loc) 2.69 kB
const model=global.getModel("flow"),controller=require("../../controllers/controller"),crypto=require("crypto"); module.exports=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,"flow",{onView:($user$$,$items$$,$next$$)=>{$items$$.forEach($item$$=>{if(global.flowEngine){let $Instances$$=(global.flowEngine.getInstancesForFlow($item$$._id.toString())||[]).filter($ins$$=>$ins$$.status=="running"||$ins$$.status=="listening");$Instances$$.length>0&&($item$$.activeInstance=$Instances$$[0])}});$next$$(null,$items$$)},onCreating:($user$$,$data$$,$next$$)=>{$data$$.nodes=$data$$.nodes||[]; $data$$.nodes.forEach($n$$=>{delete $n$$.outputData;delete $n$$.status;delete ($n$$.data||{}).nodeState});$next$$(null,$data$$)},onUpdating:($user$$,$data$$,$obj$$,$next$$)=>{$data$$.nodes=$data$$.nodes||$obj$$.nodes||[];$data$$.nodes.forEach($n$$=>{delete $n$$.outputData;delete $n$$.status;delete ($n$$.data||{}).nodeState});$next$$(null,$data$$,$obj$$)}});$contr_router$$.route();$contr_router$$.createRoute("start/:flow_id",async($data$$,$next$$)=>{if(!global.flowEngine)return $next$$({error:"Flow Engine ch\u01b0a \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o"}); let $flow_id$$=$data$$.params.flow_id;var $Instances$$=(global.flowEngine.getInstancesForFlow($flow_id$$)||[]).filter($ins$$=>$ins$$.status=="running"||$ins$$.status=="listening");$Instances$$.length>0&&$next$$(null,{flowInstanceId:$Instances$$[0].instanceId});$data$$=$data$$.body||{};$Instances$$=`flow-${crypto.randomBytes(20).toString("hex")}`;try{await global.flowEngine.startFlow($flow_id$$,$data$$,$Instances$$),$next$$(null,{flowInstanceId:$Instances$$})}catch($e$$){Logger.error("Error start flow", $e$$),$next$$($e$$)}},{method:"POST",not_use_worker:!0});$contr_router$$.createRoute("stop/:flowId",async($flowId_req$$,$next$$)=>{if(!global.flowEngine)return $next$$({error:"Flow Engine ch\u01b0a \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o"});({flowId:$flowId_req$$}=$flowId_req$$.params);const $stopped$$=global.flowEngine.stopFlow($flowId_req$$);$stopped$$?$next$$(null,$stopped$$):$next$$({error:`Flow ${$flowId_req$$} not found or already stopped`})},{method:"GET",not_use_worker:!0});$contr_router$$.createRoute("status/:flowInstanceId", async($flowInstanceId$jscomp$1_req$$,$next$$)=>{if(!global.flowEngine)return $next$$({error:"Flow Engine ch\u01b0a \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o"});$flowInstanceId$jscomp$1_req$$=$flowInstanceId$jscomp$1_req$$.params.flowInstanceId;const $status$$=global.flowEngine.getFlowStatus($flowInstanceId$jscomp$1_req$$);$status$$?$next$$(null,$status$$):$next$$({error:"Flow "+$flowInstanceId$jscomp$1_req$$+" not found"})},{method:"GET",not_use_worker:!0})};