feat: init
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
use crate::error::AppError;
|
||||
use crate::models::notifications::NotificationDelivery;
|
||||
use crate::service::NotificationService;
|
||||
use crate::session::Session;
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::util::clamp_limit_offset;
|
||||
|
||||
impl NotificationDelivery {
|
||||
pub async fn list_for_user(
|
||||
pool: &sqlx::PgPool,
|
||||
user_id: Uuid,
|
||||
limit: i64,
|
||||
offset: i64,
|
||||
) -> Result<Vec<Self>, AppError> {
|
||||
sqlx::query_as::<_, NotificationDelivery>(
|
||||
"SELECT id, notification_id, user_id, channel, destination, status, provider, \
|
||||
provider_message_id, attempts, last_error, scheduled_at, sent_at, delivered_at, failed_at, created_at, updated_at \
|
||||
FROM notification_delivery WHERE user_id = $1 \
|
||||
ORDER BY created_at DESC LIMIT $2 OFFSET $3",
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(limit)
|
||||
.bind(offset)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
|
||||
pub async fn list_for_notification(
|
||||
pool: &sqlx::PgPool,
|
||||
notification_id: Uuid,
|
||||
user_id: Uuid,
|
||||
limit: i64,
|
||||
offset: i64,
|
||||
) -> Result<Vec<Self>, AppError> {
|
||||
sqlx::query_as::<_, NotificationDelivery>(
|
||||
"SELECT d.id, d.notification_id, d.user_id, d.channel, d.destination, d.status, d.provider, \
|
||||
d.provider_message_id, d.attempts, d.last_error, d.scheduled_at, d.sent_at, d.delivered_at, d.failed_at, d.created_at, d.updated_at \
|
||||
FROM notification_delivery d \
|
||||
JOIN notification n ON d.notification_id = n.id \
|
||||
WHERE d.notification_id = $1 AND n.user_id = $2 \
|
||||
ORDER BY d.created_at DESC LIMIT $3 OFFSET $4",
|
||||
)
|
||||
.bind(notification_id)
|
||||
.bind(user_id)
|
||||
.bind(limit)
|
||||
.bind(offset)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
}
|
||||
|
||||
impl NotificationService {
|
||||
pub async fn list_deliveries(
|
||||
&self,
|
||||
session: &Session,
|
||||
limit: i64,
|
||||
offset: i64,
|
||||
) -> Result<Vec<NotificationDelivery>, AppError> {
|
||||
let user_id = session.user().ok_or(AppError::Unauthorized)?;
|
||||
let (limit, offset) = clamp_limit_offset(limit, offset);
|
||||
NotificationDelivery::list_for_user(self.ctx.db.reader(), user_id, limit, offset).await
|
||||
}
|
||||
|
||||
pub async fn list_deliveries_for_notification(
|
||||
&self,
|
||||
session: &Session,
|
||||
notification_id: Uuid,
|
||||
limit: i64,
|
||||
offset: i64,
|
||||
) -> Result<Vec<NotificationDelivery>, AppError> {
|
||||
let user_id = session.user().ok_or(AppError::Unauthorized)?;
|
||||
let (limit, offset) = clamp_limit_offset(limit, offset);
|
||||
NotificationDelivery::list_for_notification(
|
||||
self.ctx.db.reader(),
|
||||
notification_id,
|
||||
user_id,
|
||||
limit,
|
||||
offset,
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user