88 lines
3.0 KiB
Python
88 lines
3.0 KiB
Python
import pytz
|
|
import datetime
|
|
from constance import config
|
|
|
|
from appa.celery import app
|
|
from appa.models import *
|
|
from appa.call_api import api as call_api
|
|
from appa.medicine_api import api as medicine_api
|
|
|
|
|
|
@app.task(bind=True, acks_late=True)
|
|
def send_call_request_task(self, ids=None):
|
|
call_requests = CallRequest.objects.filter(
|
|
id__in=ids,
|
|
is_active=True
|
|
).exclude(
|
|
status__in=[
|
|
CallRequest.Status.APPROVED,
|
|
CallRequest.Status.CANCELED
|
|
]
|
|
)
|
|
|
|
for call_request in call_requests:
|
|
call_api.add_call_request(call_request)
|
|
|
|
|
|
@app.task(bind=True, acks_late=True)
|
|
def send_daily_call_request_task(self, ids=None):
|
|
tomorrow = datetime.date.today() + datetime.timedelta(days=1)
|
|
call_requests = CallRequest.objects.filter(
|
|
date=tomorrow,
|
|
status=CallRequest.Status.PENDING,
|
|
is_active=True
|
|
)
|
|
|
|
for call_request in call_requests:
|
|
call_api.add_call_request(call_request)
|
|
|
|
|
|
@app.task(bind=True, acks_late=True)
|
|
def update_call_requests_task(self):
|
|
call_lpu = {call_lpu.lpu_id: call_lpu for call_lpu in CallLPU.objects.all()}
|
|
call_specialities = {medical_speciality.speciality_id: medical_speciality
|
|
for medical_speciality in CallMedicalSpeciality.objects.all()}
|
|
|
|
now = datetime.datetime.now().astimezone(pytz.timezone('Asia/Vladivostok'))
|
|
bookings = medicine_api.get_bookings(now.date() + datetime.timedelta(days=1))
|
|
for booking in bookings:
|
|
lpu = None
|
|
speciality = None
|
|
if booking['lpu']['code'] in call_lpu:
|
|
lpu = call_lpu[booking['lpu']['code']]
|
|
|
|
if booking['user'] is None:
|
|
continue
|
|
|
|
if booking['user']['doctor']['speciality'] in call_specialities:
|
|
speciality = call_specialities[booking['user']['doctor']['speciality']]
|
|
|
|
if lpu is None or speciality is None:
|
|
continue
|
|
|
|
CallRequest.objects.update_or_create(
|
|
defaults=dict(
|
|
date=datetime.datetime.strptime(booking['date_start'].split('T')[0], '%Y-%m-%d').date(),
|
|
booking_date=booking['date_start'],
|
|
patient_id=booking['patient']['id'],
|
|
patient_first_name=booking['patient']['first_name'],
|
|
patient_last_name=booking['patient']['last_name'],
|
|
patient_middle_name=booking['patient']['middle_name'],
|
|
patient_phone='7' + booking['patient']['phone_mobile'],
|
|
doctor_name=booking['user']['display'],
|
|
doctor_speciality=booking['user']['doctor']['speciality_display'],
|
|
service_name=speciality.name,
|
|
organization=lpu.name,
|
|
address=lpu.address,
|
|
),
|
|
booking_id=booking['id'],
|
|
)
|
|
|
|
|
|
@app.task(bind=True, acks_late=True)
|
|
def check_call_requests_task(self):
|
|
for call_request in CallRequest.objects.filter(
|
|
request_status=CallRequest.RequestStatus.SENT
|
|
):
|
|
call_api.get_record(call_request)
|