Skip to content

Commit

Permalink
feat: add onboarding_a column to onboarding table (#1340)
Browse files Browse the repository at this point in the history
  • Loading branch information
mamadoudicko authored Oct 6, 2023
1 parent f13f08c commit 16cea2c
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 34 deletions.
32 changes: 23 additions & 9 deletions backend/models/databases/supabase/onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ class OnboardingUpdatableProperties(BaseModel):

"""Properties that can be received on onboarding update"""

onboarding_a: Optional[bool]
onboarding_b1: Optional[bool]
onboarding_b2: Optional[bool]
onboarding_b3: Optional[bool]

class Config:
extra = "forbid"

class GetOnboardingResponse(BaseModel):

class OnboardingStates(BaseModel):
"""Response when getting onboarding"""

onboarding_a: bool
onboarding_b1: bool
onboarding_b2: bool
onboarding_b3: bool
Expand All @@ -29,13 +34,18 @@ class Onboarding(Repository):
def __init__(self, supabase_client):
self.db = supabase_client

def get_user_onboarding(self, user_id: UUID) -> GetOnboardingResponse | None:
def get_user_onboarding(self, user_id: UUID) -> OnboardingStates | None:
"""
Get user onboarding information by user_id
"""
onboarding_data = (
self.db.from_("onboarding")
.select("user_id", "onboarding_b1", "onboarding_b2", "onboarding_b3")
self.db.from_("onboardings")
.select(
"onboarding_a",
"onboarding_b1",
"onboarding_b2",
"onboarding_b3",
)
.filter("user_id", "eq", user_id)
.limit(1)
.execute()
Expand All @@ -44,15 +54,19 @@ def get_user_onboarding(self, user_id: UUID) -> GetOnboardingResponse | None:
if onboarding_data == []:
return None

return GetOnboardingResponse(**onboarding_data[0])
return OnboardingStates(**onboarding_data[0])

def update_user_onboarding(
self, user_id: UUID, onboarding: OnboardingUpdatableProperties
) -> GetOnboardingResponse:
) -> OnboardingStates:
"""Update user onboarding information by user_id"""
update_data = {
key: value for key, value in onboarding.dict().items() if value is not None
}

response = (
self.db.from_("onboarding")
.update(onboarding.dict())
self.db.from_("onboardings")
.update(update_data)
.match({"user_id": user_id})
.execute()
.data
Expand All @@ -61,4 +75,4 @@ def update_user_onboarding(
if not response:
raise HTTPException(404, "User onboarding not updated")

return GetOnboardingResponse(**response[0])
return OnboardingStates(**response[0])
10 changes: 0 additions & 10 deletions backend/models/onboarding.py

This file was deleted.

4 changes: 2 additions & 2 deletions backend/repository/onboarding/get_user_onboarding.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from uuid import UUID

from models.databases.supabase.onboarding import GetOnboardingResponse
from models.databases.supabase.onboarding import OnboardingStates
from models.settings import get_supabase_db


def get_user_onboarding(user_id: UUID) -> GetOnboardingResponse | None:
def get_user_onboarding(user_id: UUID) -> OnboardingStates | None:
"""
Get a user's onboarding status
Expand Down
4 changes: 2 additions & 2 deletions backend/repository/onboarding/udpate_user_onboarding.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from uuid import UUID

from models.databases.supabase.onboarding import (
GetOnboardingResponse,
OnboardingStates,
OnboardingUpdatableProperties,
)
from models.settings import get_supabase_db


def update_user_onboarding(
user_id: UUID, onboarding: OnboardingUpdatableProperties
) -> GetOnboardingResponse:
) -> OnboardingStates:
"""Update user onboarding information by user_id"""

supabase_db = get_supabase_db()
Expand Down
6 changes: 3 additions & 3 deletions backend/routes/onboarding_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
)
from fastapi import APIRouter, Depends
from models.databases.supabase.onboarding import (
GetOnboardingResponse,
OnboardingStates,
OnboardingUpdatableProperties,
)
from models.user_identity import UserIdentity
Expand All @@ -21,7 +21,7 @@
)
async def get_user_onboarding_handler(
current_user: UserIdentity = Depends(get_current_user),
) -> GetOnboardingResponse | None:
) -> OnboardingStates | None:
"""
Get user onboarding information for the current user
"""
Expand All @@ -37,7 +37,7 @@ async def get_user_onboarding_handler(
async def update_user_onboarding_handler(
onboarding: OnboardingUpdatableProperties,
current_user: UserIdentity = Depends(get_current_user),
) -> GetOnboardingResponse:
) -> OnboardingStates:
"""
Update user onboarding information for the current user
"""
Expand Down
6 changes: 3 additions & 3 deletions scripts/20231004150000_add_onboarding_table.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-- Create the onboarding table
CREATE TABLE IF NOT EXISTS onboardings (
user_id UUID NOT NULL REFERENCES auth.users (id),
onboarding_b1 BOOLEAN NOT NULL DEFAULT false,
onboarding_b2 BOOLEAN NOT NULL DEFAULT false,
onboarding_b3 BOOLEAN NOT NULL DEFAULT false,
onboarding_b1 BOOLEAN NOT NULL DEFAULT true,
onboarding_b2 BOOLEAN NOT NULL DEFAULT true,
onboarding_b3 BOOLEAN NOT NULL DEFAULT true,
PRIMARY KEY (user_id)
)
17 changes: 17 additions & 0 deletions scripts/20231005170000_add_onboarding_a_to_onboarding_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- Check if onboarding_a column exists
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'onboardings' AND column_name = 'onboarding_a') THEN
ALTER TABLE onboardings ADD COLUMN onboarding_a BOOLEAN NOT NULL DEFAULT true;
END IF;
END $$;

COMMIT;


-- Update migrations table
INSERT INTO migrations (name)
SELECT '20231005170000_add_onboarding_a_to_onboarding_table'
WHERE NOT EXISTS (
SELECT 1 FROM migrations WHERE name = '20231005170000_add_onboarding_a_to_onboarding_table'
);
11 changes: 6 additions & 5 deletions scripts/tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,10 @@ CREATE TABLE IF NOT EXISTS knowledge_vectors (
-- Create the onboarding table
CREATE TABLE IF NOT EXISTS onboardings (
user_id UUID NOT NULL REFERENCES auth.users (id),
onboarding_b1 BOOLEAN NOT NULL DEFAULT false,
onboarding_b2 BOOLEAN NOT NULL DEFAULT false,
onboarding_b3 BOOLEAN NOT NULL DEFAULT false,
onboarding_a BOOLEAN NOT NULL DEFAULT true,
onboarding_b1 BOOLEAN NOT NULL DEFAULT true,
onboarding_b2 BOOLEAN NOT NULL DEFAULT true,
onboarding_b3 BOOLEAN NOT NULL DEFAULT true,
PRIMARY KEY (user_id)
)

Expand All @@ -279,9 +280,9 @@ CREATE POLICY "Access Quivr Storage 1jccrwz_2" ON storage.objects FOR UPDATE TO
CREATE POLICY "Access Quivr Storage 1jccrwz_3" ON storage.objects FOR DELETE TO anon USING (bucket_id = 'quivr');

INSERT INTO migrations (name)
SELECT '20231004150000_add_onboarding_table'
SELECT '20231005170000_add_onboarding_a_to_onboarding_table'
WHERE NOT EXISTS (
SELECT 1 FROM migrations WHERE name = '20231004150000_add_onboarding_table'
SELECT 1 FROM migrations WHERE name = '20231005170000_add_onboarding_a_to_onboarding_table'
);


0 comments on commit 16cea2c

Please sign in to comment.