{
  "name": "Recuperación de Carritos Abandonados Multicanal",
  "flow": [
    {
      "id": 1,
      "module": "gateway:CustomWebhook",
      "version": 1,
      "parameters": {
        "webhookId": "{{WEBHOOK_CARRITO_ABANDONADO_ID}}"
      },
      "mapper": {},
      "metadata": {
        "designer": { "x": 0, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "webhookId", "type": "text", "label": "Webhook ID" }
        ]
      }
    },
    {
      "id": 2,
      "module": "whatsapp-business:sendMessage",
      "version": 1,
      "parameters": {
        "phoneNumberId": "{{WHATSAPP_PHONE_ID}}",
        "recipientPhone": "{{1.customer.phone}}"
      },
      "mapper": {
        "type": "text",
        "text": "¡Hola {{1.customer.first_name}}! 👋\n\nNotamos que dejaste algunos productos en tu carrito:\n\n🛒 {{1.line_items[0].title}} x {{1.line_items[0].quantity}}\n💰 Total: ${{1.total_price}}\n\n¿Necesitas ayuda para completar tu compra? Tu carrito te está esperando:\n👉 {{1.abandoned_checkout_url}}"
      },
      "metadata": {
        "designer": { "x": 300, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "phoneNumberId", "type": "text", "label": "Phone Number ID" }
        ]
      }
    },
    {
      "id": 3,
      "module": "builtin:Sleep",
      "version": 1,
      "parameters": {},
      "mapper": {
        "duration": 14400
      },
      "metadata": {
        "designer": { "x": 600, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "duration", "type": "number", "label": "Delay (seconds): 4 hours" }
        ]
      }
    },
    {
      "id": 4,
      "module": "mailchimp:sendEmail",
      "version": 2,
      "parameters": {
        "apiKey": "{{MAILCHIMP_API_KEY}}"
      },
      "mapper": {
        "to": "{{1.customer.email}}",
        "subject": "{{1.customer.first_name}}, tu carrito te está esperando 🛒",
        "templateId": "{{MAILCHIMP_TEMPLATE_CARRITO}}",
        "mergeVars": {
          "FNAME": "{{1.customer.first_name}}",
          "PRODUCT_NAME": "{{1.line_items[0].title}}",
          "PRODUCT_IMAGE": "{{1.line_items[0].image_url}}",
          "PRODUCT_PRICE": "${{1.line_items[0].price}}",
          "TOTAL": "${{1.total_price}}",
          "CHECKOUT_URL": "{{1.abandoned_checkout_url}}"
        }
      },
      "filter": {
        "name": "Verificar si no compró",
        "conditions": [
          { "a": "{{1.customer.orders_count}}", "o": "number:equal", "b": "{{1.initialOrdersCount}}" }
        ]
      },
      "metadata": {
        "designer": { "x": 900, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "apiKey", "type": "text", "label": "API Key" }
        ]
      }
    },
    {
      "id": 5,
      "module": "builtin:Sleep",
      "version": 1,
      "parameters": {},
      "mapper": {
        "duration": 72000
      },
      "metadata": {
        "designer": { "x": 1200, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "duration", "type": "number", "label": "Delay (seconds): 20 hours" }
        ]
      }
    },
    {
      "id": 6,
      "module": "shopify:createPriceRule",
      "version": 2,
      "parameters": {
        "shopName": "{{SHOPIFY_STORE_NAME}}",
        "accessToken": "{{SHOPIFY_ACCESS_TOKEN}}"
      },
      "mapper": {
        "title": "RECOVERY10-{{1.customer.id}}-{{formatDate(now; \"YYYYMMDD\")}}",
        "targetType": "line_item",
        "allocationMethod": "across",
        "valueType": "percentage",
        "value": "-10",
        "customerSelection": "prerequisite",
        "prerequisiteCustomerIds": ["{{1.customer.id}}"],
        "usageLimit": 1,
        "startsAt": "{{now}}",
        "endsAt": "{{addDays(now; 2)}}"
      },
      "metadata": {
        "designer": { "x": 1500, "y": -150 },
        "restore": {},
        "expect": [
          { "name": "shopName", "type": "text", "label": "Shop Name" }
        ]
      }
    },
    {
      "id": 7,
      "module": "whatsapp-business:sendMessage",
      "version": 1,
      "parameters": {
        "phoneNumberId": "{{WHATSAPP_PHONE_ID}}",
        "recipientPhone": "{{1.customer.phone}}"
      },
      "mapper": {
        "type": "text",
        "text": "{{1.customer.first_name}}, ¡tenemos una sorpresa para ti! 🎁\n\nUsa el código *{{6.title}}* para obtener un *10% de descuento* en tu compra.\n\n🛒 Tu carrito: {{1.abandoned_checkout_url}}\n\n⏰ El código es válido por 48 horas.\n\n¡No dejes pasar esta oportunidad!"
      },
      "filter": {
        "name": "Verificar si no compró aún",
        "conditions": [
          { "a": "{{1.customer.orders_count}}", "o": "number:equal", "b": "{{1.initialOrdersCount}}" }
        ]
      },
      "metadata": {
        "designer": { "x": 1500, "y": 150 },
        "restore": {},
        "expect": [
          { "name": "phoneNumberId", "type": "text", "label": "Phone Number ID" }
        ]
      }
    },
    {
      "id": 8,
      "module": "builtin:BasicRouter",
      "version": 1,
      "parameters": {},
      "mapper": {},
      "routes": [
        {
          "label": "Recuperado",
          "filter": {
            "conditions": [
              { "a": "{{1.customer.orders_count}}", "o": "number:greaterThan", "b": "{{1.initialOrdersCount}}" }
            ]
          },
          "flow": [9]
        },
        {
          "label": "No Recuperado",
          "flow": [9]
        }
      ],
      "metadata": {
        "designer": { "x": 1800, "y": 0 },
        "restore": {},
        "expect": []
      }
    },
    {
      "id": 9,
      "module": "google-sheets:addRow",
      "version": 2,
      "parameters": {
        "spreadsheetId": "{{SPREADSHEET_CARRITOS_ID}}",
        "sheetId": "Resultados",
        "includeHeaders": true
      },
      "mapper": {
        "values": {
          "0": "{{formatDate(now; \"DD/MM/YYYY\")}}",
          "1": "{{1.customer.email}}",
          "2": "{{1.customer.first_name}}",
          "3": "${{1.total_price}}",
          "4": "{{if(1.customer.orders_count > 1.initialOrdersCount; \"Recuperado\"; \"Perdido\")}}",
          "5": "{{1.line_items[0].title}}"
        }
      },
      "metadata": {
        "designer": { "x": 2100, "y": 0 },
        "restore": {
          "spreadsheetId": { "label": "Carritos Abandonados" }
        },
        "expect": [
          { "name": "spreadsheetId", "type": "text", "label": "Spreadsheet ID" }
        ]
      }
    }
  ],
  "metadata": {
    "instant": true,
    "version": 1,
    "scenario": {
      "roundtrips": 1,
      "maxErrors": 3,
      "autoCommit": true,
      "autoCommitTriggerLast": true,
      "sequential": true
    },
    "designer": {
      "orphans": []
    },
    "zone": "us1.make.com"
  }
}