mirror of
https://github.com/abhinavxd/libredesk.git
synced 2026-10-23 04:55:22 +00:00
refactor entire backend
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
-- name: insert-contact
|
||||
-- Step 1: Check if contact exists
|
||||
-- name: upsert-contact
|
||||
-- Check if contact exists
|
||||
WITH existing_contact AS (
|
||||
SELECT contact_id
|
||||
FROM public.contact_methods
|
||||
WHERE source = $1 AND source_id = $2
|
||||
)
|
||||
|
||||
-- Step 2: Insert contact if it does not exist
|
||||
-- Insert contact if it does not exist
|
||||
, ins_contact AS (
|
||||
INSERT INTO public.contacts (first_name, last_name, email, phone_number, avatar_url)
|
||||
SELECT $4, $5, $6, $7, $8
|
||||
@@ -14,7 +14,7 @@ WITH existing_contact AS (
|
||||
RETURNING id
|
||||
)
|
||||
|
||||
-- Step 3: Determine which contact ID to use
|
||||
-- Determine which contact ID to use
|
||||
, final_contact AS (
|
||||
SELECT contact_id AS id FROM existing_contact
|
||||
UNION ALL
|
||||
@@ -22,7 +22,7 @@ WITH existing_contact AS (
|
||||
LIMIT 1
|
||||
)
|
||||
|
||||
-- Step 4: Insert contact method if it does not exist
|
||||
-- Insert contact method if it does not exist
|
||||
, ins_contact_method AS (
|
||||
INSERT INTO public.contact_methods (contact_id, source, source_id, inbox_id)
|
||||
SELECT id, $1, $2, $3
|
||||
@@ -30,6 +30,6 @@ WITH existing_contact AS (
|
||||
ON CONFLICT DO NOTHING
|
||||
)
|
||||
|
||||
-- Step 5: Return the final contact ID
|
||||
-- Return the final contact ID
|
||||
SELECT id AS contact_id FROM final_contact;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user