Updateinsert customers

This commit is contained in:
Jonas Linter
2025-10-10 16:47:19 +02:00
parent 165914d686
commit 1248772f60
6 changed files with 228 additions and 62 deletions

View File

@@ -263,6 +263,93 @@ class TestWixWebhookEndpoint:
data = response.json()
assert data["status"] == "success"
def test_wix_webhook_updates_existing_customer(self, client):
"""Test that same contact_id updates customer instead of duplicate."""
# First submission
first_submission = {
"data": {
"submissionId": "test-submission-001",
"submissionTime": "2025-10-07T05:48:41.855Z",
"contact": {
"name": {"first": "John", "last": "Doe"},
"email": "john.doe@example.com",
"phones": [{"e164Phone": "+1234567890"}],
"locale": "en-US",
"contactId": "fixed-contact-id-123",
},
"field:anrede": "Mr.",
"field:date_picker_a7c8": "2024-12-25",
"field:date_picker_7e65": "2024-12-31",
"field:number_7cf5": "2",
"field:anzahl_kinder": "0",
}
}
response = client.post("/api/webhook/wix-form", json=first_submission)
assert response.status_code == 200
# Second submission with same contact_id but different data
second_submission = {
"data": {
"submissionId": "test-submission-002",
"submissionTime": "2025-10-08T10:30:00.000Z",
"contact": {
"name": {"first": "John", "last": "Smith"}, # Changed last name
"email": "john.smith@example.com", # Changed email
"phones": [{"e164Phone": "+9876543210"}], # Changed phone
"locale": "de-DE", # Changed locale
"contactId": "fixed-contact-id-123", # Same contact_id
},
"field:anrede": "Dr.", # Changed prefix
"field:date_picker_a7c8": "2025-01-10",
"field:date_picker_7e65": "2025-01-15",
"field:number_7cf5": "4",
"field:anzahl_kinder": "2",
"field:alter_kind_1": "5",
"field:alter_kind_2": "10",
}
}
response = client.post("/api/webhook/wix-form", json=second_submission)
assert response.status_code == 200
# Verify only one customer exists with updated information
async def check_db():
from sqlalchemy import select # noqa: PLC0415
engine = client.app.state.engine
async_session = async_sessionmaker(engine, expire_on_commit=False)
async with async_session() as session:
# Check only one customer exists
result = await session.execute(select(Customer))
customers = result.scalars().all()
assert len(customers) == 1, "Should have exactly one customer"
customer = customers[0]
# Verify customer was updated with new information
assert customer.given_name == "John"
assert customer.surname == "Smith", "Last name updated"
assert (
customer.email_address == "john.smith@example.com"
), "Email updated"
assert customer.phone == "+9876543210", "Phone updated"
assert customer.name_prefix == "Dr.", "Prefix updated"
assert customer.language == "de", "Language updated"
assert customer.contact_id == "fixed-contact-id-123"
# Check both reservations were created
result = await session.execute(select(Reservation))
reservations = result.scalars().all()
expected_reservations = 2
assert len(reservations) == expected_reservations
# Both reservations should be linked to the same customer
assert all(r.customer_id == customer.id for r in reservations)
import asyncio # noqa: PLC0415
asyncio.run(check_db())
class TestGenericWebhookEndpoint:
"""Test generic webhook endpoint."""