Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update endpoints to handle Enterprise plans #4783

Merged
merged 21 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
87299fd
check total org usage if user has enterprise plan
LMNTL Dec 5, 2023
fa455b9
generate custom portal config for enterprise plan
LMNTL Dec 20, 2023
83bcf50
only check for Enterprise users when Stripe is enabled
LMNTL Dec 22, 2023
d328818
remove stray extra import
LMNTL Dec 22, 2023
4a71e9c
refactor service usage tests utils into their own base class
LMNTL Dec 27, 2023
371b2b9
add owner to /api/v2/organizations/ response
LMNTL Jan 4, 2024
a809947
get username in serializer instead of ID
LMNTL Jan 4, 2024
b8e6a6b
refactor needs_custom_config
LMNTL Jan 10, 2024
88a6bab
test organization usage with enterprise plans
LMNTL Jan 11, 2024
b94dc4d
add transform_quantity to Price serializer and update docs
LMNTL Jan 12, 2024
6359228
fix failing service usage tests
LMNTL Jan 12, 2024
087f5f2
clear cache in tearDown method instead of deactivating cache during t…
LMNTL Jan 12, 2024
0547f82
limit service usage data to 400 organization members
LMNTL Jan 12, 2024
1ac1dd5
use subquery to get users if necessary, add additional tests
LMNTL Jan 12, 2024
b817a8b
add test for caching on org service usage
LMNTL Jan 17, 2024
9eb34c6
revert subquery, since the necessary data is on two different tables
LMNTL Jan 17, 2024
cfde876
add comment for ORGANIZATION_USER_LIMIT setting
LMNTL Jan 17, 2024
b32015e
clarify comment and simplify user filtering
LMNTL Jan 19, 2024
d64aeee
mark test_endpoint_speed as expected to fail
LMNTL Jan 19, 2024
abc57ed
use asset.owner_id instead of asset.owner.id
LMNTL Jan 19, 2024
7eea092
rename owner field to owner_username in org serializer
LMNTL Jan 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions kobo/apps/stripe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,14 @@ def generate_portal_link(user, organization_id, price):
if not len(all_configs):
return Response({'error': "Missing Stripe billing configuration."}, status=status.HTTP_502_BAD_GATEWAY)

is_price_for_addon = price.product.metadata.get('product_type', '') == 'addon'

if is_price_for_addon:
"""
Recurring add-ons aren't included in the default billing configuration.
This lets us hide them as an 'upgrade' option for paid plan users.
Here, we try getting the portal configuration that lets us switch to the provided price.
"""
"""
Recurring add-ons and the Enterprise plan aren't included in the default billing configuration.
This lets us hide them as an 'upgrade' option for paid plan users.
"""
needs_custom_config = price.product.metadata.get('product_type', '') == 'addon'
needs_custom_config = needs_custom_config or price.product.metadata.get('plan_type', '') == 'enterprise'
LMNTL marked this conversation as resolved.
Show resolved Hide resolved
if needs_custom_config:
# Try getting the portal configuration that lets us switch to the provided price
current_config = next(
(config for config in all_configs if (
config['active'] and
Expand All @@ -350,7 +350,7 @@ def generate_portal_link(user, organization_id, price):
)), None
)

if is_price_for_addon:
if needs_custom_config:
"""
we couldn't find a custom configuration, let's try making a new one
add the price we're switching into to the list of prices that allow subscription updates
Expand Down
21 changes: 12 additions & 9 deletions kpi/serializers/v2/service_usage.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from django.contrib.auth.models import User
from django.conf import settings
from django.db.models import Sum, Q
from django.db.models.functions import Coalesce
from django.utils import timezone
from rest_framework import serializers
from rest_framework.fields import empty

from kobo.apps.organizations.models import Organization
from kobo.apps.stripe.constants import ACTIVE_STRIPE_STATUSES
from kobo.apps.trackers.models import NLPUsageCounter
from kpi.deployment_backends.kc_access.shadow_models import (
KobocatXForm,
Expand Down Expand Up @@ -219,16 +222,16 @@ def _get_organization_details(self):
# Couldn't find organization, proceed as normal
return

"""
Commented out until the Enterprise plan is implemented

# If the user is in an organization, get all org users so we can query their total org usage
self._user_ids = list(
User.objects.values_list('pk', flat=True).filter(
organizations_organization__id=organization_id
if settings.STRIPE_ENABLED:
# if the user is in an organization and has an enterprise plan, get all org users and their total usage
enterprise_users = User.objects.values_list('pk', flat=True).filter(
organizations_organization__id=organization_id,
organizations_organization__djstripe_customers__subscriptions__status__in=ACTIVE_STRIPE_STATUSES,
organizations_organization__djstripe_customers__subscriptions__items__price__product__metadata__has_key='plan_type',
organizations_organization__djstripe_customers__subscriptions__items__price__product__metadata__plan_type='enterprise',
)
)
"""
if enterprise_users.exists():
bufke marked this conversation as resolved.
Show resolved Hide resolved
self._user_ids = list(enterprise_users)

# If they have a subscription, use its start date to calculate beginning of current month/year's usage
billing_details = organization.active_subscription_billing_details
Expand Down