diff --git a/corporate/migrations/0023_zulipsponsorshiprequest_customer.py b/corporate/migrations/0023_zulipsponsorshiprequest_customer.py new file mode 100644 index 0000000000..8e77be32e3 --- /dev/null +++ b/corporate/migrations/0023_zulipsponsorshiprequest_customer.py @@ -0,0 +1,20 @@ +# Generated by Django 4.2.7 on 2023-11-26 16:00 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("corporate", "0022_session_is_manual_license_management_upgrade_session"), + ] + + operations = [ + migrations.AddField( + model_name="zulipsponsorshiprequest", + name="customer", + field=models.ForeignKey( + null=True, on_delete=django.db.models.deletion.CASCADE, to="corporate.customer" + ), + ), + ] diff --git a/corporate/migrations/0024_zulipsponsorshiprequest_fill_customer_data.py b/corporate/migrations/0024_zulipsponsorshiprequest_fill_customer_data.py new file mode 100644 index 0000000000..b9979bda63 --- /dev/null +++ b/corporate/migrations/0024_zulipsponsorshiprequest_fill_customer_data.py @@ -0,0 +1,18 @@ +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("corporate", "0023_zulipsponsorshiprequest_customer"), + ] + + operations = [ + migrations.RunSQL( + """ + UPDATE corporate_zulipsponsorshiprequest + SET customer_id = ( + SELECT id FROM corporate_customer WHERE corporate_customer.realm_id = corporate_zulipsponsorshiprequest.realm_id + ) + """ + ), + ] diff --git a/corporate/migrations/0025_alter_zulipsponsorshiprequest_customer.py b/corporate/migrations/0025_alter_zulipsponsorshiprequest_customer.py new file mode 100644 index 0000000000..403260cca8 --- /dev/null +++ b/corporate/migrations/0025_alter_zulipsponsorshiprequest_customer.py @@ -0,0 +1,20 @@ +# Generated by Django 4.2.7 on 2023-11-26 16:13 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("corporate", "0024_zulipsponsorshiprequest_fill_customer_data"), + ] + + operations = [ + migrations.AlterField( + model_name="zulipsponsorshiprequest", + name="customer", + field=models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, to="corporate.customer" + ), + ), + ] diff --git a/corporate/migrations/0026_remove_zulipsponsorshiprequest_realm.py b/corporate/migrations/0026_remove_zulipsponsorshiprequest_realm.py new file mode 100644 index 0000000000..6572fc145c --- /dev/null +++ b/corporate/migrations/0026_remove_zulipsponsorshiprequest_realm.py @@ -0,0 +1,16 @@ +# Generated by Django 4.2.7 on 2023-11-26 16:14 + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("corporate", "0025_alter_zulipsponsorshiprequest_customer"), + ] + + operations = [ + migrations.RemoveField( + model_name="zulipsponsorshiprequest", + name="realm", + ), + ] diff --git a/corporate/models.py b/corporate/models.py index 113a1649a2..1b582f5b42 100644 --- a/corporate/models.py +++ b/corporate/models.py @@ -358,7 +358,7 @@ class LicenseLedger(models.Model): class ZulipSponsorshipRequest(models.Model): - realm = models.ForeignKey(Realm, on_delete=CASCADE) + customer = models.ForeignKey(Customer, on_delete=CASCADE) requested_by = models.ForeignKey(UserProfile, on_delete=CASCADE) org_type = models.PositiveSmallIntegerField( diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py index f6ae872a3d..8a36bf7d3c 100644 --- a/corporate/tests/test_stripe.py +++ b/corporate/tests/test_stripe.py @@ -1804,8 +1804,10 @@ class StripeTest(StripeTestCase): response = self.client_post("/json/billing/sponsorship", data) self.assert_json_success(response) + customer = get_customer_by_realm(user.realm) + assert customer is not None sponsorship_request = ZulipSponsorshipRequest.objects.filter( - realm=user.realm, requested_by=user + customer=customer, requested_by=user ).first() assert sponsorship_request is not None self.assertEqual(sponsorship_request.org_website, data["website"]) diff --git a/corporate/views/upgrade.py b/corporate/views/upgrade.py index bf371647e0..3ea9cdb473 100644 --- a/corporate/views/upgrade.py +++ b/corporate/views/upgrade.py @@ -167,8 +167,10 @@ def sponsorship( if form.is_valid(): with transaction.atomic(): + # Ensures customer is created first before updating sponsorship status. + billing_session.update_customer_sponsorship_status(True) sponsorship_request = ZulipSponsorshipRequest( - realm=realm, + customer=billing_session.get_customer(), requested_by=user, org_website=form.cleaned_data["website"], org_description=form.cleaned_data["description"], @@ -184,7 +186,6 @@ def sponsorship( realm.org_type = org_type realm.save(update_fields=["org_type"]) - billing_session.update_customer_sponsorship_status(True) do_change_is_billing_admin(user, True) org_type_display_name = get_org_type_display_name(org_type)