Skip to content

Commit

Permalink
Merge branch 'develop' into feature/grpc-whatsapp-integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
BarbosaJackson committed Dec 21, 2021
2 parents 4ef3248 + 77c7cea commit 1e98d97
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 109 deletions.
22 changes: 15 additions & 7 deletions connect/api/grpc/project/services.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import grpc
from django_grpc_framework import generics
from google.protobuf import empty_pb2

Expand Down Expand Up @@ -109,15 +110,22 @@ def CreateChannel(self, request, context):
project = Project.objects.get(uuid=project_uuid)

grpc_instance = utils.get_grpc_types().get("flow")
response = grpc_instance.create_channel(
user=serializer.validated_data.get("user"),
project_uuid=str(project.flow_organization),
data=serializer.validated_data.get("data"),
channeltype_code=serializer.validated_data.get("channeltype_code"),
)

try:
response = grpc_instance.create_channel(
user=serializer.validated_data.get("user"),
project_uuid=str(project.flow_organization),
data=serializer.validated_data.get("data"),
channeltype_code=serializer.validated_data.get("channeltype_code"),
)

except grpc.RpcError as error:
if error.code() is grpc.StatusCode.INVALID_ARGUMENT:
self.context.abort(grpc.StatusCode.INVALID_ARGUMENT, "Bad Request")
raise error

return CreateChannelResponse(
uuid=response.uuid,
uuid=response.uuid, name=response.name, config=response.config, address=response.address
)

def ReleaseChannel(self, request, context):
Expand Down
4 changes: 4 additions & 0 deletions connect/api/v1/organization/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Meta:
"payment_warnings",
"problem_capture_invoice",
"currenty_invoice",
"contract_on",
]
ref_name = None

Expand All @@ -54,6 +55,7 @@ class Meta:
BillingPlan.PLAN_CHOICES, label=_("plan"), default=BillingPlan.PLAN_FREE
)
is_active = serializers.BooleanField()
contract_on = serializers.DateField()
final_card_number = serializers.CharField(
read_only=True,
allow_null=True,
Expand Down Expand Up @@ -96,6 +98,7 @@ class Meta:
"authorization",
"created_at",
"is_suspended",
"extra_integration",
]
ref_name = None

Expand All @@ -118,6 +121,7 @@ class Meta:
required=False,
help_text=_("Whether this organization is currently suspended."),
)
extra_integration = serializers.IntegerField(read_only=True)

def create(self, validated_data):
task = tasks.create_organization.delay( # pragma: no cover
Expand Down
9 changes: 6 additions & 3 deletions connect/api/v1/organization/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ def remove_card_setup(

self.check_object_permissions(self.request, organization)

if organization.organization_billing.remove_credit_card:

if organization.organization_billing.plan != organization.organization_billing.PLAN_CUSTOM \
and organization.organization_billing.remove_credit_card:
organization.is_suspended = True
organization.organization_billing.is_active = False
organization.organization_billing.save(update_fields=["is_active"])
organization.save(update_fields=["is_suspended"])

return JsonResponse(data={"status": True}, status=status.HTTP_200_OK)
Expand Down Expand Up @@ -274,12 +276,12 @@ def closing_plan(self, request, organization_uuid): # pragma: no cover
permission_classes=[AllowAny],
)
def reactivate_plan(self, request, organization_uuid): # pragma: no cover
result = {}

organization = get_object_or_404(Organization, uuid=organization_uuid)
org_billing = organization.organization_billing
org_billing.termination_date = None
org_billing.is_active = True
org_billing.contract_on = timezone.now().date()
org_billing.save()

for project in organization.project.all():
Expand All @@ -294,6 +296,7 @@ def reactivate_plan(self, request, organization_uuid): # pragma: no cover
"plan": org_billing.plan,
"is_active": org_billing.is_active,
"termination_date": org_billing.termination_date,
"contract_on": org_billing.contract_on,
}
return JsonResponse(data=result, status=status.HTTP_200_OK)

Expand Down
31 changes: 0 additions & 31 deletions connect/common/management/commands/teste.py

This file was deleted.

21 changes: 21 additions & 0 deletions connect/common/migrations/0031_billingplan_contract_on.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.2.9 on 2021-12-09 12:42

import datetime
from django.db import migrations, models
from django.utils.timezone import utc


class Migration(migrations.Migration):

dependencies = [
('common', '0030_project_extra_active_integration'),
]

operations = [
migrations.AddField(
model_name='billingplan',
name='contract_on',
field=models.DateField(auto_now_add=True, default=datetime.datetime(2021, 12, 9, 12, 42, 40, 540676, tzinfo=utc), verbose_name='date of contract plan'),
preserve_default=False,
),
]
18 changes: 18 additions & 0 deletions connect/common/migrations/0032_alter_billingplan_plan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.9 on 2021-12-20 17:33

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('common', '0031_billingplan_contract_on'),
]

operations = [
migrations.AlterField(
model_name='billingplan',
name='plan',
field=models.CharField(choices=[('free', 'free'), ('enterprise', 'enterprise'), ('custom', 'custom')], max_length=10, verbose_name='plan'),
),
]
36 changes: 24 additions & 12 deletions connect/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,12 @@ class Meta:

PLAN_FREE = "free"
PLAN_ENTERPRISE = "enterprise"
PLAN_CUSTOM = "custom"

PLAN_CHOICES = [
(PLAN_FREE, _("free")),
(PLAN_ENTERPRISE, _("enterprise")),
(PLAN_CUSTOM, _("custom"))
]

organization = models.OneToOneField(
Expand All @@ -537,6 +539,7 @@ class Meta:
)
fixed_discount = models.FloatField(_("fixed discount"), default=0)
plan = models.CharField(_("plan"), max_length=10, choices=PLAN_CHOICES)
contract_on = models.DateField(_("date of contract plan"), auto_now_add=True)
is_active = models.BooleanField(_("active plan"), default=True)
stripe_customer = models.CharField(
verbose_name=_("Stripe Customer"),
Expand Down Expand Up @@ -622,6 +625,12 @@ def remove_credit_card(self):
gateway = billing.get_gateway("stripe")
unstore = gateway.unstore(identification=self.stripe_customer)
if unstore['status'] == 'SUCCESS':
self.card_brand = None
self.card_expiration_date = None
self.final_card_number = None
self.cardholder_name = None
self.stripe_configured_card = False
self.save()
return True
return False

Expand All @@ -638,20 +647,23 @@ def currenty_invoice(self):
total_contact_count=Sum("contact_count")
).get("total_contact_count")

return {
"total_contact": contact_count,
"amount_currenty": Decimal(
amount_currenty = 0

if self.plan == BillingPlan.PLAN_ENTERPRISE:
amount_currenty = Decimal(
float(
float(
self.organization.organization_billing.calculate_amount(
contact_count=0 if contact_count is None else contact_count
)
)
+ settings.BILLING_COST_PER_WHATSAPP
* self.organization.extra_integration
) + (settings.BILLING_COST_PER_WHATSAPP * self.organization.extra_integration)
)
* float(1 - self.fixed_discount / 100)
).quantize(Decimal(".01"), decimal.ROUND_HALF_UP),
).quantize(Decimal(".01"), decimal.ROUND_HALF_UP)

return {
"total_contact": contact_count,
"amount_currenty": amount_currenty
}

def change_plan(self, plan):
Expand All @@ -667,17 +679,16 @@ def change_plan(self, plan):

def add_additional_information(self, data: dict):
count = 0
print(type(data['extra_integration']))
if data['additional_info']:
if not (data['additional_info'] is None):
self.additional_billing_information = data['additional_info']
count += 1
if data['cpf']:
if not (data['cpf'] is None):
self.cpf = data['cpf']
count += 1
if data['cnpj']:
if not (data['cnpj'] is None):
self.cnpj = data['cnpj']
count += 1
if data['extra_integration']:
if not (data['extra_integration'] is None):
self.organization.extra_integration = data['extra_integration']
self.organization.save()
count += 1
Expand Down Expand Up @@ -800,6 +811,7 @@ def free_active_contacts_limit(self, value):
def precification(self):
return {
"currency": "USD",
"extra_whatsapp_integration": settings.BILLING_COST_PER_WHATSAPP,
"range": [
{
"from": 1,
Expand Down
Loading

0 comments on commit 1e98d97

Please sign in to comment.