{
    "schema": "1.0",
    "key": "comment-on-threads-open-for-more-than-a-certain-number-of-days",
    "title": "Relancer les threads ouverts depuis plus de 7 jours",
    "tasks": [
        {
            "name": "data-fetch-comment-threads",
            "taskReferenceName": "fetch_comment_threads",
            "description": "Fetch comment threads",
            "type": "SUB_WORKFLOW",
            "optional": false,
            "inputParameters": {
                "apiKey": "${workflow.variable.api_key}",
                "mode": "ALL"
            }
        },
        {
            "name": "code-execute-javascript",
            "taskReferenceName": "set_day_old",
            "description": "Compute age in days for each comment thread",
            "optional": false,
            "type": "SUB_WORKFLOW",
            "inputParameters": {
                "data": {
                    "commentThreads": "${fetch_comment_threads.output.json}"
                },
                "expression": "function f() { var commentThreads = $.data.commentThreads; for (var i = 0; i < commentThreads.length; i++) { var commentThread = commentThreads[i]; var createdDate = new Date(commentThread.createdAt); var currentDate = new Date(); var timeDifference = currentDate.getTime() - createdDate.getTime(); commentThread.daysOld = timeDifference / (1000 * 3600 * 24); } return { commentThreads: commentThreads };}f();"
            }
        },
        {
            "name": "json-transform-jq",
            "taskReferenceName": "filter_comment_threads_opened_old_enough_and_not_assigned_to_my_account",
            "description": "Filter comment threads opened more than X days ago and not assigned to my account",
            "optional": false,
            "type": "SUB_WORKFLOW",
            "inputParameters": {
                "data": {
                    "oldInDays": 1,
                    "accountId": "${workflow.input.context.userAccountId}",
                    "commentThreads": "${set_day_old.output.result.commentThreads}"
                },
                "queryExpression": ".data.oldInDays as $oldInDays | .data.accountId as $accountId | .data.commentThreads[] | select(.isClosed == false and .accountAssigneesId[] != $accountId and .daysOld > $oldInDays)"
            }
        },
        {
            "name": "json-transform-jq",
            "taskReferenceName": "generate_replies",
            "description": "Generate replies",
            "optional": false,
            "type": "SUB_WORKFLOW",
            "inputParameters": {
                "data": {
                    "message": "This thread requires action on your part",
                    "commentThreads": "${filter_comment_threads_opened_old_enough_and_not_assigned_to_my_account.output.resultList}"
                },
                "queryExpression": ".data.message as $message | [.data.commentThreads[] | {id: .id, message: $message}]"
            }
        },
        {
            "name": "data-create-comment-thread-replies",
            "taskReferenceName": "post_replies",
            "description": "Post replies",
            "optional": false,
            "type": "SUB_WORKFLOW",
            "inputParameters": {
                "apiKey": "${workflow.variable.api_key}",
                "replies": "${generate_replies.output.result}"
            }
        }
    ]
}
