refactor: add modules folder (#1633)

# Description

New Modules folder with "user" module:
- controller: contains the current route
- entity: contains the current Models (TO be renamed DTO)
- repository: contains the current repo
- service: methods used by other modules 

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):
This commit is contained in:
Zineb El Bachiri 2023-11-15 13:17:51 +01:00 committed by GitHub
parent 223af1d3f6
commit 375f50356c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
38 changed files with 75 additions and 78 deletions

View File

@ -14,6 +14,7 @@ from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from logger import get_logger
from middlewares.cors import add_cors_middleware
from modules.user.controller.user_controller import user_router
from routes.api_key_routes import api_key_router
from routes.brain_routes import brain_router
from routes.chat_routes import chat_router
@ -27,7 +28,6 @@ from routes.onboarding_routes import onboarding_router
from routes.prompt_routes import prompt_router
from routes.subscription_routes import subscription_router
from routes.upload_routes import upload_router
from routes.user_routes import user_router
logger = get_logger(__name__)

View File

@ -2,7 +2,7 @@ from datetime import datetime
from fastapi import HTTPException
from models.settings import get_supabase_db
from models.user_identity import UserIdentity
from modules.user.entity.user_identity import UserIdentity
from pydantic import DateError

View File

@ -5,7 +5,7 @@ from fastapi import Depends, HTTPException, Request
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from middlewares.auth.api_key_handler import get_user_from_api_key, verify_api_key
from middlewares.auth.jwt_token_handler import decode_access_token, verify_token
from models import UserIdentity
from modules.user.entity.user_identity import UserIdentity
class AuthBearer(HTTPBearer):

View File

@ -4,7 +4,7 @@ from typing import Optional
from jose import jwt
from jose.exceptions import JWTError
from models import UserIdentity
from modules.user.entity.user_identity import UserIdentity
SECRET_KEY = os.environ.get("JWT_SECRET_KEY")
ALGORITHM = "HS256"

View File

@ -9,7 +9,6 @@ from .settings import (BrainRateLimiting, BrainSettings, ContactsSettings,
LLMSettings, ResendSettings, get_embeddings,
get_documents_vector_store, get_embeddings,
get_supabase_client, get_supabase_db)
from .user_identity import UserIdentity
from .user_usage import UserUsage
# TODO uncomment the below import when start using SQLalchemy

View File

@ -1,7 +1,7 @@
from logger import get_logger
from models.databases.supabase.supabase import SupabaseDB
from models.settings import get_supabase_db
from models.user_identity import UserIdentity
from modules.user.entity.user_identity import UserIdentity
logger = get_logger(__name__)

View File

View File

@ -2,13 +2,14 @@ import time
from fastapi import APIRouter, Depends, Request
from middlewares.auth import AuthBearer, get_current_user
from models import Brain, UserIdentity, UserUsage
from repository.brain import get_user_default_brain
from repository.user_identity.get_user_identity import get_user_identity
from repository.user_identity.update_user_properties import (
from models import Brain, UserUsage
from modules.user.entity.user_identity import UserIdentity
from modules.user.repository import (
UserUpdatableProperties,
get_user_identity,
update_user_properties,
)
from repository.brain import get_user_default_brain
user_router = APIRouter()

View File

@ -1,3 +1,4 @@
from .get_user_identity import get_user_identity
from .create_user_identity import create_user_identity
from .update_user_properties import update_user_properties, UserUpdatableProperties
from .get_user_id_by_user_email import get_user_id_by_user_email

View File

@ -1,7 +1,8 @@
from typing import Optional
from uuid import UUID
from models import UserIdentity, get_supabase_client
from models import get_supabase_client
from modules.user.entity.user_identity import UserIdentity
def create_user_identity(id: UUID, openai_api_key: Optional[str]) -> UserIdentity:
@ -19,5 +20,5 @@ def create_user_identity(id: UUID, openai_api_key: Optional[str]) -> UserIdentit
)
user_identity = response.data[0]
return UserIdentity(
id=user_identity.get('user_id'), openai_api_key=user_identity.get('openai_api_key') # type: ignore
id=user_identity.get("user_id"), openai_api_key=user_identity.get("openai_api_key") # type: ignore
)

View File

@ -1,8 +1,9 @@
from multiprocessing import get_logger
from uuid import UUID
from models import get_supabase_client, UserIdentity
from repository.user_identity.create_user_identity import create_user_identity
from models import get_supabase_client
from modules.user.entity.user_identity import UserIdentity
from modules.user.repository import create_user_identity
logger = get_logger()

View File

@ -2,11 +2,10 @@ from typing import Optional
from uuid import UUID
from models.settings import get_supabase_client
from models.user_identity import UserIdentity
from modules.user.entity.user_identity import UserIdentity
from modules.user.repository import create_user_identity
from pydantic import BaseModel
from repository.user_identity.create_user_identity import create_user_identity
class UserUpdatableProperties(BaseModel):
openai_api_key: Optional[str]

View File

@ -0,0 +1 @@
from .get_user_id_by_email import get_user_id_by_email

View File

@ -0,0 +1,7 @@
from uuid import UUID
from modules.user.repository import get_user_id_by_user_email
def get_user_id_by_email(email: str) -> UUID | None:
return get_user_id_by_user_email(email)

View File

@ -1 +1,2 @@
from .handle_request_validation_error import handle_request_validation_error
from .parse_message_time import parse_message_time

View File

@ -1,11 +1,8 @@
from models import BrainEntity, UserIdentity
from models import BrainEntity
from models.databases.supabase.brains import CreateBrainProperties
from routes.authorizations.types import RoleEnum
from modules.user.entity.user_identity import UserIdentity
from repository.brain import create_brain, create_brain_user, get_user_default_brain
from repository.brain.create_brain import create_brain
from repository.brain.create_brain_user import create_brain_user
from repository.brain.get_default_user_brain import get_user_default_brain
from routes.authorizations.types import RoleEnum
def get_default_user_brain_or_create_new(user: UserIdentity) -> BrainEntity:

View File

@ -1,8 +1,8 @@
from logger import get_logger
from models import BrainSubscription, get_supabase_client
from repository.brain import get_brain_for_user
from repository.user.get_user_id_by_user_email import get_user_id_by_user_email
from modules.user.service import get_user_id_by_email
logger = get_logger(__name__)
@ -57,7 +57,7 @@ class SubscriptionInvitationService:
self.update_subscription_invitation(brain_subscription)
return True
else:
user_id = get_user_id_by_user_email(brain_subscription.email)
user_id = get_user_id_by_email(brain_subscription.email)
brain_user = None
if user_id is not None:

View File

@ -1,2 +0,0 @@
from .get_user_email_by_user_id import get_user_email_by_user_id
from .get_user_id_by_user_email import get_user_id_by_user_email

View File

@ -1,11 +0,0 @@
from uuid import UUID
from models import get_supabase_client
def get_user_email_by_user_id(user_id: UUID) -> str:
supabase_client = get_supabase_client()
response = supabase_client.rpc(
"get_user_email_by_user_id", {"user_id": user_id}
).execute()
return response.data[0]["email"]

View File

@ -6,7 +6,8 @@ from asyncpg.exceptions import UniqueViolationError
from fastapi import APIRouter, Depends
from logger import get_logger
from middlewares.auth import AuthBearer, get_current_user
from models import UserIdentity, get_supabase_db
from models import get_supabase_db
from modules.user.entity.user_identity import UserIdentity
from pydantic import BaseModel
logger = get_logger(__name__)

View File

@ -3,7 +3,7 @@ from uuid import UUID
from fastapi import Depends, HTTPException, status
from middlewares.auth.auth_bearer import get_current_user
from models import UserIdentity
from modules.user.entity.user_identity import UserIdentity
from repository.brain import get_brain_for_user
from repository.brain.get_brain_details import get_brain_details
from routes.authorizations.types import RoleEnum

View File

@ -3,13 +3,14 @@ from uuid import UUID
from fastapi import APIRouter, Depends, HTTPException
from logger import get_logger
from middlewares.auth.auth_bearer import AuthBearer, get_current_user
from models import UserIdentity, UserUsage
from models import UserUsage
from models.brain_entity import PublicBrain
from models.databases.supabase.brains import (
BrainQuestionRequest,
BrainUpdatableProperties,
CreateBrainProperties,
)
from modules.user.entity.user_identity import UserIdentity
from repository.brain import (
create_brain,
create_brain_user,

View File

@ -1,5 +1,5 @@
from llm.qa_headless import HeadlessQA
from repository.user_identity import get_user_identity
from modules.user.repository import get_user_identity
from routes.chat.interface import ChatInterface

View File

@ -2,8 +2,9 @@ import time
from uuid import UUID
from fastapi import HTTPException
from models import UserIdentity, UserUsage
from models import UserUsage
from models.databases.supabase.supabase import SupabaseDB
from modules.user.entity.user_identity import UserIdentity
class NullableUUID(UUID):

View File

@ -7,16 +7,9 @@ from fastapi.responses import StreamingResponse
from llm.qa_base import QABaseBrainPicking
from llm.qa_headless import HeadlessQA
from middlewares.auth import AuthBearer, get_current_user
from models import (
Brain,
BrainEntity,
Chat,
ChatQuestion,
UserIdentity,
UserUsage,
get_supabase_db,
)
from models import Brain, BrainEntity, Chat, ChatQuestion, UserUsage, get_supabase_db
from models.databases.supabase.chats import QuestionAndAnswer
from modules.user.entity.user_identity import UserIdentity
from repository.chat import (
ChatUpdatableProperties,
CreateChatProperties,

View File

@ -5,10 +5,11 @@ from celery_worker import process_crawl_and_notify
from fastapi import APIRouter, Depends, Query, Request
from logger import get_logger
from middlewares.auth import AuthBearer, get_current_user
from models import Brain, UserIdentity, UserUsage
from models import Brain, UserUsage
from models.databases.supabase.knowledge import CreateKnowledgeProperties
from models.databases.supabase.notifications import CreateNotificationProperties
from models.notifications import NotificationsStatusEnum
from modules.user.entity.user_identity import UserIdentity
from packages.files.crawl.crawler import CrawlWebsite
from packages.files.file import convert_bytes
from repository.knowledge.add_knowledge import add_knowledge

View File

@ -2,7 +2,8 @@ from uuid import UUID
from fastapi import APIRouter, Depends, Query
from middlewares.auth import AuthBearer, get_current_user
from models import Brain, UserIdentity, get_supabase_db
from models import Brain, get_supabase_db
from modules.user.entity.user_identity import UserIdentity
from routes.authorizations.brain_authorization import (
RoleEnum,
has_brain_authorization,

View File

@ -3,7 +3,8 @@ from uuid import UUID
from fastapi import APIRouter, Depends, Query
from logger import get_logger
from middlewares.auth import AuthBearer, get_current_user
from models import Brain, UserIdentity
from models import Brain
from modules.user.entity.user_identity import UserIdentity
from repository.files.delete_file import delete_file_from_storage
from repository.files.generate_file_signed_url import generate_file_signed_url
from repository.knowledge.get_all_knowledge import get_all_knowledge

View File

@ -1,13 +1,13 @@
from fastapi import APIRouter, Depends
from middlewares.auth import (
from middlewares.auth import ( # Assuming you have a get_current_user function
AuthBearer,
get_current_user,
) # Assuming you have a get_current_user function
from middlewares.auth import AuthBearer
)
from models.databases.supabase.onboarding import (
OnboardingStates,
OnboardingUpdatableProperties,
)
from models.user_identity import UserIdentity
from modules.user.entity.user_identity import UserIdentity
from repository.onboarding.get_user_onboarding import get_user_onboarding
from repository.onboarding.update_user_onboarding import update_user_onboarding

View File

@ -3,7 +3,9 @@ from uuid import UUID
from fastapi import APIRouter, Depends, HTTPException
from middlewares.auth.auth_bearer import AuthBearer, get_current_user
from models import BrainSubscription, PromptStatusEnum, UserIdentity
from models import BrainSubscription, PromptStatusEnum
from modules.user.entity.user_identity import UserIdentity
from modules.user.service.get_user_id_by_email import get_user_id_by_email
from pydantic import BaseModel
from repository.brain import (
create_brain_user,
@ -20,7 +22,6 @@ from repository.brain_subscription import (
resend_invitation_email,
)
from repository.prompt import delete_prompt_by_id, get_prompt_by_id
from repository.user import get_user_id_by_user_email
from routes.authorizations.brain_authorization import (
RoleEnum,
has_brain_authorization,
@ -288,7 +289,7 @@ def update_brain_subscription(
detail="You can't change your own permissions",
)
user_id = get_user_id_by_user_email(user_email)
user_id = get_user_id_by_email(user_email)
if user_id is None:
raise HTTPException(

View File

@ -6,16 +6,17 @@ from celery_worker import process_file_and_notify
from fastapi import APIRouter, Depends, HTTPException, Query, Request, UploadFile
from logger import get_logger
from middlewares.auth import AuthBearer, get_current_user
from models import Brain, UserIdentity, UserUsage
from models import Brain, UserUsage
from models.databases.supabase.knowledge import CreateKnowledgeProperties
from models.databases.supabase.notifications import CreateNotificationProperties
from models.notifications import NotificationsStatusEnum
from modules.user.entity.user_identity import UserIdentity
from modules.user.repository import get_user_identity
from packages.files.file import convert_bytes, get_file_size
from repository.brain import get_brain_details
from repository.files.upload_file import upload_file_storage
from repository.knowledge.add_knowledge import add_knowledge
from repository.notification.add_notification import add_notification
from repository.user_identity import get_user_identity
from routes.authorizations.brain_authorization import (
RoleEnum,
validate_brain_authorization,

View File

@ -10,9 +10,9 @@ CREATE TABLE IF NOT EXISTS api_brain_definition (
-- Insert migration record if it doesn't exist
INSERT INTO migrations (name)
SELECT '2023110607100000_add_api_brain_definition_table'
SELECT '20231106071000_add_api_brain_definition_table'
WHERE NOT EXISTS (
SELECT 1 FROM migrations WHERE name = '2023110607100000_add_api_brain_definition_table'
SELECT 1 FROM migrations WHERE name = '20231106071000_add_api_brain_definition_table'
);
COMMIT;

View File

@ -0,0 +1,10 @@
DROP FUNCTION IF EXISTS public.get_user_id_by_user_email(text);
-- Insert migration record if it doesn't exist
INSERT INTO migrations (name)
SELECT '20231114162700_drop_get_user_email_by_user_id'
WHERE NOT EXISTS (
SELECT 1 FROM migrations WHERE name = '20231114162700_drop_get_user_email_by_user_id'
);
COMMIT;

View File

@ -220,15 +220,6 @@ CREATE TABLE IF NOT EXISTS api_brain_definition (
secrets JSON
);
CREATE OR REPLACE FUNCTION public.get_user_email_by_user_id(user_id uuid)
RETURNS TABLE (email text)
SECURITY definer
AS $$
BEGIN
RETURN QUERY SELECT au.email::text FROM auth.users au WHERE au.id = user_id;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION public.get_user_id_by_user_email(user_email text)
RETURNS TABLE (user_id uuid)
@ -445,7 +436,7 @@ $$;
INSERT INTO migrations (name)
SELECT '20231107104700_setup_vault'
SELECT '20231114162700_drop_get_user_email_by_user_id'
WHERE NOT EXISTS (
SELECT 1 FROM migrations WHERE name = '20231107104700_setup_vault'
SELECT 1 FROM migrations WHERE name = '20231114162700_drop_get_user_email_by_user_id'
);