diff --git a/backend/app/routers/auth.py b/backend/app/routers/auth.py index 9989812..777833e 100644 --- a/backend/app/routers/auth.py +++ b/backend/app/routers/auth.py @@ -25,6 +25,7 @@ from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select, func from app.database import get_db +from app.services.connection import sync_birthday_to_contacts from app.models.user import User from app.models.session import UserSession from app.models.settings import Settings @@ -686,6 +687,7 @@ async def update_profile( current_user.email = update_data["email"] if "date_of_birth" in update_data: current_user.date_of_birth = update_data["date_of_birth"] + await sync_birthday_to_contacts(db, current_user.id) if "umbral_name" in update_data: current_user.umbral_name = update_data["umbral_name"] diff --git a/backend/app/routers/settings.py b/backend/app/routers/settings.py index c99de7f..877a393 100644 --- a/backend/app/routers/settings.py +++ b/backend/app/routers/settings.py @@ -7,6 +7,7 @@ from app.models.settings import Settings from app.models.user import User from app.schemas.settings import SettingsUpdate, SettingsResponse from app.routers.auth import get_current_user, get_current_settings +from app.services.connection import sync_birthday_to_contacts router = APIRouter() @@ -94,6 +95,9 @@ async def update_settings( for key, value in update_data.items(): setattr(current_settings, key, value) + if "share_birthday" in update_data: + await sync_birthday_to_contacts(db, current_settings.user_id) + await db.commit() await db.refresh(current_settings) diff --git a/backend/app/services/connection.py b/backend/app/services/connection.py index 09cbbb9..270d487 100644 --- a/backend/app/services/connection.py +++ b/backend/app/services/connection.py @@ -9,6 +9,7 @@ from datetime import date as date_type from types import SimpleNamespace from typing import Optional +from sqlalchemy import select, update from sqlalchemy.ext.asyncio import AsyncSession from app.models.person import Person @@ -134,6 +135,25 @@ def create_person_from_connection( ) + +async def sync_birthday_to_contacts(db: AsyncSession, user_id: int) -> None: + """Sync user's DOB to all Person records where linked_user_id == user_id. + Respects share_birthday setting — if disabled, clears birthday on linked records.""" + user = await db.execute(select(User).where(User.id == user_id)) + user_obj = user.scalar_one() + settings_result = await db.execute(select(Settings).where(Settings.user_id == user_id)) + settings_obj = settings_result.scalar_one_or_none() + + share = settings_obj.share_birthday if settings_obj else False + new_birthday = user_obj.date_of_birth if share else None + + await db.execute( + update(Person) + .where(Person.linked_user_id == user_id) + .values(birthday=new_birthday) + ) + + async def detach_umbral_contact(person: Person) -> None: """Convert an umbral contact back to a standard contact. Does NOT commit.