"""
TITLE: "Send booking confirmation"
DB_TRIGGERS:
  - name: ON_LEAD_STATUS_UPDATED
    model: crm.lead
    trigger: on_write
    trigger_fields: stage_id
"""

def handle_db(records):
    # records are instances of crm.lead
    for r in records.with_context(MAGIC.env.context):
        if not r.stage_id.is_won:
            MAGIC.log(f"Skip stage {r.stage_id.name}: not won", MAGIC.LOG_DEBUG)

            continue
        if not r.partner_id:
            MAGIC.log(f"Skip lead {r.name}: no partner", MAGIC.LOG_DEBUG)
            continue
        LIB.AI.notify(r.partner_id, f"CONFIRMED:\n{r.name}\n\n{r.description}")