feat(pricing): add workspace user rates and price units
This commit is contained in:
54
apps/workspaces/services/rates.py
Normal file
54
apps/workspaces/services/rates.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from django.utils import timezone
|
||||
|
||||
from apps.workspaces.models import WorkspaceUserRate
|
||||
|
||||
|
||||
def upsert_workspace_user_rate(workspace, user_id, hourly_rate, currency="USD"):
|
||||
currency = currency.upper()
|
||||
rate = WorkspaceUserRate.objects.filter(
|
||||
workspace=workspace,
|
||||
user_id=user_id,
|
||||
is_deleted=False,
|
||||
).first()
|
||||
|
||||
if rate:
|
||||
update_fields = []
|
||||
if rate.hourly_rate != hourly_rate:
|
||||
rate.hourly_rate = hourly_rate
|
||||
update_fields.append("hourly_rate")
|
||||
if rate.currency != currency:
|
||||
rate.currency = currency
|
||||
update_fields.append("currency")
|
||||
if not rate.is_active:
|
||||
rate.is_active = True
|
||||
update_fields.append("is_active")
|
||||
if update_fields:
|
||||
update_fields.append("updated_at")
|
||||
rate.save(update_fields=update_fields)
|
||||
return rate
|
||||
|
||||
return WorkspaceUserRate.objects.create(
|
||||
workspace=workspace,
|
||||
user_id=user_id,
|
||||
hourly_rate=hourly_rate,
|
||||
currency=currency,
|
||||
effective_from=timezone.now(),
|
||||
is_active=True,
|
||||
)
|
||||
|
||||
|
||||
def update_workspace_user_rate(rate_instance, **kwargs):
|
||||
if "currency" in kwargs and kwargs["currency"]:
|
||||
kwargs["currency"] = kwargs["currency"].upper()
|
||||
|
||||
update_fields = []
|
||||
for field, value in kwargs.items():
|
||||
if hasattr(rate_instance, field) and getattr(rate_instance, field) != value:
|
||||
setattr(rate_instance, field, value)
|
||||
update_fields.append(field)
|
||||
|
||||
if update_fields:
|
||||
update_fields.append("updated_at")
|
||||
rate_instance.save(update_fields=update_fields)
|
||||
|
||||
return rate_instance
|
||||
Reference in New Issue
Block a user