id: '**[Entity]_NotSyncedNotification_Pusher_Id**'
name: '**[Entity]_NotSyncedNotification_Pusher_Name**'
description:
options: []
beforePushSteps: []
pushSteps:
  - key: FORWARD
    rootStep: true
    name: Choose step to forward
    description:
    connectorId: '**SelfConnector**'
    displayType: FORWARD
    next: |-
      {{
        if ( item.__destination_id | string.size > 0)
          "UPDATE"
        else
          "CHECK_EXISTS"
        end
      }}
  - key: CHECK_EXISTS
    name: Check Exists
    description: ''
    connectorId: '**TrelloConnectionId**'
    displayType: API
    arguments:
      id: |-
        {{"[EntityName] `" | string.append item.id | string.append "`"}}
    data:
      url: /1/cards/{{connector.options.CARD_ID}}/actions
      type: REST
      method: GET
      annonymous: false
      headers:
        'Content-Type': ['application/json']
      queries:
        filter: 'commentCard'
      body:
      acceptHttpStatuses:
        success:
          parser: regex
          expression: 2\d{2}
      responseFilters:
        jsonata:
          id:
            acceptedStatuses:
              - success
            expression: |-
              $[$contains($.data.text, "{{__this.arguments.id}}")][0].id
    next: |-
      {{
        if (pusher.current.CHECK_EXISTS?.id | string.size) > 0
          "UPDATE"
        else 
          "CREATE"
        end
      }}
  - key: CREATE
    name: Create
    description:
    connectorId: '**TrelloConnectionId**'
    displayType: API
    next:
    arguments:
      ERROR_MESSAGE: |-
        {{
          error_message = "@card [EntityName] `" + item.id + "` can not be synced due to:\n\n"
          error_count = 0
          for field in (item | object.keys)
            if (field | string.starts_with "__error_messages_") && (item[field] | string.size) > 0
              if (item[field] | string.size) == (item[field] | array.size) # string
                # a string which will always an UL/LI (e.g.: "- SKU `abc` is not found.")
                error_message += item[field] + "\n"
                error_count += 1
              else # array
                for error in item[field]
                  if (error?.message | string.size) > 0
                    error_message += "- " + error.message + "\n"
                    error_count += 1
                  else if (error | string.size) > 0
                    error_message += "- " + error + "\n"
                    error_count += 1
                  end 
                end
              end 
            end 
          end
          
          if error_count <= 0
            error_message += "- The [EntityName] takes too long to sync.\n\n"
          end

          if (item.pushed_notes | string.size) > 0
            error_message += "\n###### Additional Notes:\n\n```\n" + item.pushed_notes + "\n```\n"
            error_count += 1
          end

          error_message += "\n---\n\n###### [EntityName] Details\n\n"
          for field in (item | object.keys)
            if (field | string.starts_with "__details_") && (item[field] | string.size) > 0
              error_message += "\n**" + (field | string.replace '__details_' '') + "**\n"
              if (item[field] | string.size) == (item[field] | array.size) # string
                error_message += "- " + (field | string.replace '__details_' '') + ": `" + item[field] + "`\n\n"  
              else
                for detail in item[field]
                  for fieldDetail in (detail | object.keys)
                    error_message += "- " + fieldDetail + ": `" + detail[fieldDetail] + "`\n"
                  end
                end                
              end
            end 
          end
          error_message | string.slice 0 length:4000
        }}
    data:
      url: /1/cards/{{connector.options.CARD_ID}}/actions/comments
      type: REST
      method: POST
      annonymous: false
      headers:
        'Accept': ['application/json']
      queries:
        text: '{{__this.arguments.ERROR_MESSAGE | hexasync.string.escape_uri}}'
      body: {}
      acceptHttpStatuses:
        success:
          parser: regex
          expression: 2\d{2}
      responseFilters:
        jsonata:
          id:
            acceptedStatuses:
              - success
            expression: |-
              $.id
  - key: UPDATE
    name: Update
    description:
    connectorId: '**TrelloConnectionId**'
    displayType: API
    next:
    arguments:
      ID: |-
        {{
          if item.__destination_id | string.size >0 
            item.__destination_id
          else           
            pusher.current.CHECK_EXISTS?.id
          end
        }}
      ERROR_MESSAGE: |-
        {{
          error_message = "@card [EntityName] `" + item.id + "` can not be synced due to:\n\n"
          error_count = 0
          for field in (item | object.keys)
            if (field | string.starts_with "__error_messages_") && (item[field] | string.size) > 0
              if (item[field] | string.size) == (item[field] | array.size) # string
                # a string which will always an UL/LI (e.g.: "- SKU `abc` is not found.")
                error_message += item[field] + "\n"
                error_count += 1
              else # array
                for error in item[field]
                  if (error?.message | string.size) > 0
                    error_message += "- " + error.message + "\n"
                    error_count += 1
                  else if (error | string.size) > 0
                    error_message += "- " + error + "\n"
                    error_count += 1
                  end 
                end
              end 
            end 
          end
          
          if error_count <= 0
            error_message += "- The [EntityName] takes too long to sync.\n\n"
          end

          if (item.pushed_notes | string.size) > 0
            error_message += "\n###### Additional Notes:\n\n```\n" + item.pushed_notes + "\n```\n"
            error_count += 1
          end

          error_message += "\n---\n\n###### [EntityName] Details\n\n"
          for field in (item | object.keys)
            if (field | string.starts_with "__details_") && (item[field] | string.size) > 0
              error_message += "\n**" + (field | string.replace '__details_' '') + "**\n"
              if (item[field] | string.size) == (item[field] | array.size) # string
                error_message += "- " + (field | string.replace '__details_' '') + ": `" + item[field] + "`\n\n"  
              else
                for detail in item[field]
                  for fieldDetail in (detail | object.keys)
                    error_message += "- " + fieldDetail + ": `" + detail[fieldDetail] + "`\n"
                  end
                end                
              end
            end 
          end
          error_message | string.slice 0 length:4000
        }}
    data:
      url: /1/actions/{{__this.arguments.ID}}
      type: REST
      method: PUT
      annonymous: false
      headers:
        'Content-Type': ['application/json']
        'Accept': ['application/json']
      queries:
        text: |-
          {{__this.arguments.ERROR_MESSAGE | hexasync.string.escape_uri}}
      body: {}
      acceptHttpStatuses:
        success:
          parser: regex
          expression: 2\d{2}
      responseFilters:
        jsonata:
          id:
            acceptedStatuses:
              - success
            expression: |-
              $.id
afterPushSteps: []
responseFilters: # we can use jsonpath or scriban here
  scriban:
    id:
      expression: |-
        {{
          if (pusher.current.CREATE?.id | string.size) > 0 
            pusher.current.CREATE.id
          else if (pusher.current.UPDATE?.id | string.size) > 0 
            pusher.current.UPDATE.id
          else 
            item.__destination_id
          end
        }}
    isSuccess:
      expression: |-
        {{
          if (pusher.current.UPDATE?.id | string.size) > 0
            true
          else if (pusher.current.CREATE?.id | string.size) > 0
            true
          else
            false
          end
        }}
    message:
      expression: |-
        {{ [pusher.current.CREATE?.message, pusher.current.UPDATE?.message, pusher.current.CHECK_EXISTS?.message] | array.join "\r\n" }
