You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
947 B
Python
30 lines
947 B
Python
from sqlalchemy.orm import Session
|
|
|
|
from app.repositories.user_repository import UserRepository
|
|
|
|
|
|
# Servico simples de identidade por canal externo.
|
|
class UserService:
|
|
def __init__(self, db: Session):
|
|
"""Inicializa o servico de usuarios com repositorio persistente."""
|
|
self.repo = UserRepository(db)
|
|
|
|
def get_or_create(
|
|
self,
|
|
channel: str,
|
|
external_id: str,
|
|
name: str | None = None,
|
|
username: str | None = None,
|
|
):
|
|
"""Retorna usuario existente por canal/external_id ou cria um novo."""
|
|
user = self.repo.get_by_channel_external_id(channel=channel, external_id=external_id)
|
|
if not user:
|
|
return self.repo.create(
|
|
channel=channel,
|
|
external_id=external_id,
|
|
name=name,
|
|
username=username,
|
|
)
|
|
|
|
return self.repo.update_identity(user=user, name=name, username=username)
|