You've already forked AstralRinth
forked from didirus/AstralRinth
Automatically cancel servers with failed payments older than 30d (#3695)
This commit is contained in:
@@ -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>,
|
||||
|
||||
@@ -2284,12 +2284,19 @@ pub async fn index_billing(
|
||||
) {
|
||||
info!("Indexing billing queue");
|
||||
let res = async {
|
||||
// If a charge has continuously failed for more than a month, it should be cancelled
|
||||
let charges_to_cancel = DBCharge::get_cancellable(&pool).await?;
|
||||
|
||||
for mut charge in charges_to_cancel {
|
||||
charge.status = ChargeStatus::Cancelled;
|
||||
|
||||
let mut transaction = pool.begin().await?;
|
||||
charge.upsert(&mut transaction).await?;
|
||||
transaction.commit().await?;
|
||||
}
|
||||
|
||||
// If a charge is open and due or has been attempted more than two days ago, it should be processed
|
||||
let charges_to_do =
|
||||
crate::database::models::charge_item::DBCharge::get_chargeable(
|
||||
&pool,
|
||||
)
|
||||
.await?;
|
||||
let charges_to_do = DBCharge::get_chargeable(&pool).await?;
|
||||
|
||||
let prices = product_item::DBProductPrice::get_many(
|
||||
&charges_to_do
|
||||
|
||||
Reference in New Issue
Block a user