[
    {
        "id": "6ad2106268b17c29",
        "type": "subflow",
        "name": "Get current articles from MIP",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 60,
                "y": 160,
                "wires": [
                    {
                        "id": "60fa9cea108b9d97"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 1900,
                "y": 160,
                "wires": [
                    {
                        "id": "44b941a072873270",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "24bbb60ddc3697fc",
        "type": "http request",
        "z": "6ad2106268b17c29",
        "name": "service call to MIP",
        "method": "POST",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": true,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 410,
        "y": 160,
        "wires": [
            [
                "aaa0889d8331bbf2"
            ]
        ]
    },
    {
        "id": "60fa9cea108b9d97",
        "type": "function",
        "z": "6ad2106268b17c29",
        "name": "build request",
        "func": "let mip_params = {\n    \"params\": [\n        {\n            \"acronym\": \"operation.act.workplace\",\n            \"value\": [],\n            \"operator\": \"IN\"\n        },\n        {\n            \"acronym\": \"operation.act.status\",\n            \"value\": \"L\",\n            \"operator\": \"EQUAL\"\n        }\n\n    ],\n    \"columns\": [\n        \"operation.act.workplace\",\n        \"operation.act.status\",\n        \"operation.article\"\n    ],\n    \"returnAsObject\": true,\n    \"languageKey\": msg.languageKey\n}\n\nObject.keys(msg.mapping).forEach(function (machine) {\n    mip_params.params[0].value.push(machine)\n});\n\nmsg.payload = mip_params\n\nmsg.url = msg.baseUrl + \"/data/BOOperation/list?X-Access-Id=\" + msg.xAccessId\n\nconst jsessionid = flow.get(\"jsessionid\")\n\nlet basic_encrypted = \"Basic \" + new Buffer(msg.user + ':' + msg.password).toString('base64')\n\nif (jsessionid) {\n    msg.headers = {\n        \"Content-Type\": \"application/json\",\n        \"Cookie\": `JSESSIONID=${jsessionid}`\n    }\n} else {\n    msg.headers = {\n        \"Content-Type\": \"application/json\",\n        \"Authorization\": basic_encrypted,\n    };\n}\n\n\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 210,
        "y": 160,
        "wires": [
            [
                "24bbb60ddc3697fc"
            ]
        ]
    },
    {
        "id": "a0db039bf8e4022d",
        "type": "change",
        "z": "6ad2106268b17c29",
        "name": "save JSESSION",
        "rules": [
            {
                "t": "set",
                "p": "jsessionid",
                "pt": "flow",
                "to": "responseCookies.JSESSIONID.value",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1080,
        "y": 160,
        "wires": [
            [
                "73ea8cf86cd1bbe3"
            ]
        ]
    },
    {
        "id": "35e923fd1e790f1a",
        "type": "switch",
        "z": "6ad2106268b17c29",
        "name": "Check if invalid",
        "property": "statusCode",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "401",
                "vt": "str"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "false",
        "repair": false,
        "outputs": 2,
        "x": 880,
        "y": 160,
        "wires": [
            [
                "42f8360552a174a9"
            ],
            [
                "a0db039bf8e4022d"
            ]
        ]
    },
    {
        "id": "42f8360552a174a9",
        "type": "change",
        "z": "6ad2106268b17c29",
        "name": "Delete JSESSION",
        "rules": [
            {
                "t": "delete",
                "p": "jsessionid",
                "pt": "flow"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1090,
        "y": 80,
        "wires": [
            [
                "60fa9cea108b9d97"
            ]
        ]
    },
    {
        "id": "73ea8cf86cd1bbe3",
        "type": "change",
        "z": "6ad2106268b17c29",
        "name": "Delete META data",
        "rules": [
            {
                "t": "delete",
                "p": "payload[0]",
                "pt": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1270,
        "y": 160,
        "wires": [
            [
                "20eef0453bd1d876"
            ]
        ]
    },
    {
        "id": "aaa0889d8331bbf2",
        "type": "change",
        "z": "6ad2106268b17c29",
        "name": "Clean up for next requests",
        "rules": [
            {
                "t": "delete",
                "p": "headers",
                "pt": "msg"
            },
            {
                "t": "delete",
                "p": "url",
                "pt": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 640,
        "y": 160,
        "wires": [
            [
                "35e923fd1e790f1a"
            ]
        ]
    },
    {
        "id": "20eef0453bd1d876",
        "type": "split",
        "z": "6ad2106268b17c29",
        "name": "Split current articles",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "property": "payload",
        "x": 1500,
        "y": 160,
        "wires": [
            [
                "44b941a072873270"
            ]
        ]
    },
    {
        "id": "44b941a072873270",
        "type": "change",
        "z": "6ad2106268b17c29",
        "name": "Set payload to obj",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.obj",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1730,
        "y": 160,
        "wires": [
            []
        ]
    },
    {
        "id": "5270f9d9e16183b8",
        "type": "tab",
        "label": "MIP change product",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "3ec83c3ac3fc4648",
        "type": "inject",
        "z": "5270f9d9e16183b8",
        "name": "Every Minute",
        "props": [],
        "repeat": "60",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 200,
        "y": 240,
        "wires": [
            [
                "fa0cb209498bb945"
            ]
        ]
    },
    {
        "id": "7ccbe3224247c722",
        "type": "comment",
        "z": "5270f9d9e16183b8",
        "name": "^^ configure authentication",
        "info": "",
        "x": 1740,
        "y": 300,
        "wires": []
    },
    {
        "id": "fa0cb209498bb945",
        "type": "change",
        "z": "5270f9d9e16183b8",
        "name": "Set settings",
        "rules": [
            {
                "t": "set",
                "p": "mapping",
                "pt": "msg",
                "to": "{\"$RESOURCE_ID\":{\"locationId\":\"$LOCATION_ID\"}}",
                "tot": "json"
            },
            {
                "t": "set",
                "p": "baseUrl",
                "pt": "msg",
                "to": "",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "xAccessId",
                "pt": "msg",
                "to": "",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "user",
                "pt": "msg",
                "to": "",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "password",
                "pt": "msg",
                "to": "",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "languageKey",
                "pt": "msg",
                "to": "en",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 410,
        "y": 240,
        "wires": [
            [
                "8c43693ecced6408"
            ]
        ]
    },
    {
        "id": "8e635e628dae32a9",
        "type": "comment",
        "z": "5270f9d9e16183b8",
        "name": "^^ configure settings",
        "info": "",
        "x": 440,
        "y": 300,
        "wires": []
    },
    {
        "id": "274e44b05b5da478",
        "type": "http request",
        "z": "5270f9d9e16183b8",
        "name": "POST Changeover",
        "method": "POST",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "https://api.oee.ai/changeovers",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "bearer",
        "senderr": false,
        "headers": [],
        "x": 1710,
        "y": 240,
        "wires": [
            []
        ]
    },
    {
        "id": "91a3d0de8d0b098e",
        "type": "function",
        "z": "5270f9d9e16183b8",
        "name": "Check if article changed",
        "func": "const workplace = msg.payload[\"operation.act.workplace\"]\nlet oldArticle = flow.get(`${workplace}Article`);\nlet currentArticle = msg.payload[\"operation.article\"];\n\nif (oldArticle !== currentArticle){\n    flow.set(`${workplace}Article`, currentArticle);\n    msg.articleId = currentArticle;\n    msg.locationId = msg.mapping[workplace].locationId;\n    return msg;\n}   ",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 890,
        "y": 240,
        "wires": [
            [
                "c3472ea1fb30a81f"
            ]
        ]
    },
    {
        "id": "c3472ea1fb30a81f",
        "type": "http request",
        "z": "5270f9d9e16183b8",
        "name": "GET Product for Article ID",
        "method": "GET",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "https://api.oee.ai/products?filter[code-or-description]={{{articleId}}}&filter[location]={{{locationId}}}",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "bearer",
        "senderr": false,
        "headers": [],
        "x": 1170,
        "y": 240,
        "wires": [
            [
                "f28d873f3d0cda2a"
            ]
        ]
    },
    {
        "id": "f28d873f3d0cda2a",
        "type": "function",
        "z": "5270f9d9e16183b8",
        "name": "Create changeover payload",
        "func": "if (msg.payload.data.length > 0){\n    const productId = msg.payload.data[0].id;\n    const time = new Date().toISOString();\n\n    msg.headers = {\n        \"Content-Type\": \"application/vnd.api+json\"\n    }\n\n    msg.payload = {\n        \"data\": {\n            \"type\": \"changeovers\",\n            \"attributes\": {\n                \"time\": time,\n            },\n            \"relationships\": {\n                \"product\": {\n                    \"data\": {\n                        \"type\": \"products\",\n                        \"id\": productId,\n                    }\n                },\n                \"location\": {\n                    \"data\": {\n                        \"type\": \"locations\",\n                        \"id\": msg.locationId,\n                    }\n                }\n            }\n        }\n    };\n\n    return msg;\n}else{\n    node.error(`Article with ArticleId \"${msg.articleId}\" not found in oee.ai`);\n}\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1460,
        "y": 240,
        "wires": [
            [
                "274e44b05b5da478"
            ]
        ]
    },
    {
        "id": "8c43693ecced6408",
        "type": "subflow:6ad2106268b17c29",
        "z": "5270f9d9e16183b8",
        "name": "Get current Articles",
        "x": 650,
        "y": 240,
        "wires": [
            [
                "91a3d0de8d0b098e"
            ]
        ]
    },
    {
        "id": "047adce0500e8fe2",
        "type": "comment",
        "z": "5270f9d9e16183b8",
        "name": "^^ configure authentication",
        "info": "",
        "x": 1180,
        "y": 300,
        "wires": []
    }
]