| 1 |
use super::{CreatorTier, DbCreatorSubscription, PgPool, Result, UserId}; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#[tracing::instrument(skip_all)] |
| 11 |
pub async fn create_creator_subscription<'e>( |
| 12 |
executor: impl sqlx::PgExecutor<'e>, |
| 13 |
user_id: UserId, |
| 14 |
stripe_subscription_id: &str, |
| 15 |
stripe_customer_id: &str, |
| 16 |
tier: CreatorTier, |
| 17 |
) -> Result<Option<DbCreatorSubscription>> { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
let sub = sqlx::query_as::<_, DbCreatorSubscription>( |
| 22 |
r" |
| 23 |
INSERT INTO creator_subscriptions (user_id, stripe_subscription_id, stripe_customer_id, tier) |
| 24 |
VALUES ($1, $2, $3, $4) |
| 25 |
ON CONFLICT (user_id) DO UPDATE |
| 26 |
SET stripe_subscription_id = EXCLUDED.stripe_subscription_id, |
| 27 |
stripe_customer_id = EXCLUDED.stripe_customer_id, |
| 28 |
tier = EXCLUDED.tier, |
| 29 |
status = 'active', |
| 30 |
canceled_at = NULL, |
| 31 |
grace_enforced_at = NULL |
| 32 |
WHERE creator_subscriptions.stripe_subscription_id != EXCLUDED.stripe_subscription_id |
| 33 |
OR creator_subscriptions.status != 'active' |
| 34 |
RETURNING * |
| 35 |
", |
| 36 |
) |
| 37 |
.bind(user_id) |
| 38 |
.bind(stripe_subscription_id) |
| 39 |
.bind(stripe_customer_id) |
| 40 |
.bind(tier) |
| 41 |
.fetch_optional(executor) |
| 42 |
.await?; |
| 43 |
|
| 44 |
Ok(sub) |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
#[tracing::instrument(skip_all)] |
| 49 |
pub async fn get_creator_sub_by_stripe_id( |
| 50 |
pool: &PgPool, |
| 51 |
stripe_subscription_id: &str, |
| 52 |
) -> Result<Option<DbCreatorSubscription>> { |
| 53 |
let sub = sqlx::query_as::<_, DbCreatorSubscription>( |
| 54 |
"SELECT * FROM creator_subscriptions WHERE stripe_subscription_id = $1", |
| 55 |
) |
| 56 |
.bind(stripe_subscription_id) |
| 57 |
.fetch_optional(pool) |
| 58 |
.await?; |
| 59 |
|
| 60 |
Ok(sub) |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
#[tracing::instrument(skip_all)] |
| 65 |
pub async fn get_creator_sub_by_user( |
| 66 |
pool: &PgPool, |
| 67 |
user_id: UserId, |
| 68 |
) -> Result<Option<DbCreatorSubscription>> { |
| 69 |
let sub = sqlx::query_as::<_, DbCreatorSubscription>( |
| 70 |
"SELECT * FROM creator_subscriptions WHERE user_id = $1", |
| 71 |
) |
| 72 |
.bind(user_id) |
| 73 |
.fetch_optional(pool) |
| 74 |
.await?; |
| 75 |
|
| 76 |
Ok(sub) |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
#[tracing::instrument(skip_all)] |
| 81 |
pub async fn get_active_creator_tier( |
| 82 |
pool: &PgPool, |
| 83 |
user_id: UserId, |
| 84 |
) -> Result<Option<CreatorTier>> { |
| 85 |
let tier = sqlx::query_scalar::<_, String>( |
| 86 |
"SELECT tier FROM creator_subscriptions WHERE user_id = $1 AND status = 'active'", |
| 87 |
) |
| 88 |
.bind(user_id) |
| 89 |
.fetch_optional(pool) |
| 90 |
.await?; |
| 91 |
|
| 92 |
match tier { |
| 93 |
None => Ok(None), |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
Some(t) => match t.parse::<CreatorTier>() { |
| 100 |
Ok(parsed) => Ok(Some(parsed)), |
| 101 |
Err(_) => { |
| 102 |
tracing::error!( |
| 103 |
user_id = %user_id, tier = %t, |
| 104 |
"active creator subscription has an unrecognized tier string (enum drift)" |
| 105 |
); |
| 106 |
Err(crate::error::AppError::Internal(anyhow::anyhow!( |
| 107 |
"unrecognized creator tier '{t}' for user {user_id}" |
| 108 |
))) |
| 109 |
} |
| 110 |
}, |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
crate::db::subscription_writer::define_stripe_subscription_writer!( |
| 120 |
apply_stripe_update, |
| 121 |
"creator_subscriptions", |
| 122 |
DbCreatorSubscription |
| 123 |
); |
| 124 |
|
| 125 |
|
| 126 |
#[tracing::instrument(skip_all)] |
| 127 |
pub async fn cancel_creator_sub( |
| 128 |
pool: &PgPool, |
| 129 |
stripe_subscription_id: &str, |
| 130 |
) -> Result<Option<DbCreatorSubscription>> { |
| 131 |
let sub = sqlx::query_as::<_, DbCreatorSubscription>( |
| 132 |
r" |
| 133 |
UPDATE creator_subscriptions |
| 134 |
SET status = 'canceled', canceled_at = COALESCE(canceled_at, NOW()) |
| 135 |
WHERE stripe_subscription_id = $1 |
| 136 |
RETURNING * |
| 137 |
", |
| 138 |
) |
| 139 |
.bind(stripe_subscription_id) |
| 140 |
.fetch_optional(pool) |
| 141 |
.await?; |
| 142 |
|
| 143 |
Ok(sub) |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
#[tracing::instrument(skip_all)] |
| 149 |
pub async fn sync_user_creator_tier(pool: &PgPool, user_id: UserId) -> Result<()> { |
| 150 |
sqlx::query( |
| 151 |
r" |
| 152 |
UPDATE users SET creator_tier = ( |
| 153 |
SELECT tier FROM creator_subscriptions |
| 154 |
WHERE user_id = $1 AND status = 'active' |
| 155 |
) |
| 156 |
WHERE id = $1 |
| 157 |
", |
| 158 |
) |
| 159 |
.bind(user_id) |
| 160 |
.execute(pool) |
| 161 |
.await?; |
| 162 |
|
| 163 |
Ok(()) |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
#[tracing::instrument(skip_all)] |
| 169 |
pub async fn get_expired_grace_creators(pool: &PgPool) -> Result<Vec<UserId>> { |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
let ids: Vec<UserId> = sqlx::query_scalar( |
| 176 |
r" |
| 177 |
SELECT user_id FROM creator_subscriptions |
| 178 |
WHERE status = 'canceled' |
| 179 |
AND canceled_at IS NOT NULL |
| 180 |
AND canceled_at < NOW() - INTERVAL '30 days' |
| 181 |
AND grace_enforced_at IS NULL |
| 182 |
ORDER BY canceled_at ASC |
| 183 |
LIMIT 200 |
| 184 |
", |
| 185 |
) |
| 186 |
.fetch_all(pool) |
| 187 |
.await?; |
| 188 |
|
| 189 |
Ok(ids) |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
#[tracing::instrument(skip_all)] |
| 196 |
pub async fn mark_grace_enforced_batch(pool: &PgPool, user_ids: &[UserId]) -> Result<()> { |
| 197 |
if user_ids.is_empty() { |
| 198 |
return Ok(()); |
| 199 |
} |
| 200 |
sqlx::query( |
| 201 |
"UPDATE creator_subscriptions SET grace_enforced_at = NOW() WHERE user_id = ANY($1)", |
| 202 |
) |
| 203 |
.bind(user_ids) |
| 204 |
.execute(pool) |
| 205 |
.await?; |
| 206 |
|
| 207 |
Ok(()) |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
#[tracing::instrument(skip_all)] |
| 217 |
pub async fn count_active_paying(pool: &PgPool) -> Result<i64> { |
| 218 |
let count: (i64,) = |
| 219 |
sqlx::query_as("SELECT COUNT(*) FROM creator_subscriptions WHERE status = 'active'") |
| 220 |
.fetch_one(pool) |
| 221 |
.await?; |
| 222 |
Ok(count.0) |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
#[tracing::instrument(skip_all)] |
| 232 |
pub async fn count_trialing_or_grace(pool: &PgPool) -> Result<i64> { |
| 233 |
let count: (i64,) = sqlx::query_as( |
| 234 |
r" |
| 235 |
SELECT COUNT(*) FROM creator_subscriptions |
| 236 |
WHERE status = 'trialing' |
| 237 |
OR ( |
| 238 |
status = 'canceled' |
| 239 |
AND canceled_at IS NOT NULL |
| 240 |
AND canceled_at > NOW() - INTERVAL '30 days' |
| 241 |
AND grace_enforced_at IS NULL |
| 242 |
) |
| 243 |
", |
| 244 |
) |
| 245 |
.fetch_one(pool) |
| 246 |
.await?; |
| 247 |
Ok(count.0) |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
#[tracing::instrument(skip_all)] |
| 255 |
pub async fn is_in_grace_period(pool: &PgPool, user_id: UserId) -> Result<bool> { |
| 256 |
let in_grace: bool = sqlx::query_scalar( |
| 257 |
r" |
| 258 |
SELECT EXISTS( |
| 259 |
SELECT 1 FROM creator_subscriptions |
| 260 |
WHERE user_id = $1 |
| 261 |
AND status = 'canceled' |
| 262 |
AND canceled_at IS NOT NULL |
| 263 |
AND canceled_at > NOW() - INTERVAL '30 days' |
| 264 |
AND grace_enforced_at IS NULL |
| 265 |
) |
| 266 |
", |
| 267 |
) |
| 268 |
.bind(user_id) |
| 269 |
.fetch_one(pool) |
| 270 |
.await?; |
| 271 |
|
| 272 |
Ok(in_grace) |
| 273 |
} |
| 274 |
|