{
  "connectors": [
    {
      "id": "340e4d04-2687-4b8c-b524-9c66893851cb",
      "providerId": "a6437be0-9797-4cab-bfd9-33d0374aa03e",
      "name": "Joy",
      "options": [{ "key": "PULL_QUERY_LIMIT", "defaultValue": 1000 }]
    }
  ],
  "objectAssociations": {
    "c6f25bf7-82c4-4aee-b8ca-fe4f7ebd944e": {
      "puller": [
        {
          "id": "3317ddb3-6e9e-41d3-bc29-88f11709ebe0",
          "resultKey": "customers"
        }
      ],
      "table": [{ "id": "c6f25bf7-82c4-4aee-b8ca-fe4f7ebd944e" }]
    },
    "0fc82f61-fb57-4ef0-866e-70b58a43cf68": {
      "puller": [
        {
          "id": "fd8230ab-957e-4f33-ae77-dccd865115b1",
          "resultKey": "point_activity"
        }
      ],
      "table": [{ "id": "0fc82f61-fb57-4ef0-866e-70b58a43cf68" }]
    }
  },
  "tables": [
    {
      "id": "7d1783ed-9201-40e7-8390-cd724b67d70c",
      "columns": {
        "id": { "indexGroups": ["PRIMARY_KEY"] },
        "shopify_customer_id": { "indexGroups": ["KEY"] },
        "type": { "indexGroups": ["NONE"] },
        "accepts_marketing": { "indexGroups": ["NONE"] },
        "created_at": { "indexGroups": ["NONE"] },
        "name": { "indexGroups": ["NONE"] },
        "has_point": { "indexGroups": ["NONE"] },
        "state": { "indexGroups": ["NONE"] },
        "shop_id": { "indexGroups": ["NONE"] },
        "email": { "indexGroups": ["NONE"] },
        "order_count": { "indexGroups": ["NONE"] },
        "total_spent": { "indexGroups": ["NONE"] },
        "point": { "indexGroups": ["NONE"] },
        "updated_at": { "indexGroups": ["NONE"] },
        "latest_activity": { "indexGroups": ["VALUE_COMPARABLE"] }
      }
    },
    {
      "id": "9eb85e0e-da4c-487a-b012-032d42072b89",
      "columns": {
        "id": { "indexGroups": ["PRIMARY_KEY"] },
        "hss_shopify_customer_id": { "indexGroups": ["KEY"] },
        "shop_id": { "indexGroups": ["NONE"] },
        "customer_id": { "indexGroups": ["NONE"] },
        "email": { "indexGroups": ["NONE"] },
        "content": { "indexGroups": ["NONE"] },
        "type": { "indexGroups": ["NONE"] },
        "source": { "indexGroups": ["NONE"] },
        "event": { "indexGroups": ["NONE"] },
        "old_point": { "indexGroups": ["NONE"] },
        "new_point": { "indexGroups": ["NONE"] },
        "program_id": { "indexGroups": ["NONE"] },
        "program_title": { "indexGroups": ["NONE"] },
        "price_rule_id": { "indexGroups": ["NONE"] },
        "discount_id": { "indexGroups": ["NONE"] },
        "coupon_code": { "indexGroups": ["NONE"] },
        "order_id": { "indexGroups": ["NONE"] },
        "order_number": { "indexGroups": ["NONE"] },
        "order_currency": { "indexGroups": ["NONE"] },
        "earn_by": { "indexGroups": ["NONE"] },
        "rate_money": { "indexGroups": ["NONE"] },
        "earn_point": { "indexGroups": ["NONE"] },
        "spend_point": { "indexGroups": ["NONE"] },
        "referral_customer_email": { "indexGroups": ["NONE"] },
        "referred_customer_email": { "indexGroups": ["NONE"] },
        "milestone_order": { "indexGroups": ["NONE"] },
        "is_pending_point_place_order": { "indexGroups": ["NONE"] },
        "refund_pending_point": { "indexGroups": ["NONE"] },
        "pending_point_until": { "indexGroups": ["NONE"] },
        "reason": { "indexGroups": ["NONE"] },
        "created_at": { "indexGroups": ["NONE"] },
        "updated_at": { "indexGroups": ["NONE"] },
        "hss_joy_customer_id": { "indexGroups": ["NONE"] }
      }
    }
  ],
  "objects": [
    {
      "id": "c6f25bf7-82c4-4aee-b8ca-fe4f7ebd944e",
      "name": "[Joy] Customer data",
      "priority": "**JoyCustomerObjectPriority**",
      "valueTableName": "__hss_joy_customer",
      "ignoreRemoval": true
    },
    {
      "id": "0fc82f61-fb57-4ef0-866e-70b58a43cf68",
      "name": "[Joy] Loyalty Point Activity data",
      "priority": "**JoyLoyaltyPointActivityObjectPriority**",
      "valueTableName": "__hss_joy_loyalty_point_activity",
      "ignoreRemoval": true
    }
  ],
  "pushers": [
    {
      "id": "fa9c66dc-d813-411a-adb3-0d6eb63df696",
      "name": "[Joy] Reset Point Pusher",
      "beforePushSteps": [
        {
          "key": "RESOLVE",
          "rootStep": true,
          "displayType": "FORWARD",
          "data": {
            "responseFilters": {
              "scriban": {
                "check_exist_regex": {
                  "expression": "^AX@CardNumber\\^{{ item.ax_card_number }}"
                },
                "admin_note": {
                  "expression": "AX@CardNumber^{{ item.ax_card_number }}@CustAccount^{{ item.ax_cust_account }}@ModifiedAt^{{ item.ax_modified_date_time }}"
                },
                "user_note": { "expression": "" }
              }
            }
          }
        }
      ],
      "pushSteps": [
        {
          "key": "RESET",
          "connectorId": "340e4d04-2687-4b8c-b524-9c66893851cb",
          "displayType": "API",
          "data": {
            "url": "/rest_api/v1/shopify/reset-point",
            "type": "REST",
            "method": "POST",
            "body": "{\n  \"shopifyCustomerId\": {{ item.shopify_customer_id }},\n  \"point\": {{ item.point }},\n  \"adminNote\": \"{{ pusher.before.RESOLVE.admin_note | hexasync.string.escape_json }}\",\n  \"userNote\": \"{{ pusher.before.RESOLVE.user_note | hexasync.string.escape_json }}\"\n}",
            "acceptHttpStatuses": {
              "success": { "parser": "regex", "expression": "2\\d{2}" }
            },
            "responseFilters": {
              "jsonata": {
                "success": {
                  "acceptedStatuses": ["success"],
                  "expression": "($.data.id or $contains($.error, \"not need to be updated\")) ~> $string"
                },
                "id": {
                  "acceptedStatuses": ["success"],
                  "expression": "{{ item.shopify_customer_id }}"
                }
              }
            }
          }
        }
      ],
      "responseFilters": {
        "scriban": {
          "id": {
            "expression": "{{\n  if (pusher.current?.RESET?.success == 'true')\n    pusher.current.RESET.id\n  end \n}}"
          },
          "isSuccess": {
            "expression": "{{\n  if (pusher.current?.RESET?.success == 'true')\n    true\n  end\n}}"
          },
          "message": { "expression": "" }
        }
      }
    },
    {
      "id": "**JoyResetPointPusher**",
      "beforePushSteps": [
        { "key": "BEFORE_RESOLVE", "next": "RESOLVE" },
        { "key": "RESOLVE", "next": "AFTER_RESOLVE" },
        { "key": "AFTER_RESOLVE" }
      ]
    }
  ],
  "pullers": [
    {
      "id": "3317ddb3-6e9e-41d3-bc29-88f11709ebe0",
      "name": "[Joy] Customer data",
      "beforePullSteps": [
        {
          "key": "GET_LAST_MODIFIED_DATE",
          "connectorId": "**SelfConnector**",
          "displayType": "SQL",
          "data": {
            "timeout": "600",
            "sql": "select \n  to_char(_.updated_at::TIMESTAMPTZ, 'YYYY-MM-DD\"T\"HH24:MI:SS\"Z\"')\nfrom \n  __hss_joy_customer _ \nwhere \n  _.updated_at is not null and _.updated_at != '' \norder by \n  _.updated_at desc \nlimit \n  1;",
            "resultSets": { "result": "single" },
            "keySets": {}
          }
        }
      ],
      "pullSteps": [
        {
          "key": "PULL_DATA",
          "displayType": "API",
          "connectorId": "340e4d04-2687-4b8c-b524-9c66893851cb",
          "arguments": {
            "after": "{{ puller?.lastToken?.PULL_DATA?.after }}",
            "updated_at_min": "{{ puller.history?.GET_LAST_MODIFIED_DATE?.result }}"
          },
          "data": {
            "url": "/rest_api/v1/customers",
            "type": "REST",
            "method": "GET",
            "queries": {
              "after": "{{ __this.arguments.after }}",
              "limit": "{{ connector.options.PULL_QUERY_LIMIT }}",
              "updated_at_min": "{{ __this.arguments.updated_at_min }}"
            },
            "acceptHttpStatuses": {
              "success": { "parser": "regex", "expression": "2\\d{2}" }
            },
            "responseFilters": {
              "jsonata": {
                "customers": {
                  "acceptedStatuses": ["success"],
                  "expression": "$.data.{\n    \"id\": id,\n    \"shopify_customer_id\": shopifyCustomerId,\n    \"type\": type,\n    \"accepts_marketing\": acceptsMarketing,\n    \"created_at\": createdAt,\n    \"name\": name,\n    \"has_point\": hasPoint,\n    \"state\": state,\n    \"shop_id\": shopId,\n    \"email\": email,\n    \"order_count\": orderCount,\n    \"total_spent\": totalSpent,\n    \"point\": point,\n    \"updated_at\": updatedAt,\n    \"latest_activity\": latestActivity\n}[]"
                }
              },
              "jsonpath": {
                "after": {
                  "acceptedStatuses": ["success"],
                  "expression": "type: eval\nexpression: |-\n  $.data[-1:].id"
                }
              },
              "scriban": {
                "hasNextPage": {
                  "acceptedStatuses": ["success"],
                  "expression": "{{ (filters.jsonata.customers | array.size) > 0 }}"
                },
                "__is_direct_index": {
                  "acceptedStatuses": ["success"],
                  "expression": "{{ (puller.history?.GET_LAST_MODIFIED_DATE?.result | string.size) > 0 }}"
                }
              }
            }
          }
        }
      ],
      "responseFilters": {
        "customers": "$.PULL_DATA.customers",
        "hasNextPage": "$.PULL_DATA.hasNextPage",
        "__is_direct_index": "$.RESPONSE_FILTERS.__is_direct_index"
      }
    },
    {
      "id": "fd8230ab-957e-4f33-ae77-dccd865115b1",
      "name": "[Joy] Point Activity Puller (Push-to-Pull)",
      "isDirectIndex": true,
      "beforePullSteps": [
        {
          "key": "GET_LAST_MODIFIED_DATE",
          "connectorId": "**SelfConnector**",
          "displayType": "SQL",
          "data": {
            "timeout": "600",
            "sql": "select \n  to_char(_.updated_at::TIMESTAMPTZ, 'YYYY-MM-DD\"T\"HH24:MI:SS\"Z\"')\nfrom \n  __hss_joy_loyalty_point_activity _ \nwhere \n  _.hss_shopify_customer_id = '{{ puller.arguments.shopify_customer_id }}' AND\n  _.updated_at is not null and _.updated_at != ''\norder by \n  _.updated_at desc \nlimit \n  1;",
            "resultSets": { "result": "single" },
            "keySets": {}
          }
        }
      ],
      "pullSteps": [
        {
          "key": "PULL_DATA",
          "connectorId": "340e4d04-2687-4b8c-b524-9c66893851cb",
          "displayType": "API",
          "arguments": {
            "after": "{{ puller?.lastToken?.PULL_DATA?.after }}",
            "updated_at_min": "{{ puller.history?.GET_LAST_MODIFIED_DATE?.result }}"
          },
          "data": {
            "url": "/rest_api/v1/activities",
            "type": "REST",
            "method": "GET",
            "queries": {
              "shopifyCustomerId": "{{ puller.arguments.shopify_customer_id }}",
              "after": "{{ __this.arguments.after }}",
              "limit": "{{ connector.options.PULL_QUERY_LIMIT }}",
              "updated_at_min": "{{ __this.arguments.updated_at_min }}"
            },
            "acceptHttpStatuses": {
              "success": { "parser": "regex", "expression": "2\\d{2}" }
            },
            "responseFilters": {
              "jsonata": {
                "point_activity": {
                  "acceptedStatuses": ["success"],
                  "expression": "$.data.{\n    \"id\": id,\n    \"shop_id\": shopId,\n    \"customer_id\": customerId,\n    \"email\": email,\n    \"content\": content,\n    \"type\": type,\n    \"source\": source,\n    \"event\": event,\n    \"old_point\": oldPoint,\n    \"new_point\": newPoint,\n    \"program_id\": programId,\n    \"program_title\": programTitle,\n    \"price_rule_id\": priceRuleId,\n    \"discount_id\": discountId,\n    \"coupon_code\": couponCode,\n    \"order_id\": orderId,\n    \"order_number\": orderNumber,\n    \"order_currency\": orderCurrency,\n    \"earn_by\": earnBy,\n    \"rate_money\": rateMoney,\n    \"earn_point\": earnPoint,\n    \"spend_point\": spendPoint,\n    \"referral_customer_email\": referralCustomerEmail,\n    \"referred_customer_email\": referredCustomerEmail,\n    \"milestone_order\": milestoneOrder,\n    \"is_pending_point_place_order\": isPendingPointPlaceOrder,\n    \"refund_pending_point\": refundPendingPoint,\n    \"pending_point_until\": pendingPointUntil,\n    \"reason\": reason,\n    \"created_at\": createdAt,\n    \"updated_at\": updatedAt,\n    \n    \"hss_shopify_customer_id\": {{ puller.arguments.shopify_customer_id }},\n    \"hss_joy_customer_id\": customerId\n}[]"
                }
              },
              "jsonpath": {
                "after": {
                  "acceptedStatuses": ["success"],
                  "expression": "type: eval\nexpression: |-\n  $.data[-1:].id"
                }
              },
              "scriban": {
                "hasNextPage": {
                  "acceptedStatuses": ["success"],
                  "expression": "{{ (filters.jsonata.point_activity | array.size) > 0 }}"
                }
              }
            }
          }
        }
      ],
      "responseFilters": {
        "point_activity": "$.PULL_DATA.point_activity",
        "hasNextPage": "$.PULL_DATA.hasNextPage"
      }
    }
  ],
  "webhooks": [
    {
      "id": "e1a5e4ae-66a4-4e10-abfd-0fd669ad0ef3",
      "connectionId": "340e4d04-2687-4b8c-b524-9c66893851cb",
      "name": "[Joy] Webhook Puller",
      "targets": {
        "ACTIVITIES": {
          "id": "19c8daf8-d843-4305-a74c-477035ec6e75",
          "autoEnqueue": true,
          "objectIds": ["0fc82f61-fb57-4ef0-866e-70b58a43cf68"],
          "matchFilter": "$.Headers['x-joy-loyalty-topic'][0]",
          "matchPattern": "^points\\/[^\\/]+",
          "dataFilter": "$.RawMessage",
          "dataParser": {
            "jsonata": {
              "activity": {
                "acceptedStatuses": ["success"],
                "expression": "$.RawMessage.{\n  \"id\": id,\n  \"shop_id\": shopId,\n  \"customer_id\": customerId,\n  \"email\": email,\n  \"content\": content,\n  \"type\": type,\n  \"source\": source,\n  \"event\": event,\n  \"old_point\": oldPoint,\n  \"new_point\": newPoint,\n  \"program_id\": programId,\n  \"program_title\": programTitle,\n  \"price_rule_id\": priceRuleId,\n  \"discount_id\": discountId,\n  \"coupon_code\": couponCode,\n  \"order_id\": orderId,\n  \"order_number\": orderNumber,\n  \"order_currency\": orderCurrency,\n  \"earn_by\": earnBy,\n  \"rate_money\": rateMoney,\n  \"earn_point\": earnPoint,\n  \"spend_point\": spendPoint,\n  \"referral_customer_email\": referralCustomerEmail,\n  \"referred_customer_email\": referredCustomerEmail,\n  \"milestone_order\": milestoneOrder,\n  \"is_pending_point_place_order\": isPendingPointPlaceOrder,\n  \"refund_pending_point\": refundPendingPoint,\n  \"pending_point_until\": pendingPointUntil,\n  \"reason\": reason,\n  \"created_at\": createdAt,\n  \"updated_at\": updatedAt,\n  \n  \"hss_shopify_customer_id\": customer.shopifyCustomerId,\n  \"hss_joy_customer_id\": customerId\n}[]"
              }
            }
          }
        }
      }
    }
  ]
}
