forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreferrals.py
More file actions
36 lines (29 loc) · 1.15 KB
/
Copy pathreferrals.py
File metadata and controls
36 lines (29 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from __future__ import annotations
from typing import Any
from google.cloud import firestore
from database._client import get_customer_firestore_client, run_transactional
from utils.referrals import referral_claim_patch
def claim_referral_trial(
referred_uid: str,
referrer_uid: str,
*,
is_new_user: bool,
firestore_client: Any | None = None,
) -> tuple[bool, str]:
"""Atomically grant the referred account its one-time 30-day Operator entitlement."""
client = firestore_client or get_customer_firestore_client()
user_ref = client.collection('users').document(referred_uid)
@firestore.transactional
def apply(transaction: Any) -> tuple[bool, str]:
snapshot = user_ref.get(transaction=transaction)
patch, reason = referral_claim_patch(
referred_uid=referred_uid,
referrer_uid=referrer_uid,
is_new_user=is_new_user,
user_data=snapshot.to_dict() if snapshot.exists else None,
)
if patch is None:
return False, reason
transaction.set(user_ref, patch, merge=True)
return True, reason
return run_transactional(client, apply)