Files
ctms/backend/app/crud/acknowledgement.py
T
2026-05-28 10:56:28 +08:00

39 lines
1.1 KiB
Python

from __future__ import annotations
import uuid
from typing import Sequence
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.acknowledgement import Acknowledgement, AcknowledgementType
async def create(db: AsyncSession, ack: Acknowledgement, *, commit: bool = True) -> Acknowledgement:
db.add(ack)
if commit:
await db.commit()
await db.refresh(ack)
return ack
async def get_existing(
db: AsyncSession,
distribution_id: uuid.UUID,
user_id: uuid.UUID,
ack_type: AcknowledgementType,
) -> Acknowledgement | None:
result = await db.execute(
select(Acknowledgement).where(
Acknowledgement.distribution_id == distribution_id,
Acknowledgement.user_id == user_id,
Acknowledgement.ack_type == ack_type,
)
)
return result.scalar_one_or_none()
async def list_by_distribution(db: AsyncSession, distribution_id: uuid.UUID) -> Sequence[Acknowledgement]:
result = await db.execute(select(Acknowledgement).where(Acknowledgement.distribution_id == distribution_id))
return result.scalars().all()