Skip to content

Commit 2de9d63

Browse files
0xPoeTurbo87
authored andcommitted
fix: use handle_expiring_token to send email
Signed-off-by: hi-rustin <[email protected]>
1 parent 3a629f6 commit 2de9d63

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

src/worker/jobs/expiry_notification.rs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use chrono::SecondsFormat;
66
use crates_io_worker::BackgroundJob;
77
use diesel::dsl::IntervalDsl;
88
use diesel::{
9-
dsl::now, BoolExpressionMethods, Connection, ExpressionMethods, NullableExpressionMethods,
10-
PgConnection, QueryDsl, QueryResult, RunQueryDsl, SelectableHelper,
9+
dsl::now, BoolExpressionMethods, ExpressionMethods, NullableExpressionMethods, PgConnection,
10+
QueryDsl, QueryResult, RunQueryDsl, SelectableHelper,
1111
};
1212
use std::sync::Arc;
1313

@@ -51,35 +51,37 @@ fn check(emails: &Emails, conn: &mut PgConnection) -> anyhow::Result<()> {
5151
warn!("The maximum number of API tokens per query has been reached. More API tokens might be processed on the next run.");
5252
}
5353
for token in &expired_tokens {
54-
conn.transaction(|conn| {
55-
// Send notification.
56-
let user = User::find(conn, token.user_id)?;
57-
let Some(recipient) = user.email(conn)? else {
58-
return Err(anyhow!("No address found"));
59-
};
60-
let email = ExpiryNotificationEmail {
61-
name: &user.gh_login,
62-
token_name: &token.name,
63-
expiry_date: token.expired_at.unwrap().and_utc(),
64-
};
65-
match emails.send(&recipient, email) {
66-
Ok(_) => {
67-
// Update the token to prevent duplicate notifications.
68-
diesel::update(token)
69-
.set(api_tokens::expiry_notification_at.eq(now.nullable()))
70-
.execute(conn)?;
71-
}
72-
Err(e) => {
73-
error!(?e, ?recipient, "Failed to send notification");
74-
return Err(anyhow!("Failed to send notification: {}", e));
75-
}
76-
}
77-
Ok::<_, anyhow::Error>(())
78-
})?;
54+
if let Err(e) = handle_expiring_token(conn, token, emails) {
55+
error!(?e, "Failed to handle expiring token");
56+
}
7957
}
8058

8159
Ok(())
8260
}
61+
62+
fn handle_expiring_token(
63+
conn: &mut PgConnection,
64+
token: &ApiToken,
65+
emails: &Emails,
66+
) -> Result<(), anyhow::Error> {
67+
let user = User::find(conn, token.user_id)?;
68+
let recipient = match user.email(conn)? {
69+
Some(email) => email,
70+
None => return Err(anyhow!("No address found")),
71+
};
72+
let email = ExpiryNotificationEmail {
73+
name: &user.gh_login,
74+
token_name: &token.name,
75+
expiry_date: token.expired_at.unwrap().and_utc(),
76+
};
77+
emails.send(&recipient, email)?;
78+
// Update the token to prevent duplicate notifications.
79+
diesel::update(token)
80+
.set(api_tokens::expiry_notification_at.eq(now.nullable()))
81+
.execute(conn)?;
82+
Ok(())
83+
}
84+
8385
/// Find all tokens that are not revoked and will expire within the specified number of days.
8486
pub fn find_tokens_expiring_within_days(
8587
conn: &mut PgConnection,

0 commit comments

Comments
 (0)