| 1 | DROP PROCEDURE IF EXISTS public.proc_create_support_ticket(bigint, bigint, bigint, bigint, text, text, text, text);
|
|---|
| 2 | DROP PROCEDURE IF EXISTS public.proc_create_support_ticket(bigint, bigint, bigint, bigint, text, text, text, text, text);
|
|---|
| 3 |
|
|---|
| 4 | CREATE PROCEDURE public.proc_create_support_ticket(
|
|---|
| 5 | p_customer_id bigint,
|
|---|
| 6 | p_account_id bigint,
|
|---|
| 7 | p_subscription_id bigint,
|
|---|
| 8 | p_employee_id bigint,
|
|---|
| 9 | p_ticket_type text,
|
|---|
| 10 | p_subject text,
|
|---|
| 11 | p_description text,
|
|---|
| 12 | p_priority text DEFAULT 'medium',
|
|---|
| 13 | p_channel text DEFAULT 'phone'
|
|---|
| 14 | )
|
|---|
| 15 | LANGUAGE plpgsql
|
|---|
| 16 | AS $$
|
|---|
| 17 | DECLARE
|
|---|
| 18 | v_ticket_id bigint;
|
|---|
| 19 | BEGIN
|
|---|
| 20 | IF NOT EXISTS (
|
|---|
| 21 | SELECT 1 FROM public.accounts
|
|---|
| 22 | WHERE account_id = p_account_id AND customer_id = p_customer_id
|
|---|
| 23 | ) THEN
|
|---|
| 24 | RAISE EXCEPTION 'Account % does not belong to customer %.', p_account_id, p_customer_id;
|
|---|
| 25 | END IF;
|
|---|
| 26 | IF p_subscription_id IS NOT NULL AND NOT EXISTS (
|
|---|
| 27 | SELECT 1 FROM public.subscriptions
|
|---|
| 28 | WHERE subscription_id = p_subscription_id AND account_id = p_account_id
|
|---|
| 29 | ) THEN
|
|---|
| 30 | RAISE EXCEPTION 'Subscription % does not belong to account %.', p_subscription_id, p_account_id;
|
|---|
| 31 | END IF;
|
|---|
| 32 | IF NOT EXISTS (
|
|---|
| 33 | SELECT 1 FROM public.employees
|
|---|
| 34 | WHERE employee_id = p_employee_id AND employment_status = 'active'
|
|---|
| 35 | ) THEN
|
|---|
| 36 | RAISE EXCEPTION 'Employee % does not exist or is not active.', p_employee_id;
|
|---|
| 37 | END IF;
|
|---|
| 38 | IF NOT EXISTS (SELECT 1 FROM public.crm_ticket_types WHERE code = p_ticket_type AND is_active) THEN
|
|---|
| 39 | RAISE EXCEPTION 'Ticket type % is not valid.', p_ticket_type;
|
|---|
| 40 | END IF;
|
|---|
| 41 | IF NOT EXISTS (SELECT 1 FROM public.ticket_priorities WHERE code = p_priority AND is_active) THEN
|
|---|
| 42 | RAISE EXCEPTION 'Priority % is not valid.', p_priority;
|
|---|
| 43 | END IF;
|
|---|
| 44 | IF NOT EXISTS (SELECT 1 FROM public.interaction_channels WHERE code = p_channel AND is_active) THEN
|
|---|
| 45 | RAISE EXCEPTION 'Interaction channel % is not valid.', p_channel;
|
|---|
| 46 | END IF;
|
|---|
| 47 |
|
|---|
| 48 | INSERT INTO public.crm_tickets (
|
|---|
| 49 | customer_id, account_id, subscription_id, assigned_employee_id,
|
|---|
| 50 | ticket_type, subject, description, priority, status, created_at
|
|---|
| 51 | ) VALUES (
|
|---|
| 52 | p_customer_id, p_account_id, p_subscription_id, p_employee_id,
|
|---|
| 53 | p_ticket_type, p_subject, p_description, p_priority, 'open', CURRENT_TIMESTAMP
|
|---|
| 54 | ) RETURNING ticket_id INTO v_ticket_id;
|
|---|
| 55 |
|
|---|
| 56 | INSERT INTO public.crm_interactions (
|
|---|
| 57 | ticket_id, employee_id, interaction_type, channel, interaction_time, notes
|
|---|
| 58 | ) VALUES (
|
|---|
| 59 | v_ticket_id, p_employee_id, 'customer_contact', p_channel, CURRENT_TIMESTAMP, 'Initial customer contact'
|
|---|
| 60 | );
|
|---|
| 61 |
|
|---|
| 62 | INSERT INTO public.employee_assignments (
|
|---|
| 63 | employee_id, ticket_id, assignment_type, start_time, status
|
|---|
| 64 | ) VALUES (
|
|---|
| 65 | p_employee_id, v_ticket_id, 'ticket_owner', CURRENT_TIMESTAMP, 'assigned'
|
|---|
| 66 | );
|
|---|
| 67 |
|
|---|
| 68 | RAISE NOTICE 'Support ticket % created and assigned to employee %.', v_ticket_id, p_employee_id;
|
|---|
| 69 | END;
|
|---|
| 70 | $$;
|
|---|
| 71 |
|
|---|