Skip to main content

stackless_core/state/
stripe.rs

1//! Shared Stripe project anchors and immutable per-birth bindings.
2
3use super::row::Row;
4use super::{StateError, Store};
5
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct StripeProjectRecord {
8    pub scope: String,
9    pub context_id: String,
10    pub resource_name: String,
11    pub project_id: Option<String>,
12    pub creation_started: bool,
13}
14
15impl StripeProjectRecord {
16    fn from_row(row: &Row) -> Result<Self, StateError> {
17        Ok(Self {
18            scope: row.get_string(0)?,
19            context_id: row.get_string(1)?,
20            resource_name: row.get_string(2)?,
21            project_id: row.get_opt_string(3)?,
22            creation_started: row.get_i64(4)? != 0,
23        })
24    }
25}
26
27impl Store {
28    pub fn stripe_project(&self, scope: &str) -> Result<Option<StripeProjectRecord>, StateError> {
29        self.query_row("SELECT scope, context_id, resource_name, project_id, creation_started FROM stripe_projects WHERE scope = ?1", &[scope.into()], StripeProjectRecord::from_row)
30    }
31
32    pub fn instance_stripe_project(
33        &self,
34        owner_id: &str,
35    ) -> Result<Option<StripeProjectRecord>, StateError> {
36        self.query_row("SELECT p.scope, p.context_id, p.resource_name, p.project_id, p.creation_started FROM stripe_projects p JOIN instance_stripe_projects b ON b.scope = p.scope WHERE b.owner_id = ?1", &[owner_id.into()], StripeProjectRecord::from_row)
37    }
38
39    /// The shared project intent and the instance binding precede CLI effects.
40    pub fn bind_stripe_project(
41        &self,
42        owner_id: &str,
43        scope: &str,
44        project_id: Option<&str>,
45    ) -> Result<StripeProjectRecord, StateError> {
46        let id = uuid::Uuid::new_v4().simple().to_string();
47        self.execute("INSERT INTO stripe_projects (scope, context_id, resource_name, project_id, created_at) VALUES (?1, ?2, ?3, ?4, ?5) ON CONFLICT(scope) DO NOTHING",
48            &[scope.into(), id.clone().into(), format!("stackless-{id}").into(), project_id.map_or(super::value::Value::Null, Into::into), Self::now().into()])?;
49        self.execute("INSERT INTO instance_stripe_projects (owner_id, scope) SELECT ?1, ?2 WHERE EXISTS (SELECT 1 FROM instances WHERE instance_id = ?1 AND status = 'active') ON CONFLICT(owner_id) DO NOTHING", &[owner_id.into(), scope.into()])?;
50        let bound = self.instance_stripe_project(owner_id)?.ok_or_else(|| {
51            StateError::ResourceInvariant {
52                detail: "Stripe context requires an active instance identity".into(),
53            }
54        })?;
55        if bound.scope != scope
56            || project_id.is_some_and(|id| bound.project_id.as_deref() != Some(id))
57        {
58            return Err(StateError::ResourceInvariant { detail: "an instance's Stripe project binding cannot change; destroy it before selecting another project".into() });
59        }
60        Ok(bound)
61    }
62
63    /// A lost create response must be resolved by exact-name inventory lookup.
64    /// It never authorizes a second create request.
65    pub fn start_stripe_project_creation(&self, scope: &str) -> Result<bool, StateError> {
66        Ok(self.execute("UPDATE stripe_projects SET creation_started = 1 WHERE scope = ?1 AND project_id IS NULL AND creation_started = 0", &[scope.into()])? == 1)
67    }
68
69    pub fn stripe_project_created(&self, scope: &str, project_id: &str) -> Result<(), StateError> {
70        if project_id.is_empty() || self.execute("UPDATE stripe_projects SET project_id = ?2 WHERE scope = ?1 AND (project_id IS NULL OR project_id = ?2)", &[scope.into(), project_id.into()])? != 1 {
71            return Err(StateError::ResourceInvariant { detail: "Stripe project identity is missing or conflicts with its persisted anchor".into() });
72        }
73        Ok(())
74    }
75}