Skip to content

Commit b93c054

Browse files
committed
refactor(api): add optionnal staff discord id to api external's 'createt ticket' endpoint
1 parent b7aee94 commit b93c054

2 files changed

Lines changed: 63 additions & 10 deletions

File tree

  • rustmail_types/src/api
  • rustmail/src/api/handler/externals/tickets

rustmail/src/api/handler/externals/tickets/create.rs

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::commands::send_welcome_message;
12
use crate::db::repr::{ApiKey, Permission};
23
use crate::prelude::api::*;
34
use crate::prelude::db::*;
@@ -28,7 +29,19 @@ pub async fn handle_external_ticket_create(
2829
)
2930
})?;
3031

32+
let staff_discord_id_u64 = if let Some(staff_id) = update.staff_discord_id {
33+
Some(staff_id.parse::<u64>().map_err(|_| {
34+
(
35+
StatusCode::BAD_REQUEST,
36+
"Invalid staff Discord ID format".to_string(),
37+
)
38+
})?)
39+
} else {
40+
None
41+
};
42+
3143
let user_id = UserId::new(user_id_u64);
44+
let staff_id = staff_discord_id_u64.map(UserId::new);
3245

3346
let (mut config, db_pool, bot_http, command_tx) = {
3447
let state = bot_state.lock().await;
@@ -64,8 +77,8 @@ pub async fn handle_external_ticket_create(
6477
config.db_pool = Some(db_pool.clone());
6578

6679
println!(
67-
"API Key #{} creating ticket for Discord ID: {}",
68-
api_key.id, user_id_u64
80+
"API Key #{} creating ticket for Discord ID: {} Staff Discord ID (optional): {:?}",
81+
api_key.id, user_id_u64, staff_id
6982
);
7083

7184
let user = bot_http.get_user(user_id).await.map_err(|e| {
@@ -75,6 +88,17 @@ pub async fn handle_external_ticket_create(
7588
)
7689
})?;
7790

91+
let staff = if let Some(staff_id) = staff_id {
92+
Some(bot_http.get_user(staff_id).await.map_err(|e| {
93+
(
94+
StatusCode::NOT_FOUND,
95+
format!("Staff Discord user not found: {}", e),
96+
)
97+
})?)
98+
} else {
99+
None
100+
};
101+
78102
if user.bot {
79103
return Err((
80104
StatusCode::BAD_REQUEST,
@@ -207,16 +231,44 @@ pub async fn handle_external_ticket_create(
207231
.send(true)
208232
.await
209233
{
210-
eprintln!("Failed to send message to channel via MessageBuilder: {:?}", e);
234+
eprintln!(
235+
"Failed to send message to channel via MessageBuilder: {:?}",
236+
e
237+
);
211238
}
212239

213-
if let Err(e) = MessageBuilder::system_message(&ctx, &config)
214-
.content(&config.bot.welcome_message)
215-
.to_user(user_id)
216-
.send(true)
217-
.await
218-
{
219-
eprintln!("Failed to send DM via MessageBuilder: {:?}", e);
240+
if let Some(staff_user) = staff {
241+
if staff_user.bot {
242+
eprintln!(
243+
"Warning: staff user {} is a bot, skipping ping",
244+
staff_user.id
245+
);
246+
} else {
247+
send_welcome_message(&ctx, &channel, &config, &user).await;
248+
249+
if let Err(e) = MessageBuilder::system_message(&ctx, &config)
250+
.mention(vec![staff_user.id])
251+
.to_channel(channel.id)
252+
.send(true)
253+
.await
254+
{
255+
eprintln!(
256+
"Failed to send staff ping message via MessageBuilder: {:?}",
257+
e
258+
);
259+
}
260+
261+
println!(
262+
"API Key #{} - Staff member {} ({}) pinged in ticket for user {}",
263+
api_key.id, staff_user.name, staff_user.id, username
264+
);
265+
}
266+
} else {
267+
let _ = MessageBuilder::system_message(&ctx, &config)
268+
.content(&config.bot.welcome_message)
269+
.to_user(user_id)
270+
.send(true)
271+
.await;
220272
}
221273

222274
println!(

rustmail_types/src/api/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct ConfigResponse {
2020
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
2121
pub struct CreateTicket {
2222
pub discord_id: String,
23+
pub staff_discord_id: Option<String>,
2324
}
2425

2526
#[derive(Debug, Clone, Serialize, Deserialize)]

0 commit comments

Comments
 (0)