Tag Filter Plugin

Dear Forum,

I have a new question today. I want to create a Plugin similar to the getChildren Plugin. But it filters the children based on tags. I did some research and testing and I came up with a partial solution.
In the getChildren Plugin, const response = await waylay.resources.children(resource) gives me an object with all the children as objects. I can filter the result like this:

const input1 = {
    "0":{
        "id":"01",
        "name":"item_01",
        "tags":["a","b"],
    },
    "1":{
        "id":"02",
        "name":"item_02",
        "tags":["a","c","d"],
    },
    "2":{
        "id":"03",
        "name":"item_03",
        "tags":["a","b","f"],
    }
}
 
function search(input, keys) {
   return Object.fromEntries(
   Object.entries(input).filter(([, { tags }]) => keys.every(key => tags.includes(key)))
   )
   
}

console.log(search(input1, ["a", "b"]));

Basically I would just need to change the dummy object (input1) with the “response” from the getChildren Plugin and store the filtered object before mapping the childIds to “rawData”.
Something like the Code below.

const { resource, tag_1, tag_2 } = options.requiredProperties

if (!resource) {
  send(new Error('resource not defined'))
}
function filterfunction() {
  search(input, keys) {
    return Object.fromEntries(
    Object.entries(input).filter(([, { tags }]) => keys.every(key => tags.includes(key)))
    )
  }
}

async function execute () {
  try {
    const response = await waylay.resources.children(resource)

    const taglist = await filterfunction(response, [tag_1, tag_2])
    
    const rawData = Object.assign({}, { childIds: taglist.values.map(val => val.id) })
    send(null, { observedState: 'Found', rawData })
  } catch (error) {
    if (error.response && error.response.status === 404) {
      return send(null, {
        observedState: 'Not Found'
      })
    }

    return send(error)
  }
}

execute()

But I get an error result:

Where is my mistake?

Greetings

1 Like

Hey Friedrich!

That is a very complicated filter function you have there :sweat_smile: but I’m glad it works for you!

waylay.resources.children does not seem te return what you expect it does. You can check the docs [over here](@waylay/client 2.27.0 | Documentation children).

The returned object looks like this:

{
 "values": [{...},{...},{...},{...},{...}],
 "skip": 0,
 "limit": 100,
 "total": 5
} 

Where value is a list of child resources.

I hope that helps :blush:

Awesome!! Thank you Sander!

The Page you referenced helped so much! With this documentation I was able to create the custom Plugin in a matter of 5 minutes after researching for days now!

1 Like

Yeah the right docs can help :grin:

Happy to help!