Skip to content

Commit

Permalink
Merge 20828f0 into 5ae1b3d
Browse files Browse the repository at this point in the history
  • Loading branch information
AlisoSouza committed Dec 6, 2021
2 parents 5ae1b3d + 20828f0 commit 8d9d4b0
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 3 deletions.
24 changes: 22 additions & 2 deletions connect/api/v1/organization/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,16 @@ def add_additional_billing_information(self, request, organization_uuid):
organization = get_object_or_404(Organization, uuid=organization_uuid)
cpf = request.data.get('cpf') if 'cpf' in request.data else None
cnpj = request.data.get('cnpj') if 'cnpj' in request.data else None
extra_integration = request.data.get('extra_integration') if 'extra_integration' in request.data else None
additional_info = request.data.get('additional_billing_info') if 'additional_billing_info' in request.data else None
response = [
{
'status': 'SUCESS',
'response': {
'CPF': cpf,
'CNPJ': cnpj,
'additional_information': additional_info
'additional_information': additional_info,
'extra_integration': extra_integration
}
},
{
Expand All @@ -411,7 +413,8 @@ def add_additional_billing_information(self, request, organization_uuid):
{
'additional_info': additional_info,
'cpf': cpf,
'cnpj': cnpj
'cnpj': cnpj,
'extra_integration': extra_integration
}
)
return JsonResponse(data=response[result], status=status.HTTP_200_OK)
Expand All @@ -428,6 +431,23 @@ def get_billing_precification(self, request):
billing_data = GenericBillingData.objects.first() if GenericBillingData.objects.all().exists() else GenericBillingData.objects.create()
return JsonResponse(data=billing_data.precification, status=status.HTTP_200_OK)

@action(
detail=True,
methods=["GET"],
url_name='extra-integrations',
url_path='billing/extra-integrations/(?P<organization_uuid>[^/.]+)',
authentication_classes=[ExternalAuthentication],
permission_classes=[AllowAny]
)
def get_extra_active_integrations(self, request, organization_uuid):
organization = get_object_or_404(Organization, uuid=organization_uuid)
self.check_object_permissions(self.request, organization)
response = {
"extra_active_integrations": organization.extra_active_integrations,
"limit_extra_integrations": organization.extra_integration
}
return JsonResponse(data=response, status=status.HTTP_200_OK)


class OrganizationAuthorizationViewSet(
MultipleFieldLookupMixin,
Expand Down
44 changes: 44 additions & 0 deletions connect/api/v1/tests/test_organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,47 @@ def test_get_active_contacts_limit(self):
def tearDown(self):
self.project.delete()
self.organization.delete()


class ExtraIntegrationsTestCase(TestCase):
def setUp(self):
self.factory = RequestFactory()

self.owner, self.owner_token = create_user_and_token("owner")
self.user, self.user_token = create_user_and_token()
self.organization = Organization.objects.create(
name="test organization", description="", inteligence_organization=1,
organization_billing__cycle=BillingPlan.BILLING_CYCLE_MONTHLY,
organization_billing__plan="free", extra_integration=4,
)

self.project = Project.objects.create(
name="Unit Test Project", flow_organization="57257d94-e54b-4ec1-8952-113a81610465",
organization_id=self.organization.uuid)

self.organization_authorization = self.organization.authorizations.create(
user=self.owner, role=OrganizationAuthorization.ROLE_ADMIN
)

def request(self, value, param, token=None):
authorization_header = (
{"HTTP_AUTHORIZATION": "Token {}".format(token.key)} if token else {}
)
request = self.factory.get(
f"/v1/organization/org/billing/{value}/{param}", **authorization_header
)
response = OrganizationViewSet.as_view({"get": "get_extra_active_integrations"})(
request, organization_uuid=self.organization.uuid
)

content_data = json.loads(response.content)

return response, content_data

def test_extra_integration(self):
response, content_data = self.request(
"extra-integrations",
self.organization.uuid,
self.owner_token,
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
18 changes: 18 additions & 0 deletions connect/common/migrations/0030_project_extra_active_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.9 on 2021-12-03 19:41

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('common', '0029_auto_20211202_1729'),
]

operations = [
migrations.AddField(
model_name='project',
name='extra_active_integration',
field=models.IntegerField(default=0, verbose_name='Whatsapp Integrations'),
),
]
14 changes: 13 additions & 1 deletion connect/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ class Meta:
default=False, help_text=_("Whether this organization is currently suspended.")
)
extra_integration = models.IntegerField(_("Whatsapp Extra Integration"), default=0)

objects = OrganizationManager()

def __str__(self):
Expand Down Expand Up @@ -211,6 +210,13 @@ def active_contacts(self):
active_contact_counter += project.contact_count
return active_contact_counter

@property
def extra_active_integrations(self):
active_integrations_counter = 0
for project in self.project.all():
active_integrations_counter += project.extra_active_integration
return 0 if active_integrations_counter <= 1 else active_integrations_counter - 1


class OrganizationAuthorization(models.Model):
class Meta:
Expand Down Expand Up @@ -327,6 +333,7 @@ class Meta:
flow_count = models.IntegerField(_("Flows count"), default=0)
contact_count = models.IntegerField(_("Contacts count"), default=0)
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
extra_active_integration = models.IntegerField(_("Whatsapp Integrations"), default=0)

def __str__(self):
return f"{self.uuid} - Project: {self.name} - Org: {self.organization.name}"
Expand Down Expand Up @@ -660,6 +667,7 @@ def change_plan(self, plan):

def add_additional_information(self, data: dict):
count = 0
print(type(data['extra_integration']))
if data['additional_info']:
self.additional_billing_information = data['additional_info']
count += 1
Expand All @@ -669,6 +677,10 @@ def add_additional_information(self, data: dict):
if data['cnpj']:
self.cnpj = data['cnpj']
count += 1
if data['extra_integration']:
self.organization.extra_integration = data['extra_integration']
self.organization.save()
count += 1
if count > 0:
self.save()
return 0
Expand Down
4 changes: 4 additions & 0 deletions connect/common/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ def sync_updates_projects():
after=after
).get("active_contacts")

integrations = len(list(flow_instance.list_channel(project_uuid=str(project.flow_organization))))
project.extra_active_integration = integrations

project.name = str(flow_result.get("name"))
project.timezone = str(flow_result.get("timezone"))
project.date_format = str(flow_result.get("date_format"))
Expand All @@ -288,6 +291,7 @@ def sync_updates_projects():
"inteligence_count",
"flow_count",
"contact_count",
"extra_active_integration",
]
)

Expand Down
10 changes: 10 additions & 0 deletions connect/grpc/types/flow/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,13 @@ def release_channel(self, channel_uuid: str, user: str):
)
)
return response

def list_channel(self, project_uuid: str, channel_type: str = "WA"):
stub = channel_pb2_grpc.ChannelControllerStub(self.channel)
response = stub.List(
channel_pb2.ChannelListRequest(
org=project_uuid,
channel_type=channel_type
)
)
return response

0 comments on commit 8d9d4b0

Please sign in to comment.