# Task Update Trigger Summary

## Overview
A new trigger `trg_task_update_process_metrics` has been created that fires on **ANY** task update (not just status changes to "Fixed"). This trigger calls three functions to process store metrics and category data.

## Trigger Details

**Trigger Name:** `trg_task_update_process_metrics`  
**Function:** `fn_task_update_process_metrics()`  
**Fires:** `AFTER UPDATE` on `task` table  
**Scope:** `FOR EACH ROW`

## Functions Called

The trigger calls three functions in sequence:

1. **`process_store_category_data`**
   - Parameters: `customer_id`, `store_code`, `week_start_date` (from updated task)
   - Returns: `jsonb`

2. **`update_product_store_data`**
   - Parameters: `customer_id`, `store_code`, `week_start_date` (from updated task)
   - Returns: `void`

3. **`process_store_metrics_set_based`**
   - Parameters: `store_code`, `week_start_date`, `customer_id`, `timeout` (from updated task)
   - Returns: `jsonb`

## Parameters Passed

All parameters are extracted from the `NEW` row (the updated task):
- `NEW.customer_id` → passed to all functions
- `NEW.store_code` → passed as comma-separated text (even if single value)
- `NEW.week_start_date` → passed as comma-separated text (even if single value)

## Old Trigger Removed

The old trigger `trg_task_fixed_update_visibility` has been removed. This trigger only fired when status changed from "Not Fixed" to "Fixed".

## Logging

All trigger executions are logged to `trigger_execution_log` table with:
- Trigger name: `trg_task_update_process_metrics`
- Task details (id, customer_id, store_code, week_start_date)
- Status (processing, success, error)
- Execution duration
- Notes about each function call

## Files Created

1. **`task_update_trigger.sql`** - Trigger function definition
2. **`create_task_update_trigger.sql`** - Script to create the trigger
3. **`TASK_UPDATE_TRIGGER_SUMMARY.md`** - This documentation

## Installation

The trigger has been deployed via migration. To recreate manually:

```sql
-- Run the function definition
\i database/functions/task_update_trigger.sql

-- Create the trigger
\i database/functions/create_task_update_trigger.sql
```

## Testing

The trigger has been tested and verified to:
- Fire on any task update
- Call all three functions successfully
- Log execution details
- Handle errors gracefully

## Notes

- The trigger fires on **any** UPDATE to the task table, not just status changes
- All three functions are called even if one fails (errors are logged but don't stop execution)
- Timeout is set to 10s per function call
- The trigger checks for required fields (customer_id, store_code, week_start_date) before processing

