[
    {
        "id": "add565707d291876",
        "type": "subflow",
        "name": "Post products",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 200,
                "y": 220,
                "wires": [
                    {
                        "id": "7ef5e7c148cf0a7b"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 800,
                "y": 220,
                "wires": [
                    {
                        "id": "db341d98f56b996c",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "7ef5e7c148cf0a7b",
        "type": "function",
        "z": "add565707d291876",
        "name": "Formatting json payload",
        "func": "msg.headers = {\n    \"Content-Type\": \"application/vnd.api+json\",\n    \"Authorization\": \"Bearer \" + msg.bearerToken\n}\n\nmsg.payload = {\n    \"data\": {\n        \"type\": \"products\",\n        \"attributes\": {\n            \"code\": msg.code,\n            \"description\": msg.description,\n            \"color\": msg.color,\n            \"production-unit\": msg[\"production-unit\"],\n            \"cycle-time-mode\": msg[\"cycle-time-mode\"],\n            \"cycle-time-unit\": msg[\"cycle-time-unit\"],\n            \"target-speed\": msg[\"target-speed\"],\n            \"units-multiplier\": msg[\"units-multiplier\"],\n            \"units-divisor\": msg[\"units-divisor\"],\n            \"info\": msg.info\n        },\n        \"relationships\": {\n            \"location\": {\n                \"data\": {\n                    \"type\": \"locations\",\n                    \"id\": msg.location\n                }\n            }\n        }\n    }\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 370,
        "y": 220,
        "wires": [
            [
                "db341d98f56b996c"
            ]
        ]
    },
    {
        "id": "db341d98f56b996c",
        "type": "http request",
        "z": "add565707d291876",
        "name": "POST product to oee.ai",
        "method": "POST",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "https://api.oee.ai/products",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 630,
        "y": 220,
        "wires": [
            []
        ]
    },
    {
        "id": "f51662145c16c667",
        "type": "subflow",
        "name": "Patch products",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 100,
                "y": 120,
                "wires": [
                    {
                        "id": "5d3c0107d679e17b"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 2160,
                "y": 140,
                "wires": [
                    {
                        "id": "69a6b32b047006fa",
                        "port": 0
                    },
                    {
                        "id": "242b84f899e798f3",
                        "port": 1
                    }
                ]
            }
        ],
        "env": [],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "afab12efbf77182d",
        "type": "http request",
        "z": "f51662145c16c667",
        "name": "Get existing product",
        "method": "GET",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "https://api.oee.ai/products?filter[code-or-description]={{{code}}}&filter[location]={{{location}}}",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 740,
        "y": 120,
        "wires": [
            [
                "242b84f899e798f3"
            ]
        ]
    },
    {
        "id": "d960b3810cd39ee7",
        "type": "function",
        "z": "f51662145c16c667",
        "name": "Formatting json payload",
        "func": "msg.headers = {\n    \"Content-Type\": \"application/vnd.api+json\",\n    \"Authorization\": \"Bearer \" + msg.bearerToken\n};\n\nmsg.method = \"PATCH\";\n\nmsg.oldProductId = msg.payload.data[0].id;\n\nmsg.payload = {\n    \"data\": {\n        \"id\": msg.oldProductId,\n        \"type\": \"products\",\n        \"attributes\": {\n            \"code\": msg.code,\n            \"description\": msg.description,\n            \"color\": msg.color,\n            \"production-unit\": msg[\"production-unit\"],\n            \"cycle-time-mode\": msg[\"cycle-time-mode\"],\n            \"cycle-time-unit\": msg[\"cycle-time-unit\"],\n            \"target-speed\": msg[\"target-speed\"],\n            \"units-multiplier\": msg[\"units-multiplier\"],\n            \"units-divisor\": msg[\"units-divisor\"],\n            \"info\": msg.info\n        },\n        \"relationships\": {\n            \"location\": {\n                \"data\": {\n                    \"type\": \"locations\",\n                    \"id\": msg.location\n                }\n            }\n        }\n    }\n};\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1470,
        "y": 100,
        "wires": [
            [
                "6b35a9eeab226efd"
            ]
        ]
    },
    {
        "id": "6b35a9eeab226efd",
        "type": "http request",
        "z": "f51662145c16c667",
        "name": "Patch product",
        "method": "use",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "https://api.oee.ai/products/{{{oldProductId}}}",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 1680,
        "y": 100,
        "wires": [
            [
                "69a6b32b047006fa"
            ]
        ]
    },
    {
        "id": "5d3c0107d679e17b",
        "type": "change",
        "z": "f51662145c16c667",
        "name": "delete response header",
        "rules": [
            {
                "t": "delete",
                "p": "headers",
                "pt": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 270,
        "y": 120,
        "wires": [
            [
                "693a2e9dcaf63e5c"
            ]
        ]
    },
    {
        "id": "9701071e2dd0a71e",
        "type": "change",
        "z": "f51662145c16c667",
        "name": "delete response header",
        "rules": [
            {
                "t": "delete",
                "p": "headers",
                "pt": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1230,
        "y": 100,
        "wires": [
            [
                "d960b3810cd39ee7"
            ]
        ]
    },
    {
        "id": "693a2e9dcaf63e5c",
        "type": "function",
        "z": "f51662145c16c667",
        "name": "Set Authorization header",
        "func": "msg.headers = {\n    Authorization: 'Bearer ' + msg.bearerToken,\n};\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 510,
        "y": 120,
        "wires": [
            [
                "afab12efbf77182d"
            ]
        ]
    },
    {
        "id": "242b84f899e798f3",
        "type": "function",
        "z": "f51662145c16c667",
        "name": "Check if product changed",
        "func": "function compareProperties(a, b, properties) {\n  return properties.every((prop) => {\n    if (prop === 'target-speed') {\n      return parseFloat(a[prop]) === parseFloat(b[prop]);\n    } else {\n      return a[prop] === b[prop];\n    }\n  });\n}\n\n\nlet existingProduct = {};\n\n// potential bug when product code is substring of other product i.e. h123 and h123-02\nif (msg.payload.data.length > 1) {\n  existingProduct = msg.payload.data.find(\n    (entry) => entry.attributes.code === msg.code\n  ).attributes;\n} else {\n  existingProduct = msg.payload.data[0].attributes;\n}\n\nconst propertiesToCompare = [\n  'code',\n  'description',\n  'target-speed',\n  'info',\n  'color',\n  'production-unit',\n  'cycle-time-mode',\n  'units-multiplier',\n  'units-divisor',\n];\n\nif (compareProperties(existingProduct, msg, propertiesToCompare)) {\n  return [null, msg];\n}\n\nreturn [msg, null];\n\n",
        "outputs": 2,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 990,
        "y": 120,
        "wires": [
            [
                "9701071e2dd0a71e"
            ],
            []
        ]
    },
    {
        "id": "69a6b32b047006fa",
        "type": "change",
        "z": "f51662145c16c667",
        "name": "delete response header and method",
        "rules": [
            {
                "t": "delete",
                "p": "headers",
                "pt": "msg"
            },
            {
                "t": "delete",
                "p": "method",
                "pt": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1930,
        "y": 100,
        "wires": [
            []
        ]
    },
    {
        "id": "d644b32827ab5ff7",
        "type": "subflow",
        "name": "Send products to oee.ai",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 140,
                "y": 260,
                "wires": [
                    {
                        "id": "c989c8789ff6d538"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 920,
                "y": 280,
                "wires": [
                    {
                        "id": "a575dcb265c27b3d",
                        "port": 0
                    },
                    {
                        "id": "81d1d177247aa9df",
                        "port": 1
                    }
                ]
            }
        ],
        "env": [],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "81d1d177247aa9df",
        "type": "switch",
        "z": "d644b32827ab5ff7",
        "name": "Check if product already exists",
        "property": "statusCode",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "422",
                "vt": "num"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "false",
        "repair": false,
        "outputs": 2,
        "x": 530,
        "y": 260,
        "wires": [
            [
                "a575dcb265c27b3d"
            ],
            []
        ]
    },
    {
        "id": "a575dcb265c27b3d",
        "type": "subflow:f51662145c16c667",
        "z": "d644b32827ab5ff7",
        "name": "",
        "x": 780,
        "y": 240,
        "wires": [
            []
        ]
    },
    {
        "id": "c989c8789ff6d538",
        "type": "subflow:add565707d291876",
        "z": "d644b32827ab5ff7",
        "name": "",
        "x": 290,
        "y": 260,
        "wires": [
            [
                "81d1d177247aa9df"
            ]
        ]
    },
    {
        "id": "85329502497afd43",
        "type": "tab",
        "label": "Synchronizing products",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "d51decb8abd0ce7d",
        "type": "comment",
        "z": "85329502497afd43",
        "name": "^^ enter your oee.ai API token",
        "info": "You can find your API Token on oee.ai",
        "x": 760,
        "y": 340,
        "wires": []
    },
    {
        "id": "32c78a420ef9da7e",
        "type": "comment",
        "z": "85329502497afd43",
        "name": "Replace the Data input with your source and data transformation",
        "info": "",
        "x": 390,
        "y": 260,
        "wires": []
    },
    {
        "id": "8096e16cbce01bec",
        "type": "inject",
        "z": "85329502497afd43",
        "name": "Data Input",
        "props": [],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 240,
        "y": 300,
        "wires": [
            [
                "75e7127a5313045d"
            ]
        ]
    },
    {
        "id": "4f5140e8bc674013",
        "type": "change",
        "z": "85329502497afd43",
        "name": "Set API Token",
        "rules": [
            {
                "t": "set",
                "p": "bearerToken",
                "pt": "msg",
                "to": "",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 720,
        "y": 300,
        "wires": [
            [
                "b70d6d4f32343397"
            ]
        ]
    },
    {
        "id": "75e7127a5313045d",
        "type": "change",
        "z": "85329502497afd43",
        "name": "Mapping product attributes",
        "rules": [
            {
                "t": "set",
                "p": "code",
                "pt": "msg",
                "to": "123",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "description",
                "pt": "msg",
                "to": "123",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "color",
                "pt": "msg",
                "to": "azure",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "production-unit",
                "pt": "msg",
                "to": "parts",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "cycle-time-mode",
                "pt": "msg",
                "to": "parts_per_time",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "cycle-time-unit",
                "pt": "msg",
                "to": "minutes",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "target-speed",
                "pt": "msg",
                "to": "10",
                "tot": "num"
            },
            {
                "t": "set",
                "p": "units-multiplier",
                "pt": "msg",
                "to": "1",
                "tot": "num"
            },
            {
                "t": "set",
                "p": "units-divisor",
                "pt": "msg",
                "to": "1",
                "tot": "num"
            },
            {
                "t": "set",
                "p": "info",
                "pt": "msg",
                "to": "This is an example",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "location",
                "pt": "msg",
                "to": "",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 480,
        "y": 300,
        "wires": [
            [
                "4f5140e8bc674013"
            ]
        ]
    },
    {
        "id": "f327bafd429eef87",
        "type": "comment",
        "z": "85329502497afd43",
        "name": "^^ set product attributes",
        "info": "Format your message to the data model of oee.ai\nThen map your message values to the predefined\nmessage values in the change node",
        "x": 470,
        "y": 340,
        "wires": []
    },
    {
        "id": "b70d6d4f32343397",
        "type": "subflow:d644b32827ab5ff7",
        "z": "85329502497afd43",
        "name": "",
        "x": 1020,
        "y": 300,
        "wires": [
            []
        ]
    }
]
