Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(referral): template the real email (#2147)
Sean Aye01efe78cfcb5
diff --git a/rust/cloud-storage/Cargo.lock b/rust/cloud-storage/Cargo.lockindex e2ea3f84fa..91e73b3acc 100644--- a/rust/cloud-storage/Cargo.lock+++ b/rust/cloud-storage/Cargo.lock@@ -8077,9 +8077,13 @@ dependencies = [name = "referral_invitation"version = "0.1.0"dependencies = [+ "askama",+ "macro_env","macro_user_id","notification","serde",+ "serde_json",+ "url",][[package]]diff --git a/rust/cloud-storage/Cargo.toml b/rust/cloud-storage/Cargo.tomlindex 4f6add6f93..af1cd844d8 100644--- a/rust/cloud-storage/Cargo.toml+++ b/rust/cloud-storage/Cargo.toml@@ -133,6 +133,7 @@ members = [[workspace.dependencies]anyhow = "1.0.93"+askama = "0.14"async-openai = { version = "0.28.3", features = ["byot"] }async-stream = "0.3.6"async-trait = "0.1.86"diff --git a/rust/cloud-storage/email_formatting/Cargo.toml b/rust/cloud-storage/email_formatting/Cargo.tomlindex 864c77170b..b1c82e4d49 100644--- a/rust/cloud-storage/email_formatting/Cargo.toml+++ b/rust/cloud-storage/email_formatting/Cargo.toml@@ -5,7 +5,7 @@ publish = falseversion = "0.1.0"[dependencies]-askama = "0.14"+askama = { workspace = true }chrono = { workspace = true }macro_user_id = { path = "../macro_user_id" }model_notifications = { path = "../model_notifications" }diff --git a/rust/cloud-storage/referral_invitation/Cargo.toml b/rust/cloud-storage/referral_invitation/Cargo.tomlindex 2fbe0a2553..6157592959 100644--- a/rust/cloud-storage/referral_invitation/Cargo.toml+++ b/rust/cloud-storage/referral_invitation/Cargo.toml@@ -6,6 +6,12 @@ version = "0.1.0"[dependencies]+askama = { workspace = true }+macro_env = { path = "../macro_env" }macro_user_id = { path = "../macro_user_id" }notification = { path = "../notification" }serde = { workspace = true }+url = { workspace = true }++[dev-dependencies]+serde_json = { workspace = true }diff --git a/rust/cloud-storage/referral_invitation/src/lib.rs b/rust/cloud-storage/referral_invitation/src/lib.rsindex 907e41cc67..d7e8fdd5af 100644--- a/rust/cloud-storage/referral_invitation/src/lib.rs+++ b/rust/cloud-storage/referral_invitation/src/lib.rs@@ -3,12 +3,18 @@//!//! Contains the [`InviteToMacro`] notification type and the [`ReferralCode`] newtype.+#[cfg(test)]+mod test;++use askama::Template;+use macro_env::Environment;use macro_user_id::email::EmailStr;use notification::domain::models::{Notification, NotificationExtEmail, RateLimitConfig, RateLimitKey, queue_message::EmailContent,};use serde::{Deserialize, Serialize};use std::time::Duration;+use url::Url;/// Wrapper for the referral code to make it type safe#[derive(Debug, Clone, Serialize, Deserialize)]@@ -16,7 +22,8 @@ use std::time::Duration;pub struct ReferralCode(pub String);/// The metadata for a referral-to-macro notification.-#[derive(Debug, Serialize, Deserialize, Clone)]+#[derive(Debug, Serialize, Deserialize, Clone, Template)]+#[template(path = "invite.html")]pub struct InviteToMacro {/// The recipient email.pub recipient_email: EmailStr<'static>,@@ -25,17 +32,41 @@ pub struct InviteToMacro {pub referral_code: ReferralCode,}+impl InviteToMacro {+ fn referral_url(&self) -> Url {+ let env = Environment::new_or_prod();+ get_url(env, &self.referral_code)+ }+}+impl Notification for InviteToMacro {const TYPE_NAME: &'static str = "invite_to_macro";}const MINUTES_PER_WEEK: u64 = 60 * 24 * 7;+fn get_url(env: Environment, code: &ReferralCode) -> Url {+ let host = match env {+ Environment::Production => "https://macro.com",+ Environment::Develop => "https://dev.macro.com",+ Environment::Local => "http://localhost:3000",+ };+ let mut url = Url::parse(host).expect("all the inputs are static, valid values");+ url.set_path("/app/signup");+ url.query_pairs_mut()+ .clear()+ .append_pair("referral_code", &code.0)+ .finish();+ url+}+impl NotificationExtEmail for InviteToMacro {fn format_email(&self) -> EmailContent {EmailContent {subject: "You have been invited to join Macro".to_string(),- body: "Temporary email body placeholder".to_string(),+ body: self+ .render()+ .expect("InviteToMacro template render failed in format_email"),}}diff --git a/rust/cloud-storage/referral_invitation/src/test.rs b/rust/cloud-storage/referral_invitation/src/test.rsnew file mode 100644index 0000000000..51e987064a--- /dev/null+++ b/rust/cloud-storage/referral_invitation/src/test.rs@@ -0,0 +1,64 @@+use super::*;++fn make_invite() -> InviteToMacro {+ InviteToMacro {+ recipient_email: EmailStr::try_from("[email protected]".to_string()).unwrap(),+ referral_code: ReferralCode("ABC123".to_string()),+ }+}++#[test]+fn referral_url_does_not_panic() {+ let invite = make_invite();+ let _url = invite.referral_url();+}++#[test]+fn get_url_all_environments() {+ let code = ReferralCode("CODE".to_string());+ let cases = [+ (Environment::Production, "macro.com"),+ (Environment::Develop, "dev.macro.com"),+ (Environment::Local, "localhost"),+ ];+ for (env, expected_host) in cases {+ let url = get_url(env, &code);+ assert_eq!(url.host_str().unwrap(), expected_host);+ assert!(url.as_str().contains("referral_code=CODE"));+ assert_eq!(url.path(), "/app/signup");+ }+}++#[test]+fn format_email_does_not_panic() {+ let invite = make_invite();+ let referral_url = invite.referral_url().to_string();+ let email = invite.format_email();+ assert!(!email.subject.is_empty());+ assert!(!email.body.is_empty());+ assert!(+ email.body.contains(&referral_url),+ "email body should contain the referral URL"+ );+}++#[test]+fn rate_limit_config_does_not_panic() {+ let config = InviteToMacro::rate_limit_config();+ assert_eq!(config.max_count, 1);+ assert_eq!(config.window, Duration::from_mins(MINUTES_PER_WEEK));+}++#[test]+fn rate_limit_key_does_not_panic() {+ let invite = make_invite();+ let _key = invite.rate_limit_key();+}++#[test]+fn serialization_roundtrip() {+ let invite = make_invite();+ let json = serde_json::to_string(&invite).unwrap();+ let deserialized: InviteToMacro = serde_json::from_str(&json).unwrap();+ assert_eq!(deserialized.referral_code.0, "ABC123");+}diff --git a/rust/cloud-storage/email_formatting/templates/invite.html b/rust/cloud-storage/referral_invitation/templates/invite.htmlsimilarity index 99%rename from rust/cloud-storage/email_formatting/templates/invite.htmlrename to rust/cloud-storage/referral_invitation/templates/invite.htmlindex c28e137bcb..a393345854 100644--- a/rust/cloud-storage/email_formatting/templates/invite.html+++ b/rust/cloud-storage/referral_invitation/templates/invite.html@@ -173,7 +173,7 @@padding: 2px 10px;color: #111;"- href="https://macro.com"+ href="{{ referral_url() }}"target="_blank">Join User