| 1 | DROP VIEW IF EXISTS public.v_customer_support_ticket_timeline;
|
|---|
| 2 |
|
|---|
| 3 | CREATE VIEW public.v_customer_support_ticket_timeline AS
|
|---|
| 4 | SELECT
|
|---|
| 5 | c.customer_id,
|
|---|
| 6 | CASE WHEN c.customer_type = 'business' THEN c.company_name ELSE c.first_name || ' ' || c.last_name END AS customer_name,
|
|---|
| 7 | a.account_number,
|
|---|
| 8 | s.subscription_number,
|
|---|
| 9 | p.plan_name,
|
|---|
| 10 | t.ticket_id,
|
|---|
| 11 | t.ticket_type,
|
|---|
| 12 | t.subject,
|
|---|
| 13 | t.priority,
|
|---|
| 14 | t.status AS ticket_status,
|
|---|
| 15 | t.created_at AS ticket_created_at,
|
|---|
| 16 | t.closed_at AS ticket_closed_at,
|
|---|
| 17 | ROUND(EXTRACT(EPOCH FROM (t.closed_at - t.created_at))::numeric / 3600.0, 2) AS resolution_hours,
|
|---|
| 18 | owner.first_name || ' ' || owner.last_name AS assigned_employee,
|
|---|
| 19 | ci.interaction_id,
|
|---|
| 20 | ci.interaction_type,
|
|---|
| 21 | ci.channel,
|
|---|
| 22 | ci.interaction_time,
|
|---|
| 23 | ci.notes,
|
|---|
| 24 | ci.old_status,
|
|---|
| 25 | ci.new_status,
|
|---|
| 26 | actor.first_name || ' ' || actor.last_name AS interaction_employee,
|
|---|
| 27 | ROW_NUMBER() OVER (PARTITION BY t.ticket_id ORDER BY ci.interaction_time NULLS LAST, ci.interaction_id) AS interaction_sequence
|
|---|
| 28 | FROM public.customers c
|
|---|
| 29 | JOIN public.crm_tickets t ON t.customer_id = c.customer_id
|
|---|
| 30 | LEFT JOIN public.accounts a ON a.account_id = t.account_id
|
|---|
| 31 | LEFT JOIN public.subscriptions s ON s.subscription_id = t.subscription_id
|
|---|
| 32 | LEFT JOIN public.plans p ON p.plan_id = s.plan_id
|
|---|
| 33 | LEFT JOIN public.employees owner ON owner.employee_id = t.assigned_employee_id
|
|---|
| 34 | LEFT JOIN public.crm_interactions ci ON ci.ticket_id = t.ticket_id
|
|---|
| 35 | LEFT JOIN public.employees actor ON actor.employee_id = ci.employee_id;
|
|---|
| 36 |
|
|---|