Christmas gifts and timed gift e-mails
- Gifts bought 15 Nov - 6 Jan get a Christmas card ("Boldog karácsonyt és
csillagos 2027-et!", snowflakes) and a Christmas e-mail subject.
- The buyer can time the gift e-mail (e.g. Christmas morning); "Küldés most"
replaces a waiting one, a refund takes it back.
- Admin: timed e-mails by hour (waiting / sent / failed), e-mail state per gift.
- /karacsony landing page; seasonal hints on /csomagok and /ajandek.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -3413,7 +3413,7 @@ async fn a_paid_gift_goes_to_its_recipient_and_adds_the_whole_package(pool: PgPo
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
confirmation["gift"],
|
||||
json!({"promo_code": code, "recipient_name": "Bea", "recipient_email": "bea@example.com"})
|
||||
json!({"promo_code": code, "recipient_name": "Bea", "recipient_email": "bea@example.com", "send_at": null})
|
||||
);
|
||||
assert!(confirmation["gift_card"].is_null(), "the recipient has the card");
|
||||
assert_eq!(confirmation["url"], "https://sorsfonalak.test/ajandekaim");
|
||||
@@ -3426,7 +3426,10 @@ async fn a_paid_gift_goes_to_its_recipient_and_adds_the_whole_package(pool: PgPo
|
||||
.unwrap();
|
||||
assert_eq!(invoices, 1);
|
||||
let r = app.get(&format!("/v1/payments/checkout/{session}"), Some(&anna)).await;
|
||||
assert_eq!(r.body["gift"], json!({"id": gift_id, "recipient_name": "Bea", "recipient_email": "bea@example.com"}));
|
||||
assert_eq!(
|
||||
r.body["gift"],
|
||||
json!({"id": gift_id, "recipient_name": "Bea", "recipient_email": "bea@example.com", "send_at": null})
|
||||
);
|
||||
|
||||
let r = app.get("/v1/gifts", Some(&anna)).await;
|
||||
let gift = &r.body["gifts"][0];
|
||||
@@ -3459,8 +3462,10 @@ async fn a_paid_gift_goes_to_its_recipient_and_adds_the_whole_package(pool: PgPo
|
||||
sqlx::query("UPDATE gifts SET last_sent_at = now() - interval '11 minutes'").execute(&app.pool).await.unwrap();
|
||||
let r = send("cili@example.com").await;
|
||||
assert_eq!(r.status, StatusCode::OK, "{}", r.body);
|
||||
assert_eq!((&r.body["recipient_email"], &r.body["sendings"]), (&json!("cili@example.com"), &json!(2)));
|
||||
// The first e-mail had not gone out yet (no worker here): the corrected address replaces it.
|
||||
assert_eq!((&r.body["recipient_email"], &r.body["sendings"]), (&json!("cili@example.com"), &json!(1)));
|
||||
assert_eq!(app.outbox_count("cili@example.com", "gift_package").await, 1);
|
||||
assert_eq!(app.outbox_count("bea@example.com", "gift_package").await, 0);
|
||||
let r = app
|
||||
.post(
|
||||
&format!("/v1/gifts/{gift_id}/send"),
|
||||
@@ -3960,3 +3965,70 @@ async fn the_recommendation_question_comes_once_after_two_contents(pool: PgPool)
|
||||
);
|
||||
assert_eq!(nps["comments"][0]["comment"], "Nagyon tetszik.");
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn a_gift_can_be_e_mailed_at_a_chosen_time(pool: PgPool) {
|
||||
// The stand-in payment: paid at once.
|
||||
let app = TestApp::new(pool).await;
|
||||
let anna = app.register("anna@example.com", "Anna").await;
|
||||
let admin = app.register("admin@example.com", "Admin").await;
|
||||
let at = (time::OffsetDateTime::now_utc() + time::Duration::days(10))
|
||||
.replace_nanosecond(0)
|
||||
.unwrap()
|
||||
.format(&time::format_description::well_known::Rfc3339)
|
||||
.unwrap();
|
||||
let body = |send_at: &str, email: Option<&str>| {
|
||||
json!({"package": "couple", "recipient_name": "Bea", "recipient_email": email,
|
||||
"recipient_email_confirm": email, "send_at": send_at})
|
||||
};
|
||||
// In the past, or with nobody to e-mail: refused.
|
||||
let past = app.post("/v1/gifts/checkout", Some(&anna), body("2020-01-01T08:00:00Z", Some("bea@example.com"))).await;
|
||||
assert_eq!(past.status, StatusCode::BAD_REQUEST);
|
||||
let nobody = app.post("/v1/gifts/checkout", Some(&anna), body(&at, None)).await;
|
||||
assert_eq!(nobody.status, StatusCode::BAD_REQUEST);
|
||||
|
||||
let r = app.post("/v1/gifts/checkout", Some(&anna), body(&at, Some("bea@example.com"))).await;
|
||||
assert_eq!(r.status, StatusCode::OK, "{}", r.body);
|
||||
let id = r.body["id"].as_str().unwrap().to_string();
|
||||
// Queued for that moment, not sent yet.
|
||||
let (due, sent): (time::OffsetDateTime, Option<time::OffsetDateTime>) = sqlx::query_as(
|
||||
"SELECT next_attempt_at, sent_at FROM email_outbox WHERE to_email = 'bea@example.com' AND template = 'gift_package'",
|
||||
)
|
||||
.fetch_one(&app.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!((due.format(&time::format_description::well_known::Rfc3339).unwrap(), sent), (at.clone(), None));
|
||||
let r = app.get("/v1/gifts", Some(&anna)).await;
|
||||
let gift = &r.body["gifts"][0];
|
||||
assert_eq!(
|
||||
(&gift["email_state"], &gift["send_at"], &gift["can_send"]),
|
||||
(&json!("scheduled"), &json!(at), &json!(true))
|
||||
);
|
||||
let r = app.get("/v1/admin/gifts", Some(&admin)).await;
|
||||
assert_eq!(r.body["gifts"][0]["email_state"], "scheduled");
|
||||
let slot = &r.body["schedule"][0];
|
||||
assert_eq!((&slot["waiting"], &slot["sent"]), (&json!(1), &json!(0)));
|
||||
|
||||
// Sent now after all: the timed one is taken back (one e-mail, not two), and it is no extra sending.
|
||||
let r = app
|
||||
.post(
|
||||
&format!("/v1/gifts/{id}/send"),
|
||||
Some(&anna),
|
||||
json!({"recipient_email": "bea@example.com", "recipient_email_confirm": "bea@example.com"}),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::OK, "{}", r.body);
|
||||
assert_eq!(
|
||||
(&r.body["send_at"], &r.body["sendings"], &r.body["email_state"]),
|
||||
(&Value::Null, &json!(1), &json!("sending"))
|
||||
);
|
||||
assert_eq!(app.outbox_count("bea@example.com", "gift_package").await, 1);
|
||||
|
||||
// A timed gift refunded before it goes out: the e-mail stays unsent.
|
||||
let r = app.post("/v1/gifts/checkout", Some(&anna), body(&at, Some("cili@example.com"))).await;
|
||||
let other = r.body["id"].as_str().unwrap().to_string();
|
||||
assert_eq!(app.outbox_count("cili@example.com", "gift_package").await, 1);
|
||||
let r = app.post(&format!("/v1/admin/gifts/{other}/refund"), Some(&admin), json!({})).await;
|
||||
assert_eq!(r.status, StatusCode::OK, "{}", r.body);
|
||||
assert_eq!(app.outbox_count("cili@example.com", "gift_package").await, 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user