| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
use sqlx::PgPool; |
| 17 |
|
| 18 |
use super::validated_types::Cents; |
| 19 |
use super::{TransactionId, UserId}; |
| 20 |
use crate::error::Result; |
| 21 |
|
| 22 |
|
| 23 |
#[derive(Debug, sqlx::FromRow)] |
| 24 |
pub struct PlatformCredit { |
| 25 |
pub transaction_id: TransactionId, |
| 26 |
pub seller_id: UserId, |
| 27 |
pub amount_cents: Cents, |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
pub async fn claim_unsettled_credit(pool: &PgPool) -> Result<Option<PlatformCredit>> { |
| 36 |
let row = sqlx::query_as!( |
| 37 |
PlatformCredit, |
| 38 |
r#" |
| 39 |
UPDATE transactions |
| 40 |
SET platform_credit_claimed_at = NOW() |
| 41 |
WHERE id = ( |
| 42 |
SELECT id FROM transactions |
| 43 |
WHERE status = 'completed' |
| 44 |
AND platform_credit_cents > 0 |
| 45 |
AND platform_credit_settled_at IS NULL |
| 46 |
AND platform_credit_claimed_at IS NULL |
| 47 |
ORDER BY completed_at |
| 48 |
LIMIT 1 |
| 49 |
FOR UPDATE SKIP LOCKED |
| 50 |
) |
| 51 |
RETURNING id AS "transaction_id!: TransactionId", |
| 52 |
seller_id AS "seller_id!: UserId", |
| 53 |
platform_credit_cents AS "amount_cents!: Cents" |
| 54 |
"#, |
| 55 |
) |
| 56 |
.fetch_optional(pool) |
| 57 |
.await?; |
| 58 |
|
| 59 |
Ok(row) |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
pub async fn mark_settled( |
| 67 |
pool: &PgPool, |
| 68 |
transaction_id: TransactionId, |
| 69 |
transfer_id: &str, |
| 70 |
) -> Result<()> { |
| 71 |
sqlx::query( |
| 72 |
"UPDATE transactions SET platform_credit_settled_at = NOW(), platform_credit_transfer_id = $2 WHERE id = $1", |
| 73 |
) |
| 74 |
.bind(transaction_id) |
| 75 |
.bind(transfer_id) |
| 76 |
.execute(pool) |
| 77 |
.await?; |
| 78 |
Ok(()) |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
#[derive(Debug, sqlx::FromRow)] |
| 84 |
pub struct ReversibleCredit { |
| 85 |
pub transaction_id: TransactionId, |
| 86 |
pub amount_cents: Cents, |
| 87 |
pub transfer_id: String, |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
pub async fn get_reversible_credits(pool: &PgPool, limit: i64) -> Result<Vec<ReversibleCredit>> { |
| 95 |
let rows = sqlx::query_as::<_, ReversibleCredit>( |
| 96 |
r" |
| 97 |
SELECT id AS transaction_id, |
| 98 |
platform_credit_cents AS amount_cents, |
| 99 |
platform_credit_transfer_id AS transfer_id |
| 100 |
FROM transactions |
| 101 |
WHERE status = 'refunded' |
| 102 |
AND platform_credit_cents > 0 |
| 103 |
AND platform_credit_settled_at IS NOT NULL |
| 104 |
AND platform_credit_transfer_id IS NOT NULL |
| 105 |
AND platform_credit_reversed_at IS NULL |
| 106 |
ORDER BY completed_at |
| 107 |
LIMIT $1 |
| 108 |
", |
| 109 |
) |
| 110 |
.bind(limit) |
| 111 |
.fetch_all(pool) |
| 112 |
.await?; |
| 113 |
|
| 114 |
Ok(rows) |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
pub async fn mark_reversed(pool: &PgPool, transaction_id: TransactionId) -> Result<()> { |
| 120 |
sqlx::query("UPDATE transactions SET platform_credit_reversed_at = NOW() WHERE id = $1") |
| 121 |
.bind(transaction_id) |
| 122 |
.execute(pool) |
| 123 |
.await?; |
| 124 |
Ok(()) |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
pub async fn unclaim_credit(pool: &PgPool, transaction_id: TransactionId) -> Result<()> { |
| 132 |
sqlx::query!( |
| 133 |
"UPDATE transactions SET platform_credit_claimed_at = NULL WHERE id = $1", |
| 134 |
transaction_id as TransactionId, |
| 135 |
) |
| 136 |
.execute(pool) |
| 137 |
.await?; |
| 138 |
Ok(()) |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
pub const STALE_CREDIT_BATCH: i64 = 100; |
| 143 |
|
| 144 |
|
| 145 |
#[derive(Debug, sqlx::FromRow)] |
| 146 |
pub struct StaleCredit { |
| 147 |
pub transaction_id: TransactionId, |
| 148 |
pub seller_id: UserId, |
| 149 |
pub amount_cents: Cents, |
| 150 |
pub completed_at: Option<chrono::DateTime<chrono::Utc>>, |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
pub async fn get_stale_credits(pool: &PgPool, age: chrono::Duration) -> Result<Vec<StaleCredit>> { |
| 157 |
let cutoff = chrono::Utc::now() - age; |
| 158 |
|
| 159 |
let rows = sqlx::query_as::<_, StaleCredit>( |
| 160 |
r" |
| 161 |
SELECT id AS transaction_id, seller_id, platform_credit_cents AS amount_cents, completed_at |
| 162 |
FROM transactions |
| 163 |
WHERE status = 'completed' |
| 164 |
AND platform_credit_cents > 0 |
| 165 |
AND platform_credit_settled_at IS NULL |
| 166 |
AND platform_credit_claimed_at IS NOT NULL |
| 167 |
AND platform_credit_claimed_at < $1 |
| 168 |
AND platform_credit_escalated_at IS NULL |
| 169 |
ORDER BY completed_at |
| 170 |
LIMIT $2 |
| 171 |
", |
| 172 |
) |
| 173 |
.bind(cutoff) |
| 174 |
.bind(STALE_CREDIT_BATCH) |
| 175 |
.fetch_all(pool) |
| 176 |
.await?; |
| 177 |
|
| 178 |
Ok(rows) |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
pub async fn mark_escalated(pool: &PgPool, transaction_id: TransactionId) -> Result<()> { |
| 183 |
sqlx::query!( |
| 184 |
"UPDATE transactions SET platform_credit_escalated_at = NOW() WHERE id = $1", |
| 185 |
transaction_id as TransactionId, |
| 186 |
) |
| 187 |
.execute(pool) |
| 188 |
.await?; |
| 189 |
Ok(()) |
| 190 |
} |
| 191 |
|