diff --git a/server/vbv_lernwelt/core/admin.py b/server/vbv_lernwelt/core/admin.py index a090accd..d5a98ca1 100644 --- a/server/vbv_lernwelt/core/admin.py +++ b/server/vbv_lernwelt/core/admin.py @@ -47,7 +47,6 @@ class UserAdmin(auth_admin.UserAdmin): _("Profile"), { "fields": ( - "organisation", "language", "avatar", "street", @@ -55,19 +54,21 @@ class UserAdmin(auth_admin.UserAdmin): "postal_code", "city", "country", + "invoice_address", ) }, ), ( - _("Invoice"), + _("Organisation"), { "fields": ( - "invoice_name", - "invoice_street", - "invoice_street_number", - "invoice_postal_code", - "invoice_city", - "invoice_country", + "organisation", + "organisation_detail_name", + "organisation_street", + "organisation_street_number", + "organisation_postal_code", + "organisation_city", + "organisation_country", ) }, ), diff --git a/server/vbv_lernwelt/core/migrations/0006_auto_20240124_2053.py b/server/vbv_lernwelt/core/migrations/0006_auto_20240125_0915.py similarity index 76% rename from server/vbv_lernwelt/core/migrations/0006_auto_20240124_2053.py rename to server/vbv_lernwelt/core/migrations/0006_auto_20240125_0915.py index d38bc02e..d78db882 100644 --- a/server/vbv_lernwelt/core/migrations/0006_auto_20240124_2053.py +++ b/server/vbv_lernwelt/core/migrations/0006_auto_20240125_0915.py @@ -1,4 +1,4 @@ -# Generated by Django 3.2.20 on 2024-01-24 19:53 +# Generated by Django 3.2.20 on 2024-01-25 08:15 import django.db.models.deletion from django.db import migrations, models @@ -28,38 +28,47 @@ class Migration(migrations.Migration): ), migrations.AddField( model_name="user", - name="invoice_city", + name="invoice_address", + field=models.CharField( + choices=[("prv", "Private"), ("org", "Organisation")], + default="prv", + max_length=3, + ), + ), + migrations.AddField( + model_name="user", + name="organisation_city", field=models.CharField(blank=True, max_length=255), ), migrations.AddField( model_name="user", - name="invoice_country", + name="organisation_country", field=models.ForeignKey( blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, - related_name="invoice_country", + related_name="organisation_country", to="core.country", ), ), migrations.AddField( model_name="user", - name="invoice_name", + name="organisation_detail_name", field=models.CharField(blank=True, max_length=255), ), migrations.AddField( model_name="user", - name="invoice_postal_code", + name="organisation_postal_code", field=models.CharField(blank=True, max_length=255), ), migrations.AddField( model_name="user", - name="invoice_street", + name="organisation_street", field=models.CharField(blank=True, max_length=255), ), migrations.AddField( model_name="user", - name="invoice_street_number", + name="organisation_street_number", field=models.CharField(blank=True, max_length=255), ), migrations.AddField( diff --git a/server/vbv_lernwelt/core/models.py b/server/vbv_lernwelt/core/models.py index f90b8b70..1221dee3 100644 --- a/server/vbv_lernwelt/core/models.py +++ b/server/vbv_lernwelt/core/models.py @@ -48,6 +48,14 @@ class User(AbstractUser): ("it", "Italiano"), ) + INVOICE_ADDRESS_PRIVATE = "prv" + INVOICE_ADDRESS_ORGANISATION = "org" + + INVOICE_ADDRESS_CHOICES = ( + (INVOICE_ADDRESS_PRIVATE, "Private"), + (INVOICE_ADDRESS_ORGANISATION, "Organisation"), + ) + id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) avatar = models.ForeignKey( @@ -69,6 +77,10 @@ class User(AbstractUser): Organisation, on_delete=models.SET_NULL, null=True, blank=True ) + invoice_address = models.CharField( + max_length=3, choices=INVOICE_ADDRESS_CHOICES, default="prv" + ) + street = models.CharField(max_length=255, blank=True) street_number = models.CharField(max_length=255, blank=True) postal_code = models.CharField(max_length=255, blank=True) @@ -81,14 +93,14 @@ class User(AbstractUser): blank=True, ) - invoice_name = models.CharField(max_length=255, blank=True) - invoice_street = models.CharField(max_length=255, blank=True) - invoice_street_number = models.CharField(max_length=255, blank=True) - invoice_postal_code = models.CharField(max_length=255, blank=True) - invoice_city = models.CharField(max_length=255, blank=True) - invoice_country = models.ForeignKey( + organisation_detail_name = models.CharField(max_length=255, blank=True) + organisation_street = models.CharField(max_length=255, blank=True) + organisation_street_number = models.CharField(max_length=255, blank=True) + organisation_postal_code = models.CharField(max_length=255, blank=True) + organisation_city = models.CharField(max_length=255, blank=True) + organisation_country = models.ForeignKey( Country, - related_name="invoice_country", + related_name="organisation_country", on_delete=models.SET_NULL, null=True, blank=True, diff --git a/server/vbv_lernwelt/shop/tests/test_checkout_api.py b/server/vbv_lernwelt/shop/tests/test_checkout_api.py index bd7e942b..3c0b3ec8 100644 --- a/server/vbv_lernwelt/shop/tests/test_checkout_api.py +++ b/server/vbv_lernwelt/shop/tests/test_checkout_api.py @@ -5,6 +5,7 @@ from rest_framework import status from rest_framework.test import APITestCase from vbv_lernwelt.core.admin import User +from vbv_lernwelt.core.model_utils import add_countries from vbv_lernwelt.shop.const import VV_DE_PRODUCT_SKU from vbv_lernwelt.shop.models import CheckoutInformation, CheckoutState, Product from vbv_lernwelt.shop.services import InitTransactionException @@ -20,13 +21,13 @@ TEST_ADDRESS_DATA = { "street_number": "1", "postal_code": "1234", "city": "Test City", - "country": "Test Country", + "country": "209", "company_name": "Test Company", "company_street": "Test Company Street", "company_street_number": "1", "company_postal_code": "1234", "company_city": "Test Company City", - "company_country": "Test Company Country", + "company_country": "209", } REDIRECT_URL = "http://testserver/redirect-url" @@ -49,6 +50,7 @@ class CheckoutAPITestCase(APITestCase): ) self.client.login(username=USER_USERNAME, password=USER_PASSWORD) + add_countries() @patch("vbv_lernwelt.shop.views.init_transaction") def test_checkout_happy_case(self, mock_init_transaction): @@ -90,6 +92,12 @@ class CheckoutAPITestCase(APITestCase): webhook_url=f"{REDIRECT_URL}/api/shop/transaction/webhook/", ) + user = User.objects.get(username=USER_USERNAME) + + self.assertEqual(user.street, TEST_ADDRESS_DATA["street"]) + self.assertEqual(str(user.country.country_id), TEST_ADDRESS_DATA["country"]) + self.assertEqual(user.invoice_address, User.INVOICE_ADDRESS_ORGANISATION) + @patch("vbv_lernwelt.shop.views.init_transaction") def test_incomplete_setup(self, mock_init_transaction): # GIVEN diff --git a/server/vbv_lernwelt/shop/views.py b/server/vbv_lernwelt/shop/views.py index 31edf824..84fb4907 100644 --- a/server/vbv_lernwelt/shop/views.py +++ b/server/vbv_lernwelt/shop/views.py @@ -7,6 +7,7 @@ from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from sentry_sdk import capture_exception +from vbv_lernwelt.core.models import Country, User from vbv_lernwelt.course.consts import ( COURSE_VERSICHERUNGSVERMITTLERIN_FR_ID, COURSE_VERSICHERUNGSVERMITTLERIN_ID, @@ -174,7 +175,7 @@ def checkout_vv(request): ), ) - CheckoutInformation.objects.create( + checkout_info = CheckoutInformation.objects.create( user=request.user, state=CheckoutState.ONGOING, transaction_id=transaction_id, @@ -187,6 +188,8 @@ def checkout_vv(request): **request.data["address"], ) + update_user_address(user=request.user, checkout_info=checkout_info) + return next_step_response(url=get_payment_url(transaction_id)) @@ -267,3 +270,38 @@ def checkout_cancel_url(base_url: str) -> str: def checkout_success_url(product_sku: str, base_url: str = "") -> str: return f"{base_url}/onboarding/{product_sku}/checkout/complete" + + +def update_user_address(user: User, checkout_info: CheckoutInformation): + user.street = checkout_info.street + user.street_number = checkout_info.street_number + user.postal_code = checkout_info.postal_code + user.city = checkout_info.city + try: + user.country = Country.objects.get(country_id=checkout_info.country) + except Country.DoesNotExist: + pass + + user.organisation_detail_name = checkout_info.company_name + user.organisation_street = checkout_info.company_street + user.organisation_street_number = checkout_info.company_street_number + user.organisation_postal_code = checkout_info.company_postal_code + user.organisation_city = checkout_info.company_city + try: + user.organisation_country = Country.objects.get( + country_id=checkout_info.company_country + ) + except Country.DoesNotExist: + pass + + if ( + user.organisation_detail_name + and user.organisation_street + and user.organisation_street_number + and user.organisation_postal_code + and user.organisation_city + and user.organisation_country + ): + user.invoice_address = User.INVOICE_ADDRESS_ORGANISATION + + user.save()