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:
@@ -1913,3 +1913,85 @@ async fn promo_codes_are_made_by_admins_and_redeemed_once(pool: PgPool) {
|
||||
let r = app.post("/v1/promo-codes/redeem", Some(&bela), json!({"code": codes[2]})).await;
|
||||
assert_eq!(r.status, StatusCode::TOO_MANY_REQUESTS);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn an_invitation_brings_its_package_and_codes_fill_up(pool: PgPool) {
|
||||
let app = TestApp::with_config(pool.clone(), |c| c.signup_mode = crate::config::SignupMode::Invite).await;
|
||||
let admin = app.register("admin@example.com", "Admin").await;
|
||||
let r = app
|
||||
.post(
|
||||
"/v1/admin/invitations",
|
||||
Some(&admin),
|
||||
json!({"emails": ["guest@example.com", "other@example.com"], "package": "couple"}),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::CREATED, "{}", r.body);
|
||||
let guest = &r.body["results"][0];
|
||||
let guest_token = invite_token(guest["url"].as_str().unwrap());
|
||||
let other_id = r.body["results"][1]["id"].as_str().unwrap().to_string();
|
||||
|
||||
// the e-mail shows the package and its code
|
||||
let (payload,): (Value,) = sqlx::query_as(
|
||||
"SELECT payload FROM email_outbox WHERE to_email = 'guest@example.com' AND template = 'invitation'",
|
||||
)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(payload["package_name"], "Páros");
|
||||
assert_eq!(payload["promo_code"], guest["promo_code"]);
|
||||
assert!(
|
||||
payload["redeem_url"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.ends_with(&format!("/bevaltas?kod={}", guest["promo_code"].as_str().unwrap()))
|
||||
);
|
||||
let list = app.get("/v1/admin/invitations", Some(&admin)).await.body;
|
||||
assert_eq!(list["invitations"][0]["package"]["name"], "Páros");
|
||||
|
||||
// revoking an invitation withdraws its code
|
||||
let r = app.call("DELETE", &format!("/v1/admin/invitations/{other_id}"), Some(&admin), None).await;
|
||||
assert_eq!(r.status, StatusCode::NO_CONTENT);
|
||||
let codes = app.get("/v1/admin/promo-codes?status=revoked", Some(&admin)).await.body;
|
||||
assert_eq!((codes["total"].as_i64(), &codes["codes"][0]["note"]), (Some(1), &json!("meghívó: other@example.com")));
|
||||
|
||||
// signing up redeems it by itself
|
||||
let r = google_signup(&app, "g-guest", "guest@example.com", Some(&guest_token)).await;
|
||||
assert!(r.status.is_success(), "{}", r.body);
|
||||
let token = r.body["token"].as_str().map(str::to_string).unwrap_or_else(|| {
|
||||
let cookie = r.headers.get("set-cookie").unwrap().to_str().unwrap();
|
||||
cookie.split(';').next().unwrap().split('=').nth(1).unwrap().to_string()
|
||||
});
|
||||
let me = app.get("/v1/me", Some(&token)).await.body;
|
||||
let left = &me["analyses_left"];
|
||||
assert_eq!(
|
||||
(left["natal"].as_i64(), left["synastry"].as_i64(), left["forecast"].as_i64()),
|
||||
(Some(2), Some(1), Some(2)),
|
||||
"{me}"
|
||||
);
|
||||
assert_eq!(me["package"]["id"], "couple");
|
||||
let used = app.get("/v1/admin/promo-codes?status=used", Some(&admin)).await.body;
|
||||
assert_eq!(used["codes"][0]["redeemed_by"]["email"], "guest@example.com");
|
||||
|
||||
// a code fills up: nothing to add while everything is there…
|
||||
let r = app.post("/v1/admin/promo-codes", Some(&admin), json!({"package": "couple", "count": 1})).await;
|
||||
let spare = r.body["codes"][0].as_str().unwrap().to_string();
|
||||
let r = app.post("/v1/promo-codes/redeem", Some(&token), json!({"code": spare})).await;
|
||||
assert_eq!((r.status, r.body["error"]["code"].as_str()), (StatusCode::CONFLICT, Some("promo_nothing_to_add")));
|
||||
// …used units come back, extra ones stay
|
||||
sqlx::query(
|
||||
"INSERT INTO credit_ledger (id, user_id, unit, delta, reason) \
|
||||
SELECT gen_random_uuid(), id, unit, delta, 'admin_adjust' FROM users, \
|
||||
(VALUES ('natal', -2), ('forecast', 3)) AS v(unit, delta) WHERE email = 'guest@example.com'",
|
||||
)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let r = app.post("/v1/promo-codes/redeem", Some(&token), json!({"code": spare})).await;
|
||||
assert_eq!(r.status, StatusCode::OK, "{}", r.body);
|
||||
assert_eq!(r.body["added"], json!({"natal": 2}));
|
||||
let left = &app.get("/v1/me", Some(&token)).await.body["analyses_left"];
|
||||
assert_eq!(
|
||||
(left["natal"].as_i64(), left["synastry"].as_i64(), left["forecast"].as_i64()),
|
||||
(Some(2), Some(1), Some(5))
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user