You've already forked AstralRinth
forked from didirus/AstralRinth
Offers, redemption, preview subscriptions (#4121)
* Initial db migration/impl, guarded partner routes * Add guard to /redeem * Add `public` column to products prices, only expose public prices * Query cache * Add partner subscription type * 5 days subscription interval, metadata * Create server on redeem * Query cache * Fix race condition * Unprovision Medal subscriptions * Consider due expiring charge as unprovisionable * Query cache * Use a queue * Promote to full subscription, fmt + clippy * Patch expiring charge on promotion, comments * Additional comments * Add `tags` field to Archon /create request * Address review comments * Query cache * Final fixes to edit_subscription * Appease clippy * fmt
This commit is contained in:
committed by
GitHub
parent
c02b809601
commit
9497ba70a4
@@ -57,6 +57,26 @@ impl DBProduct {
|
||||
Ok(Self::get_many(&[id], exec).await?.into_iter().next())
|
||||
}
|
||||
|
||||
pub async fn get_by_type<'a, E>(
|
||||
exec: E,
|
||||
r#type: &str,
|
||||
) -> Result<Vec<Self>, DatabaseError>
|
||||
where
|
||||
E: sqlx::PgExecutor<'a>,
|
||||
{
|
||||
let maybe_row = select_products_with_predicate!(
|
||||
"WHERE metadata ->> 'type' = $1",
|
||||
r#type
|
||||
)
|
||||
.fetch_all(exec)
|
||||
.await?;
|
||||
|
||||
maybe_row
|
||||
.into_iter()
|
||||
.map(|r| r.try_into().map_err(Into::into))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub async fn get_many(
|
||||
ids: &[DBProductId],
|
||||
exec: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
|
||||
@@ -100,10 +120,11 @@ pub struct QueryProductWithPrices {
|
||||
}
|
||||
|
||||
impl QueryProductWithPrices {
|
||||
pub async fn list<'a, E>(
|
||||
/// Lists products with at least one public price.
|
||||
pub async fn list_purchaseable<'a, E>(
|
||||
exec: E,
|
||||
redis: &RedisPool,
|
||||
) -> Result<Vec<QueryProductWithPrices>, DatabaseError>
|
||||
) -> Result<Vec<Self>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy,
|
||||
{
|
||||
@@ -118,7 +139,51 @@ impl QueryProductWithPrices {
|
||||
}
|
||||
|
||||
let all_products = product_item::DBProduct::get_all(exec).await?;
|
||||
let prices = product_item::DBProductPrice::get_all_products_prices(
|
||||
let prices =
|
||||
product_item::DBProductPrice::get_all_public_products_prices(
|
||||
&all_products.iter().map(|x| x.id).collect::<Vec<_>>(),
|
||||
exec,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let products = all_products
|
||||
.into_iter()
|
||||
.filter_map(|x| {
|
||||
Some(QueryProductWithPrices {
|
||||
id: x.id,
|
||||
metadata: x.metadata,
|
||||
prices: prices
|
||||
.remove(&x.id)
|
||||
.map(|x| x.1)?
|
||||
.into_iter()
|
||||
.map(|x| DBProductPrice {
|
||||
id: x.id,
|
||||
product_id: x.product_id,
|
||||
prices: x.prices,
|
||||
currency_code: x.currency_code,
|
||||
})
|
||||
.collect(),
|
||||
unitary: x.unitary,
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
redis
|
||||
.set_serialized_to_json(PRODUCTS_NAMESPACE, "all", &products, None)
|
||||
.await?;
|
||||
|
||||
Ok(products)
|
||||
}
|
||||
|
||||
pub async fn list_by_product_type<'a, E>(
|
||||
exec: E,
|
||||
r#type: &str,
|
||||
) -> Result<Vec<Self>, DatabaseError>
|
||||
where
|
||||
E: sqlx::PgExecutor<'a> + Copy,
|
||||
{
|
||||
let all_products = DBProduct::get_by_type(exec, r#type).await?;
|
||||
let prices = DBProductPrice::get_all_products_prices(
|
||||
&all_products.iter().map(|x| x.id).collect::<Vec<_>>(),
|
||||
exec,
|
||||
)
|
||||
@@ -126,29 +191,26 @@ impl QueryProductWithPrices {
|
||||
|
||||
let products = all_products
|
||||
.into_iter()
|
||||
.map(|x| QueryProductWithPrices {
|
||||
id: x.id,
|
||||
metadata: x.metadata,
|
||||
prices: prices
|
||||
.remove(&x.id)
|
||||
.map(|x| x.1)
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(|x| DBProductPrice {
|
||||
id: x.id,
|
||||
product_id: x.product_id,
|
||||
prices: x.prices,
|
||||
currency_code: x.currency_code,
|
||||
})
|
||||
.collect(),
|
||||
unitary: x.unitary,
|
||||
.filter_map(|x| {
|
||||
Some(QueryProductWithPrices {
|
||||
id: x.id,
|
||||
metadata: x.metadata,
|
||||
prices: prices
|
||||
.remove(&x.id)
|
||||
.map(|x| x.1)?
|
||||
.into_iter()
|
||||
.map(|x| DBProductPrice {
|
||||
id: x.id,
|
||||
product_id: x.product_id,
|
||||
prices: x.prices,
|
||||
currency_code: x.currency_code,
|
||||
})
|
||||
.collect(),
|
||||
unitary: x.unitary,
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
redis
|
||||
.set_serialized_to_json(PRODUCTS_NAMESPACE, "all", &products, None)
|
||||
.await?;
|
||||
|
||||
Ok(products)
|
||||
}
|
||||
}
|
||||
@@ -169,7 +231,11 @@ struct ProductPriceQueryResult {
|
||||
}
|
||||
|
||||
macro_rules! select_prices_with_predicate {
|
||||
($predicate:tt, $param:ident) => {
|
||||
($predicate:tt, $param1:ident) => {
|
||||
select_prices_with_predicate!($predicate, $param1, )
|
||||
};
|
||||
|
||||
($predicate:tt, $($param:ident,)+) => {
|
||||
sqlx::query_as!(
|
||||
ProductPriceQueryResult,
|
||||
r#"
|
||||
@@ -177,7 +243,7 @@ macro_rules! select_prices_with_predicate {
|
||||
FROM products_prices
|
||||
"#
|
||||
+ $predicate,
|
||||
$param
|
||||
$($param),+
|
||||
)
|
||||
};
|
||||
}
|
||||
@@ -231,33 +297,81 @@ impl DBProductPrice {
|
||||
Ok(res.remove(&product_id).map(|x| x.1).unwrap_or_default())
|
||||
}
|
||||
|
||||
pub async fn get_all_public_product_prices(
|
||||
product_id: DBProductId,
|
||||
exec: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
|
||||
) -> Result<Vec<DBProductPrice>, DatabaseError> {
|
||||
let res =
|
||||
Self::get_all_public_products_prices(&[product_id], exec).await?;
|
||||
|
||||
Ok(res.remove(&product_id).map(|x| x.1).unwrap_or_default())
|
||||
}
|
||||
|
||||
/// Gets all public prices for the given products. If a product has no public price,
|
||||
/// it won't be included in the resulting map.
|
||||
pub async fn get_all_public_products_prices(
|
||||
product_ids: &[DBProductId],
|
||||
exec: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
|
||||
) -> Result<DashMap<DBProductId, Vec<DBProductPrice>>, DatabaseError> {
|
||||
Self::get_all_products_prices_with_visibility(
|
||||
product_ids,
|
||||
Some(true),
|
||||
exec,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_all_products_prices(
|
||||
product_ids: &[DBProductId],
|
||||
exec: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
|
||||
) -> Result<DashMap<DBProductId, Vec<DBProductPrice>>, DatabaseError> {
|
||||
Self::get_all_products_prices_with_visibility(product_ids, None, exec)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn get_all_products_prices_with_visibility(
|
||||
product_ids: &[DBProductId],
|
||||
public_filter: Option<bool>,
|
||||
exec: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
|
||||
) -> Result<DashMap<DBProductId, Vec<DBProductPrice>>, DatabaseError> {
|
||||
let ids = product_ids.iter().map(|id| id.0).collect_vec();
|
||||
let ids_ref: &[i64] = &ids;
|
||||
|
||||
use futures_util::TryStreamExt;
|
||||
let prices = select_prices_with_predicate!(
|
||||
"WHERE product_id = ANY($1::bigint[])",
|
||||
ids_ref
|
||||
)
|
||||
.fetch(exec)
|
||||
.try_fold(
|
||||
DashMap::new(),
|
||||
|acc: DashMap<DBProductId, Vec<DBProductPrice>>, x| {
|
||||
if let Ok(item) = <ProductPriceQueryResult as TryInto<
|
||||
DBProductPrice,
|
||||
>>::try_into(x)
|
||||
{
|
||||
acc.entry(item.product_id).or_default().push(item);
|
||||
}
|
||||
|
||||
async move { Ok(acc) }
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
let predicate = |acc: DashMap<DBProductId, Vec<DBProductPrice>>, x| {
|
||||
if let Ok(item) = <ProductPriceQueryResult as TryInto<
|
||||
DBProductPrice,
|
||||
>>::try_into(x)
|
||||
{
|
||||
acc.entry(item.product_id).or_default().push(item);
|
||||
}
|
||||
|
||||
async move { Ok(acc) }
|
||||
};
|
||||
|
||||
let prices = match public_filter {
|
||||
None => {
|
||||
select_prices_with_predicate!(
|
||||
"WHERE product_id = ANY($1::bigint[])",
|
||||
ids_ref,
|
||||
)
|
||||
.fetch(exec)
|
||||
.try_fold(DashMap::new(), predicate)
|
||||
.await?
|
||||
}
|
||||
|
||||
Some(public) => {
|
||||
select_prices_with_predicate!(
|
||||
"WHERE product_id = ANY($1::bigint[]) AND public = $2",
|
||||
ids_ref,
|
||||
public,
|
||||
)
|
||||
.fetch(exec)
|
||||
.try_fold(DashMap::new(), predicate)
|
||||
.await?
|
||||
}
|
||||
};
|
||||
|
||||
Ok(prices)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user