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:
feco
2026-11-24 13:46:35 +00:00
co-authored by Claude Opus 5.5
parent 227e8b5b80
commit 382bd31271
22 changed files with 1016 additions and 209 deletions
@@ -0,0 +1,6 @@
-- A gift e-mailed later (e.g. on Christmas morning): when (NULL: at once),
-- and the e-mail queued for it, to show whether it went out (the outbox row
-- itself is cleaned up a week after sending).
ALTER TABLE gifts
ADD COLUMN send_at timestamptz,
ADD COLUMN email_id uuid;
+34 -7
View File
@@ -334,7 +334,13 @@ fn purchase_confirmation(lang: Lang, payload: &Value, url: String) -> Result<Con
.as_str()
.map(|e| e.chars().filter(|c| !c.is_control()).take(254).collect::<String>());
let whom = name(&payload["gift"], "recipient_name");
(code, to, whom)
// `2026-12-24 08:00`, Budapest clock: when it is e-mailed.
let when = payload["gift"]["send_at"].as_str().and_then(|t| {
let (day, clock) = t.split_once(' ')?;
let clock: String = clock.chars().filter(|c| c.is_ascii_digit() || *c == ':').take(5).collect();
Some((date(lang, day)?, clock))
});
(code, to, whom, when)
});
let contents_hu = contents.as_ref().map(|c| format!(" Tartalma: {c}.")).unwrap_or_default();
let contents_en = contents.as_ref().map(|c| format!(" It contains: {c}.")).unwrap_or_default();
@@ -359,9 +365,10 @@ fn purchase_confirmation(lang: Lang, payload: &Value, url: String) -> Result<Con
unsubscribe: None,
unsubscribe_post_url: None,
},
(Lang::Hu, Some((code, to, whom))) => Content {
(Lang::Hu, Some((code, to, whom, when))) => Content {
subject: format!("Köszönjük a vásárlást: {package} (ajándék)"),
preheader: Some(match &to {
Some(e) if when.is_some() => format!("Az ajándékot időzítve küldjük el ide: {e}."),
Some(e) => format!("Az ajándékot elküldtük ide: {e}."),
None => "Az ajándékutalványt csatoltuk ehhez a levélhez.".into(),
}),
@@ -370,7 +377,10 @@ fn purchase_confirmation(lang: Lang, payload: &Value, url: String) -> Result<Con
"Megkaptuk a fizetésed ({amount}, {paid_on}): a(z) „{package}” csomagot ajándékba vetted{}.{contents_hu} {}",
whom.as_ref().map(|w| format!(" {w} részére")).unwrap_or_default(),
match &to {
Some(e) => format!("Az ajándékkódot az utalvánnyal együtt elküldtük ide: {e}. Ha nem e-mailben adnád át, az Ajándékaim oldalon letöltheted az utalványt PDF-ben."),
Some(e) => match &when {
Some((day, clock)) => format!("Az ajándékkódot az utalvánnyal együtt {day}, {clock}-kor küldjük el ide: {e}. Addig az Ajándékaim oldalon letöltheted az utalványt PDF-ben, vagy elküldheted azonnal is."),
None => format!("Az ajándékkódot az utalvánnyal együtt elküldtük ide: {e}. Ha nem e-mailben adnád át, az Ajándékaim oldalon letöltheted az utalványt PDF-ben."),
},
None => "Az ajándékutalványt PDF-ben csatoltuk ehhez a levélhez, és az Ajándékaim oldalon is letöltheted: kinyomtathatod, vagy továbbküldheted annak, akinek szánod.".into(),
}
),
@@ -409,7 +419,7 @@ fn purchase_confirmation(lang: Lang, payload: &Value, url: String) -> Result<Con
unsubscribe: None,
unsubscribe_post_url: None,
},
(Lang::En, Some((code, to, whom))) => Content {
(Lang::En, Some((code, to, whom, when))) => Content {
subject: format!("Thank you for your purchase: {package} (gift)"),
preheader: Some(match &to {
Some(e) => format!("We sent the gift to {e}."),
@@ -420,7 +430,10 @@ fn purchase_confirmation(lang: Lang, payload: &Value, url: String) -> Result<Con
"We received your payment ({amount}, {paid_on}): you bought the “{package}” package as a gift{}.{contents_en} {}",
whom.as_ref().map(|w| format!(" for {w}")).unwrap_or_default(),
match &to {
Some(e) => format!("We sent the gift code with the gift card to {e}. To hand it over another way, download the card as a PDF on the My gifts page."),
Some(e) => match &when {
Some((day, clock)) => format!("We send the gift code with the gift card to {e} on {day} at {clock}. Until then you can download the card on the My gifts page, or send it at once."),
None => format!("We sent the gift code with the gift card to {e}. To hand it over another way, download the card as a PDF on the My gifts page."),
},
None => "The gift card is attached to this e-mail as a PDF, and you can download it on the My gifts page: print it, or forward it to the person it is for.".into(),
}
),
@@ -459,16 +472,21 @@ fn gift_package(lang: Lang, payload: &Value, url: String) -> Result<Content, Tem
payload["contents"].as_str().map(|c| c.chars().filter(|c| !c.is_control()).take(300).collect::<String>());
let from = name(payload, "from_name");
let to = name(payload, "recipient_name");
// Bought for Christmas: said so, with the wishes.
let christmas = payload["christmas_year"].as_i64().is_some();
let xmas = if christmas { "karácsonyi " } else { "" };
Ok(match lang {
Lang::Hu => Content {
subject: match &from {
Some(f) => format!("{f} ajándékot küldött neked: „{package}” csomag"),
Some(f) => format!("{f} {xmas}ajándékot küldött neked: „{package}” csomag"),
None if christmas => format!("Karácsonyi ajándékot kaptál: „{package}” csomag a Sorsfonalakon"),
None => format!("Ajándékot kaptál: „{package}” csomag a Sorsfonalakon"),
},
preheader: Some("Születési képlet és személyre szóló elemzések: a kód beváltásával a tiéd.".into()),
greeting: to.as_ref().map_or_else(|| "Szia!".into(), |t| format!("Szia {t}!")),
intro: format!(
"{} egy „{package}” csomagot a Sorsfonalakon.{}",
"{}{} egy „{package}” csomagot a Sorsfonalakon.{}",
if christmas { "Boldog karácsonyt! " } else { "" },
match &from {
Some(f) => format!("{f} ajándékozott neked"),
None => "Ajándékba kaptál".into(),
@@ -497,6 +515,7 @@ fn gift_package(lang: Lang, payload: &Value, url: String) -> Result<Content, Tem
},
Lang::En => Content {
subject: match &from {
Some(f) if christmas => format!("{f} sent you a Christmas gift: the “{package}” package"),
Some(f) => format!("{f} sent you a gift: the “{package}” package"),
None => format!("A gift for you: the “{package}” package on Sorsfonalak"),
},
@@ -1021,6 +1040,11 @@ mod tests {
);
assert!(hu.html.contains("&lt;b&gt;Ölel&lt;/b&gt;") && hu.html.contains(">ABCD-EFGH-JKMN</div>"));
assert!(hu.text.contains("PDF-ben is csatoltuk") && hu.text.contains("mert Ági a Sorsfonalakon"));
let mut xmas = payload.clone();
xmas["christmas_year"] = 2027.into();
let hu = render("gift_package", "hu", &xmas).unwrap();
assert_eq!(hu.subject, "Ági karácsonyi ajándékot küldött neked: „Páros” csomag");
assert!(hu.text.contains("Boldog karácsonyt! Ági ajándékozott neked"));
let anonymous = serde_json::json!({"url": "u", "package_name": "Család", "promo_code": "X1-<script>"});
let hu = render("gift_package", "hu", &anonymous).unwrap();
assert_eq!(hu.subject, "Ajándékot kaptál: „Család” csomag a Sorsfonalakon");
@@ -1046,6 +1070,9 @@ mod tests {
assert!(hu.text.contains("AJÁNDÉKKÓD: ABCD-EFGH-JKMN") && hu.text.contains("14 napon belül"));
assert!(hu.text.contains("a beváltással az elállási jog megszűnik"));
assert!(!hu.text.contains("jóváírtuk"));
payload["gift"]["send_at"] = Value::from("2026-12-24 08:00");
let hu = render("purchase_confirmation", "hu", &payload).unwrap();
assert!(hu.text.contains("2026. december 24., 08:00-kor küldjük el ide: bea@example.com"), "{}", hu.text);
payload["gift"]["recipient_email"] = Value::Null;
let hu = render("purchase_confirmation", "hu", &payload).unwrap();
assert!(hu.text.contains("PDF-ben csatoltuk ehhez a levélhez"));
+1
View File
@@ -28,6 +28,7 @@ fn main() {
code: &code,
redeem_url: &url,
recipient_name: Some(to.as_str()),
christmas: std::env::var("CHRISTMAS").ok().and_then(|y| y.parse().ok()),
from_name: Some(from.as_str()),
message: (!message.is_empty()).then_some(message.as_str()),
};
+97 -12
View File
@@ -38,6 +38,8 @@ const GOLD: &str = "#a07a2c";
const MUTED: &str = "#5f6578";
const LINE: &str = "#d9c9a3";
const PAPER: &str = "#fdfaf3";
/// The Christmas snowflakes: gold, lighter than the text.
const GOLD_SOFT: &str = "#c9a862";
/// Where the code box starts; the message fills the space above it.
const CODE_TOP: f32 = 636.0;
@@ -64,6 +66,27 @@ pub struct Card<'a> {
pub from_name: Option<&'a str>,
/// The buyer's message (line breaks kept).
pub message: Option<&'a str>,
/// A Christmas gift: the card is festive and wishes a happy new year
/// (this one).
pub christmas: Option<i32>,
}
/// "2027-et", "2028-at": a year in the accusative, by how its last word sounds.
pub fn year_accusative(year: i32) -> String {
let ending = match year.rem_euclid(10) {
1 | 4 | 7 | 9 => "et",
2 => "t",
3 | 8 => "at",
5 => "öt",
6 => "ot",
// Tens: "…tízet", "…húszat", "…harmincat"…; hundreds and thousands: "…százat", "…ezret".
_ => match (year / 10).rem_euclid(10) {
1 | 4 | 7 | 9 => "et",
0 if (year / 100).rem_euclid(10) == 0 => "et",
_ => "at",
},
};
format!("{year}-{ending}")
}
#[derive(Debug)]
@@ -252,6 +275,11 @@ impl Message {
let (text, signature) = match (message, from) {
(Some(m), from) => (format!("„{m}”"), from.map(|f| format!("– {f}"))),
(None, Some(f)) => (format!("Szeretettel küldi: {f}"), None),
(None, None) if card.christmas.is_some() => (
"Boldog karácsonyt! Fedezd fel, mit mesélnek rólad a csillagok, és mit hoz neked a következő év."
.to_string(),
None,
),
(None, None) => (
"Fedezd fel, mit mesélnek rólad a csillagok: a születési képleted, személyre szóló elemzések, \
és válaszok a kérdéseidre."
@@ -323,6 +351,26 @@ fn star(x: f32, y: f32, r: f32, fill: &str) -> String {
)
}
/// A six-armed snowflake of gold lines, centred on (x, y).
fn snowflake(x: f32, y: f32, r: f32, color: &str) -> String {
let mut d = String::new();
for k in 0..6 {
let a = (k as f32 * 60.0 - 90.0).to_radians();
let (sin, cos) = a.sin_cos();
let _ = write!(d, "M{x} {y}L{} {}", x + r * cos, y + r * sin);
// Two small branches on each arm.
let (bx, by) = (x + r * 0.6 * cos, y + r * 0.6 * sin);
for side in [-1.0_f32, 1.0] {
let b = a + side * 0.75;
let _ = write!(d, "M{bx} {by}L{} {}", bx + r * 0.32 * b.cos(), by + r * 0.32 * b.sin());
}
}
format!(
r#"<path d="{d}" fill="none" stroke="{color}" stroke-width="{}" stroke-linecap="round"/>"#,
(r / 9.0).max(0.6)
)
}
/// The QR code of the redeem link as a path of `size` points at (x, y).
fn qr(url: &str, x: f32, y: f32, size: f32) -> String {
let Ok(code) = qrcode::QrCode::with_error_correction_level(url, qrcode::EcLevel::M) else {
@@ -388,7 +436,7 @@ pub fn svg(card: &Card) -> String {
);
// "AJÁNDÉKUTALVÁNY" between two short lines.
let eyebrow = "AJÁNDÉKUTALVÁNY";
let eyebrow = if card.christmas.is_some() { "KARÁCSONYI AJÁNDÉK" } else { "AJÁNDÉKUTALVÁNY" };
let spacing = 3.2;
let ew = width(Face::SansBold, eyebrow, 9.0, spacing) - spacing;
let _ = write!(
@@ -413,7 +461,12 @@ pub fn svg(card: &Card) -> String {
for (x, y, r) in
[(124.0, 164.0, 5.0), (462.0, 148.0, 6.5), (470.0, 304.0, 4.0), (134.0, 322.0, 6.0), (420.0, 350.0, 3.0)]
{
s.push_str(&star(x, y, r, LINE));
// Christmas: snowflakes instead of stars, a little bigger.
if card.christmas.is_some() {
s.push_str(&snowflake(x, y, r * 1.9, GOLD_SOFT));
} else {
s.push_str(&star(x, y, r, LINE));
}
}
let inner = WHEEL.split_once("viewBox=\"0 0 680 680\">").map_or("", |(_, rest)| rest);
let inner = inner.trim_end().trim_end_matches("</svg>");
@@ -451,17 +504,32 @@ pub fn svg(card: &Card) -> String {
y += 15.0;
}
// A divider with a small diamond.
// A divider with a small diamond; at Christmas, the wishes on it.
let divider = y + 10.0;
let _ = write!(
s,
r#"<line x1="{}" y1="{divider}" x2="{}" y2="{divider}" stroke="{LINE}" stroke-width="0.7"/><line x1="{}" y1="{divider}" x2="{}" y2="{divider}" stroke="{LINE}" stroke-width="0.7"/><path d="M{cx} {} l3.5 3.5 l-3.5 3.5 l-3.5 -3.5 z" fill="{GOLD}"/>"#,
cx - 150.0,
cx - 10.0,
cx + 10.0,
cx + 150.0,
divider - 3.5
);
if let Some(year) = card.christmas {
let wish = format!("Boldog karácsonyt és csillagos {}!", year_accusative(year));
let ww = width(Face::SerifItalic, &wish, 16.0, 0.0);
let _ = write!(
s,
r#"<line x1="{}" y1="{divider}" x2="{}" y2="{divider}" stroke="{LINE}" stroke-width="0.7"/><line x1="{}" y1="{divider}" x2="{}" y2="{divider}" stroke="{LINE}" stroke-width="0.7"/><text x="{cx}" y="{}" text-anchor="middle" font-family="{SERIF_ITALIC_FAMILY}" font-style="italic" font-weight="500" font-size="16" fill="{GOLD}">{}</text>"#,
cx - ww / 2.0 - 70.0,
cx - ww / 2.0 - 12.0,
cx + ww / 2.0 + 12.0,
cx + ww / 2.0 + 70.0,
divider + 5.0,
escape(&wish)
);
} else {
let _ = write!(
s,
r#"<line x1="{}" y1="{divider}" x2="{}" y2="{divider}" stroke="{LINE}" stroke-width="0.7"/><line x1="{}" y1="{divider}" x2="{}" y2="{divider}" stroke="{LINE}" stroke-width="0.7"/><path d="M{cx} {} l3.5 3.5 l-3.5 3.5 l-3.5 -3.5 z" fill="{GOLD}"/>"#,
cx - 150.0,
cx - 10.0,
cx + 10.0,
cx + 150.0,
divider - 3.5
);
}
// The message next to the avatar, centred in the room above the code box.
let room_top = divider + 22.0;
@@ -568,6 +636,7 @@ mod tests {
recipient_name: Some("Bea"),
from_name: from,
message,
christmas: None,
}
}
@@ -625,6 +694,22 @@ mod tests {
usvg::Tree::from_str(&svg, &usvg::Options::default()).unwrap();
}
#[test]
fn a_christmas_card_wishes_a_happy_new_year() {
let contents = contents();
let plain = svg(&card(&contents, None, None));
assert!(plain.contains("AJÁNDÉKUTALVÁNY") && !plain.contains("karácsony"));
let festive = Card { christmas: Some(2027), ..card(&contents, None, None) };
let svg = svg(&festive);
assert!(svg.contains("KARÁCSONYI AJÁNDÉK") && svg.contains("Boldog karácsonyt és csillagos 2027-et!"));
assert!(svg.contains("mit hoz neked a következő év"));
assert!(pdf(&festive).unwrap().starts_with(b"%PDF-"));
assert_eq!(
[2027, 2028, 2030, 2026, 2032, 2035, 2100, 2000].map(year_accusative),
["2027-et", "2028-at", "2030-at", "2026-ot", "2032-t", "2035-öt", "2100-at", "2000-et"]
);
}
#[test]
fn without_a_message_the_card_still_says_something() {
let contents = contents();