Share rewards: a question for a visit, an analysis for a new user
A click proves little, so the first visit of a shared link now earns a question (once per user) instead of a natal analysis. A natal analysis is earned by bringing a new user: the share page remembers the link, the Google sign-up sends it, and when that user makes their own chart the sharer gets one, at most three (referrals table, referral_reward). This is off until sign-up opens (REFERRAL_REWARDS, default false). The share button and its small print follow; /v1/me tells what was earned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -801,7 +801,7 @@ const PUBLIC_ROUTES: [&str; 15] = [
|
||||
];
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn a_visit_by_someone_else_earns_one_analysis_once(pool: PgPool) {
|
||||
async fn a_visit_by_someone_else_earns_one_question_once(pool: PgPool) {
|
||||
let app = TestApp::new(pool).await;
|
||||
let anna = app.register("anna@example.com", "Anna").await;
|
||||
let chart = app.chart(&anna, "Saját", true, "1990-03-15").await;
|
||||
@@ -812,7 +812,7 @@ async fn a_visit_by_someone_else_earns_one_analysis_once(pool: PgPool) {
|
||||
let visit = format!("/v1/public/shares/{}/visit", r.body["slug"].as_str().unwrap());
|
||||
let browser = ("user-agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) Mobile/15E148");
|
||||
let me = || async { app.get("/v1/me", Some(&anna)).await.body };
|
||||
let before = me().await["analyses_left"]["natal"].as_i64().unwrap();
|
||||
let before = me().await["analyses_left"]["question"].as_i64().unwrap();
|
||||
|
||||
// robots, a missing user agent, the sharer's address and the sharer do not count
|
||||
let elsewhere = ("x-forwarded-for", "198.51.100.1, 172.70.1.1");
|
||||
@@ -826,7 +826,10 @@ async fn a_visit_by_someone_else_earns_one_analysis_once(pool: PgPool) {
|
||||
assert_eq!((r.status, &r.body["counted"]), (StatusCode::OK, &json!(false)), "{headers:?}");
|
||||
}
|
||||
let body = me().await;
|
||||
assert_eq!((body["analyses_left"]["natal"].as_i64(), &body["share_reward_granted"]), (Some(before), &json!(false)));
|
||||
assert_eq!(
|
||||
(body["analyses_left"]["question"].as_i64(), &body["share_reward_granted"]),
|
||||
(Some(before), &json!(false))
|
||||
);
|
||||
// reading the share counts nothing either
|
||||
app.get(&visit.replace("/visit", ""), None).await;
|
||||
|
||||
@@ -837,7 +840,7 @@ async fn a_visit_by_someone_else_earns_one_analysis_once(pool: PgPool) {
|
||||
}
|
||||
let body = me().await;
|
||||
assert_eq!(
|
||||
(body["analyses_left"]["natal"].as_i64(), &body["share_reward_granted"]),
|
||||
(body["analyses_left"]["question"].as_i64(), &body["share_reward_granted"]),
|
||||
(Some(before + 1), &json!(true))
|
||||
);
|
||||
let r = app.get("/v1/shares", Some(&anna)).await;
|
||||
@@ -848,7 +851,7 @@ async fn a_visit_by_someone_else_earns_one_analysis_once(pool: PgPool) {
|
||||
app.post("/v1/shares", Some(&anna), json!({"kind": "chart", "target_id": chart["id"], "title": "Új"})).await;
|
||||
let other = format!("/v1/public/shares/{}/visit", r.body["slug"].as_str().unwrap());
|
||||
app.call_with("POST", &other, None, None, &[browser, elsewhere]).await;
|
||||
assert_eq!(me().await["analyses_left"]["natal"].as_i64(), Some(before + 1));
|
||||
assert_eq!(me().await["analyses_left"]["question"].as_i64(), Some(before + 1));
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
@@ -2137,3 +2140,70 @@ async fn user_invitations_wait_for_the_launch(pool: PgPool) {
|
||||
app.chart(&admin, "Saját", true, "1980-01-01").await;
|
||||
assert_eq!(app.post("/v1/invites", Some(&admin), json!({})).await.status, StatusCode::CREATED);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn a_new_user_from_a_shared_link_earns_an_analysis(pool: PgPool) {
|
||||
let app = TestApp::new(pool.clone()).await;
|
||||
let anna = app.register("anna@example.com", "Anna").await;
|
||||
let chart = app.chart(&anna, "Saját", true, "1990-03-15").await;
|
||||
let r = app.post("/v1/shares", Some(&anna), json!({"kind": "chart", "target_id": chart["id"]})).await;
|
||||
let slug = r.body["slug"].as_str().unwrap().to_string();
|
||||
let natal = |token: String| {
|
||||
let app = &app;
|
||||
async move { app.get("/v1/me", Some(&token)).await.body["analyses_left"]["natal"].as_i64().unwrap() }
|
||||
};
|
||||
let before = natal(anna.clone()).await;
|
||||
let join = |email: String| {
|
||||
let (app, slug) = (&app, slug.clone());
|
||||
async move {
|
||||
let r = app
|
||||
.post(
|
||||
"/v1/auth/register",
|
||||
None,
|
||||
json!({"email": email, "password": "titkos-jelszo-123", "referral": slug}),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::CREATED, "{}", r.body);
|
||||
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()
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
// signing up is not enough: the reward comes with their own chart
|
||||
let bela = join("bela@example.com".into()).await;
|
||||
assert_eq!(natal(anna.clone()).await, before);
|
||||
app.chart(&bela, "Saját", true, "1985-08-02").await;
|
||||
assert_eq!(natal(anna.clone()).await, before + 1);
|
||||
// a second chart of theirs changes nothing
|
||||
app.chart(&bela, "Másik", false, "1970-01-01").await;
|
||||
assert_eq!(natal(anna.clone()).await, before + 1);
|
||||
|
||||
// at most three
|
||||
for i in 0..3 {
|
||||
let t = join(format!("p{i}@example.com")).await;
|
||||
app.chart(&t, "Saját", true, "1980-05-05").await;
|
||||
}
|
||||
assert_eq!(natal(anna.clone()).await, before + 3);
|
||||
let me = app.get("/v1/me", Some(&anna)).await.body;
|
||||
assert_eq!(me["referral_rewards"], json!({"earned": 3, "limit": 3}));
|
||||
|
||||
// nothing while the rewards are off
|
||||
let off = TestApp::with_config(pool, |c| c.referral_rewards = false).await;
|
||||
let r = off
|
||||
.post(
|
||||
"/v1/auth/register",
|
||||
None,
|
||||
json!({"email": "late@example.com", "password": "titkos-jelszo-123", "referral": slug}),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::CREATED);
|
||||
let count: i64 = sqlx::query_scalar(
|
||||
"SELECT count(*) FROM referrals r JOIN users u ON u.id = r.user_id WHERE u.email = 'late@example.com'",
|
||||
)
|
||||
.fetch_one(&off.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(count, 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user