refactor entire backend

This commit is contained in:
Abhinav Raut
2024-07-25 02:03:56 +05:30
parent 8b763fb167
commit 7ac9a2fe67
41 changed files with 800 additions and 752 deletions
+6 -6
View File
@@ -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;