Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add proper handling of already created users #137

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions commcare_connect/opportunity/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
UserVisit,
)
from commcare_connect.users.helpers import create_hq_user
from commcare_connect.users.models import ConnectIDUserLink


class OpportunityViewSet(viewsets.ReadOnlyModelViewSet):
Expand Down Expand Up @@ -64,7 +65,12 @@ def post(self, *args, **kwargs):
if not created:
return Response(status=200, data="Opportunity is already claimed")

if opportunity.learn_app.cc_domain != opportunity.deliver_app.cc_domain:
create_hq_user(self.request.user, opportunity.deliver_app.cc_domain, opportunity.api_key)

domain = opportunity.deliver_app.cc_domain
if not ConnectIDUserLink.objects.filter(user=self.request.user, domain=domain).exists():
user_created = create_hq_user(self.request.user, domain, opportunity.api_key)
if not user_created:
return Response("Failed to create user", status=400)
ConnectIDUserLink.objects.create(
commcare_username=self.request.user.username, user=self.request.user, domain=domain
)
return Response(status=201)
17 changes: 17 additions & 0 deletions commcare_connect/users/migrations/0011_connectiduserlink_domain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.5 on 2023-09-28 19:47

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("users", "0010_user_phone_number"),
]

operations = [
migrations.AddField(
model_name="connectiduserlink",
name="domain",
field=models.CharField(blank=True, max_length=255, null=True),
),
]
1 change: 1 addition & 0 deletions commcare_connect/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class ConnectIDUserLink(models.Model):
on_delete=models.CASCADE,
)
commcare_username = models.TextField()
domain = models.CharField(max_length=255, null=True, blank=True)

class Meta:
constraints = [models.UniqueConstraint(fields=["user", "commcare_username"], name="connect_user")]
9 changes: 5 additions & 4 deletions commcare_connect/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ def start_learn_app(request):
return HttpResponse("Opportunity requires API Key", status=400)
app = opportunity.learn_app
domain = app.cc_domain
user_created = create_hq_user(request.user, domain, api_key)
if not user_created:
return HttpResponse("Failed to create user", status=400)
ConnectIDUserLink.objects.create(commcare_username=request.user.username, user=request.user)
if not ConnectIDUserLink.objects.filter(user=request.user, domain=domain).exists():
user_created = create_hq_user(request.user, domain, api_key)
if not user_created:
return HttpResponse("Failed to create user", status=400)
ConnectIDUserLink.objects.create(commcare_username=request.user.username, user=request.user, domain=domain)
try:
access_object = OpportunityAccess.objects.get(user=request.user, opportunity=opportunity)
except OpportunityAccess.DoesNotExist:
Expand Down