Automatically cancel servers with failed payments older than 30d (#3695)

This commit is contained in:
Emma Alexia
2025-05-25 15:36:11 -04:00
committed by GitHub
parent 2ffd7476aa
commit dc0d923cee
3 changed files with 139 additions and 5 deletions

View File

@@ -254,6 +254,27 @@ impl DBCharge {
.collect::<Result<Vec<_>, serde_json::Error>>()?)
}
pub async fn get_cancellable(
exec: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
) -> Result<Vec<DBCharge>, DatabaseError> {
let charge_type = ChargeType::Subscription.as_str();
let res = select_charges_with_predicate!(
r#"
WHERE
charge_type = $1 AND
status = 'failed' AND due < NOW() - INTERVAL '30 days'
"#,
charge_type
)
.fetch_all(exec)
.await?;
Ok(res
.into_iter()
.map(|r| r.try_into())
.collect::<Result<Vec<_>, serde_json::Error>>()?)
}
pub async fn remove(
id: DBChargeId,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,