| 1 | //! Outlook mailbox working hours (`/me/mailboxSettings`), the setting Teams and |
| 2 | //! Outlook read for work days and hours. |
| 3 | //! |
| 4 | //! `MailboxSettings.Read` is not granted to the Teams client; `ReadWrite` is. |
| 5 | |
| 6 | use chrono::{NaiveTime, Weekday}; |
| 7 | use serde_json::{Value, json}; |
| 8 | use session::{Method, Scope}; |
| 9 | |
| 10 | use crate::client::Graph; |
| 11 | use crate::error::{Error, Result}; |
| 12 | use crate::urls; |
| 13 | |
| 14 | const MAILBOX_SCOPE: &str = "MailboxSettings.ReadWrite"; |
| 15 | const TIME_FORMAT: &str = "%H:%M:%S%.f"; |
| 16 | |
| 17 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 18 | pub struct MailboxWorkingHours { |
| 19 | pub days: Vec<Weekday>, |
| 20 | pub start: NaiveTime, |
| 21 | pub end: NaiveTime, |
| 22 | /// Windows or IANA name as the mailbox stores it; written back unchanged. |
| 23 | pub time_zone: Option<String>, |
| 24 | } |
| 25 | |
| 26 | impl Graph { |
| 27 | pub async fn working_hours(&self) -> Result<MailboxWorkingHours> { |
| 28 | let url = urls::mailbox_settings(); |
| 29 | let body = self.get(&url, &Scope::graph(MAILBOX_SCOPE)).await?; |
| 30 | parse_working_hours(&body) |
| 31 | .ok_or_else(|| Error::from(session::Error::api(200, &url, body.clone()))) |
| 32 | } |
| 33 | |
| 34 | pub async fn set_working_hours(&self, hours: &MailboxWorkingHours) -> Result<()> { |
| 35 | self.session() |
| 36 | .request( |
| 37 | Method::Patch, |
| 38 | &urls::mailbox_settings(), |
| 39 | &Scope::graph(MAILBOX_SCOPE), |
| 40 | Some(working_hours_body(hours)), |
| 41 | ) |
| 42 | .await?; |
| 43 | Ok(()) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | pub fn parse_working_hours(body: &Value) -> Option<MailboxWorkingHours> { |
| 48 | let hours = body.get("workingHours")?; |
| 49 | let days = hours |
| 50 | .get("daysOfWeek")? |
| 51 | .as_array()? |
| 52 | .iter() |
| 53 | .filter_map(Value::as_str) |
| 54 | .filter_map(|day| day.parse::<Weekday>().ok()) |
| 55 | .collect(); |
| 56 | let time = |key: &str| { |
| 57 | let text = hours.get(key)?.as_str()?; |
| 58 | NaiveTime::parse_from_str(text, TIME_FORMAT) |
| 59 | .or_else(|_| NaiveTime::parse_from_str(text, "%H:%M")) |
| 60 | .ok() |
| 61 | }; |
| 62 | Some(MailboxWorkingHours { |
| 63 | days, |
| 64 | start: time("startTime")?, |
| 65 | end: time("endTime")?, |
| 66 | time_zone: hours |
| 67 | .pointer("/timeZone/name") |
| 68 | .and_then(Value::as_str) |
| 69 | .map(str::to_owned), |
| 70 | }) |
| 71 | } |
| 72 | |
| 73 | pub fn working_hours_body(hours: &MailboxWorkingHours) -> Value { |
| 74 | let mut working = json!({ |
| 75 | "daysOfWeek": hours.days.iter().map(|day| day_name(*day)).collect::<Vec<_>>(), |
| 76 | "startTime": graph_time(hours.start), |
| 77 | "endTime": graph_time(hours.end), |
| 78 | }); |
| 79 | if let Some(name) = &hours.time_zone { |
| 80 | working["timeZone"] = json!({ "name": name }); |
| 81 | } |
| 82 | json!({ "workingHours": working }) |
| 83 | } |
| 84 | |
| 85 | /// Graph writes seven fraction digits; working hours never carry fractions. |
| 86 | fn graph_time(time: NaiveTime) -> String { |
| 87 | format!("{}.0000000", time.format("%H:%M:%S")) |
| 88 | } |
| 89 | |
| 90 | fn day_name(day: Weekday) -> &'static str { |
| 91 | match day { |
| 92 | Weekday::Mon => "monday", |
| 93 | Weekday::Tue => "tuesday", |
| 94 | Weekday::Wed => "wednesday", |
| 95 | Weekday::Thu => "thursday", |
| 96 | Weekday::Fri => "friday", |
| 97 | Weekday::Sat => "saturday", |
| 98 | Weekday::Sun => "sunday", |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | #[cfg(test)] |
| 103 | mod tests { |
| 104 | use super::*; |
| 105 | |
| 106 | fn captured() -> Value { |
| 107 | // Shape of GET /me/mailboxSettings as returned on 2026-11-15. |
| 108 | json!({ |
| 109 | "timeFormat": "HH:mm", |
| 110 | "dateFormat": "d MMM yyyy", |
| 111 | "workingHours": { |
| 112 | "daysOfWeek": ["monday", "tuesday", "wednesday", "thursday", "friday"], |
| 113 | "startTime": "08:00:00.0000000", |
| 114 | "endTime": "17:00:00.0000000", |
| 115 | "timeZone": {"name": "W. Europe Standard Time"} |
| 116 | } |
| 117 | }) |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn mailbox_working_hours_parse() { |
| 122 | let hours = parse_working_hours(&captured()).unwrap(); |
| 123 | assert_eq!( |
| 124 | hours.days, |
| 125 | [ |
| 126 | Weekday::Mon, |
| 127 | Weekday::Tue, |
| 128 | Weekday::Wed, |
| 129 | Weekday::Thu, |
| 130 | Weekday::Fri |
| 131 | ] |
| 132 | ); |
| 133 | assert_eq!(hours.start, NaiveTime::from_hms_opt(8, 0, 0).unwrap()); |
| 134 | assert_eq!(hours.end, NaiveTime::from_hms_opt(17, 0, 0).unwrap()); |
| 135 | assert_eq!(hours.time_zone.as_deref(), Some("W. Europe Standard Time")); |
| 136 | } |
| 137 | |
| 138 | #[test] |
| 139 | fn mailbox_working_hours_body_round_trips() { |
| 140 | let mut hours = parse_working_hours(&captured()).unwrap(); |
| 141 | hours.days.retain(|day| *day != Weekday::Fri); |
| 142 | hours.start = NaiveTime::from_hms_opt(7, 30, 0).unwrap(); |
| 143 | let body = working_hours_body(&hours); |
| 144 | assert_eq!( |
| 145 | body["workingHours"]["daysOfWeek"], |
| 146 | json!(["monday", "tuesday", "wednesday", "thursday"]) |
| 147 | ); |
| 148 | assert_eq!(body["workingHours"]["startTime"], "07:30:00.0000000"); |
| 149 | assert_eq!( |
| 150 | body["workingHours"]["timeZone"]["name"], |
| 151 | "W. Europe Standard Time" |
| 152 | ); |
| 153 | assert_eq!(parse_working_hours(&body).unwrap(), hours); |
| 154 | } |
| 155 | |
| 156 | #[test] |
| 157 | fn mailbox_without_working_hours_is_none() { |
| 158 | assert!(parse_working_hours(&json!({"timeFormat": "HH:mm"})).is_none()); |
| 159 | } |
| 160 | } |