> For the complete documentation index, see [llms.txt](https://grouple.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://grouple.gitbook.io/docs/supabase/database-triggers.md).

# Database Triggers

### Overview <a href="#overview" id="overview"></a>

Grouple uses 14 database triggers to automate critical business logic — ensuring emails are sent, currencies are synced, timestamps are recorded, and settlements are created automatically without manual API calls.

### [​](https://grouple.mintlify.app/supabase/triggers#triggers-by-table)Triggers by Table <a href="#triggers-by-table" id="triggers-by-table"></a>

#### [​](https://grouple.mintlify.app/supabase/triggers#enquiries-table)`enquiries` Table <a href="#enquiries-table" id="enquiries-table"></a>

| Trigger                        | Event  | Timing | Action                                                                                            |
| ------------------------------ | ------ | ------ | ------------------------------------------------------------------------------------------------- |
| `tr_sync_enquiry_currency`     | INSERT | BEFORE | Calls `sync_enquiry_currency()` — sets currency\_code from the city’s default currency            |
| `on_enquiry_created_broadcast` | INSERT | AFTER  | Calls `broadcast_enquiry_to_venues()` — matches and creates venue\_leads for relevant venues      |
| `send_enquiry_notification`    | INSERT | AFTER  | Calls `send-lead-email` Edge Function via HTTP — sends lead notification emails to matched venues |

#### [​](https://grouple.mintlify.app/supabase/triggers#venue_leads-table)`venue_leads` Table <a href="#venue_leads-table" id="venue_leads-table"></a>

| Trigger                                    | Event  | Timing | Action                                                                                                                                |
| ------------------------------------------ | ------ | ------ | ------------------------------------------------------------------------------------------------------------------------------------- |
| `before_insert_venue_leads_set_rfq_number` | INSERT | BEFORE | Calls `set_merchant_rfq_number()` — assigns a sequential RFQ number per merchant                                                      |
| `send_email_on_new_lead`                   | INSERT | AFTER  | Calls `grouple_notifications.trigger_lead_email()` — additional lead email notification                                               |
| `trg_set_venue_lead_timestamps`            | UPDATE | BEFORE | Calls `fn_set_venue_lead_timestamps()` — records accepted\_at, declined\_at, closed\_won\_at, closed\_lost\_at based on status change |
| `trg_record_response_time`                 | UPDATE | AFTER  | Calls `fn_record_response_time()` — logs response time to `lead_response_times` when merchant first responds                          |

#### [​](https://grouple.mintlify.app/supabase/triggers#proposals-table)`proposals` Table <a href="#proposals-table" id="proposals-table"></a>

| Trigger                       | Event  | Timing | Action                                                                                                                         |
| ----------------------------- | ------ | ------ | ------------------------------------------------------------------------------------------------------------------------------ |
| `trigger_sync_settlement`     | UPDATE | AFTER  | Calls `sync_confirmed_proposal_to_settlements()` — creates/updates `commission_settlements` when proposal is confirmed         |
| `proposal-updated-email`      | UPDATE | AFTER  | Calls `send-proposal-email` Edge Function via HTTP — emails customer when proposal is updated/sent                             |
| `proposal-confirmation-email` | UPDATE | AFTER  | Calls `reservation-confirmation-emails` Edge Function via HTTP — sends confirmation to both parties when proposal is confirmed |

#### [​](https://grouple.mintlify.app/supabase/triggers#customers-table)`customers` Table <a href="#customers-table" id="customers-table"></a>

| Trigger                              | Event  | Timing | Action                                                                                                                   |
| ------------------------------------ | ------ | ------ | ------------------------------------------------------------------------------------------------------------------------ |
| `on_customer_created_link_enquiries` | INSERT | AFTER  | Calls `link_customer_to_enquiries()` — links pre-existing enquiries (submitted before signup) to the new customer record |

#### [​](https://grouple.mintlify.app/supabase/triggers#merchants-table)`merchants` Table <a href="#merchants-table" id="merchants-table"></a>

| Trigger                       | Event  | Timing | Action                                                                                                                |
| ----------------------------- | ------ | ------ | --------------------------------------------------------------------------------------------------------------------- |
| `trigger_sync_venue_currency` | UPDATE | AFTER  | Calls `sync_venue_currency_from_merchant()` — syncs currency\_code to all venues when merchant updates their currency |

#### [​](https://grouple.mintlify.app/supabase/triggers#venues-table)`venues` Table <a href="#venues-table" id="venues-table"></a>

| Trigger                | Event  | Timing | Action                                                                                                        |
| ---------------------- | ------ | ------ | ------------------------------------------------------------------------------------------------------------- |
| `tr_check_venue_limit` | INSERT | BEFORE | Calls `check_venue_limit()` — prevents venue creation if merchant has reached their plan’s `max_venues` limit |

#### [​](https://grouple.mintlify.app/supabase/triggers#venue_knowledge-table)`venue_knowledge` Table <a href="#venue_knowledge-table" id="venue_knowledge-table"></a>

| Trigger                   | Event  | Timing | Action                                                                                                          |
| ------------------------- | ------ | ------ | --------------------------------------------------------------------------------------------------------------- |
| `tr_check_menu_pdf_limit` | INSERT | BEFORE | Calls `check_menu_pdf_limit()` — prevents PDF upload if merchant has reached their plan’s `max_menu_pdfs` limit |

### [​](https://grouple.mintlify.app/supabase/triggers#key-trigger-flows)Key Trigger Flows <a href="#key-trigger-flows" id="key-trigger-flows"></a>

#### [​](https://grouple.mintlify.app/supabase/triggers#new-enquiry-%E2%86%92-leads-created-%E2%86%92-emails-sent)New Enquiry → Leads Created → Emails Sent <a href="#new-enquiry-leads-created-emails-sent" id="new-enquiry-leads-created-emails-sent"></a>

```
Customer submits enquiry
        ↓
BEFORE INSERT: tr_sync_enquiry_currency → sets currency from city
        ↓
AFTER INSERT: on_enquiry_created_broadcast → fn_match_venues_for_enquiry()
  Creates venue_leads records for matched venues
        ↓
AFTER INSERT on venue_leads: before_insert_venue_leads_set_rfq_number
  Sets merchant_rfq_number
        ↓
AFTER INSERT on venue_leads: send_email_on_new_lead
  Sends lead email to merchant
        ↓
AFTER INSERT on enquiries: send_enquiry_notification
  Also calls send-lead-email Edge Function (belt-and-suspenders)
```

#### [​](https://grouple.mintlify.app/supabase/triggers#proposal-confirmed-%E2%86%92-settlement-created-%E2%86%92-emails-sent)Proposal Confirmed → Settlement Created → Emails Sent <a href="#proposal-confirmed-settlement-created-emails-sent" id="proposal-confirmed-settlement-created-emails-sent"></a>

```
Customer confirms proposal → proposal.status = "confirmed"
        ↓
AFTER UPDATE: trigger_sync_settlement
  Creates commission_settlements record with applied_commission_rate
        ↓
AFTER UPDATE: proposal-confirmation-email
  Calls reservation-confirmation-emails Edge Function
  Sends confirmation to customer + merchant
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://grouple.gitbook.io/docs/supabase/database-triggers.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
