Gift packages: buy a main package for someone, with a printable gift card

- /ajandek: the package, whom it is for and from whom, the recipient's
  address typed twice (or "I hand it over"), a message; paid on Stripe
  like any package, the invoice is the buyer's
- On payment the gift gets its own code (not a promo code: it cannot be
  withdrawn, and it adds the whole package); redeemed on /bevaltas
- The recipient's e-mail carries the message and the code, with the gift
  card attached as a PDF; the buyer's confirmation says it was a gift
- /ajandekaim: codes, state, the card to download, sending it by e-mail
  (again, or to another address) up to three times
- crates/gift-card draws the A4 card (example chart, questions avatar,
  QR code) as SVG and makes a PDF with krilla, fonts compiled in; the
  backend draws downloads on a blocking thread, the worker the e-mails
- Telegram notices say "(ajándék)"; unpaid gifts go after a week

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-23 20:28:11 +00:00
co-authored by Claude Opus 5.5
parent 699ce2abbd
commit c90232f40c
46 changed files with 3647 additions and 88 deletions
+61 -1
View File
@@ -153,6 +153,48 @@ async fn invitation_headers_for_spam_filters(db: PgPool) {
assert!(raw.contains("multipart/alternative") && raw.contains("text/plain") && raw.contains("text/html"));
}
#[sqlx::test(migrator = "db::MIGRATOR")]
async fn gift_e_mails_carry_the_gift_card(db: PgPool) {
let card = json!({
"package": "Páros", "contents": ["2 részletes képletelemzés", "3 kérdés"], "code": "ABCD-EFGH-JKMN",
"redeem_url": "https://example.com/bevaltas?kod=ABCD-EFGH-JKMN", "recipient_name": "Bea", "from_name": "Ági",
"message": "Boldog szülinapot!",
});
for (to, template, card) in [
("bea@example.com", "gift_package", card.clone()),
// A broken card: the e-mail still goes, without it.
("cili@example.com", "gift_package", json!({"package": "Páros"})),
] {
sqlx::query("INSERT INTO email_outbox (id, to_email, template, locale, payload) VALUES ($1, $2, $3, 'hu', $4)")
.bind(Uuid::now_v7())
.bind(to)
.bind(template)
.bind(json!({
"url": "https://example.com/bevaltas?kod=ABCD-EFGH-JKMN", "package_name": "Páros",
"contents": "2 részletes képletelemzés, 3 kérdés", "promo_code": "ABCD-EFGH-JKMN",
"from_name": "Ági", "message": "Boldog szülinapot!", "gift_card": card,
}))
.execute(&db)
.await
.unwrap();
}
queue(&db, "anna@example.com", "verify_email").await;
let d = delivery(db.clone(), vec![]);
assert_eq!(d.deliver_due().await.unwrap(), 3);
let Mailer::Memory { sent, .. } = &d.mailer else { unreachable!() };
let sent = sent.lock().unwrap();
let raw = |to: &str| sent.iter().find(|(t, _, _)| t == to).unwrap().2.clone();
let bea = raw("bea@example.com");
assert!(bea.contains("multipart/mixed") && bea.contains("multipart/alternative"), "{bea}");
assert!(bea.contains("Content-Type: application/pdf"));
assert!(bea.contains("sorsfonalak-ajandek-ABCD-EFGH-JKMN.pdf"), "{}", &bea[..bea.find("JVBERi0").unwrap_or(0)]);
// The PDF itself, base64 ("%PDF-" is "JVBERi0").
assert!(bea.contains("JVBERi0"));
for plain in [raw("cili@example.com"), raw("anna@example.com")] {
assert!(!plain.contains("application/pdf") && !plain.contains("multipart/mixed"));
}
}
#[sqlx::test(migrator = "db::MIGRATOR")]
async fn concurrent_workers_do_not_send_twice(db: PgPool) {
for i in 0..15 {
@@ -203,6 +245,21 @@ async fn cleanup_removes_only_expired_data(db: PgPool) {
.await
.unwrap();
sqlx::query("UPDATE email_outbox SET sent_at = now() WHERE id = $1").bind(recent).execute(&db).await.unwrap();
// Gifts: one never paid (gone after a week), one just started, one paid long ago.
let gifts = [("8 days", false), ("1 hour", false), ("30 days", true)].map(|g| (Uuid::now_v7(), g));
for (id, (age, paid)) in &gifts {
sqlx::query(
"INSERT INTO gifts (id, package, recipient_email, created_at, paid_at, code) \
VALUES ($1, 'couple', 'bea@example.com', now() - $2::interval, CASE WHEN $3 THEN now() END, \
CASE WHEN $3 THEN 'ABCDEFGHJKMN' END)",
)
.bind(id)
.bind(age)
.bind(paid)
.execute(&db)
.await
.unwrap();
}
let deleted = crate::cleanup::run_once(&db).await.unwrap();
assert_eq!(
@@ -214,9 +271,12 @@ async fn cleanup_removes_only_expired_data(db: PgPool) {
("invites", 0),
("invitations", 0),
("prompt_previews", 0),
("generation_errors", 0)
("generation_errors", 0),
("gifts", 1),
]
);
let kept: Vec<Uuid> = sqlx::query_scalar("SELECT id FROM gifts ORDER BY id").fetch_all(&db).await.unwrap();
assert_eq!(kept, vec![gifts[1].0, gifts[2].0]);
let (left,): (Vec<Uuid>,) =
sqlx::query_as("SELECT array_agg(id ORDER BY id) FROM email_outbox").fetch_one(&db).await.unwrap();
assert_eq!(left, vec![recent, unsent]);