2024-10-16 16:57:59 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
2024-10-23 11:18:03 +02:00
|
|
|
import arrow
|
2024-10-16 16:57:59 +02:00
|
|
|
from arrow import Arrow
|
|
|
|
|
2024-10-23 11:18:03 +02:00
|
|
|
from app import config
|
|
|
|
from app.models import PartnerUser, PartnerSubscription, User, Job
|
2024-10-16 16:57:59 +02:00
|
|
|
from app.user_audit_log_utils import emit_user_audit_log, UserAuditLogAction
|
|
|
|
|
|
|
|
|
|
|
|
def create_partner_user(
|
|
|
|
user: User, partner_id: int, partner_email: str, external_user_id: str
|
|
|
|
) -> PartnerUser:
|
|
|
|
instance = PartnerUser.create(
|
|
|
|
user_id=user.id,
|
|
|
|
partner_id=partner_id,
|
|
|
|
partner_email=partner_email,
|
|
|
|
external_user_id=external_user_id,
|
|
|
|
)
|
2024-10-23 11:18:03 +02:00
|
|
|
Job.create(
|
|
|
|
name=config.JOB_SEND_ALIAS_CREATION_EVENTS,
|
|
|
|
payload={"user_id": user.id},
|
|
|
|
run_at=arrow.now(),
|
|
|
|
)
|
2024-10-16 16:57:59 +02:00
|
|
|
emit_user_audit_log(
|
|
|
|
user=user,
|
|
|
|
action=UserAuditLogAction.LinkAccount,
|
|
|
|
message=f"Linked account to partner_id={partner_id} | partner_email={partner_email} | external_user_id={external_user_id}",
|
|
|
|
)
|
|
|
|
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
|
|
|
def create_partner_subscription(
|
|
|
|
partner_user: PartnerUser,
|
|
|
|
expiration: Optional[Arrow],
|
|
|
|
msg: Optional[str] = None,
|
|
|
|
) -> PartnerSubscription:
|
|
|
|
instance = PartnerSubscription.create(
|
|
|
|
partner_user_id=partner_user.id,
|
|
|
|
end_at=expiration,
|
|
|
|
)
|
|
|
|
|
|
|
|
message = "User upgraded through partner subscription"
|
|
|
|
if msg:
|
|
|
|
message += f" | {msg}"
|
|
|
|
emit_user_audit_log(
|
|
|
|
user=partner_user.user,
|
|
|
|
action=UserAuditLogAction.Upgrade,
|
|
|
|
message=message,
|
|
|
|
)
|
|
|
|
|
|
|
|
return instance
|