Skip to content

Commit

Permalink
Merge branch 'develop' into 'main'
Browse files Browse the repository at this point in the history
Develop

See merge request locker/api-core!413
  • Loading branch information
phuongntt-cystack committed Mar 27, 2024
2 parents edb3ef9 + 6b68043 commit 678af6c
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 25 deletions.
10 changes: 10 additions & 0 deletions locker_server/api/sub/resources/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ def to_representation(self, instance):
"country_phone_code": instance.country_phone_code,
}
return data


class IndividualPlanSerializer(serializers.Serializer):
def to_representation(self, instance):
data = {
"id": instance.plan_id,
"name": instance.name,
"alias": instance.alias,
}
return data
25 changes: 18 additions & 7 deletions locker_server/api/sub/resources/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from locker_server.api.v1_0.resources.views import ResourcePwdViewSet as ResourceV1PwdViewSet
from locker_server.settings import locker_server_settings
from locker_server.shared.constants.transactions import PLAN_TYPE_PM_ENTERPRISE
from .serializers import CountrySerializer
from .serializers import CountrySerializer, IndividualPlanSerializer


class ResourcePwdViewSet(ResourceV1PwdViewSet):
Expand All @@ -16,6 +16,8 @@ class ResourcePwdViewSet(ResourceV1PwdViewSet):
def get_serializer_class(self):
if self.action == "countries":
self.serializer_class = CountrySerializer
elif self.action == "list_individual_plans":
self.serializer_class = IndividualPlanSerializer
return super(ResourcePwdViewSet, self).get_serializer_class()

@action(methods=["get"], detail=False)
Expand Down Expand Up @@ -58,18 +60,27 @@ def list_enterprise_id(self, request, *args, **kwargs):
def list_channel(self, request, *args, **kwargs):
user_channels = [
{
"id": "organic"
"id": "organic",
"name": "Organic"
},
{
"id": "ads"
"id": "ads",
"name": "Ads"
},
{
"id": "affiliate"
"id": "affiliate",
"name": "Affiliate"
}
]
return Response(status=status.HTTP_200_OK, data=user_channels)

@action(methods=["get"], detail=False)
def list_saas_market(self, request, *args, **kwargs):
saas_markets = self.payment_service.list_saas_market()
return Response(status=status.HTTP_200_OK, data=saas_markets)
def list_payment_sources(self, request, *args, **kwargs):
payment_sources = self.resource_service.list_payment_sources()
return Response(status=status.HTTP_200_OK, data=payment_sources)

@action(methods=["get"], detail=False)
def list_individual_plans(self, request, *args, **kwargs):
individual_plans = self.resource_service.list_individual_plans()
serializer = self.get_serializer(individual_plans, many=True)
return Response(status=status.HTTP_200_OK, data=serializer.data)
3 changes: 2 additions & 1 deletion locker_server/api/sub/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
url(r'^resources/server_type$', views.ResourcePwdViewSet.as_view({'get': 'server_type'})),
url(r'^resources/enterprise_ids$', views.ResourcePwdViewSet.as_view({'get': 'list_enterprise_id'})),
url(r'^resources/channels$', views.ResourcePwdViewSet.as_view({'get': 'list_channel'})),
url(r'^resources/saas_markets$', views.ResourcePwdViewSet.as_view({'get': 'list_saas_market'})),
url(r'^resources/payment_sources$', views.ResourcePwdViewSet.as_view({'get': 'list_payment_sources'})),
url(r'^resources/individual_plans$', views.ResourcePwdViewSet.as_view({'get': 'list_individual_plans'})),
url(r'^resources/cystack_platform/pm/plans$', views.ResourcePwdViewSet.as_view({'get': 'plans'})),
url(r'^resources/cystack_platform/pm/enterprise/plans$',
views.ResourcePwdViewSet.as_view({'get': 'enterprise_plans'})),
Expand Down
12 changes: 6 additions & 6 deletions locker_server/api/v1_0/payments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,7 @@ def plan_limit(self, request, *args, **kwargs):
def statistic_income(self, request, *args, **kwargs):
query_params = self.request.query_params
current_time = now()
from_param = self.check_int_param(
self.request.query_params.get("from")) or current_time - 1000 * 86400
from_param = self.check_int_param(self.request.query_params.get("from"))
to_param = self.check_int_param(self.request.query_params.get("to")) or current_time
duration_param = self.request.query_params.get("duration") or "monthly"
dashboard_result = self.payment_service.statistic_income(**{
Expand All @@ -572,10 +571,11 @@ def statistic_income(self, request, *args, **kwargs):
"duration": duration_param,
"status": query_params.get("status"),
"plan": query_params.get("plan"),
"utm_source": query_params.get("utm_source"),
"payment_source": query_params.get("payment_source"),
"payment_method": query_params.get("payment_method"),
"user_id": query_params.get("user_id"),
"enterprise_id": query_params.get("enterprise_id"),
"channel": query_params.get("channel")

})
return Response(status=status.HTTP_200_OK, data=dashboard_result)
Expand All @@ -584,8 +584,7 @@ def statistic_income(self, request, *args, **kwargs):
def statistic_amount(self, request, *args, **kwargs):
query_params = self.request.query_params
current_time = now()
from_param = self.check_int_param(
self.request.query_params.get("from")) or current_time - 1000 * 86400
from_param = self.check_int_param(self.request.query_params.get("from"))
to_param = self.check_int_param(self.request.query_params.get("to")) or current_time
duration_param = self.request.query_params.get("duration") or "monthly"
statistic_result = self.payment_service.statistic_amount(**{
Expand All @@ -594,10 +593,11 @@ def statistic_amount(self, request, *args, **kwargs):
"duration": duration_param,
"status": query_params.get("status"),
"plan": query_params.get("plan"),
"utm_source": query_params.get("utm_source"),
"payment_source": query_params.get("payment_source"),
"payment_method": query_params.get("payment_method"),
"user_id": query_params.get("user_id"),
"enterprise_id": query_params.get("enterprise_id"),
"channel": query_params.get("channel")

})
return Response(status=status.HTTP_200_OK, data=statistic_result)
Expand Down
17 changes: 9 additions & 8 deletions locker_server/api_orm/repositories/payment_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def list_payments_orm(cls, **filters) -> List[PaymentORM]:
status_param = filters.get("status") # success,failed,pending
plan_param = filters.get("plan") # premium, lifetime,family,enterprise
channel_param = filters.get("channel") # organic,ads, affiliate
source_param = filters.get("source") # stacksocial, dealmirror, saasmantra, stripe, ios, android
payment_source = filters.get("payment_source") # stacksocial, dealmirror, saasmantra, stripe, ios, android
payment_method_param = filters.get("payment_method")
user_id_param = filters.get("user_id")
enterprise_id_param = filters.get("enterprise_id")
Expand All @@ -115,7 +115,8 @@ def list_payments_orm(cls, **filters) -> List[PaymentORM]:
payments_orm = payments_orm.filter(created_time__lte=to_param)

if plan_param:
payments_orm = payments_orm.filter(plan=plan_param)
plans_param = plan_param.split(",")
payments_orm = payments_orm.filter(plan__in=plans_param)
if status_param:
payments_orm = payments_orm.filter(status=status_param)
if channel_param:
Expand All @@ -128,22 +129,22 @@ def list_payments_orm(cls, **filters) -> List[PaymentORM]:
payments_orm = payments_orm.filter(user_id=user_id_param)
if enterprise_id_param:
payments_orm = payments_orm.filter(enterprise_id=enterprise_id_param)
if source_param:
source_param = source_param.lower()
if source_param == "stripe":
if payment_source:
payment_source = payment_source.lower()
if payment_source == "stripe":
payments_orm = payments_orm.filter(stripe_invoice_id__isnull=False)
elif source_param == "ios":
elif payment_source == "ios":
payments_orm = payments_orm.filter(
Q(metadata__contains="ios") & Q(mobile_invoice_id__isnull=False)
)
elif source_param == "android":
elif payment_source == "android":
payments_orm = payments_orm.filter(
Q(metadata__contains="android") &
Q(mobile_invoice_id__isnull=False)

)
else:
payments_orm = payments_orm.filter(saas_market__icontains=source_param)
payments_orm = payments_orm.filter(saas_market__icontains=payment_source)
payments_orm = payments_orm.select_related('user')
payments_orm = payments_orm.order_by("-created_time")
return payments_orm
Expand Down
3 changes: 2 additions & 1 deletion locker_server/containers/defaults/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class ServiceFactory(containers.DeclarativeContainer):
ResourceService,
plan_repository=RepositoryFactory.plan_repository,
country_repository=RepositoryFactory.country_repository,
mail_provider_repository=RepositoryFactory.mail_provider_repository
mail_provider_repository=RepositoryFactory.mail_provider_repository,
payment_repository=RepositoryFactory.payment_repository,
)

user_service = providers.Factory(
Expand Down
41 changes: 39 additions & 2 deletions locker_server/core/services/resource_service.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from typing import List
from typing import List, Dict

from locker_server.core.entities.configuration.mail_provider import MailProvider
from locker_server.core.entities.payment.country import Country
from locker_server.core.entities.user_plan.pm_plan import PMPlan
from locker_server.core.repositories.country_repository import CountryRepository
from locker_server.core.repositories.mail_provider_repository import MailProviderRepository
from locker_server.core.repositories.payment_repository import PaymentRepository
from locker_server.core.repositories.plan_repository import PlanRepository
from locker_server.shared.constants.transactions import *

Expand All @@ -13,11 +14,15 @@ class ResourceService:
"""
This class represents Use Cases related Resource
"""

def __init__(self, plan_repository: PlanRepository, country_repository: CountryRepository,
mail_provider_repository: MailProviderRepository):
mail_provider_repository: MailProviderRepository,
payment_repository: PaymentRepository
):
self.plan_repository = plan_repository
self.country_repository = country_repository
self.mail_provider_repository = mail_provider_repository
self.payment_repository = payment_repository

def list_countries(self) -> List[Country]:
return self.country_repository.list_countries()
Expand All @@ -35,3 +40,35 @@ def list_enterprise_plans(self) -> List[PMPlan]:

def list_mail_providers(self) -> List[MailProvider]:
return self.mail_provider_repository.list_mail_providers()

def list_all_plans(self) -> List[PMPlan]:
return self.plan_repository.list_plans()

def list_individual_plans(self) -> List[PMPlan]:
return self.plan_repository.list_plans(**{
"is_team_plan": False
})

def list_payment_sources(self) -> List[Dict]:
saas_markets = self.payment_repository.list_saas_market()
for saas_market in saas_markets:
saas_market.update({
"id": saas_market.get("name")
})
payment_sources = [
{
"id": "stripe",
"name": "Stripe"
},
{
"id": "ios",
"name": "App Store"

},
{
"id": "android",
"name": "Google Play"

}
]
return payment_sources + saas_markets

0 comments on commit 678af6c

Please sign in to comment.