1use super::*;
2
3#[derive(Debug)]
4pub struct CreateProcessedStripeEventParams {
5 pub stripe_event_id: String,
6 pub stripe_event_type: String,
7 pub stripe_event_created_timestamp: i64,
8}
9
10impl Database {
11 /// Creates a new processed Stripe event.
12 pub async fn create_processed_stripe_event(
13 &self,
14 params: &CreateProcessedStripeEventParams,
15 ) -> Result<()> {
16 self.transaction(|tx| async move {
17 processed_stripe_event::Entity::insert(processed_stripe_event::ActiveModel {
18 stripe_event_id: ActiveValue::set(params.stripe_event_id.clone()),
19 stripe_event_type: ActiveValue::set(params.stripe_event_type.clone()),
20 stripe_event_created_timestamp: ActiveValue::set(
21 params.stripe_event_created_timestamp,
22 ),
23 ..Default::default()
24 })
25 .exec_without_returning(&*tx)
26 .await?;
27
28 Ok(())
29 })
30 .await
31 }
32
33 /// Returns the processed Stripe event with the specified event ID.
34 pub async fn get_processed_stripe_event_by_event_id(
35 &self,
36 event_id: &str,
37 ) -> Result<Option<processed_stripe_event::Model>> {
38 self.transaction(|tx| async move {
39 Ok(processed_stripe_event::Entity::find_by_id(event_id)
40 .one(&*tx)
41 .await?)
42 })
43 .await
44 }
45
46 /// Returns the processed Stripe events with the specified event IDs.
47 pub async fn get_processed_stripe_events_by_event_ids(
48 &self,
49 event_ids: &[&str],
50 ) -> Result<Vec<processed_stripe_event::Model>> {
51 self.transaction(|tx| async move {
52 Ok(processed_stripe_event::Entity::find()
53 .filter(
54 processed_stripe_event::Column::StripeEventId.is_in(event_ids.iter().copied()),
55 )
56 .all(&*tx)
57 .await?)
58 })
59 .await
60 }
61
62 /// Returns whether the Stripe event with the specified ID has already been processed.
63 pub async fn already_processed_stripe_event(&self, event_id: &str) -> Result<bool> {
64 Ok(self
65 .get_processed_stripe_event_by_event_id(event_id)
66 .await?
67 .is_some())
68 }
69}