{
    "schema": "1.0",
    "key": "init-table-from-owner-items",
    "title": "Initialize my table from the owner account items",
    "description": "Shared job, owned by the account that owns the items and shared to its partner accounts. Run by a partner account, it triggers the export-items-by-ean job on the owner account (data-job-execution-create with wait=true and an API key of the owner stored in a job variable), waits for its completion, then imports the produced items file on the same shared table, on the running account's side (table-import-items). The table being the same, every field is imported as-is.",
    "userInputs": [
        {
            "key": "eans",
            "type": "TEXT",
            "defaultValue": "3838782461613",
            "required": true,
            "title": "EANs of the items to import (one per line)"
        }
    ],
    "tasks": [
        {
            "name": "code-execute-javascript",
            "taskReferenceName": "parse_eans",
            "description": "Parse the EAN list into an array and a comma-separated string",
            "optional": false,
            "type": "SUB_WORKFLOW",
            "inputParameters": {
                "expressionFile": "file://assets/parse-eans.js",
                "data": {
                    "eans": "${workflow.input.eans}"
                }
            }
        },
        {
            "name": "data-job-find",
            "taskReferenceName": "find_export_job",
            "description": "Find the export job on the owner account, by job key",
            "optional": false,
            "type": "SUB_WORKFLOW",
            "inputParameters": {
                "apiKey": "${workflow.globalVariables.api_key}",
                "findMode": "REQUEST",
                "request": {
                    "method": "INLINE",
                    "contentType": "application/json",
                    "model": "default",
                    "json": {
                        "criteria": {
                            "type": "eq",
                            "field": "key",
                            "value": "export-items-by-ean"
                        }
                    }
                }
            }
        },
        {
            "name": "data-job-execution-create",
            "taskReferenceName": "run_export_on_owner_account",
            "description": "Run the export job on the owner account and wait for its result",
            "optional": false,
            "type": "SUB_WORKFLOW",
            "inputParameters": {
                "apiKey": "${workflow.globalVariables.api_key}",
                "request": {
                    "method": "INLINE",
                    "contentType": "application/json",
                    "model": "default",
                    "json": {
                        "operations": [
                            {
                                "key": "a",
                                "context": {},
                                "element": {
                                    "jobId": "${find_export_job.output.result.json[0].id}",
                                    "info": {
                                        "title": "Export items by EAN (triggered by a shared job)"
                                    },
                                    "input": {
                                        "eansCsv": "${parse_eans.output.result.eansCsv}"
                                    }
                                }
                            }
                        ]
                    }
                },
                "wait": true,
                "waitOptions": {
                    "pollingIntervalSeconds": 10,
                    "timeoutSeconds": 1800
                }
            }
        },
        {
            "name": "table-import-items",
            "taskReferenceName": "import_items",
            "description": "Import the exported items on the shared table",
            "optional": false,
            "type": "SUB_WORKFLOW",
            "inputParameters": {
                "request": "${run_export_on_owner_account.output.result.json[0].response.output.items_file}",
                "accountKey": "${workflow.globalVariables.account_key}",
                "mode": "CREATE_OR_UPDATE"
            }
        }
    ],
    "outputParameters": {
        "requested_eans_count": "${parse_eans.output.result.count}",
        "exported_items_file": "${run_export_on_owner_account.output.result.json[0].response.output.items_file}",
        "import_report": "${import_items.output.report}"
    }
}
