Skip to content

Commit

Permalink
adding a endpoint to check if exists the project and create a new use…
Browse files Browse the repository at this point in the history
…r when the user request not exists in project; just create a user when he not exists in platform/keycloak
  • Loading branch information
Jackson Barbosa committed Jan 9, 2025
1 parent 24f33ad commit 897ba5d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 22 deletions.
42 changes: 22 additions & 20 deletions connect/api/v2/commerce/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,22 @@ def create(self, validated_data):

try:
# Create Keycloak user
try:
user_dto = KeycloakUserDTO(
email=user_email,
company_name=validated_data.get("organization_name"),
)
create_keycloak_user_use_case = CreateKeycloakUserUseCase(user_dto)
user_info = create_keycloak_user_use_case.execute()
except Exception as e:
raise serializers.ValidationError({"keycloak_error": str(e)})

users = User.objects.filter(email=user_email)
if users.count() == 0:
try:
user_dto = KeycloakUserDTO(
email=user_email,
company_name=validated_data.get("organization_name"),
)
create_keycloak_user_use_case = CreateKeycloakUserUseCase(user_dto)
user_info = create_keycloak_user_use_case.execute()
# Send email to user
user = user_info.get("user")
user.send_email_access_password(user_info.get("password"))
except Exception as e:
raise serializers.ValidationError({"keycloak_error": str(e)})
else:
user = users.first()
# Create organization
organization = Organization.objects.create(
name=validated_data.get("organization_name"),
Expand All @@ -112,31 +118,27 @@ def create(self, validated_data):
).default,
)
organization.authorizations.create(
user=user_info.get("user"), role=OrganizationRole.ADMIN.value
user=user, role=OrganizationRole.ADMIN.value
)
self.publish_create_org_message(organization, user_info.get("user"))
self.publish_create_org_message(organization, user)

# Create project
project = Project.objects.create(
name=validated_data.get("project_name"),
vtex_account=validated_data.get("vtex_account"),
timezone="America/Sao_Paulo",
organization=organization,
created_by=user_info.get("user"),
created_by=user,
is_template=False,
project_type=TypeProject.COMMERCE,
)
self.send_request_flow_product(user_info.get("user"))
self.publish_create_project_message(project, user_info.get("user"))

# Send email to user
user = user_info.get("user")
user.send_email_access_password(user_info.get("password"))
self.send_request_flow_product(user)
self.publish_create_project_message(project, user)

data = {
"organization": organization,
"project": project,
"user": user_info.get("user"),
"user": user,
}
except Exception as e:
raise serializers.ValidationError({"error": str(e)})
Expand Down
54 changes: 53 additions & 1 deletion connect/api/v2/commerce/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from rest_framework.response import Response
from rest_framework import status
from rest_framework import views
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import CreateModelMixin
from connect.api.v2.commerce.permissions import CanCommunicateInternally
from connect.api.v2.commerce.serializers import CommerceSerializer
from connect.api.v2.paginations import CustomCursorPagination
from connect.common.models import Organization
from connect.common.models import Organization, Project, ProjectAuthorization, OrganizationRole
from connect.authentication.models import User
from connect.usecases.users.create import CreateKeycloakUserUseCase
from connect.usecases.users.user_dto import KeycloakUserDTO


class CommerceOrganizationViewSet(CreateModelMixin, GenericViewSet):
Expand All @@ -27,3 +31,51 @@ def create(self, request, *args, **kwargs):
}

return Response(response, status=status.HTTP_201_CREATED)


class CommerceProjectCheckExists(views.APIView):

permission_classes = [CanCommunicateInternally]

def get(self, request):
data = request.data
project = Project.objects.filter(vtex_account=data.get("vtex_account"))

if project.count() > 0:
project = project.first()
else:
return Response(
{
"message": f"Project with vtex_account {data.get('vtex_account')} doesn't exists!",
"data": {
"has_project": False
}
}, status=status.HTTP_200_OK)

organization = project.organization
permission = ProjectAuthorization.objects.filter(project=project, user__email=data.get("user_email"))

if permission.count() > 0:
permission = permission.first()
else:
try:
user = User.objects.get(email=data.get("user_email"))
except Exception:
user_dto = KeycloakUserDTO(
email=data.get("user_email"),
company_name=project.organization.name,
)
create_keycloak_user_use_case = CreateKeycloakUserUseCase(user_dto)
user_info = create_keycloak_user_use_case.execute()
user = user_info.get("user")
organization.authorizations.create(
user=user, role=OrganizationRole.ADMIN.value
)
return Response(
{
"message": f"Project {project.name} exists and user {permission.user.email} has permission",
"data": {
"project_uuid": project.uuid,
"has_project": True
}
}, status=status.HTTP_200_OK)
7 changes: 6 additions & 1 deletion connect/api/v2/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
TemplateFeatureViewSet,
TemplateSuggestionViewSet,
)
from connect.api.v2.commerce.views import CommerceOrganizationViewSet
from connect.api.v2.commerce.views import CommerceOrganizationViewSet, CommerceProjectCheckExists
from connect.api.v2.organizations import views as organization_views
from connect.api.v2.projects import views as project_views
from connect.api.v2.internals import views as connect_internal_views
Expand Down Expand Up @@ -128,6 +128,11 @@
RecentActivityViewSet.as_view({"post": "create", "get": "list"}),
name="recent-activities",
),
path(
"commerce/check-project",
CommerceProjectCheckExists.as_view(),
name="check-exists-project"
),
]
urlpatterns += [
path("", include(projects_router.urls)),
Expand Down

0 comments on commit 897ba5d

Please sign in to comment.