diff --git a/commcare_connect/opportunity/api/views.py b/commcare_connect/opportunity/api/views.py index 55a9cbbe..aa8d6ed5 100644 --- a/commcare_connect/opportunity/api/views.py +++ b/commcare_connect/opportunity/api/views.py @@ -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): @@ -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) diff --git a/commcare_connect/users/migrations/0011_connectiduserlink_domain.py b/commcare_connect/users/migrations/0011_connectiduserlink_domain.py new file mode 100644 index 00000000..ac67d76c --- /dev/null +++ b/commcare_connect/users/migrations/0011_connectiduserlink_domain.py @@ -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), + ), + ] diff --git a/commcare_connect/users/models.py b/commcare_connect/users/models.py index 32fd1ea0..1ec5beff 100644 --- a/commcare_connect/users/models.py +++ b/commcare_connect/users/models.py @@ -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")] diff --git a/commcare_connect/users/views.py b/commcare_connect/users/views.py index 79a85b58..024fd65d 100644 --- a/commcare_connect/users/views.py +++ b/commcare_connect/users/views.py @@ -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: