2024-10-17 12:13:24 -07:00
|
|
|
from sqlalchemy import and_, select, update
|
2024-10-17 10:02:26 -07:00
|
|
|
|
2017-08-03 14:05:13 +01:00
|
|
|
from app import db
|
2021-04-14 07:11:01 +01:00
|
|
|
from app.dao.dao_utils import autocommit
|
2017-08-03 14:05:13 +01:00
|
|
|
from app.models import InboundNumber
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_get_inbound_numbers():
|
2024-10-17 10:02:26 -07:00
|
|
|
stmt = select(InboundNumber).order_by(InboundNumber.updated_at)
|
2024-10-17 11:40:04 -07:00
|
|
|
return db.session.execute(stmt).scalars().all()
|
2017-08-03 14:05:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_get_available_inbound_numbers():
|
2024-12-20 08:09:19 -08:00
|
|
|
stmt = select(InboundNumber).where(
|
2023-08-29 14:54:30 -07:00
|
|
|
InboundNumber.active, InboundNumber.service_id.is_(None)
|
2024-10-17 10:02:26 -07:00
|
|
|
)
|
2024-10-17 11:40:04 -07:00
|
|
|
return db.session.execute(stmt).scalars().all()
|
2017-08-03 14:05:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_get_inbound_number_for_service(service_id):
|
2024-12-20 08:09:19 -08:00
|
|
|
stmt = select(InboundNumber).where(InboundNumber.service_id == service_id)
|
2024-10-17 11:48:20 -07:00
|
|
|
return db.session.execute(stmt).scalars().first()
|
2017-08-03 14:05:13 +01:00
|
|
|
|
|
|
|
|
|
2017-08-10 17:51:47 +01:00
|
|
|
def dao_get_inbound_number(inbound_number_id):
|
2024-12-20 08:09:19 -08:00
|
|
|
stmt = select(InboundNumber).where(InboundNumber.id == inbound_number_id)
|
2024-10-17 11:48:20 -07:00
|
|
|
return db.session.execute(stmt).scalars().first()
|
2017-08-10 17:51:47 +01:00
|
|
|
|
|
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2017-08-04 16:05:03 +01:00
|
|
|
def dao_set_inbound_number_to_service(service_id, inbound_number):
|
|
|
|
|
inbound_number.service_id = service_id
|
|
|
|
|
db.session.add(inbound_number)
|
2017-08-04 19:09:05 +01:00
|
|
|
|
|
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2017-08-14 18:20:25 +01:00
|
|
|
def dao_set_inbound_number_active_flag(service_id, active):
|
2024-12-20 08:09:19 -08:00
|
|
|
stmt = select(InboundNumber).where(InboundNumber.service_id == service_id)
|
2024-10-17 12:03:23 -07:00
|
|
|
inbound_number = db.session.execute(stmt).scalars().first()
|
2017-08-04 19:09:05 +01:00
|
|
|
inbound_number.active = active
|
|
|
|
|
|
|
|
|
|
db.session.add(inbound_number)
|
2017-09-21 15:18:52 +01:00
|
|
|
|
|
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2017-09-21 15:18:52 +01:00
|
|
|
def dao_allocate_number_for_service(service_id, inbound_number_id):
|
2024-10-17 12:13:24 -07:00
|
|
|
stmt = (
|
|
|
|
|
update(InboundNumber)
|
|
|
|
|
.where(
|
|
|
|
|
and_(
|
|
|
|
|
InboundNumber.id == inbound_number_id, # noqa
|
|
|
|
|
InboundNumber.active == True, # noqa
|
|
|
|
|
InboundNumber.service_id == None, # noqa
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.values({"service_id": service_id})
|
|
|
|
|
)
|
|
|
|
|
result = db.session.execute(stmt)
|
|
|
|
|
if result.rowcount == 0:
|
2017-09-21 15:18:52 +01:00
|
|
|
raise Exception("Inbound number: {} is not available".format(inbound_number_id))
|
2024-10-17 12:13:24 -07:00
|
|
|
return db.session.get(InboundNumber, inbound_number_id)
|