So, I have two sets, as arrays, of options, one existing (that I looked up in the destination system) and one new (from the exporting system) - here simplified:
{
"id":"1"
"existingOptions":[
{
"propId":"1",
"optionId":"2"
},
{
"propId":"2",
"optionId":"34"
}
],
"newOptions":[
{
"propId":"1",
"optionId":"2"
},
{
"propId":"3",
"optionId":"5"
}
]
}
I want to compare and find which objects need to be created, updated and deleted at the destination system. Since node transformers don’t have access to outside objects I will copy the data I need into each one of them. So, here, copy root id into each newOptions object, and each of the arrays into each of the other array objects.
$.newOptions[*].id = $.id
$.existingOptions[*].newOptions = $.newOptions
$.newOptions[*].existingOptions = $.existingOptions
Now that we have the necessary data accessible inside each node, we can iterate through them (with the “Node, transform nodes” transformer) and set some flags (as key-values).
First we go through the newOptions, and set “method” keys:
if propId exists in existingOptions set “PUT” (update), else set “POST” (create).
Secondly, in existingOptions, I need to find which propId’s are NOT in the newOptions. There, add the key “method”: “DELETE”.
$.existingOptions[?(!($.propId in $.newOptions[*].propId))].method = "DELETE"
Then, copy that object to the newOptions.
$.newOptions = $.newOptions + $.existingOptions[0]
expected result JSON:
{
"id": "1",
"existingOptions": [
{
"propId": "1",
"optionId": "2",
"newOptions": [
{
"propId": "1",
"optionId": "2"
},
{
"propId": "3",
"optionId": "5"
}
]
},
{
"propId": "2",
"optionId": "34",
"newOptions": [
{
"propId": "1",
"optionId": "2"
},
{
"propId": "3",
"optionId": "5"
}
]
}
],
"newOptions": [
{
"propId": "1",
"optionId": "2",
"id": "1",
"existingOptions": [
{
"propId": "1",
"optionId": "2",
},
{
"propId": "2",
"optionId": "34",
}
],
"method": "PUT"
},
{
"propId": "3",
"optionId": "5",
"id": "1",
"existingOptions": [
{
"propId": "1",
"optionId": "2",
},
{
"propId": "2",
"optionId": "34",
}
],
"method": "POST"
},
{
"propId": "2",
"optionId": "34",
"id": "1",
"method": "DELETE"
}
]
}
Now, finally I can make a new iteration over the newOptions and make those requests with a HTTP transformer.
Hope I got this right - quite a long way to do some simple programming using just JSON. Are there other shorter, better and/or easier ways to this same thing in Alumio?
PS The goal for this this is to go through product variant properties inside a product sync. route using transformers. The properties and options themselves will already have been synced first.