| 1 | CREATE OR REPLACE PROCEDURE proc_create_support_ticket(
|
|---|
| 2 | p_customer_id BIGINT,
|
|---|
| 3 | p_account_id BIGINT,
|
|---|
| 4 | p_subscription_id BIGINT,
|
|---|
| 5 | p_employee_id BIGINT,
|
|---|
| 6 | p_ticket_type TEXT,
|
|---|
| 7 | p_subject TEXT,
|
|---|
| 8 | p_description TEXT,
|
|---|
| 9 | p_priority TEXT DEFAULT 'medium'
|
|---|
| 10 | )
|
|---|
| 11 | LANGUAGE plpgsql
|
|---|
| 12 | AS $$
|
|---|
| 13 | BEGIN
|
|---|
| 14 | -- Check customer exists
|
|---|
| 15 | IF NOT EXISTS (
|
|---|
| 16 | SELECT 1
|
|---|
| 17 | FROM customers
|
|---|
| 18 | WHERE customer_id = p_customer_id
|
|---|
| 19 | ) THEN
|
|---|
| 20 | RAISE EXCEPTION 'Customer % does not exist.', p_customer_id;
|
|---|
| 21 | END IF;
|
|---|
| 22 |
|
|---|
| 23 | -- Check employee exists and is active
|
|---|
| 24 | IF NOT EXISTS (
|
|---|
| 25 | SELECT 1
|
|---|
| 26 | FROM employees
|
|---|
| 27 | WHERE employee_id = p_employee_id
|
|---|
| 28 | AND employment_status = 'active'
|
|---|
| 29 | ) THEN
|
|---|
| 30 | RAISE EXCEPTION 'Employee % does not exist or is not active.', p_employee_id;
|
|---|
| 31 | END IF;
|
|---|
| 32 |
|
|---|
| 33 | -- Insert new support ticket
|
|---|
| 34 | INSERT INTO crm_tickets (
|
|---|
| 35 | customer_id,
|
|---|
| 36 | account_id,
|
|---|
| 37 | subscription_id,
|
|---|
| 38 | assigned_employee_id,
|
|---|
| 39 | ticket_type,
|
|---|
| 40 | subject,
|
|---|
| 41 | description,
|
|---|
| 42 | priority,
|
|---|
| 43 | status,
|
|---|
| 44 | created_at
|
|---|
| 45 | )
|
|---|
| 46 | VALUES (
|
|---|
| 47 | p_customer_id,
|
|---|
| 48 | p_account_id,
|
|---|
| 49 | p_subscription_id,
|
|---|
| 50 | p_employee_id,
|
|---|
| 51 | p_ticket_type,
|
|---|
| 52 | p_subject,
|
|---|
| 53 | p_description,
|
|---|
| 54 | p_priority,
|
|---|
| 55 | 'open',
|
|---|
| 56 | CURRENT_TIMESTAMP
|
|---|
| 57 | );
|
|---|
| 58 |
|
|---|
| 59 | RAISE NOTICE 'Support ticket created successfully.';
|
|---|
| 60 | END;
|
|---|
| 61 | $$; |
|---|