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