use chrono::{DateTime, Utc};
use keepsake::{RelationDefinition, RelationId, RelationSpec};
use uuid::Uuid;
use super::{
KeepsakeRepository, NoopRelationCache, RelationCache, RepositoryResult, TimedExpiryCandidate,
};
#[derive(Debug, Clone, Copy)]
pub struct TimedKeepsakeRepository<'repo, C = NoopRelationCache> {
pub(super) repository: &'repo KeepsakeRepository<C>,
pub(super) at: DateTime<Utc>,
}
impl<'repo, C> TimedKeepsakeRepository<'repo, C>
where
C: RelationCache,
{
#[must_use]
pub const fn repository(&self) -> &'repo KeepsakeRepository<C> {
self.repository
}
#[must_use]
pub const fn timestamp(&self) -> DateTime<Utc> {
self.at
}
pub async fn upsert_relation(
&self,
relation: &RelationDefinition,
) -> RepositoryResult<RelationDefinition> {
self.repository.upsert_relation(relation, self.at).await
}
pub async fn upsert_relation_spec<Spec>(&self) -> RepositoryResult<RelationDefinition>
where
Spec: RelationSpec,
{
self.repository.upsert_relation_spec::<Spec>(self.at).await
}
pub async fn set_relation_enabled(
&self,
relation_id: RelationId,
enabled: bool,
) -> RepositoryResult<bool> {
self.repository
.set_relation_enabled(relation_id, enabled, self.at)
.await
}
pub async fn due_timed_expiry(
&self,
limit: i64,
) -> RepositoryResult<Vec<TimedExpiryCandidate>> {
self.repository.due_timed_expiry(self.at, limit).await
}
pub async fn expire_due_timed(&self, limit: i64) -> RepositoryResult<u64> {
self.repository.expire_due_timed(self.at, limit).await
}
#[cfg(feature = "fulfillment-counters")]
pub async fn upsert_counter_projection(
&self,
keepsake_id: Uuid,
key: &str,
value: i64,
) -> RepositoryResult<()> {
self.repository
.upsert_counter_projection(keepsake_id, key, value, self.at)
.await
}
}