Initial commit
This commit is contained in:
commit
376a7a9fe5
71 changed files with 2326 additions and 0 deletions
35
src/user/service.py
Normal file
35
src/user/service.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
"""
|
||||
Module specific business logic for user module
|
||||
|
||||
Functions:
|
||||
- add_user_to_db
|
||||
|
||||
Exports:
|
||||
- add_user_to_db
|
||||
"""
|
||||
from authlib.jose import JWTClaims
|
||||
from fastapi import HTTPException
|
||||
|
||||
from src.user.schemas import OIDCUser
|
||||
from src.user.models import User
|
||||
from src.database import get_db
|
||||
|
||||
|
||||
async def add_user_to_db(user_claims: JWTClaims) -> int:
|
||||
try:
|
||||
valid_user = OIDCUser(first_name=user_claims["given_name"], last_name=user_claims["family_name"], email=user_claims["email"], oidc_id=user_claims["sub"])
|
||||
except Exception as e:
|
||||
print(e)
|
||||
raise HTTPException(status_code=422, detail="Invalid or missing OIDC data")
|
||||
|
||||
db = next(get_db())
|
||||
db_user = db.query(User).filter(User.oidc_id == valid_user.oidc_id).first()
|
||||
|
||||
if not db_user:
|
||||
user_model = User(**valid_user.model_dump())
|
||||
db.add(user_model)
|
||||
db.commit()
|
||||
return user_model.id
|
||||
else:
|
||||
# Verify details still match and update accordingly.
|
||||
return db_user.id
|
||||
Loading…
Add table
Add a link
Reference in a new issue