Invitations with a package; promo codes fill up the balance
An admin can pick a package when inviting: each invitee gets a promo code (in the e-mail and its preview, kept on resend), redeemed by itself at sign up; revoking the invitation withdraws it, and the list shows the package. Redeeming a promo code now fills the units up to the package's amounts: used units come back, more than that stays. A code that would add nothing stays open, with a message saying so; the answer tells what was added. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -198,7 +198,20 @@ impl Source {
|
||||
}
|
||||
}
|
||||
|
||||
/// Records an order and gives its contents, inside the caller's transaction.
|
||||
impl Package {
|
||||
/// What the package gives, unit by unit.
|
||||
pub fn contents(&self) -> [(Unit, i64); 5] {
|
||||
[
|
||||
(Unit::Natal, self.natal),
|
||||
(Unit::Synastry, self.synastry),
|
||||
(Unit::Forecast, self.forecast),
|
||||
(Unit::Person, self.persons),
|
||||
(Unit::Question, self.questions),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
/// Records an order and adds its contents, inside the caller's transaction.
|
||||
pub async fn grant(
|
||||
tx: &mut PgConnection,
|
||||
user_id: Uuid,
|
||||
@@ -206,6 +219,51 @@ pub async fn grant(
|
||||
source: Source,
|
||||
created_by: Option<Uuid>,
|
||||
note: Option<&str>,
|
||||
) -> Result<Uuid, ApiError> {
|
||||
record(tx, user_id, package, source, created_by, note, &package.contents()).await
|
||||
}
|
||||
|
||||
/// Fills the user's units up to what the package gives (promo codes): a unit
|
||||
/// below the package's amount is raised to it, used units come back, more
|
||||
/// than that stays as it is. `persons_left` is the free person slots now
|
||||
/// (`None`: unlimited, nothing to add). Returns the order and what was added
|
||||
/// (nothing at all: no order is recorded).
|
||||
pub async fn top_up(
|
||||
tx: &mut PgConnection,
|
||||
user_id: Uuid,
|
||||
package: &Package,
|
||||
persons_left: Option<i64>,
|
||||
note: Option<&str>,
|
||||
) -> Result<Option<(Uuid, Vec<(Unit, i64)>)>, ApiError> {
|
||||
sqlx::query("SELECT id FROM users WHERE id = $1 FOR UPDATE").bind(user_id).execute(&mut *tx).await?;
|
||||
let balance = crate::credits::balance(tx, user_id).await?;
|
||||
let added: Vec<(Unit, i64)> = package
|
||||
.contents()
|
||||
.into_iter()
|
||||
.map(|(unit, n)| {
|
||||
let have = match unit {
|
||||
Unit::Person => persons_left.unwrap_or(i64::MAX),
|
||||
unit => balance.of(unit),
|
||||
};
|
||||
(unit, (n - have).max(0))
|
||||
})
|
||||
.filter(|&(_, n)| n > 0)
|
||||
.collect();
|
||||
if added.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let order = record(tx, user_id, package, Source::Promo, None, note, &added).await?;
|
||||
Ok(Some((order, added)))
|
||||
}
|
||||
|
||||
async fn record(
|
||||
tx: &mut PgConnection,
|
||||
user_id: Uuid,
|
||||
package: &Package,
|
||||
source: Source,
|
||||
created_by: Option<Uuid>,
|
||||
note: Option<&str>,
|
||||
contents: &[(Unit, i64)],
|
||||
) -> Result<Uuid, ApiError> {
|
||||
let order = Uuid::now_v7();
|
||||
let price = match source {
|
||||
@@ -225,14 +283,7 @@ pub async fn grant(
|
||||
.bind(created_by)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
let contents = [
|
||||
(Unit::Natal, package.natal),
|
||||
(Unit::Synastry, package.synastry),
|
||||
(Unit::Forecast, package.forecast),
|
||||
(Unit::Person, package.persons),
|
||||
(Unit::Question, package.questions),
|
||||
];
|
||||
for (unit, n) in contents {
|
||||
for &(unit, n) in contents {
|
||||
if n == 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user