Skip to content

Commit f4b9520

Browse files
authored
Handle Integrity error for wrong bdr_person_id (#352)
* Added fix * Adding uv lock file * Added comment
1 parent a128fcc commit f4b9520

File tree

3 files changed

+379
-227
lines changed

3 files changed

+379
-227
lines changed

app/callbooker/process.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
from datetime import datetime, timedelta, timezone
44

5+
from sqlalchemy.exc import IntegrityError
56
from sqlmodel import select
67

78
from app.callbooker.google import AdminGoogleCalendar
@@ -95,7 +96,25 @@ async def get_or_create_contact_company(event: CBSalesCall, db: DBSession) -> tu
9596
company_data = event.company_dict()
9697
company = Company(**company_data)
9798
db.add(company)
98-
db.commit()
99+
try:
100+
db.commit()
101+
except IntegrityError:
102+
# Previously tc2_admin_id was passed as bdr_id, but with the rebuild we expect the hermes_admin_id.
103+
# So this handles those old urls.
104+
db.rollback()
105+
bdr_id = company_data.get('bdr_person_id')
106+
if not bdr_id:
107+
raise
108+
109+
admin = db.exec(select(Admin).where(Admin.tc2_admin_id == bdr_id)).one_or_none()
110+
if not admin:
111+
logger.error(f'Could not find admin with tc2_admin_id {bdr_id}')
112+
raise
113+
114+
company.bdr_person_id = admin.id
115+
db.add(company)
116+
db.commit()
117+
99118
db.refresh(company)
100119
logger.info(f'Created company {company.id}')
101120

tests/callbooker/test_callbooker_booking.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,52 @@ async def test_sales_call_with_bdr_person(
430430
company = db.exec(select(Company)).first()
431431
assert company.bdr_person_id == bdr_person.tc2_admin_id
432432

433+
@patch('fastapi.BackgroundTasks.add_task')
434+
@patch('app.callbooker.google.AdminGoogleCalendar._create_resource')
435+
async def test_sales_call_with_bdr_person_resolves_tc2_admin_id(
436+
self, mock_gcal_builder, mock_add_task, client, db, test_pipeline, test_stage, test_config
437+
):
438+
"""Test booking sales call when tc2_admin_id is passed as bdr_person_id gets resolved to admin.id"""
439+
from sqlalchemy.exc import IntegrityError
440+
441+
mock_gcal_builder.side_effect = fake_gcal_builder()
442+
sales_person = db.create(Admin(first_name='Sales', last_name='Person', username='sales@example.com'))
443+
bdr_person = db.create(
444+
Admin(
445+
first_name='BDR',
446+
last_name='Person',
447+
username='bdr@example.com',
448+
tc2_admin_id=4253776,
449+
is_bdr_person=True,
450+
)
451+
)
452+
453+
meeting_data = CB_MEETING_DATA.copy()
454+
meeting_data['bdr_person_id'] = 4253776
455+
456+
original_commit = db.commit
457+
commit_count = [0]
458+
459+
def mock_commit():
460+
commit_count[0] += 1
461+
if commit_count[0] == 1:
462+
raise IntegrityError('', '', '', '')
463+
return original_commit()
464+
465+
db.commit = mock_commit
466+
467+
try:
468+
r = client.post(
469+
client.app.url_path_for('book-sales-call'), json={'admin_id': sales_person.id, **meeting_data}
470+
)
471+
472+
assert r.status_code == 200
473+
474+
company = db.exec(select(Company)).first()
475+
assert company.bdr_person_id == bdr_person.id
476+
finally:
477+
db.commit = original_commit
478+
433479
@patch('fastapi.BackgroundTasks.add_task')
434480
@patch('app.callbooker.google.AdminGoogleCalendar._create_resource')
435481
async def test_sales_call_uses_pipeline_dft_entry_stage(

0 commit comments

Comments
 (0)