{
  "name": "PBD WF04 \u2014 Daily Review Dispatcher (5\u2605 auto-send / draft rest)",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 22 * * *"
            }
          ]
        }
      },
      "id": "cron-daily-5pm",
      "name": "Daily 5 PM CT",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "resource": "deal",
        "operation": "search",
        "filterGroupsUi": {
          "filterGroupsValues": [
            {
              "filtersUi": {
                "filterValues": [
                  {
                    "propertyName": "dealstage",
                    "operator": "EQ",
                    "value": "service_complete"
                  },
                  {
                    "propertyName": "closedate",
                    "operator": "GTE",
                    "value": "={{ $now.minus({hours: 72}).toISO() }}"
                  },
                  {
                    "propertyName": "closedate",
                    "operator": "LTE",
                    "value": "={{ $now.minus({hours: 24}).toISO() }}"
                  }
                ]
              }
            }
          ]
        },
        "additionalFields": {
          "propertiesToInclude": [
            "dealname",
            "amount",
            "service_type",
            "customer_satisfaction_rating",
            "associated_contact_id"
          ]
        }
      },
      "id": "hs-search-deals",
      "name": "Find Recently Completed Deals",
      "type": "n8n-nodes-base.hubspot",
      "typeVersion": 2,
      "position": [
        460,
        300
      ],
      "credentials": {
        "hubspotApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": false,
                  "leftValue": "",
                  "typeValidation": "loose"
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.properties.customer_satisfaction_rating }}",
                    "rightValue": "5",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ]
              },
              "outputKey": "five_star"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": false,
                  "leftValue": "",
                  "typeValidation": "loose"
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.properties.customer_satisfaction_rating }}",
                    "rightValue": "5",
                    "operator": {
                      "type": "string",
                      "operation": "notEquals"
                    }
                  }
                ]
              },
              "outputKey": "needs_draft"
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra"
        }
      },
      "id": "switch-rating",
      "name": "Route by Rating",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "resource": "contact",
        "operation": "get",
        "contactId": "={{ $json.properties.associated_contact_id }}"
      },
      "id": "hs-get-contact-5star",
      "name": "Get Contact (5\u2605)",
      "type": "n8n-nodes-base.hubspot",
      "typeVersion": 2,
      "position": [
        900,
        200
      ],
      "credentials": {
        "hubspotApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.brevo.com/v3/smtp/email",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"sender\": {\"name\": \"Pals By Design\", \"email\": \"service@palsbydesign.co\"},\n  \"to\": [{\"email\": \"{{ $json.properties.email }}\", \"name\": \"{{ $json.properties.firstname }} {{ $json.properties.lastname }}\"}],\n  \"subject\": \"Thank you from Pals By Design \u2014 quick favor?\",\n  \"htmlContent\": \"<p>Hi {{ $json.properties.firstname }},</p><p>Thank you for trusting Pals By Design with your recent service. We genuinely appreciate working with you.</p><p>Would you take 60 seconds to share a quick review? It helps neighbors in the Grafton area find us.</p><p><a href='https://g.page/r/palsbydesign/review'>Leave a Google review</a></p><p>\u2014 The Pals By Design team<br>(262) 302-5844</p>\"\n}",
        "options": {}
      },
      "id": "brevo-send-5star",
      "name": "Send 5\u2605 Review Email",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        200
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "engagement",
        "operation": "create",
        "type": "task",
        "metadata": {
          "subject": "Draft review request \u2014 {{ $json.properties.dealname }}",
          "body": "Customer rating: {{ $json.properties.customer_satisfaction_rating }}/5. Review the situation before sending a review request. Service: {{ $json.properties.service_type }}.",
          "status": "NOT_STARTED",
          "forObjectType": "DEAL",
          "forObjectId": "={{ $json.id }}"
        }
      },
      "id": "hs-create-task",
      "name": "Create HubSpot Task (Draft)",
      "type": "n8n-nodes-base.hubspot",
      "typeVersion": 2,
      "position": [
        900,
        400
      ],
      "credentials": {
        "hubspotApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "options": {}
      },
      "id": "aggregate-summary",
      "name": "Aggregate for Summary",
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        1340,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.brevo.com/v3/smtp/email",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"sender\": {\"name\": \"PBD n8n\", \"email\": \"service@palsbydesign.co\"},\n  \"to\": [{\"email\": \"legacy@palsbydesign.co\", \"name\": \"Mitchel\"}],\n  \"subject\": \"Review dispatcher summary \u2014 {{ $now.toFormat('LLL d') }}\",\n  \"htmlContent\": \"<p>Daily review-request dispatcher results:</p><p>Auto-sent (5\u2605): {{ $json.data.filter(d => d.properties.customer_satisfaction_rating === '5').length }}</p><p>Drafts queued in HubSpot: {{ $json.data.filter(d => d.properties.customer_satisfaction_rating !== '5').length }}</p>\"\n}",
        "options": {}
      },
      "id": "brevo-summary",
      "name": "Email Summary to Mitchel",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1560,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Daily 5 PM CT": {
      "main": [
        [
          {
            "node": "Find Recently Completed Deals",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find Recently Completed Deals": {
      "main": [
        [
          {
            "node": "Route by Rating",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route by Rating": {
      "main": [
        [
          {
            "node": "Get Contact (5\u2605)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create HubSpot Task (Draft)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Contact (5\u2605)": {
      "main": [
        [
          {
            "node": "Send 5\u2605 Review Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send 5\u2605 Review Email": {
      "main": [
        [
          {
            "node": "Aggregate for Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create HubSpot Task (Draft)": {
      "main": [
        [
          {
            "node": "Aggregate for Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate for Summary": {
      "main": [
        [
          {
            "node": "Email Summary to Mitchel",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "timezone": "America/Chicago"
  },
  "tags": [
    {
      "name": "pbd"
    },
    {
      "name": "reviews"
    }
  ]
}