Spaces:
Sleeping
Sleeping
File size: 5,152 Bytes
2a29b09 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | # π Supabase Audit Webhook Integration Guide
This guide explains how to configure Supabase Database Webhooks to send real-time alerts (e.g., to Slack, MS Teams, or Discord) whenever a user performs a financial statement analysis and a new entry is added to the `financial_analysis_audit` table.
---
## 1. Supabase Dashboard Configuration
Follow these steps to enable the database webhook directly in the Supabase Dashboard:
1. **Navigate to Database Webhooks**:
- Go to your [Supabase Project Dashboard](https://supabase.com/dashboard).
- Select your project, click on **Database** in the left sidebar, and click on **Webhooks** (or go to **Integrations** -> **Webhooks**).
- Click the **Enable Webhooks** button if webhooks are not already enabled.
2. **Create Webhook**:
- Click **Create Webhook**.
- **Name**: `send_audit_notification`
- **Schema**: `public`
- **Table**: `financial_analysis_audit`
- **Events**: Toggle on **`INSERT`** only (so it fires only when a new log is created).
- **Webhook Type**: Select **HTTP POST**.
- **HTTP URL**:
- If routing via **Zapier/Make**, enter your webhook trigger URL.
- If routing directly to Slack, enter your **Supabase Edge Function URL** (see Section 3 for direct Slack formatting).
- **Timeout**: `10` seconds (default).
- Click **Create Webhook**.
---
## 2. Event Payload Schema
When a new row is inserted, Supabase POSTs a JSON payload with the following schema:
```json
{
"type": "INSERT",
"table": "financial_analysis_audit",
"schema": "public",
"record": {
"id": 42,
"timestamp": "2026-05-22T10:09:00Z",
"user_email": "analyst@company.com",
"company_ticker": "TCS.NS",
"company_name": "Tata Consultancy Services Limited",
"selected_formats": "xlsx, docx, pptx",
"ai_model_used": "groq, gemini",
"status": "completed"
},
"old_record": null
}
```
---
## 3. Formatting for Slack / Teams (Supabase Edge Function)
Since Slack and Teams require specific JSON formats (e.g., `{"text": "..."}`), you cannot point the Supabase Database Webhook directly to Slack's webhook URL without a translator.
Below is the code for a lightweight **Supabase Edge Function** in TypeScript that acts as a translator.
### Edge Function Code (`index.ts`)
```typescript
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
const SLACK_WEBHOOK_URL = Deno.env.get("SLACK_WEBHOOK_URL") || "";
serve(async (req) => {
try {
const payload = await req.json();
const record = payload.record;
if (!record) {
return new Response(JSON.stringify({ error: "No record found" }), { status: 400 });
}
// Build a rich Slack block layout
const slackMessage = {
text: `π New Financial Analysis Run: ${record.company_name} (${record.company_ticker})`,
blocks: [
{
type: "header",
text: {
type: "plain_text",
text: "π Financial Statement Analysis Logged",
emoji: true
}
},
{
type: "section",
fields: [
{
type: "mrkdwn",
text: `*User:* \`${record.user_email}\``
},
{
type: "mrkdwn",
text: `*Company:* *${record.company_name}* (\`${record.company_ticker}\`)`
},
{
type: "mrkdwn",
text: `*Formats Generated:* \`${record.selected_formats}\``
},
{
type: "mrkdwn",
text: `*AI Engines Used:* \`${record.ai_model_used}\``
}
]
},
{
type: "context",
elements: [
{
type: "mrkdwn",
text: `*Status:* ${record.status === "completed" ? "β
Completed Successfully" : "β Failed"} | *Time:* \`${record.timestamp}\``
}
]
}
]
};
// Forward formatted payload to Slack
const response = await fetch(SLACK_WEBHOOK_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(slackMessage),
});
return new Response(JSON.stringify({ success: true }), { status: 200 });
} catch (error) {
return new Response(JSON.stringify({ error: error.message }), { status: 500 });
}
})
```
### Steps to Deploy the Edge Function:
1. Install Supabase CLI: `npm install -g supabase`
2. Login: `supabase login`
3. Initialize in your folder: `supabase init`
4. Create Edge Function: `supabase functions new slack-notifier`
5. Replace the generated code in `supabase/functions/slack-notifier/index.ts` with the TypeScript code above.
6. Set the Slack Webhook secret:
```bash
supabase secrets set SLACK_WEBHOOK_URL=https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
```
7. Deploy the function:
```bash
supabase functions deploy slack-notifier --project-ref your-project-ref-id
```
8. Set the Webhook URL in Supabase Dashboard to point to your new deployed function:
`https://your-project-ref-id.supabase.co/functions/v1/slack-notifier`
|