Skip to content

Commit

Permalink
Merge branch 'develop' of https://git.cystack.org/locker/api-core int…
Browse files Browse the repository at this point in the history
…o develop
  • Loading branch information
khaitranquang committed Mar 28, 2024
2 parents 956316f + 5310006 commit 6426120
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 12 deletions.
12 changes: 5 additions & 7 deletions locker_server/api/v1_0/payments/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,12 @@ class CancelPlanSerializer(serializers.Serializer):

class AdminCreateRefundPaymentSerializer(serializers.Serializer):
user_id = serializers.IntegerField()
total_price = serializers.FloatField()
plan = serializers.ChoiceField(choices=LIST_PLAN_TYPE)
payment_method = serializers.ChoiceField(choices=LIST_PAYMENT_METHOD)
currency = serializers.ChoiceField(choices=LIST_CURRENCY, default=CURRENCY_USD)
status = serializers.ChoiceField(choices=LIST_PAYMENT_STATUS, default=PAYMENT_STATUS_PAID, required=False)
refund = serializers.FloatField()
source = serializers.CharField(max_length=128, allow_blank=False, allow_null=False)
reason = serializers.CharField(max_length=512, required=False, allow_null=True, allow_blank=True)

currency = serializers.ChoiceField(choices=LIST_CURRENCY, default=CURRENCY_USD)
enterprise_id = serializers.CharField(required=False, max_length=128, allow_blank=True, allow_null=True)
description = serializers.CharField(required=False, max_length=256, allow_null=True, allow_blank=True)
metadata = serializers.CharField(required=False, max_length=512, allow_null=True, allow_blank=True)
saas_market = serializers.ChoiceField(choices=LIST_SAAS_MARKET, required=False)
bank_id = serializers.IntegerField(required=False, default=0)
status = serializers.ChoiceField(choices=LIST_PAYMENT_STATUS, default=PAYMENT_STATUS_PAID, required=False)
5 changes: 5 additions & 0 deletions locker_server/api/v1_0/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,12 @@ def retrieve(self, request, *args, **kwargs):
ciphers_count = self.cipher_service.statistic_multiple_cipher_by_user_id(
user_id=user.user_id
)
devices_count = self.device_service.statistic_multiple_device_by_user_id(
user_id=user.user_id
)
data["items"] = ciphers_count
data["devices"] = devices_count

usable_plan_alias, db_plan_alias = self.get_usable_plan(user_id=user.user_id)
data["current_plan"] = db_plan_alias
data["usable_plan"] = usable_plan_alias
Expand Down
8 changes: 8 additions & 0 deletions locker_server/api_orm/repositories/device_repository.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Union, Dict, Optional, List
from abc import ABC, abstractmethod

from django.db.models import Count, Case, When

from locker_server.api_orm.model_parsers.wrapper import get_model_parser
from locker_server.api_orm.models.wrapper import get_device_model, get_device_access_token_model
from locker_server.core.entities.user.device import Device
Expand Down Expand Up @@ -50,6 +52,12 @@ def get_fcm_ids_by_user_ids(self, user_ids: List[int]) -> List[str]:
def is_active(self, device_id) -> bool:
return DeviceAccessTokenORM.objects.filter(device_id=device_id).exists()

def statistic_multiple_device_by_user_id(self, user_id) -> Dict:
devices_orm = DeviceORM.objects.filter(user_id=user_id)
devices_orm = devices_orm.values("client_id").annotate(count=Count('client_id')).order_by('-count')
devices_count = {item["client_id"]: item["count"] for item in list(devices_orm)}
return devices_count

# ------------------------ Create Device resource --------------------- #
def retrieve_or_create(self, user_id: int, **data) -> Device:
device_orm = DeviceORM.retrieve_or_create(user_id=user_id, **data)
Expand Down
4 changes: 4 additions & 0 deletions locker_server/core/repositories/device_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def get_fcm_ids_by_user_ids(self, user_ids: List[int]) -> List[str]:
def is_active(self, device_id) -> bool:
pass

@abstractmethod
def statistic_multiple_device_by_user_id(self, user_id) -> Dict:
pass

# ------------------------ Create Device resource --------------------- #
@abstractmethod
def retrieve_or_create(self, user_id: int, **data) -> Device:
Expand Down
7 changes: 6 additions & 1 deletion locker_server/core/services/device_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Optional
from typing import List, Optional, Dict

from locker_server.core.entities.factor2.device_factor2 import DeviceFactor2
from locker_server.core.entities.user.device import Device
Expand Down Expand Up @@ -53,3 +53,8 @@ def get_device_factor2_by_device_identifier(self, user_id: int, device_identifie
if not device_factor2:
raise DeviceFactor2DoesNotExistException
return device_factor2

def statistic_multiple_device_by_user_id(self, user_id: int) -> Dict:
return self.device_repository.statistic_multiple_device_by_user_id(
user_id=user_id
)
14 changes: 13 additions & 1 deletion locker_server/core/services/payment_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,9 +778,21 @@ def create_refund_payment(self, **refund_payment_data) -> Optional[Payment]:
user = self.user_repository.get_user_by_id(user_id=user_id)
if not user:
raise UserDoesNotExistException
user_plan = self.user_plan_repository.get_user_plan(user_id=user_id)
payment_source = refund_payment_data.get("source")
total_price = refund_payment_data.get("refund")
reason = refund_payment_data.get("reason")
metadata = {}
metadata.update({
"payment_source": payment_source,
"reason": reason
})
refund_payment_data.update({
"transaction_type": TRANSACTION_TYPE_REFUND,
"description": refund_payment_data.get("description", "Refund payment")
"description": refund_payment_data.get("description", "Refund payment"),
"plan": user_plan.pm_plan.alias if user_plan else None,
"total_price": total_price,
"metadata": metadata
})
refund_payment = self.payment_repository.create_payment(**refund_payment_data)
return refund_payment
7 changes: 4 additions & 3 deletions locker_server/shared/constants/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@
BANKING_ID_WEB_SECURITY = "CW"

# ------------ Saas market ------------------------------- #
SAAS_MARKET_STACK_COMMERCE = "StackCommerce"
SAAS_MARKET_PITCH_GROUND = "PitchGround"
SAAS_MARKET_STACK_SOCIAL = "StackSocial"
SAAS_MARKET_DEAL_FUEL = "DealFuel"
LIST_SAAS_MARKET = [SAAS_MARKET_STACK_COMMERCE, SAAS_MARKET_DEAL_FUEL, SAAS_MARKET_PITCH_GROUND]
SAAS_MARKET_DEAL_MIRROR = "DealMirror"
SAAS_MARKET_PITCH_GROUND = "PitchGround"
LIST_PAYMENT_SOURCE = [SAAS_MARKET_STACK_SOCIAL, SAAS_MARKET_DEAL_FUEL, SAAS_MARKET_PITCH_GROUND]

0 comments on commit 6426120

Please sign in to comment.