2018-06-07 16:01:41 +01:00
|
|
|
from datetime import timedelta
|
|
|
|
|
|
2018-07-02 17:19:23 +01:00
|
|
|
from flask import current_app
|
2024-10-18 14:31:40 -07:00
|
|
|
from sqlalchemy import desc, func, select
|
2018-06-07 16:01:41 +01:00
|
|
|
|
2018-06-04 17:29:58 +01:00
|
|
|
from app import db
|
2021-04-14 07:11:01 +01:00
|
|
|
from app.dao.dao_utils import autocommit
|
2024-10-21 07:18:52 -07:00
|
|
|
from app.dao.inbound_sms_dao import Pagination
|
2018-06-04 17:29:58 +01:00
|
|
|
from app.models import Complaint
|
2023-05-10 08:39:50 -07:00
|
|
|
from app.utils import get_midnight_in_utc
|
2018-06-04 17:29:58 +01:00
|
|
|
|
|
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2018-06-04 17:29:58 +01:00
|
|
|
def save_complaint(complaint):
|
|
|
|
|
db.session.add(complaint)
|
|
|
|
|
|
|
|
|
|
|
2018-07-02 17:19:23 +01:00
|
|
|
def fetch_paginated_complaints(page=1):
|
2024-10-21 07:18:52 -07:00
|
|
|
page_size = current_app.config["PAGE_SIZE"]
|
|
|
|
|
total_count = db.session.scalar(select(func.count()).select_from(Complaint))
|
|
|
|
|
offset = (page - 1) * page_size
|
2024-10-21 08:01:57 -07:00
|
|
|
stmt = (
|
|
|
|
|
select(Complaint)
|
|
|
|
|
.order_by(desc(Complaint.created_at))
|
|
|
|
|
.offset(offset)
|
|
|
|
|
.limit(page_size)
|
|
|
|
|
)
|
2024-10-21 07:37:12 -07:00
|
|
|
result = db.session.execute(stmt).scalars().all()
|
2024-10-21 07:18:52 -07:00
|
|
|
pagination = Pagination(result, page=page, per_page=page_size, total=total_count)
|
|
|
|
|
return pagination
|
2018-07-02 17:19:23 +01:00
|
|
|
|
|
|
|
|
|
2018-06-04 17:29:58 +01:00
|
|
|
def fetch_complaints_by_service(service_id):
|
2024-10-18 14:31:40 -07:00
|
|
|
stmt = (
|
|
|
|
|
select(Complaint)
|
2024-12-19 11:10:03 -08:00
|
|
|
.where(Complaint.service_id == service_id)
|
2023-08-29 14:54:30 -07:00
|
|
|
.order_by(desc(Complaint.created_at))
|
|
|
|
|
)
|
2024-10-18 14:31:40 -07:00
|
|
|
return db.session.execute(stmt).scalars().all()
|
2018-06-07 10:30:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_count_of_complaints(start_date, end_date):
|
2023-05-10 08:39:50 -07:00
|
|
|
start_date = get_midnight_in_utc(start_date)
|
|
|
|
|
end_date = get_midnight_in_utc(end_date + timedelta(days=1))
|
2018-06-07 16:01:41 +01:00
|
|
|
|
2024-10-18 14:31:40 -07:00
|
|
|
stmt = (
|
|
|
|
|
select(func.count())
|
|
|
|
|
.select_from(Complaint)
|
2024-12-20 08:09:19 -08:00
|
|
|
.where(Complaint.created_at >= start_date, Complaint.created_at < end_date)
|
2024-10-18 14:31:40 -07:00
|
|
|
)
|
|
|
|
|
return db.session.execute(stmt).scalar() or 0
|