72 lines
2.2 KiB
PL/PgSQL
72 lines
2.2 KiB
PL/PgSQL
-- Funzione RPC pubblica (senza autenticazione) per la pagina di tracciamento
|
|
-- stato ticket lato cliente: https://flux.catelli.it/ticket/<id>
|
|
-- Espone SOLO dati pubblici (mai internal_notes, internal_cost, password o dati sensibili).
|
|
|
|
CREATE OR REPLACE FUNCTION public.get_public_ticket(p_ticket_id uuid)
|
|
RETURNS jsonb
|
|
LANGUAGE plpgsql
|
|
SECURITY DEFINER
|
|
SET search_path = public
|
|
AS $$
|
|
DECLARE
|
|
v_result jsonb;
|
|
BEGIN
|
|
SELECT jsonb_build_object(
|
|
'id', t.id,
|
|
'reference_id', t.reference_id,
|
|
'created_at', t.created_at,
|
|
'closed_at', t.closed_at,
|
|
'returned_at', t.returned_at,
|
|
'estimated_delivery_at', t.estimated_delivery_at,
|
|
'ticket_type', t.ticket_type,
|
|
'ticket_status', t.ticket_status,
|
|
'ticket_result', t.ticket_result,
|
|
'warranty_type', t.warranty_type,
|
|
'request', t.request,
|
|
'included_accessories', t.included_accessories,
|
|
'has_courtesy_device', COALESCE(t.has_courtesy_device, false),
|
|
'customer_price', COALESCE(t.customer_price, 0),
|
|
'public_notes', t.public_notes,
|
|
'resolution_notes', t.resolution_notes,
|
|
'target_model_name', m.name_with_brand,
|
|
'customer_name', c.name,
|
|
'company_name', comp.name,
|
|
'company_phone', comp.phone,
|
|
'company_email', comp.email,
|
|
'company_address', comp.address,
|
|
'company_city', comp.city,
|
|
'store_name', s.name,
|
|
'timeline', COALESCE(
|
|
(
|
|
SELECT jsonb_agg(
|
|
jsonb_build_object(
|
|
'id', tr.id,
|
|
'created_at', tr.created_at,
|
|
'event_type', tr.event_type,
|
|
'message', tr.message
|
|
)
|
|
ORDER BY tr.created_at DESC
|
|
)
|
|
FROM public.trackings tr
|
|
WHERE tr.parent_id = t.id
|
|
AND tr.parent_type = 'ticket'
|
|
AND COALESCE(tr.is_internal, true) = false
|
|
),
|
|
'[]'::jsonb
|
|
)
|
|
)
|
|
INTO v_result
|
|
FROM public.tickets t
|
|
LEFT JOIN public.models m ON m.id = t.target_model_id
|
|
LEFT JOIN public.customers c ON c.id = t.customer_id
|
|
LEFT JOIN public.companies comp ON comp.id = t.company_id
|
|
LEFT JOIN public.stores s ON s.id = t.store_id
|
|
WHERE t.id = p_ticket_id;
|
|
|
|
RETURN v_result;
|
|
END;
|
|
$$;
|
|
|
|
REVOKE ALL ON FUNCTION public.get_public_ticket(uuid) FROM PUBLIC;
|
|
GRANT EXECUTE ON FUNCTION public.get_public_ticket(uuid) TO anon, authenticated;
|