Files
notifications-api/app/dao/service_data_retention_dao.py

72 lines
2.2 KiB
Python
Raw Permalink Normal View History

2024-10-30 12:17:59 -07:00
from sqlalchemy import select, update
from app import db
from app.dao.dao_utils import autocommit
from app.models import ServiceDataRetention
2024-05-23 13:59:51 -07:00
from app.utils import utc_now
def fetch_service_data_retention_by_id(service_id, data_retention_id):
2024-10-30 12:17:59 -07:00
stmt = select(ServiceDataRetention).where(
ServiceDataRetention.service_id == service_id,
ServiceDataRetention.id == data_retention_id,
)
return db.session.execute(stmt).scalars().first()
def fetch_service_data_retention(service_id):
2024-10-30 12:17:59 -07:00
stmt = (
select(ServiceDataRetention)
.where(ServiceDataRetention.service_id == service_id)
2023-08-29 14:54:30 -07:00
.order_by(
# in the order that models.notification_types are created (email, sms, letter)
ServiceDataRetention.notification_type
)
)
2024-10-30 12:17:59 -07:00
return db.session.execute(stmt).scalars().all()
def fetch_service_data_retention_by_notification_type(service_id, notification_type):
2024-10-30 12:17:59 -07:00
stmt = select(ServiceDataRetention).where(
ServiceDataRetention.service_id == service_id,
ServiceDataRetention.notification_type == notification_type,
)
return db.session.execute(stmt).scalars().first()
@autocommit
def insert_service_data_retention(service_id, notification_type, days_of_retention):
2023-08-29 14:54:30 -07:00
new_data_retention = ServiceDataRetention(
service_id=service_id,
notification_type=notification_type,
days_of_retention=days_of_retention,
)
db.session.add(new_data_retention)
return new_data_retention
@autocommit
2023-08-29 14:54:30 -07:00
def update_service_data_retention(
service_data_retention_id, service_id, days_of_retention
):
2024-10-30 12:17:59 -07:00
stmt = (
update(ServiceDataRetention)
.where(
ServiceDataRetention.id == service_data_retention_id,
ServiceDataRetention.service_id == service_id,
)
.values({"days_of_retention": days_of_retention, "updated_at": utc_now()})
)
result = db.session.execute(stmt)
return result.rowcount
2023-08-29 14:54:30 -07:00
def fetch_service_data_retention_for_all_services_by_notification_type(
notification_type,
):
2024-10-30 12:17:59 -07:00
stmt = select(ServiceDataRetention).where(
2023-08-29 14:54:30 -07:00
ServiceDataRetention.notification_type == notification_type
2024-10-30 12:17:59 -07:00
)
return db.session.execute(stmt).scalars().all()