Script to remove all flowcards in Flows and Advanced Flows from a certain app
Use this script to remove all flowcards from a certain app from flows and advanced flows. Lines are removed too, tokens are not.
Execute this in the WebApi Playground .
Note: Work in progress.
const version = '1.0.5';
//You need to run this script in the API Playground to really remove all flowcards.
//const flowNames = ['AddLogCards (Test)', 'DeleteCardsFlow']; // Use this line to run a few select flows by name.
const flowNames = null; // Use this line to run all flows
//const appId = 'homey:app:nl.nielsdeklerk.log'; // Will delete all cards and removed the lines for all Simple Log cards!
const appId = 'homey:app:nu.dijker.papertrails'; // Will delete all cards and removed the lines for all Papertrails cards!
const reallyExecute = false; //set this to false for a test run (in HS), true to really run (in the Playground)
//const reallyExecute = true; //set this to false for a test run (in HS), true to really run (in the Playground)
async function run() {
const lineHolders = ['outputError', 'outputSuccess', 'outputTrue', 'outputFalse'];
const flowGroups = ['triggers', 'conditions', 'actions'];
let APIv3 = Number.parseInt((await Homey.system.getInfo()).homeyVersion.split('.')) >= 10;
let afs = await Homey.flow.getAdvancedFlows();
afs = _.toArray(afs);
//return afs;
if (flowNames) afs = _.filter(afs, x => flowNames.indexOf(x.name) > -1);
afs = _.filter(afs, af=> _.find(af.cards, c=>c.ownerUri==appId));
for(let i = 0;i<afs.length;i++) {
const af = afs[i];
const cardsToDelete = _.pickBy(af.cards, c=>c.ownerUri==appId);
af.cards = _.pickBy(af.cards, c=>c.ownerUri!==appId);
for(const key in af.cards) {
//console.log(key);
const card = af.cards[key];
let lines;
for(let j = 0;j<lineHolders.length;j++) {
if(card[lineHolders[j]] && card[lineHolders[j]].length>0 && Object.keys(lines = _.pickBy(cardsToDelete, (c,k)=> card[lineHolders[j]].indexOf(k)>-1)).length ) {
_.each(lines,(c,k)=> card[lineHolders[j]].splice(card[lineHolders[j]].indexOf(k),1));
if(card[lineHolders[j]].length==0) delete card[lineHolders[j]];
}
}
}
}
let flows = await Homey.flow.getFlows();
flows = _.toArray(flows);
//return afs;
if (flowNames) flows = _.filter(flows, x => flowNames.indexOf(x.name) > -1);
flows = _.filter(flows, af=> _.find(af.triggers, c=>c.uri==appId) || _.find(af.conditions, c=>c.uri==appId) || _.find(af.actions, c=>c.uri==appId));
for(let i = 0;i<flows.length;i++) {
const flow = flows[i];
for(let j = 0;j<flowGroups.length;j++) {
if(flow[flowGroups[j]] && flow[flowGroups[j]].length>0) {
flow[flowGroups[j]] = _.filter(flow[flowGroups[j]], c=>c.uri!=appId);
// _.each(lines,(c,k)=> card[flowGroups[j]].splice(card[flowGroups[j]].indexOf(k),1));
// if(card[flowGroups[j]].length==0) delete card[lineHolders[j]];
}
}
}
if(reallyExecute) _.each(afs, async af=>{
try {
await Homey.flow.updateAdvancedFlow({id:af.id, advancedflow:{cards:af.cards}});
} catch(err) {
console.log(err);
}
});
if(reallyExecute) _.each(flows, async flow=>{
try {
await Homey.flow.updateFlow({id:flow.id, flow:{actions:flow.actions, conditions:flow.conditions, trigger:flow.trigger}});
} catch(err) {
console.log(err);
}
}); else console.log(flows);
return {afs, flows};
}
run().then(x=>{ if(this!=null) this.log(x)});