[
    {
        "id": "e4de7c01e09a776f",
        "type": "subflow",
        "name": "Post shift",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 200,
                "y": 400,
                "wires": [
                    {
                        "id": "a49b15b920a72364"
                    }
                ]
            }
        ],
        "out": [],
        "env": [],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "a49b15b920a72364",
        "type": "function",
        "z": "e4de7c01e09a776f",
        "name": "Formatting json payload",
        "func": "msg.headers = {\n    \"Authorization\": \"Bearer \" + msg.bearerToken,\n    \"Content-Type\": \"application/vnd.api+json\"\n};\n\nmsg.payload = {\n    \"data\": {\n        \"type\": \"shifts\",\n        \"attributes\": {\n            \"name\": msg.shiftName,\n            \"rrule\": msg.rrule,\n            \"duration\": msg.duration,\n            \"mode\": msg.mode,\n            \"color\": msg.color\n        },\n        \"relationships\": {\n            \"schedule\": {\n                \"data\": {\n                    \"type\": \"schedules\",\n                    \"id\": msg.schedule\n                }\n            }\n        }\n    }\n};\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 410,
        "y": 400,
        "wires": [
            [
                "90b0febb18e8d827"
            ]
        ]
    },
    {
        "id": "90b0febb18e8d827",
        "type": "http request",
        "z": "e4de7c01e09a776f",
        "name": "POST shift",
        "method": "POST",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "https://api.oee.ai/shifts",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 610,
        "y": 400,
        "wires": [
            [
                "e3a52c265232f222"
            ]
        ]
    },
    {
        "id": "e3a52c265232f222",
        "type": "change",
        "z": "e4de7c01e09a776f",
        "name": "Delete response header",
        "rules": [
            {
                "t": "delete",
                "p": "headers",
                "pt": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 830,
        "y": 400,
        "wires": [
            [
                "41debf62367c281b"
            ]
        ]
    },
    {
        "id": "41debf62367c281b",
        "type": "debug",
        "z": "e4de7c01e09a776f",
        "name": "Response from oee.ai",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1080,
        "y": 400,
        "wires": []
    },
    {
        "id": "fd9e8ae80b46227a",
        "type": "subflow",
        "name": "Send shifts to oee.ai",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 20,
                "y": 80,
                "wires": [
                    {
                        "id": "7e6da3c796a23dc8"
                    }
                ]
            }
        ],
        "out": [],
        "env": [],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "8632f17598829578",
        "type": "subflow:e4de7c01e09a776f",
        "z": "fd9e8ae80b46227a",
        "name": "",
        "x": 840,
        "y": 80,
        "wires": []
    },
    {
        "id": "0e5877e09b93e4ad",
        "type": "change",
        "z": "fd9e8ae80b46227a",
        "name": "Mapping shift attributes",
        "rules": [
            {
                "t": "set",
                "p": "shiftName",
                "pt": "msg",
                "to": "payload.shiftName",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "rrule",
                "pt": "msg",
                "to": "payload.rrule",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "duration",
                "pt": "msg",
                "to": "payload.duration",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "mode",
                "pt": "msg",
                "to": "payload.mode",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "color",
                "pt": "msg",
                "to": "msg.payload.color",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "schedule",
                "pt": "msg",
                "to": "scheduleId",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 630,
        "y": 80,
        "wires": [
            [
                "8632f17598829578"
            ]
        ]
    },
    {
        "id": "fc7521805a23e0f7",
        "type": "split",
        "z": "fd9e8ae80b46227a",
        "name": "Split shifts",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 410,
        "y": 80,
        "wires": [
            [
                "0e5877e09b93e4ad"
            ]
        ]
    },
    {
        "id": "7e6da3c796a23dc8",
        "type": "change",
        "z": "fd9e8ae80b46227a",
        "name": "Set payload to shiftsData",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "shiftsData",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 190,
        "y": 80,
        "wires": [
            [
                "fc7521805a23e0f7"
            ]
        ]
    },
    {
        "id": "dd9318a3a7c749d0",
        "type": "subflow",
        "name": "Patch schedule",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 140,
                "y": 320,
                "wires": [
                    {
                        "id": "198e50541822ebc7"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 1720,
                "y": 320,
                "wires": [
                    {
                        "id": "2b194f788ed9a0f2",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "b00ba5b4bd2d5157",
        "type": "http request",
        "z": "dd9318a3a7c749d0",
        "name": "Get existing schedule",
        "method": "GET",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "https://api.oee.ai/production-orders?filter[locations]={{{location}}}",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 560,
        "y": 320,
        "wires": [
            [
                "bfae403982c9a1e0"
            ]
        ]
    },
    {
        "id": "edac0e4a9396652f",
        "type": "function",
        "z": "dd9318a3a7c749d0",
        "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.payload = {\n    \"data\": {\n        \"id\": msg.oldScheduleId,\n        \"type\": \"schedules\",\n        \"attributes\": {\n            \"name\": msg.scheduleName,\n            \"end-based\": msg[\"end-based\"]\n        },\n        \"relationships\": {\n            \"location\": {\n                \"data\": {\n                    \"type\": \"locations\",\n                    \"id\": msg.location\n                }\n            }\n        }\n    }\n};\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1310,
        "y": 320,
        "wires": [
            [
                "2b194f788ed9a0f2"
            ]
        ]
    },
    {
        "id": "2b194f788ed9a0f2",
        "type": "http request",
        "z": "dd9318a3a7c749d0",
        "name": "Patch production order",
        "method": "use",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "https://api.oee.ai/schedules/{{{oldScheduleId}}}",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 1560,
        "y": 320,
        "wires": [
            []
        ]
    },
    {
        "id": "bfae403982c9a1e0",
        "type": "change",
        "z": "dd9318a3a7c749d0",
        "name": "Delete response header",
        "rules": [
            {
                "t": "delete",
                "p": "headers",
                "pt": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 810,
        "y": 320,
        "wires": [
            [
                "b91181d482d3d869"
            ]
        ]
    },
    {
        "id": "198e50541822ebc7",
        "type": "function",
        "z": "dd9318a3a7c749d0",
        "name": "Set Authorization header",
        "func": "msg.headers = {\n    Authorization: 'Bearer ' + msg.bearerToken,\n};\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 310,
        "y": 320,
        "wires": [
            [
                "b00ba5b4bd2d5157"
            ]
        ]
    },
    {
        "id": "b91181d482d3d869",
        "type": "function",
        "z": "dd9318a3a7c749d0",
        "name": "Find correct schedule",
        "func": "let match = msg.payload.data.find((schedule) => {\n    return schedule.attributes.name === msg.scheduleName;\n});\n\nmsg.oldScheduleId = match.id;\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1060,
        "y": 320,
        "wires": [
            [
                "edac0e4a9396652f"
            ]
        ]
    },
    {
        "id": "3ef6e3cb08fd4645",
        "type": "subflow",
        "name": "Post schedule",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 360,
                "y": 280,
                "wires": [
                    {
                        "id": "16f20561eeb96ab6"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 1160,
                "y": 280,
                "wires": [
                    {
                        "id": "db876e05e8954483",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "16f20561eeb96ab6",
        "type": "function",
        "z": "3ef6e3cb08fd4645",
        "name": "Formatting json payload",
        "func": "msg.headers = {\n    \"Authorization\": \"Bearer \" + msg.bearerToken,\n    \"Content-Type\": \"application/vnd.api+json\"\n};\n\nmsg.payload = {\n    \"data\": {\n        \"type\": \"schedules\",\n        \"attributes\": {\n            \"name\": msg.scheduleName,\n            \"end-based\": msg[\"end-based\"]\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": 530,
        "y": 280,
        "wires": [
            [
                "8b789b3c687e3db2"
            ]
        ]
    },
    {
        "id": "8b789b3c687e3db2",
        "type": "http request",
        "z": "3ef6e3cb08fd4645",
        "name": "POST schedule",
        "method": "POST",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "https://api.oee.ai/schedules",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 760,
        "y": 280,
        "wires": [
            [
                "db876e05e8954483"
            ]
        ]
    },
    {
        "id": "db876e05e8954483",
        "type": "change",
        "z": "3ef6e3cb08fd4645",
        "name": "Delete response header",
        "rules": [
            {
                "t": "delete",
                "p": "headers",
                "pt": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 990,
        "y": 280,
        "wires": [
            []
        ]
    },
    {
        "id": "96e5bd1b60b318b2",
        "type": "subflow",
        "name": "Send schedules to oee.ai",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 480,
                "y": 340,
                "wires": [
                    {
                        "id": "15ad974df513bfef"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 1580,
                "y": 340,
                "wires": [
                    {
                        "id": "9ce0601df1e13c43",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "daf88fda59f965b3",
        "type": "switch",
        "z": "96e5bd1b60b318b2",
        "name": "Check if schedule already exists",
        "property": "statusCode",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "422",
                "vt": "num"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "false",
        "repair": false,
        "outputs": 2,
        "x": 870,
        "y": 340,
        "wires": [
            [
                "bc73d729c9e96211"
            ],
            [
                "a6b9c3d06b7fdf8c",
                "9ce0601df1e13c43"
            ]
        ]
    },
    {
        "id": "15ad974df513bfef",
        "type": "subflow:3ef6e3cb08fd4645",
        "z": "96e5bd1b60b318b2",
        "name": "",
        "x": 610,
        "y": 340,
        "wires": [
            [
                "daf88fda59f965b3"
            ]
        ]
    },
    {
        "id": "bc73d729c9e96211",
        "type": "subflow:dd9318a3a7c749d0",
        "z": "96e5bd1b60b318b2",
        "name": "",
        "x": 1120,
        "y": 280,
        "wires": [
            [
                "a6b9c3d06b7fdf8c",
                "9ce0601df1e13c43"
            ]
        ]
    },
    {
        "id": "a6b9c3d06b7fdf8c",
        "type": "debug",
        "z": "96e5bd1b60b318b2",
        "name": "Respons from oee.ai",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1420,
        "y": 300,
        "wires": []
    },
    {
        "id": "9ce0601df1e13c43",
        "type": "change",
        "z": "96e5bd1b60b318b2",
        "name": "Save schedule id",
        "rules": [
            {
                "t": "set",
                "p": "scheduleId",
                "pt": "msg",
                "to": "payload.data.id",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1410,
        "y": 340,
        "wires": [
            []
        ]
    },
    {
        "id": "9e66472980aab6f0",
        "type": "tab",
        "label": "Synchronizing shifts",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "4f808fcdb8794d55",
        "type": "comment",
        "z": "9e66472980aab6f0",
        "name": "Replace the Data input with your source and data transformation",
        "info": "",
        "x": 370,
        "y": 180,
        "wires": []
    },
    {
        "id": "aaa22de052f7f128",
        "type": "inject",
        "z": "9e66472980aab6f0",
        "name": "Data Input",
        "props": [],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 200,
        "y": 220,
        "wires": [
            [
                "2e6915a91e7f4466"
            ]
        ]
    },
    {
        "id": "167466639d86460a",
        "type": "change",
        "z": "9e66472980aab6f0",
        "name": "Mapping schedule attributes",
        "rules": [
            {
                "t": "set",
                "p": "scheduleName",
                "pt": "msg",
                "to": "Q1 schedule",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "end-based",
                "pt": "msg",
                "to": "false",
                "tot": "bool"
            },
            {
                "t": "set",
                "p": "location",
                "pt": "msg",
                "to": "",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1100,
        "y": 220,
        "wires": [
            [
                "5c8f83a28d8986f3"
            ]
        ]
    },
    {
        "id": "7fae43045599e197",
        "type": "comment",
        "z": "9e66472980aab6f0",
        "name": "^^ set schedule 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": 1090,
        "y": 260,
        "wires": []
    },
    {
        "id": "6ab2cecafd2430ea",
        "type": "comment",
        "z": "9e66472980aab6f0",
        "name": "^^ enter your oee.ai API token",
        "info": "You can find your API Token on oee.ai",
        "x": 800,
        "y": 260,
        "wires": []
    },
    {
        "id": "aa7a250d8a9016fa",
        "type": "change",
        "z": "9e66472980aab6f0",
        "name": "Set API Token",
        "rules": [
            {
                "t": "set",
                "p": "bearerToken",
                "pt": "msg",
                "to": "",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 760,
        "y": 220,
        "wires": [
            [
                "167466639d86460a"
            ]
        ]
    },
    {
        "id": "2e6915a91e7f4466",
        "type": "function",
        "z": "9e66472980aab6f0",
        "name": "Build shifts attributes array",
        "func": "msg.shiftsData = [\n    { \n        \"shiftName\": \"Morning shift\", \n        \"rrule\": \"DTSTART:20210331T060000Z\\nRRULE:FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR,SA,SU;INTERVAL=1\", \n        \"duration\": \"08:00\", \n        \"mode\": \"work\", \n        \"color\": \"azure\" \n    }, \n    { \n        \"shiftName\": \"Late shift\", \n        \"rrule\": \"DTSTART: 20210331T140000Z\\nRRULE:FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR,SA,SU;INTERVAL=1\", \n        \"duration\": \"08:00\",\n        \"mode\": \"work\", \n        \"color\": \"azure\" \n    }, \n    { \n        \"shiftName\": \"Night shift\", \n        \"rrule\": \"DTSTART:20211012T220000Z\\nRRULE:FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR,SA,SU;INTERVAL=1\",\n        \"duration\": \"08:00\", \n        \"mode\": \"work\", \n        \"color\": \"azure\" \n    }\n]\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 500,
        "y": 220,
        "wires": [
            [
                "aa7a250d8a9016fa"
            ]
        ]
    },
    {
        "id": "5c8f83a28d8986f3",
        "type": "subflow:96e5bd1b60b318b2",
        "z": "9e66472980aab6f0",
        "name": "",
        "x": 1370,
        "y": 220,
        "wires": [
            [
                "9f59e0e470ee9951"
            ]
        ]
    },
    {
        "id": "9f59e0e470ee9951",
        "type": "subflow:fd9e8ae80b46227a",
        "z": "9e66472980aab6f0",
        "name": "",
        "x": 1620,
        "y": 220,
        "wires": []
    },
    {
        "id": "b6ab43b651bf9892",
        "type": "comment",
        "z": "9e66472980aab6f0",
        "name": "^^ array of shifts in this format",
        "info": "",
        "x": 510,
        "y": 260,
        "wires": []
    }
]
