Skip to content

Commit dbf5803

Browse files
authored
Merge pull request #376 from tutorcruncher/apt-clear-fix
Stop clearing all appointments
2 parents e5c0ccf + cc90239 commit dbf5803

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

tcsocket/app/views/appointments.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,10 @@ async def appointment_webhook_delete(request):
153153

154154
async def appointment_webhook_clear(request):
155155
conn = await request['conn_manager'].get_connection()
156-
v = await conn.execute(sa_appointments.delete().where(ser_c.company == request['company'].id))
157-
r = await conn.execute(sa_services.delete().where(ser_c.company == request['company'].id))
156+
services = await conn.execute(select([ser_c.id]).where(ser_c.company == request['company'].id))
157+
ids = [s[0] async for s in services]
158+
v = await conn.execute(sa_appointments.delete().where(apt_c.service.in_(ids)))
159+
r = await conn.execute(sa_services.delete().where(ser_c.id.in_(ids)))
158160
return json_response(request, status='success' if r.rowcount or v.rowcount else 'appointments not found')
159161

160162

tcsocket/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ SQLAlchemy==1.3.23
22
aiodns==2.0.0
33
aiohttp==3.7.4.post0
44
aiopg==1.1.0
5+
aioredis==1.3.1
56
arq==0.21
67
boto3==1.17.62
78
cchardet==2.1.7

tests/test_appointments_set.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,25 @@ async def test_delete_old_appointments(db_conn, company, settings):
174174

175175

176176
async def test_clear_apts(cli, db_conn, company):
177+
company2 = await create_company(db_conn, 'compan2_public', 'compan2_private', name='company2')
178+
await db_conn.execute(
179+
sa_services.insert().values(
180+
**dict(
181+
id=2,
182+
company=company2.id,
183+
name='testing service',
184+
extra_attributes=[
185+
{
186+
'name': 'Foobar',
187+
'type': 'text_short',
188+
'machine_name': 'foobar',
189+
'value': 'this is the value of foobar',
190+
}
191+
],
192+
colour='#abc',
193+
)
194+
)
195+
)
177196
await create_appointment(db_conn, company, appointment_extra={'id': 1})
178197
for i in range(10):
179198
await create_appointment(
@@ -187,16 +206,29 @@ async def test_clear_apts(cli, db_conn, company):
187206
),
188207
)
189208

190-
assert 11 == await count(db_conn, sa_appointments)
191-
assert 1 == await count(db_conn, sa_services)
209+
for i in range(11, 21):
210+
await create_appointment(
211+
db_conn,
212+
company2,
213+
create_service=False,
214+
appointment_extra=dict(
215+
id=i + 2,
216+
start=datetime(2032, 1, 1, 12, 0, 0) + timedelta(days=i + 1),
217+
finish=datetime(2032, 1, 1, 13, 0, 0) + timedelta(days=i + 1),
218+
),
219+
service_extra=dict(id=2),
220+
)
221+
222+
assert 21 == await count(db_conn, sa_appointments)
223+
assert 2 == await count(db_conn, sa_services)
192224

193225
url = cli.server.app.router['webhook-appointment-clear'].url_for(company='thepublickey')
194226
r = await signed_request(cli, url, method_='DELETE')
195227
assert r.status == 200
196228
assert {'status': 'success'} == await r.json()
197229

198-
assert 0 == await count(db_conn, sa_appointments)
199-
assert 0 == await count(db_conn, sa_services)
230+
assert 10 == await count(db_conn, sa_appointments)
231+
assert 1 == await count(db_conn, sa_services)
200232

201233

202234
async def test_mass_apts(cli, db_conn, company):

0 commit comments

Comments
 (0)