-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce3831f
commit d1ab22e
Showing
12 changed files
with
308 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod confirmation; | ||
pub mod send; | ||
pub mod templates; | ||
pub mod shipped; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,25 @@ | ||
use crate::{api::stripe::UserData, Mailer}; | ||
use actix_web::Result; | ||
|
||
use askama::Template; | ||
|
||
use lettre::{ | ||
message::{MultiPart, SinglePart}, | ||
AsyncTransport, Message, | ||
}; | ||
|
||
use super::templates::{Confirmation, Template}; | ||
use super::{confirmation, shipped}; | ||
|
||
use std::sync::Arc; | ||
|
||
pub async fn send_confirmation(user: UserData, mailer: Arc<Mailer>) -> Result<(), String> { | ||
let confirmation = Confirmation::from(&user); | ||
let confirmation = confirmation::Confirmation::from(&user); | ||
let html = SinglePart::html(confirmation.render().unwrap()); | ||
let plaintext = SinglePart::plain(confirmation.render_plaintext()); | ||
|
||
let email = Message::builder() | ||
.from("Kiggyshop <[email protected]>".parse().unwrap()) | ||
.to("Fay <[email protected]>".parse().unwrap()) | ||
.to(user.email.parse().unwrap()) | ||
.subject("Thank you for your order!") | ||
.multipart( | ||
MultiPart::alternative() | ||
|
@@ -30,3 +33,24 @@ pub async fn send_confirmation(user: UserData, mailer: Arc<Mailer>) -> Result<() | |
Err(e) => Err(e.to_string()), | ||
} | ||
} | ||
|
||
pub async fn send_tracking(shipping: shipped::Shipped, mailer: Arc<Mailer>) -> Result<(), String> { | ||
let html = SinglePart::html(shipping.render().unwrap()); | ||
let plaintext = SinglePart::plain(shipping.render_plaintext()); | ||
|
||
let email = Message::builder() | ||
.from("KiggyShop <[email protected]>".parse().unwrap()) | ||
.to(shipping.email.parse().unwrap()) | ||
.subject("Your orderhas shipped!") | ||
.multipart( | ||
MultiPart::alternative() | ||
.singlepart(html) | ||
.singlepart(plaintext), | ||
) | ||
.unwrap(); | ||
|
||
match mailer.send(email).await { | ||
Ok(_) => Ok(()), | ||
Err(e) => Err(e.to_string()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use model::order; | ||
|
||
#[derive(Debug, Clone, askama::Template)] | ||
#[template(path = "shipped.html")] | ||
pub struct Shipped { | ||
id: u32, | ||
name: String, | ||
pub email: String, | ||
tracking: String, | ||
} | ||
|
||
impl TryFrom<order::Order> for Shipped { | ||
type Error = String; | ||
|
||
fn try_from( | ||
order::Order { | ||
id, | ||
name, | ||
email, | ||
tracking_number, | ||
.. | ||
}: order::Order, | ||
) -> Result<Self, Self::Error> { | ||
if let Some(tracking) = tracking_number { | ||
Ok(Self { | ||
id, | ||
name, | ||
email, | ||
tracking, | ||
}) | ||
} else { | ||
Err(format!("No tracking # on order {id}")) | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<order::TableOrder> for Shipped { | ||
type Error = String; | ||
|
||
fn try_from( | ||
order::TableOrder { | ||
id, | ||
name, | ||
email, | ||
tracking_number, | ||
.. | ||
}: order::TableOrder, | ||
) -> Result<Self, Self::Error> { | ||
if let Some(tracking) = tracking_number { | ||
Ok(Self { | ||
id: id as u32, | ||
name, | ||
email, | ||
tracking, | ||
}) | ||
} else { | ||
Err(format!("Order {id} does not contain a tracking number!")) | ||
} | ||
} | ||
} | ||
|
||
impl Shipped { | ||
pub fn new<T, U>(id: T, email: U, name: U, tracking: U) -> Self | ||
where | ||
T: Into<u32>, | ||
U: std::fmt::Display, | ||
{ | ||
let id = id.into(); | ||
let name = name.to_string(); | ||
let email = email.to_string(); | ||
let tracking = tracking.to_string(); | ||
Self { | ||
id, | ||
name, | ||
email, | ||
tracking, | ||
} | ||
} | ||
|
||
pub fn render_plaintext(&self) -> String { | ||
format!( | ||
r#"Hi, {}! Your order has shipped!\nOrder #: {}\nUSPS tracking #: {}"#, | ||
self.name, self.id, self.tracking | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
{ | ||
"name": "Fay Carsons", | ||
"email": "[email protected]", | ||
"id": 1, | ||
"name": "foobar", | ||
"email": "[email protected]", | ||
"total": 23, | ||
"cart": { | ||
"0": 10, | ||
"1": 20 | ||
}, | ||
"address": { | ||
"name": "Fay", | ||
"name": "foobar", | ||
"number": 2323, | ||
"street": "Thin st", | ||
"city": "Richmond", | ||
"state": "VA", | ||
"street": "Large st", | ||
"city": "Oakland", | ||
"state": "CA", | ||
"zipcode": 23233 | ||
}, | ||
"shipped": false | ||
|
Oops, something went wrong.