diff --git a/crates/db/migrations/0037_gift_schedule.sql b/crates/db/migrations/0037_gift_schedule.sql new file mode 100644 index 0000000..449297d --- /dev/null +++ b/crates/db/migrations/0037_gift_schedule.sql @@ -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; diff --git a/crates/email-templates/src/lib.rs b/crates/email-templates/src/lib.rs index 7510916..a5547b2 100644 --- a/crates/email-templates/src/lib.rs +++ b/crates/email-templates/src/lib.rs @@ -320,6 +320,8 @@ fn purchase_confirmation(lang: Lang, payload: &Value, url: String) -> Result format!("Szia {n}!"), (true, None) => "Szia!".to_owned(), @@ -334,7 +336,13 @@ fn purchase_confirmation(lang: Lang, payload: &Value, url: String) -> Result()); 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 +367,10 @@ fn purchase_confirmation(lang: Lang, payload: &Value, url: String) -> Result 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,13 +379,15 @@ fn purchase_confirmation(lang: Lang, payload: &Value, url: String) -> Result 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(), } ), quote: Some( - "A vásárlástól számított 14 napon belül, amíg az ajándékkódot nem váltották be, elállhatsz a vásárlástól: írj az info@sorsfonalak.hu címre, és visszatérítjük az árát. A fizetés előtt kérted, hogy a kód beváltásakor a szolgáltatás azonnal megkezdődjön: a beváltással az elállási jog megszűnik." - .into(), + format!("A vásárlástól számított 14 napon belül, amíg az ajándékkódot nem váltották be, elállhatsz a vásárlástól: írd meg nekünk az oldalon, belépve ({feedback_url}), és visszatérítjük az árát. A fizetés előtt kérted, hogy a kód beváltásakor a szolgáltatás azonnal megkezdődjön: a beváltással az elállási jog megszűnik."), ), points: None, code: code.map(|c| ("AJÁNDÉKKÓD", c)), @@ -409,7 +420,7 @@ fn purchase_confirmation(lang: Lang, payload: &Value, url: String) -> Result 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,13 +431,15 @@ fn purchase_confirmation(lang: Lang, payload: &Value, url: String) -> Result 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(), } ), quote: Some( - "Within 14 days of the purchase, while the gift code is not redeemed, you may withdraw from the purchase: write to info@sorsfonalak.hu and we refund it. Before paying you asked for the service to start as soon as the code is redeemed: redeeming it ends the right of withdrawal." - .into(), + format!("Within 14 days of the purchase, while the gift code is not redeemed, you may withdraw from the purchase: tell us on the site, logged in ({feedback_url}), and we refund it. Before paying you asked for the service to start as soon as the code is redeemed: redeeming it ends the right of withdrawal."), ), points: None, code: code.map(|c| ("GIFT CODE", c)), @@ -459,16 +472,21 @@ fn gift_package(lang: Lang, payload: &Value, url: String) -> Result()); 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 { 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("<b>Ölel</b>") && hu.html.contains(">ABCD-EFGH-JKMN")); 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-