| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
use super::super::{ |
| 9 |
ItemId, KeyCode, PgPool, ProjectId, PromoCodeId, Result, TransactionId, UserId, |
| 10 |
}; |
| 11 |
|
| 12 |
|
| 13 |
pub struct ClaimParams<'a> { |
| 14 |
pub buyer_id: UserId, |
| 15 |
pub item_id: ItemId, |
| 16 |
pub seller_id: UserId, |
| 17 |
pub item_title: &'a str, |
| 18 |
pub seller_username: &'a str, |
| 19 |
pub share_contact: bool, |
| 20 |
|
| 21 |
pub parent_transaction_id: Option<TransactionId>, |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
pub platform_credit_cents: i64, |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#[tracing::instrument(skip_all)] |
| 36 |
pub async fn claim_free_item<'e>( |
| 37 |
executor: impl sqlx::PgExecutor<'e>, |
| 38 |
params: &ClaimParams<'_>, |
| 39 |
) -> Result<bool> { |
| 40 |
let claim_id = format!("free-claim-{}-{}", params.buyer_id, params.item_id); |
| 41 |
let result = sqlx::query!( |
| 42 |
r#" |
| 43 |
INSERT INTO transactions (buyer_id, seller_id, item_id, amount_cents, platform_fee_cents, stripe_checkout_session_id, status, completed_at, item_title, seller_username, share_contact, parent_transaction_id, platform_credit_cents) |
| 44 |
VALUES ($1, $2, $3, 0, 0, $4, 'completed', NOW(), $5, $6, $7, $8, $9) |
| 45 |
ON CONFLICT (buyer_id, item_id) WHERE status = 'completed' AND item_id IS NOT NULL DO NOTHING |
| 46 |
"#, |
| 47 |
params.buyer_id as UserId, |
| 48 |
params.seller_id as UserId, |
| 49 |
params.item_id as ItemId, |
| 50 |
claim_id, |
| 51 |
params.item_title, |
| 52 |
params.seller_username, |
| 53 |
params.share_contact, |
| 54 |
params.parent_transaction_id as Option<TransactionId>, |
| 55 |
params.platform_credit_cents, |
| 56 |
) |
| 57 |
.execute(executor) |
| 58 |
.await?; |
| 59 |
|
| 60 |
Ok(result.rows_affected() > 0) |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
#[tracing::instrument(skip_all)] |
| 70 |
pub async fn claim_free_items_batch<'e>( |
| 71 |
executor: impl sqlx::PgExecutor<'e>, |
| 72 |
buyer_id: UserId, |
| 73 |
seller_id: UserId, |
| 74 |
seller_username: &str, |
| 75 |
parent_transaction_id: Option<TransactionId>, |
| 76 |
items: &[(ItemId, &str)], |
| 77 |
) -> Result<u64> { |
| 78 |
if items.is_empty() { |
| 79 |
return Ok(0); |
| 80 |
} |
| 81 |
let item_ids: Vec<ItemId> = items.iter().map(|(id, _)| *id).collect(); |
| 82 |
let item_titles: Vec<&str> = items.iter().map(|(_, title)| *title).collect(); |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
let result = sqlx::query( |
| 87 |
r" |
| 88 |
INSERT INTO transactions |
| 89 |
(buyer_id, seller_id, item_id, amount_cents, platform_fee_cents, |
| 90 |
stripe_checkout_session_id, status, completed_at, item_title, |
| 91 |
seller_username, share_contact, parent_transaction_id) |
| 92 |
SELECT |
| 93 |
$1, $2, t.item_id, 0, 0, |
| 94 |
'free-claim-' || $1::text || '-' || t.item_id::text, |
| 95 |
'completed', NOW(), t.item_title, $3, false, $4 |
| 96 |
FROM UNNEST($5::uuid[], $6::text[]) AS t(item_id, item_title) |
| 97 |
ON CONFLICT (buyer_id, item_id) WHERE status = 'completed' AND item_id IS NOT NULL DO NOTHING |
| 98 |
", |
| 99 |
) |
| 100 |
.bind(buyer_id) |
| 101 |
.bind(seller_id) |
| 102 |
.bind(seller_username) |
| 103 |
.bind(parent_transaction_id) |
| 104 |
.bind(&item_ids) |
| 105 |
.bind(&item_titles) |
| 106 |
.execute(executor) |
| 107 |
.await?; |
| 108 |
|
| 109 |
Ok(result.rows_affected()) |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
pub struct LicenseKeyParams<'a> { |
| 114 |
pub key_code: &'a KeyCode, |
| 115 |
pub max_activations: Option<i32>, |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
#[tracing::instrument(skip_all)] |
| 131 |
pub async fn claim_free_with_promo_code( |
| 132 |
pool: &PgPool, |
| 133 |
promo_code_id: PromoCodeId, |
| 134 |
params: &ClaimParams<'_>, |
| 135 |
license_key_params: Option<&LicenseKeyParams<'_>>, |
| 136 |
) -> Result<(bool, bool)> { |
| 137 |
let mut tx = pool.begin().await?; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
let claim_id = format!("free-claim-{}-{}", params.buyer_id, params.item_id); |
| 144 |
let result = sqlx::query!( |
| 145 |
r#" |
| 146 |
INSERT INTO transactions (buyer_id, seller_id, item_id, amount_cents, platform_fee_cents, stripe_checkout_session_id, status, completed_at, item_title, seller_username, share_contact, promo_code_id, platform_credit_cents) |
| 147 |
VALUES ($1, $2, $3, 0, 0, $4, 'completed', NOW(), $5, $6, $7, $8, $9) |
| 148 |
ON CONFLICT (buyer_id, item_id) WHERE status = 'completed' AND item_id IS NOT NULL DO NOTHING |
| 149 |
"#, |
| 150 |
params.buyer_id as UserId, |
| 151 |
params.seller_id as UserId, |
| 152 |
params.item_id as ItemId, |
| 153 |
claim_id, |
| 154 |
params.item_title, |
| 155 |
params.seller_username, |
| 156 |
params.share_contact, |
| 157 |
promo_code_id as PromoCodeId, |
| 158 |
params.platform_credit_cents, |
| 159 |
) |
| 160 |
.execute(&mut *tx) |
| 161 |
.await?; |
| 162 |
|
| 163 |
let claimed = result.rows_affected() > 0; |
| 164 |
|
| 165 |
if !claimed { |
| 166 |
tx.rollback().await?; |
| 167 |
return Ok((true, false)); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
let code_result = sqlx::query!( |
| 176 |
r#" |
| 177 |
UPDATE promo_codes SET use_count = use_count + 1 |
| 178 |
WHERE id = $1 |
| 179 |
AND (max_uses IS NULL OR use_count < max_uses) |
| 180 |
AND (starts_at IS NULL OR starts_at <= NOW()) |
| 181 |
AND (expires_at IS NULL OR expires_at > NOW()) |
| 182 |
"#, |
| 183 |
promo_code_id as PromoCodeId, |
| 184 |
) |
| 185 |
.execute(&mut *tx) |
| 186 |
.await?; |
| 187 |
|
| 188 |
if code_result.rows_affected() == 0 { |
| 189 |
tx.rollback().await?; |
| 190 |
return Ok((false, false)); |
| 191 |
} |
| 192 |
|
| 193 |
crate::db::items::increment_sales_count(&mut *tx, params.item_id).await?; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
if let Some(lk) = license_key_params { |
| 200 |
let attempt = sqlx::query!( |
| 201 |
r#" |
| 202 |
INSERT INTO license_keys (item_id, owner_id, transaction_id, key_code, max_activations) |
| 203 |
VALUES ($1, $2, NULL, $3, $4) |
| 204 |
"#, |
| 205 |
params.item_id as ItemId, |
| 206 |
params.buyer_id as UserId, |
| 207 |
lk.key_code as &KeyCode, |
| 208 |
lk.max_activations, |
| 209 |
) |
| 210 |
.execute(&mut *tx) |
| 211 |
.await; |
| 212 |
|
| 213 |
if let Err(sqlx::Error::Database(e)) = &attempt |
| 214 |
&& e.code().as_deref() == Some("23505") |
| 215 |
{ |
| 216 |
let retry_code = crate::helpers::generate_key_code(); |
| 217 |
tracing::warn!(item_id = %params.item_id, "license key 23505 collision; retrying once"); |
| 218 |
sqlx::query!( |
| 219 |
r#" |
| 220 |
INSERT INTO license_keys (item_id, owner_id, transaction_id, key_code, max_activations) |
| 221 |
VALUES ($1, $2, NULL, $3, $4) |
| 222 |
"#, |
| 223 |
params.item_id as ItemId, |
| 224 |
params.buyer_id as UserId, |
| 225 |
retry_code as KeyCode, |
| 226 |
lk.max_activations, |
| 227 |
) |
| 228 |
.execute(&mut *tx) |
| 229 |
.await?; |
| 230 |
} else { |
| 231 |
attempt?; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
tx.commit().await?; |
| 236 |
Ok((true, true)) |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
#[tracing::instrument(skip_all)] |
| 242 |
pub async fn remove_free_item_from_library( |
| 243 |
pool: &PgPool, |
| 244 |
user_id: UserId, |
| 245 |
item_id: ItemId, |
| 246 |
) -> Result<bool> { |
| 247 |
|
| 248 |
let row: Option<Option<crate::db::PromoCodeId>> = sqlx::query_scalar!( |
| 249 |
r#" |
| 250 |
DELETE FROM transactions |
| 251 |
WHERE buyer_id = $1 AND item_id = $2 AND amount_cents = 0 AND status = 'completed' |
| 252 |
RETURNING promo_code_id AS "promo_code_id: crate::db::PromoCodeId" |
| 253 |
"#, |
| 254 |
user_id as UserId, |
| 255 |
item_id as ItemId, |
| 256 |
) |
| 257 |
.fetch_optional(pool) |
| 258 |
.await?; |
| 259 |
|
| 260 |
let deleted = row.is_some(); |
| 261 |
|
| 262 |
if let Some(Some(pc_id)) = row { |
| 263 |
crate::db::promo_codes::release_use_count(pool, pc_id) |
| 264 |
.await |
| 265 |
.ok(); |
| 266 |
} |
| 267 |
|
| 268 |
Ok(deleted) |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
#[tracing::instrument(skip_all)] |
| 280 |
pub async fn claim_free_project( |
| 281 |
pool: &PgPool, |
| 282 |
buyer_id: UserId, |
| 283 |
seller_id: UserId, |
| 284 |
project_id: ProjectId, |
| 285 |
item_title: &str, |
| 286 |
seller_username: &str, |
| 287 |
share_contact: bool, |
| 288 |
) -> Result<bool> { |
| 289 |
let result = sqlx::query!( |
| 290 |
r#" |
| 291 |
INSERT INTO transactions (buyer_id, seller_id, project_id, amount_cents, platform_fee_cents, |
| 292 |
status, completed_at, item_title, seller_username, share_contact) |
| 293 |
VALUES ($1, $2, $3, 0, 0, 'completed', NOW(), $4, $5, $6) |
| 294 |
ON CONFLICT (buyer_id, project_id) WHERE status = 'completed' AND project_id IS NOT NULL DO NOTHING |
| 295 |
"#, |
| 296 |
buyer_id as UserId, |
| 297 |
seller_id as UserId, |
| 298 |
project_id as ProjectId, |
| 299 |
item_title, |
| 300 |
seller_username, |
| 301 |
share_contact, |
| 302 |
) |
| 303 |
.execute(pool) |
| 304 |
.await?; |
| 305 |
|
| 306 |
Ok(result.rows_affected() > 0) |
| 307 |
} |
| 308 |
|