Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/apps/account_payment/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def mutate(cls, root, info, account_id, subscription_id):

subscription = Subscription.objects.get(id=subscription_id)
assert admin.id == subscription.admin.id
add_user(account.gcp_email or account.email)
add_user(account.gcp_email or account.email, account=account)
subscription.subscribers.add(account)
return cls(ok=True)
except Exception as e:
Expand Down Expand Up @@ -490,7 +490,7 @@ def mutate(cls, root, info, email):

if not is_email_in_group(email):
try:
add_user(email)
add_user(email, account=user)
except Exception:
pass

Expand Down
42 changes: 26 additions & 16 deletions backend/apps/account_payment/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@

from backend.apps.account.models import Account, Subscription
from backend.custom.client import send_discord_message as send
from backend.custom.environment import get_backend_url
from backend.custom.environment import get_backend_url, is_dev, is_stg

logger = logger.bind(module="payment")

def _normalize_plus(email: str) -> str:
"""Normaliza: trim, lower e remove +alias antes do @."""
email = email.strip().lower()
local, _, domain = email.partition("@")
if "+" in local:
local = local.split("+", 1)[0]
return f"{local}@{domain}"

def get_subscription(event: Event) -> Subscription:
"""Get internal subscription model, mirror of stripe"""
Expand Down Expand Up @@ -58,12 +65,23 @@ def get_service() -> Resource:
return build("admin", "directory_v1", credentials=credentials)


def add_user(email: str, group_key: str = None, role: str = "MEMBER"):
def add_user(email: str, account: Account = None, group_key: str = None, role: str = "MEMBER"):
"""Add user to google group"""
if is_dev() or is_stg():
if account is None:
try:
normalized_email = _normalize_plus(email)
account = Account.objects.get(Q(email__iexact=email) | Q(email__iexact=normalized_email))
except Account.DoesNotExist:
account = None

if not (account and account.is_admin):
logger.info(f"Ignorando adição do usuário '{email}' em ambiente de dev/staging pois não é admin.")
return

if not group_key:
group_key = settings.GOOGLE_DIRECTORY_GROUP_KEY
if "+" in email and email.index("+") < email.index("@"):
email = email.split("+")[0] + "@" + email.split("@")[1]
email = _normalize_plus(email)
try:
service = get_service()
service.members().insert(
Expand All @@ -78,14 +96,6 @@ def add_user(email: str, group_key: str = None, role: str = "MEMBER"):
logger.error(e)
raise e

def _normalize_plus(email: str) -> str:
"""Normaliza: trim, lower e remove +alias antes do @."""
email = email.strip().lower()
local, _, domain = email.partition("@")
if "+" in local:
local = local.split("+", 1)[0]
return f"{local}@{domain}"

def remove_user(email: str, group_key: str = None) -> None:
"""Remove user from Google Group"""
if not email or "@" not in email:
Expand Down Expand Up @@ -196,9 +206,9 @@ def handle_subscription(event: Event):

# Add user to google group if subscription exists or not
if account:
add_user(account.gcp_email or account.email)
add_user(account.gcp_email or account.email, account=account)
else:
add_user(event.customer.email)
add_user(event.customer.email, account=None)
else:
if subscription:
logger.info(f"Removendo a inscrição do cliente {event.customer.email}")
Expand Down Expand Up @@ -276,9 +286,9 @@ def resume_subscription(event: Event, **kwargs):

try:
if account:
add_user(account.gcp_email or account.email)
add_user(account.gcp_email or account.email, account=account)
else:
add_user(event.customer.email)
add_user(event.customer.email, account=None)
except Exception as e:
logger.error(e)

Expand Down
Loading