diff --git a/locker_server/api/v1_0/payments/serializers.py b/locker_server/api/v1_0/payments/serializers.py index a27bf86..0806106 100644 --- a/locker_server/api/v1_0/payments/serializers.py +++ b/locker_server/api/v1_0/payments/serializers.py @@ -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) diff --git a/locker_server/api/v1_0/users/views.py b/locker_server/api/v1_0/users/views.py index 5ec161e..1bdd7b7 100644 --- a/locker_server/api/v1_0/users/views.py +++ b/locker_server/api/v1_0/users/views.py @@ -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 diff --git a/locker_server/api_orm/repositories/device_repository.py b/locker_server/api_orm/repositories/device_repository.py index c4dc6b9..64157a4 100644 --- a/locker_server/api_orm/repositories/device_repository.py +++ b/locker_server/api_orm/repositories/device_repository.py @@ -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 @@ -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) diff --git a/locker_server/core/repositories/device_repository.py b/locker_server/core/repositories/device_repository.py index caf1e02..4073126 100644 --- a/locker_server/core/repositories/device_repository.py +++ b/locker_server/core/repositories/device_repository.py @@ -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: diff --git a/locker_server/core/services/device_service.py b/locker_server/core/services/device_service.py index ce0b97d..1f745fd 100644 --- a/locker_server/core/services/device_service.py +++ b/locker_server/core/services/device_service.py @@ -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 @@ -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 + ) diff --git a/locker_server/core/services/payment_service.py b/locker_server/core/services/payment_service.py index 4bb7789..8162d94 100644 --- a/locker_server/core/services/payment_service.py +++ b/locker_server/core/services/payment_service.py @@ -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 diff --git a/locker_server/shared/constants/transactions.py b/locker_server/shared/constants/transactions.py index d16f80a..fc47254 100644 --- a/locker_server/shared/constants/transactions.py +++ b/locker_server/shared/constants/transactions.py @@ -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]