diff --git a/tcsocket/app/views/appointments.py b/tcsocket/app/views/appointments.py index 324634f..651eb0c 100644 --- a/tcsocket/app/views/appointments.py +++ b/tcsocket/app/views/appointments.py @@ -153,8 +153,10 @@ async def appointment_webhook_delete(request): async def appointment_webhook_clear(request): conn = await request['conn_manager'].get_connection() - v = await conn.execute(sa_appointments.delete().where(ser_c.company == request['company'].id)) - r = await conn.execute(sa_services.delete().where(ser_c.company == request['company'].id)) + services = await conn.execute(select([ser_c.id]).where(ser_c.company == request['company'].id)) + ids = [s[0] async for s in services] + v = await conn.execute(sa_appointments.delete().where(apt_c.service.in_(ids))) + r = await conn.execute(sa_services.delete().where(ser_c.id.in_(ids))) return json_response(request, status='success' if r.rowcount or v.rowcount else 'appointments not found') diff --git a/tcsocket/requirements.txt b/tcsocket/requirements.txt index b7d67ef..3fedd01 100644 --- a/tcsocket/requirements.txt +++ b/tcsocket/requirements.txt @@ -2,6 +2,7 @@ SQLAlchemy==1.3.23 aiodns==2.0.0 aiohttp==3.7.4.post0 aiopg==1.1.0 +aioredis==1.3.1 arq==0.21 boto3==1.17.62 cchardet==2.1.7 diff --git a/tests/test_appointments_set.py b/tests/test_appointments_set.py index 371ce98..20f1779 100644 --- a/tests/test_appointments_set.py +++ b/tests/test_appointments_set.py @@ -174,6 +174,25 @@ async def test_delete_old_appointments(db_conn, company, settings): async def test_clear_apts(cli, db_conn, company): + company2 = await create_company(db_conn, 'compan2_public', 'compan2_private', name='company2') + await db_conn.execute( + sa_services.insert().values( + **dict( + id=2, + company=company2.id, + name='testing service', + extra_attributes=[ + { + 'name': 'Foobar', + 'type': 'text_short', + 'machine_name': 'foobar', + 'value': 'this is the value of foobar', + } + ], + colour='#abc', + ) + ) + ) await create_appointment(db_conn, company, appointment_extra={'id': 1}) for i in range(10): await create_appointment( @@ -187,16 +206,29 @@ async def test_clear_apts(cli, db_conn, company): ), ) - assert 11 == await count(db_conn, sa_appointments) - assert 1 == await count(db_conn, sa_services) + for i in range(11, 21): + await create_appointment( + db_conn, + company2, + create_service=False, + appointment_extra=dict( + id=i + 2, + start=datetime(2032, 1, 1, 12, 0, 0) + timedelta(days=i + 1), + finish=datetime(2032, 1, 1, 13, 0, 0) + timedelta(days=i + 1), + ), + service_extra=dict(id=2), + ) + + assert 21 == await count(db_conn, sa_appointments) + assert 2 == await count(db_conn, sa_services) url = cli.server.app.router['webhook-appointment-clear'].url_for(company='thepublickey') r = await signed_request(cli, url, method_='DELETE') assert r.status == 200 assert {'status': 'success'} == await r.json() - assert 0 == await count(db_conn, sa_appointments) - assert 0 == await count(db_conn, sa_services) + assert 10 == await count(db_conn, sa_appointments) + assert 1 == await count(db_conn, sa_services) async def test_mass_apts(cli, db_conn, company):