Skip to content

Commit

Permalink
Merge pull request #137 from dimagi/ce/user-already-created
Browse files Browse the repository at this point in the history
add proper handling of already created users
  • Loading branch information
calellowitz authored Sep 28, 2023
2 parents efcebca + c1f7a3f commit 73b2b7b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
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

0 comments on commit 73b2b7b

Please sign in to comment.