diff --git a/Cargo.lock b/Cargo.lock index 55f6730..7e4d77f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -48,6 +48,18 @@ dependencies = [ "password-hash", ] +[[package]] +name = "arrayref" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb" + +[[package]] +name = "arrayvec" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fb67a6e08acf24fdeccbac2cb6ac4305825bd1f117462e0e6f2f193345ad56" + [[package]] name = "astro-engine" version = "0.1.0" @@ -177,6 +189,7 @@ dependencies = [ "db", "deunicode", "email-templates", + "gift-card", "hmac", "horoscope-client", "http-body-util", @@ -255,6 +268,20 @@ name = "bytemuck" version = "1.25.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95832e849adfb21180ccb6826a99da14e5d266ae5c2e668e1602cf234f153797" +dependencies = [ + "bytemuck_derive", +] + +[[package]] +name = "bytemuck_derive" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a1f896587b6f2c069c73d2f0913e2d590c3990285cd2f0b6aa02b786b4c679c" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] [[package]] name = "byteorder" @@ -307,6 +334,12 @@ dependencies = [ "rand_core 0.10.1", ] +[[package]] +name = "color_quant" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" + [[package]] name = "const-oid" version = "0.9.6" @@ -324,6 +357,15 @@ dependencies = [ "version_check", ] +[[package]] +name = "core_maths" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77745e017f5edba1a9c1d854f6f3a52dac8a12dd5af5d2f54aecf61e43d80d30" +dependencies = [ + "libm", +] + [[package]] name = "cpufeatures" version = "0.2.17" @@ -400,6 +442,12 @@ dependencies = [ "typenum", ] +[[package]] +name = "data-url" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be1e0bca6c3637f992fc1cc7cbc52a78c1ef6db076dbf1059c4323d6a2048376" + [[package]] name = "db" version = "0.1.0" @@ -527,6 +575,15 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "euclid" +version = "0.22.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1a05365e3b1c6d1650318537c7460c6923f1abdd272ad6842baa2b509957a06" +dependencies = [ + "num-traits", +] + [[package]] name = "event-listener" version = "5.4.2" @@ -569,6 +626,15 @@ dependencies = [ "zlib-rs", ] +[[package]] +name = "float-cmp" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" +dependencies = [ + "num-traits", +] + [[package]] name = "flume" version = "0.11.1" @@ -586,6 +652,38 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" +[[package]] +name = "font-types" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b38ad915f6dadd993ced50848a8291a543bd41ca62bc10740d5e64e2ab4cfd7" +dependencies = [ + "bytemuck", +] + +[[package]] +name = "fontconfig-parser" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbc773e24e02d4ddd8395fd30dc147524273a83e54e0f312d986ea30de5f5646" +dependencies = [ + "roxmltree 0.20.0", +] + +[[package]] +name = "fontdb" +version = "0.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "457e789b3d1202543297a350643cf459f836cade38934e7a4cf6a39e7cde2905" +dependencies = [ + "fontconfig-parser", + "log", + "memmap2", + "slotmap", + "tinyvec", + "ttf-parser", +] + [[package]] name = "form_urlencoded" version = "1.2.2" @@ -725,6 +823,29 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "gif" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee8cfcc411d9adbbaba82fb72661cc1bcca13e8bba98b364e62b2dba8f960159" +dependencies = [ + "color_quant", + "weezl", +] + +[[package]] +name = "gift-card" +version = "0.1.0" +dependencies = [ + "base64 0.22.1", + "krilla", + "krilla-svg", + "qrcode", + "resvg", + "ttf-parser", + "usvg", +] + [[package]] name = "hashbrown" version = "0.15.5" @@ -1065,6 +1186,12 @@ dependencies = [ "quick-error", ] +[[package]] +name = "imagesize" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09e54e57b4c48b40f7aec75635392b12b3421fa26fe8b4332e63138ed278459c" + [[package]] name = "indexmap" version = "2.14.2" @@ -1148,6 +1275,62 @@ dependencies = [ "simple_asn1", ] +[[package]] +name = "krilla" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27da593198b20eeba65caeb73c2bbeec3e53ab08fa549898312ce81c4fce5e33" +dependencies = [ + "base64 0.22.1", + "bumpalo", + "flate2", + "float-cmp", + "gif", + "image-webp", + "imagesize", + "indexmap", + "pdf-writer", + "png", + "rustc-hash", + "rustybuzz", + "siphasher", + "skrifa", + "smallvec", + "subsetter", + "tiny-skia-path", + "xmp-writer", + "yoke", + "zune-jpeg", +] + +[[package]] +name = "krilla-svg" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1237d7c37b16ca9fbc2e72dde13a10d321f9a44aceee35c062bc0a43bfc7ce16" +dependencies = [ + "flate2", + "fontdb", + "krilla", + "resvg", + "skrifa", + "smallvec", + "tiny-skia", + "usvg", +] + +[[package]] +name = "kurbo" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b60dfc32f652b926df6192e55525b16d186c69d47876c3ead4da5cc9f8450e2" +dependencies = [ + "arrayvec", + "euclid", + "polycool", + "smallvec", +] + [[package]] name = "lazy_static" version = "1.5.1" @@ -1277,6 +1460,15 @@ version = "2.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" +[[package]] +name = "memmap2" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b0ca1155a9ecc25aa12e8313a119f32edf0255fea2f44839c4ac0a12b2ac157" +dependencies = [ + "libc", +] + [[package]] name = "mime" version = "0.3.18" @@ -1449,6 +1641,18 @@ dependencies = [ "subtle", ] +[[package]] +name = "pdf-writer" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5e456864a7a304047bff84977dc6fb162bd956475d40ba50b2dcecaada7f753" +dependencies = [ + "bitflags", + "itoa", + "memchr", + "ryu", +] + [[package]] name = "pem" version = "3.0.6" @@ -1474,6 +1678,12 @@ version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" +[[package]] +name = "pico-args" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315" + [[package]] name = "pin-project-lite" version = "0.2.17" @@ -1526,6 +1736,15 @@ dependencies = [ "miniz_oxide 0.8.9", ] +[[package]] +name = "polycool" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50596ddc09eb5ad5f75cacd40209568e66df71baf86e1499a0e99c4cff12a5a6" +dependencies = [ + "arrayvec", +] + [[package]] name = "portable-atomic" version = "1.15.0" @@ -1580,6 +1799,12 @@ version = "0.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea" +[[package]] +name = "qrcode" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d68782463e408eb1e668cf6152704bd856c78c5b6417adaee3203d8f4c1fc9ec" + [[package]] name = "quick-error" version = "2.0.1" @@ -1754,6 +1979,16 @@ dependencies = [ "rand_core 0.10.1", ] +[[package]] +name = "read-fonts" +version = "0.39.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4ed38b89c2c77ff968c524145ad65fb010f38af5c7a224b53b81d47ac2daa81" +dependencies = [ + "bytemuck", + "font-types", +] + [[package]] name = "redox_syscall" version = "0.5.18" @@ -1827,6 +2062,32 @@ dependencies = [ "webpki-roots", ] +[[package]] +name = "resvg" +version = "0.47.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9be183ad6a216aa96f33e4c8033b0988b8b3ea6fd2359d19af5bac4643fd8e81" +dependencies = [ + "gif", + "image-webp", + "log", + "pico-args", + "rgb", + "svgtypes", + "tiny-skia", + "usvg", + "zune-jpeg", +] + +[[package]] +name = "rgb" +version = "0.8.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b34b781b31e5d73e9fbc8689c70551fd1ade9a19e3e28cfec8580a79290cc4" +dependencies = [ + "bytemuck", +] + [[package]] name = "ring" version = "0.17.14" @@ -1841,6 +2102,21 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "roxmltree" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97" + +[[package]] +name = "roxmltree" +version = "0.21.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9da7f59328b09405b181fee1018925fad50f157afb43c0c0f5c522c256dd5907" +dependencies = [ + "memchr", +] + [[package]] name = "rsa" version = "0.9.10" @@ -1909,6 +2185,24 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" +[[package]] +name = "rustybuzz" +version = "0.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd3c7c96f8a08ee34eff8857b11b49b07d71d1c3f4e88f8a88d4c9e9f90b1702" +dependencies = [ + "bitflags", + "bytemuck", + "core_maths", + "log", + "smallvec", + "ttf-parser", + "unicode-bidi-mirroring", + "unicode-ccc", + "unicode-properties", + "unicode-script", +] + [[package]] name = "ryu" version = "1.0.23" @@ -2075,12 +2369,46 @@ dependencies = [ "time", ] +[[package]] +name = "simplecss" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a9c6883ca9c3c7c90e888de77b7a5c849c779d25d74a1269b0218b14e8b136c" +dependencies = [ + "log", +] + +[[package]] +name = "siphasher" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33f4fe9184a62d842c9ef383018f3306d8ba224fd9d836f56d7288308847c256" + +[[package]] +name = "skrifa" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c34617370ae968efb7161bb2beb517d9084659aae19e24b89e3db25b46e4564" +dependencies = [ + "bytemuck", + "read-fonts", +] + [[package]] name = "slab" version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" +[[package]] +name = "slotmap" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdd58c3c93c3d278ca835519292445cb4b0d4dc59ccfdf7ceadaab3f8aeb4038" +dependencies = [ + "version_check", +] + [[package]] name = "smallvec" version = "1.16.5" @@ -2321,6 +2649,15 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" +[[package]] +name = "strict-num" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731" +dependencies = [ + "float-cmp", +] + [[package]] name = "stringprep" version = "0.1.5" @@ -2332,12 +2669,34 @@ dependencies = [ "unicode-properties", ] +[[package]] +name = "subsetter" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38803281d1c23166c5ebcb455439a5d2afe711cc909cf88af72448c297756ad6" +dependencies = [ + "kurbo", + "rustc-hash", + "skrifa", + "write-fonts", +] + [[package]] name = "subtle" version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" +[[package]] +name = "svgtypes" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f12b12995e3638c70618c1facb1ce65302f6a6f706b675f07c58a5d7391120e" +dependencies = [ + "kurbo", + "siphasher", +] + [[package]] name = "swisseph-sys" version = "0.1.0" @@ -2452,6 +2811,32 @@ dependencies = [ "time-core", ] +[[package]] +name = "tiny-skia" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47ffee5eaaf5527f630fb0e356b90ebdec84d5d18d937c5e440350f88c5a91ea" +dependencies = [ + "arrayref", + "arrayvec", + "bytemuck", + "cfg-if", + "log", + "png", + "tiny-skia-path", +] + +[[package]] +name = "tiny-skia-path" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edca365c3faccca67d06593c5980fa6c57687de727a03131735bb85f01fdeeb9" +dependencies = [ + "arrayref", + "bytemuck", + "strict-num", +] + [[package]] name = "tinystr" version = "0.8.5" @@ -2657,6 +3042,15 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "ttf-parser" +version = "0.25.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2df906b07856748fa3f6e0ad0cbaa047052d4a7dd609e231c4f72cee8c36f31" +dependencies = [ + "core_maths", +] + [[package]] name = "typenum" version = "1.20.1" @@ -2669,6 +3063,18 @@ version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5" +[[package]] +name = "unicode-bidi-mirroring" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dfa6e8c60bb66d49db113e0125ee8711b7647b5579dc7f5f19c42357ed039fe" + +[[package]] +name = "unicode-ccc" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce61d488bcdc9bc8b5d1772c404828b17fc481c0a582b5581e95fb233aef503e" + [[package]] name = "unicode-ident" version = "1.0.27" @@ -2690,6 +3096,18 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "993ad287eb20e8ba755312801cfe1413e0f25d604075863acaddca10b04c1833" +[[package]] +name = "unicode-script" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa04c955db05429d464c74d495c1ee677e1866e783a4c3747f9c0cef7756cabf" + +[[package]] +name = "unicode-vo" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1d386ff53b415b7fe27b50bb44679e2cc4660272694b7b6f3326d8480823a94" + [[package]] name = "untrusted" version = "0.9.0" @@ -2708,6 +3126,34 @@ dependencies = [ "serde", ] +[[package]] +name = "usvg" +version = "0.47.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d46cf96c5f498d36b7a9693bc6a7075c0bb9303189d61b2249b0dc3d309c07de" +dependencies = [ + "base64 0.22.1", + "data-url", + "flate2", + "fontdb", + "imagesize", + "kurbo", + "log", + "pico-args", + "roxmltree 0.21.2", + "rustybuzz", + "simplecss", + "siphasher", + "strict-num", + "svgtypes", + "tiny-skia-path", + "ttf-parser", + "unicode-bidi", + "unicode-script", + "unicode-vo", + "xmlwriter", +] + [[package]] name = "utf8_iter" version = "1.1.0" @@ -2867,6 +3313,12 @@ dependencies = [ "rustls-pki-types", ] +[[package]] +name = "weezl" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a28ac98ddc8b9274cb41bb4d9d4d5c425b6020c50c46f25559911905610b4a88" + [[package]] name = "whoami" version = "1.6.1" @@ -3047,6 +3499,7 @@ dependencies = [ "base64 0.22.1", "db", "email-templates", + "gift-card", "hmac", "lettre", "rand 0.9.5", @@ -3062,12 +3515,37 @@ dependencies = [ "uuid", ] +[[package]] +name = "write-fonts" +version = "0.48.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb731d4c4d93eacc69a1ad2f270f905788a98e4a3438267bcafbe08d3431c8d8" +dependencies = [ + "font-types", + "indexmap", + "kurbo", + "log", + "read-fonts", +] + [[package]] name = "writeable" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" +[[package]] +name = "xmlwriter" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec7a2a501ed189703dba8b08142f057e887dfc4b2cc4db2d343ac6376ba3e0b9" + +[[package]] +name = "xmp-writer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9440ea3e5aeabb0ac63af70daf835274065238cdd0cec83418f417eae38bacee" + [[package]] name = "yoke" version = "0.8.4" diff --git a/Cargo.toml b/Cargo.toml index eebf6bf..22a3aae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ members = [ "crates/horoscope-client", "crates/analysis-prompt", "crates/email-templates", + "crates/gift-card", "services/horoscope-api", "services/backend", "services/worker", @@ -29,6 +30,7 @@ db = { path = "crates/db" } horoscope-client = { path = "crates/horoscope-client" } analysis-prompt = { path = "crates/analysis-prompt" } email-templates = { path = "crates/email-templates" } +gift-card = { path = "crates/gift-card" } anyhow = "1" argon2 = { version = "0.5", features = ["std"] } diff --git a/crates/db/migrations/0035_gifts.sql b/crates/db/migrations/0035_gifts.sql new file mode 100644 index 0000000..695786e --- /dev/null +++ b/crates/db/migrations/0035_gifts.sql @@ -0,0 +1,40 @@ +-- Gifts: a package of the main ladder paid for someone else. The buyer pays +-- as for any package (stripe_checkouts.gift_id); when the payment is +-- confirmed the gift gets its code, which is e-mailed to the recipient, or +-- the buyer hands it over (the gift card can be downloaded as a PDF). +-- A gift code is not a promo code: it cannot be withdrawn (it was paid for), +-- and it adds the whole package. +CREATE TABLE gifts ( + id uuid PRIMARY KEY, + buyer_id uuid REFERENCES users (id) ON DELETE SET NULL, + package text NOT NULL, + -- Whom it is for (on the card and in the e-mail); cleared with the buyer's account. + recipient_name text, + -- Who gets it by e-mail; NULL: the buyer hands the card over. + recipient_email text, + -- The giver's name and message, on the card and in the e-mail. + from_name text, + message text, + created_at timestamptz NOT NULL DEFAULT now(), + -- Paid: the code is made (and sent). + paid_at timestamptz, + -- Upper case, without separators (shown in groups of four), like promo + -- codes, and never the same as one. + code text UNIQUE CHECK (code ~ '^[2-9A-HJKMNP-Z]{8,32}$'), + redeemed_by uuid REFERENCES users (id) ON DELETE SET NULL, + redeemed_at timestamptz, + order_id uuid REFERENCES package_orders (id) ON DELETE SET NULL, + -- E-mails to the recipient so far (sending again is limited). + sendings integer NOT NULL DEFAULT 0, + last_sent_at timestamptz, + CHECK (paid_at IS NULL OR code IS NOT NULL), + CHECK (redeemed_at IS NULL OR paid_at IS NOT NULL) +); +CREATE INDEX gifts_buyer_idx ON gifts (buyer_id, created_at DESC); + +ALTER TABLE stripe_checkouts ADD COLUMN gift_id uuid UNIQUE REFERENCES gifts (id) ON DELETE SET NULL; + +-- A redeemed gift is recorded as an order with source `gift`. +ALTER TABLE package_orders DROP CONSTRAINT package_orders_source_check; +ALTER TABLE package_orders ADD CONSTRAINT package_orders_source_check + CHECK (source IN ('admin', 'mock', 'stripe', 'promo', 'gift')); diff --git a/crates/email-templates/src/lib.rs b/crates/email-templates/src/lib.rs index 440bec6..0c827ca 100644 --- a/crates/email-templates/src/lib.rs +++ b/crates/email-templates/src/lib.rs @@ -51,6 +51,8 @@ struct Content { quote: Option, /// A short list after the intro, with its heading. points: Option<(&'static str, Vec<&'static str>)>, + /// A code to type in, shown large in a box, with its label. + code: Option<(&'static str, String)>, button: &'static str, url: String, note: String, @@ -76,6 +78,7 @@ impl Content { reason: None, unsubscribe: None, unsubscribe_post_url: None, + code: None, } } } @@ -178,6 +181,7 @@ pub fn render(template: &str, locale: &str, payload: &Value) -> Result waitlist_joined(lang, payload, url)?, ("purchase_confirmation", _) => purchase_confirmation(lang, payload, url)?, ("gift", _) => gift(lang, payload, url)?, + ("gift_package", _) => gift_package(lang, payload, url)?, _ => return Err(TemplateError::UnknownTemplate(template.to_owned())), }; Ok(content.render(lang)) @@ -253,6 +257,7 @@ fn invitation(lang: Lang, payload: &Value, url: String) -> Result Content { subject: match &inviter { @@ -287,6 +292,7 @@ fn invitation(lang: Lang, payload: &Value, url: String) -> Result Result format!("Hi {n},"), (false, None) => "Hi,".to_owned(), }; - Ok(match lang { - Lang::Hu => Content { + // A package bought as a gift: its code, and where it went (none: the + // buyer hands it over, the card is attached). + let gift = payload["gift"].is_object().then(|| { + let code = clean_code(&payload["gift"]["promo_code"]); + let to = payload["gift"]["recipient_email"] + .as_str() + .map(|e| e.chars().filter(|c| !c.is_control()).take(254).collect::()); + let whom = name(&payload["gift"], "recipient_name"); + (code, to, whom) + }); + 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(); + Ok(match (lang, gift) { + (Lang::Hu, None) => Content { subject: format!("Köszönjük a vásárlást: {package}"), preheader: Some(format!("A(z) {package} csomag a tiéd.")), greeting: greeting(true), - intro: format!( - "Megkaptuk a fizetésed ({amount}, {paid_on}), és jóváírtuk a(z) „{package}” csomagot.{}", - contents.map(|c| format!(" Tartalma: {c}.")).unwrap_or_default() - ), + intro: format!("Megkaptuk a fizetésed ({amount}, {paid_on}), és jóváírtuk a(z) „{package}” csomagot.{contents_hu}"), quote: Some( "A fizetés előtt kérted a szolgáltatás azonnali megkezdését, és tudomásul vetted, hogy ezzel elveszíted a 14 napos elállási jogodat. A csomagot ennek megfelelően azonnal teljesítettük." .into(), ), points: None, + code: None, button: "Megnézem a csomagjaimat", url, note: format!( @@ -343,7 +359,37 @@ fn purchase_confirmation(lang: Lang, payload: &Value, url: String) -> Result Content { + (Lang::Hu, Some((code, to, whom))) => Content { + subject: format!("Köszönjük a vásárlást: {package} (ajándék)"), + preheader: Some(match &to { + Some(e) => format!("Az ajándékot elküldtük ide: {e}."), + None => "Az ajándékutalványt csatoltuk ehhez a levélhez.".into(), + }), + greeting: greeting(true), + intro: format!( + "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."), + 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 fizetés előtt kérted a szolgáltatás azonnali megkezdését, és tudomásul vetted, hogy ezzel elveszíted a 14 napos elállási jogodat. Az ajándékkódot ennek megfelelően azonnal kiállítottuk." + .into(), + ), + points: None, + code: code.map(|c| ("AJÁNDÉKKÓD", c)), + button: "Ajándékaim", + url, + note: format!( + "A kód egyszer váltható be, és nem jár le. A számlát külön e-mailben küldjük, és a Számláim oldalon is letöltheted: {invoices_url}. A vásárlás feltételei (ÁSZF): {terms_url}. Ha valami nem működik, írj nekünk az info@sorsfonalak.hu címre." + ), + reason: Some("Ezt a levelet a vásárlásod visszaigazolásaként kaptad.".into()), + unsubscribe: None, + unsubscribe_post_url: None, + }, + (Lang::En, None) => Content { subject: format!("Thank you for your purchase: {package}"), preheader: Some(format!("The {package} package is yours.")), greeting: greeting(false), @@ -353,6 +399,7 @@ fn purchase_confirmation(lang: Lang, payload: &Value, url: String) -> Result Result Content { + subject: format!("Thank you for your purchase: {package} (gift)"), + preheader: Some(match &to { + Some(e) => format!("We sent the gift to {e}."), + None => "The gift card is attached to this e-mail.".into(), + }), + greeting: greeting(false), + intro: format!( + "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."), + 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( + "Before paying you asked us to start the service at once and acknowledged that you thereby lose your 14-day right of withdrawal. The gift code was issued at once accordingly." + .into(), + ), + points: None, + code: code.map(|c| ("GIFT CODE", c)), + button: "My gifts", + url, + note: format!( + "The code can be redeemed once and does not expire. The invoice comes in a separate e-mail and can be downloaded on the Invoices page: {invoices_url}. Terms of purchase (ÁSZF): {terms_url}. If something does not work, write to info@sorsfonalak.hu." + ), + reason: Some("You got this e-mail as the confirmation of your purchase.".into()), + unsubscribe: None, + unsubscribe_post_url: None, + }, + }) +} + +/// A promo code from a payload, as shown: letters, digits and dashes only. +fn clean_code(value: &Value) -> Option { + value + .as_str() + .map(|c| c.chars().filter(|c| c.is_ascii_alphanumeric() || *c == '-').take(40).collect::()) + .filter(|c| !c.is_empty()) +} + +/// A package someone bought for the recipient: who sends it, their message, +/// the code and its redeem link (the worker attaches the gift card as a PDF). +fn gift_package(lang: Lang, payload: &Value, url: String) -> Result { + let package = name(payload, "package_name").ok_or(TemplateError::MissingField("package_name"))?; + let code = clean_code(&payload["promo_code"]).ok_or(TemplateError::MissingField("promo_code"))?; + let contents = + payload["contents"].as_str().map(|c| c.chars().filter(|c| !c.is_control()).take(300).collect::()); + let from = name(payload, "from_name"); + let to = name(payload, "recipient_name"); + Ok(match lang { + Lang::Hu => Content { + subject: match &from { + Some(f) => format!("{f} ajándékot küldött neked: „{package}” csomag"), + 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.{}", + match &from { + Some(f) => format!("{f} ajándékozott neked"), + None => "Ajándékba kaptál".into(), + }, + contents.map(|c| format!(" Tartalma: {c}.")).unwrap_or_default() + ), + quote: message(payload), + code: Some(("AJÁNDÉKKÓD", code)), + points: Some(( + "Így váltod be:", + vec![ + "kattints a gombra, és lépj be, vagy regisztrálj, ha még nincs fiókod;", + "a kód már ki lesz töltve, csak a beváltásra kell kattintanod;", + "utána a csomag a tiéd: elkészítheted a képleted és az elemzéseidet.", + ], + )), + button: "Ajándék beváltása", + url, + note: "A kód egyszer váltható be, és nem jár le. Az ajándékutalványt PDF-ben is csatoltuk: kinyomtathatod vagy elteheted.".into(), + reason: Some(match &from { + Some(f) => format!("Ezt a levelet azért kaptad, mert {f} a Sorsfonalakon ajándékot vásárolt neked erre a címre."), + None => "Ezt a levelet azért kaptad, mert valaki a Sorsfonalakon ajándékot vásárolt neked erre a címre.".into(), + }), + unsubscribe: None, + unsubscribe_post_url: None, + }, + Lang::En => Content { + subject: match &from { + Some(f) => format!("{f} sent you a gift: the “{package}” package"), + None => format!("A gift for you: the “{package}” package on Sorsfonalak"), + }, + preheader: Some("Your birth chart and personal readings: redeem the code and they are yours.".into()), + greeting: to.as_ref().map_or_else(|| "Hi,".into(), |t| format!("Hi {t},")), + intro: format!( + "{} the “{package}” package on Sorsfonalak.{}", + match &from { + Some(f) => format!("{f} gave you"), + None => "You got".into(), + }, + contents.map(|c| format!(" It contains: {c}.")).unwrap_or_default() + ), + quote: message(payload), + code: Some(("GIFT CODE", code)), + points: Some(( + "How to redeem it:", + vec![ + "click the button and log in, or sign up if you have no account yet;", + "the code is filled in for you: just click Redeem;", + "the package is yours: make your chart and your readings.", + ], + )), + button: "Redeem the gift", + url, + note: "The code can be redeemed once and does not expire. The gift card is attached as a PDF: print it or keep it.".into(), + reason: Some(match &from { + Some(f) => format!("You got this e-mail because {f} bought you a gift on Sorsfonalak for this address."), + None => "You got this e-mail because someone bought you a gift on Sorsfonalak for this address.".into(), + }), + unsubscribe: None, + unsubscribe_post_url: None, + }, }) } @@ -397,6 +565,7 @@ fn gift(lang: Lang, payload: &Value, url: String) -> Result Content { subject: "A gift from Sorsfonalak".into(), @@ -414,6 +583,7 @@ fn gift(lang: Lang, payload: &Value, url: String) -> Result Result Content { subject: "You are on the Sorsfonalak waitlist".into(), @@ -463,6 +634,7 @@ fn waitlist_joined(lang: Lang, payload: &Value, url: String) -> Result Result { @@ -535,6 +708,7 @@ fn synastry_invite(lang: Lang, payload: &Value, url: String) -> Result
{}
{}
+"#, + escape(label), + escape(code) + ) + }); let mut small = Vec::new(); if let Some(reason) = &self.reason { small.push(escape(reason)); @@ -683,7 +868,7 @@ impl Content { SORSFONALAK {greeting} {intro} -{quote}{points}{button} +{quote}{code}{points}{button} {note} {url_text} @@ -819,6 +1004,53 @@ mod tests { assert_eq!(render("purchase_confirmation", "hu", &missing), Err(TemplateError::MissingField("amount"))); } + #[test] + fn gift_packages_carry_the_message_and_the_code() { + let payload = serde_json::json!({ + "url": "https://sorsfonalak.hu/bevaltas?kod=ABCD-EFGH-JKMN", "package_name": "Páros", + "contents": "2 részletes képletelemzés, 1 kapcsolati elemzés", "promo_code": "ABCD-EFGH-JKMN", + "recipient_name": "Bea", "from_name": "Ági", "message": "Boldog születésnapot!\nÖlel" + }); + let hu = render("gift_package", "hu", &payload).unwrap(); + assert!(hu.text.starts_with("Szia Bea!")); + assert_eq!(hu.subject, "Ági ajándékot küldött neked: „Páros” csomag"); + assert!(hu.text.contains("Ági ajándékozott neked egy „Páros” csomagot a Sorsfonalakon. Tartalma: 2 részletes")); + assert!( + hu.text.contains("> Boldog születésnapot!\n> Ölel") + && hu.text.contains("AJÁNDÉKKÓD: ABCD-EFGH-JKMN") + ); + 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 anonymous = serde_json::json!({"url": "u", "package_name": "Család", "promo_code": "X1-