feat(projects): add project-specific member rates
This commit is contained in:
63
apps/projects/services/rates.py
Normal file
63
apps/projects/services/rates.py
Normal file
@@ -0,0 +1,63 @@
|
||||
from django.utils import timezone
|
||||
|
||||
from apps.projects.models import ProjectUserRate
|
||||
|
||||
|
||||
def get_current_project_user_rate(*, project, user):
|
||||
return (
|
||||
ProjectUserRate.objects.filter(
|
||||
project=project,
|
||||
user=user,
|
||||
is_active=True,
|
||||
is_deleted=False,
|
||||
)
|
||||
.order_by("-effective_from", "-updated_at")
|
||||
.first()
|
||||
)
|
||||
|
||||
|
||||
def upsert_project_user_rate(*, project, user, hourly_rate, currency="USD"):
|
||||
currency = currency.upper()
|
||||
rate = (
|
||||
ProjectUserRate.all_objects.filter(
|
||||
project=project,
|
||||
user=user,
|
||||
)
|
||||
.order_by("-updated_at", "-created_at")
|
||||
.first()
|
||||
)
|
||||
|
||||
if rate:
|
||||
update_fields = []
|
||||
if rate.is_deleted:
|
||||
rate.restore()
|
||||
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 ProjectUserRate.objects.create(
|
||||
project=project,
|
||||
user=user,
|
||||
hourly_rate=hourly_rate,
|
||||
currency=currency,
|
||||
effective_from=timezone.now(),
|
||||
is_active=True,
|
||||
)
|
||||
|
||||
|
||||
def remove_project_user_rate(*, project, user):
|
||||
rate = get_current_project_user_rate(project=project, user=user)
|
||||
if not rate:
|
||||
return False
|
||||
rate.delete()
|
||||
return True
|
||||
Reference in New Issue
Block a user