| 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 | /// Mailbox language (`en-CH`); Outlook derives the first day of week from it. |
| 25 | pub locale: Option<String>, |
| 26 | } |
| 27 | |
| 28 | impl Graph { |
| 29 | pub async fn working_hours(&self) -> Result<MailboxWorkingHours> { |
| 30 | let url = urls::mailbox_settings(); |
| 31 | let body = self.get(&url, &Scope::graph(MAILBOX_SCOPE)).await?; |
| 32 | parse_working_hours(&body) |
| 33 | .ok_or_else(|| Error::from(session::Error::api(200, &url, body.clone()))) |
| 34 | } |
| 35 | |
| 36 | pub async fn set_working_hours(&self, hours: &MailboxWorkingHours) -> Result<()> { |
| 37 | self.session() |
| 38 | .request( |
| 39 | Method::Patch, |
| 40 | &urls::mailbox_settings(), |
| 41 | &Scope::graph(MAILBOX_SCOPE), |
| 42 | Some(working_hours_body(hours)), |
| 43 | ) |
| 44 | .await?; |
| 45 | Ok(()) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | pub fn parse_working_hours(body: &Value) -> Option<MailboxWorkingHours> { |
| 50 | let hours = body.get("workingHours")?; |
| 51 | let days = hours |
| 52 | .get("daysOfWeek")? |
| 53 | .as_array()? |
| 54 | .iter() |
| 55 | .filter_map(Value::as_str) |
| 56 | .filter_map(|day| day.parse::<Weekday>().ok()) |
| 57 | .collect(); |
| 58 | let time = |key: &str| { |
| 59 | let text = hours.get(key)?.as_str()?; |
| 60 | NaiveTime::parse_from_str(text, TIME_FORMAT) |
| 61 | .or_else(|_| NaiveTime::parse_from_str(text, "%H:%M")) |
| 62 | .ok() |
| 63 | }; |
| 64 | Some(MailboxWorkingHours { |
| 65 | days, |
| 66 | start: time("startTime")?, |
| 67 | end: time("endTime")?, |
| 68 | time_zone: hours |
| 69 | .pointer("/timeZone/name") |
| 70 | .and_then(Value::as_str) |
| 71 | .map(str::to_owned), |
| 72 | locale: body |
| 73 | .pointer("/language/locale") |
| 74 | .and_then(Value::as_str) |
| 75 | .filter(|locale| !locale.is_empty()) |
| 76 | .map(str::to_owned), |
| 77 | }) |
| 78 | } |
| 79 | |
| 80 | pub fn working_hours_body(hours: &MailboxWorkingHours) -> Value { |
| 81 | let mut working = json!({ |
| 82 | "daysOfWeek": hours.days.iter().map(|day| day_name(*day)).collect::<Vec<_>>(), |
| 83 | "startTime": graph_time(hours.start), |
| 84 | "endTime": graph_time(hours.end), |
| 85 | }); |
| 86 | if let Some(name) = &hours.time_zone { |
| 87 | working["timeZone"] = json!({ "name": name }); |
| 88 | } |
| 89 | json!({ "workingHours": working }) |
| 90 | } |
| 91 | |
| 92 | /// Graph writes seven fraction digits; working hours never carry fractions. |
| 93 | fn graph_time(time: NaiveTime) -> String { |
| 94 | format!("{}.0000000", time.format("%H:%M:%S")) |
| 95 | } |
| 96 | |
| 97 | fn day_name(day: Weekday) -> &'static str { |
| 98 | match day { |
| 99 | Weekday::Mon => "monday", |
| 100 | Weekday::Tue => "tuesday", |
| 101 | Weekday::Wed => "wednesday", |
| 102 | Weekday::Thu => "thursday", |
| 103 | Weekday::Fri => "friday", |
| 104 | Weekday::Sat => "saturday", |
| 105 | Weekday::Sun => "sunday", |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | #[cfg(test)] |
| 110 | mod tests { |
| 111 | use super::*; |
| 112 | |
| 113 | fn captured() -> Value { |
| 114 | // Shape of GET /me/mailboxSettings as returned on 2026-11-15. |
| 115 | json!({ |
| 116 | "timeFormat": "HH:mm", |
| 117 | "dateFormat": "d MMM yyyy", |
| 118 | "language": {"locale": "en-CH", "displayName": "English (Switzerland)"}, |
| 119 | "workingHours": { |
| 120 | "daysOfWeek": ["monday", "tuesday", "wednesday", "thursday", "friday"], |
| 121 | "startTime": "08:00:00.0000000", |
| 122 | "endTime": "17:00:00.0000000", |
| 123 | "timeZone": {"name": "W. Europe Standard Time"} |
| 124 | } |
| 125 | }) |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn mailbox_working_hours_parse() { |
| 130 | let hours = parse_working_hours(&captured()).unwrap(); |
| 131 | assert_eq!( |
| 132 | hours.days, |
| 133 | [ |
| 134 | Weekday::Mon, |
| 135 | Weekday::Tue, |
| 136 | Weekday::Wed, |
| 137 | Weekday::Thu, |
| 138 | Weekday::Fri |
| 139 | ] |
| 140 | ); |
| 141 | assert_eq!(hours.start, NaiveTime::from_hms_opt(8, 0, 0).unwrap()); |
| 142 | assert_eq!(hours.end, NaiveTime::from_hms_opt(17, 0, 0).unwrap()); |
| 143 | assert_eq!(hours.time_zone.as_deref(), Some("W. Europe Standard Time")); |
| 144 | assert_eq!(hours.locale.as_deref(), Some("en-CH")); |
| 145 | } |
| 146 | |
| 147 | #[test] |
| 148 | fn mailbox_working_hours_body_round_trips() { |
| 149 | let mut hours = parse_working_hours(&captured()).unwrap(); |
| 150 | hours.days.retain(|day| *day != Weekday::Fri); |
| 151 | hours.start = NaiveTime::from_hms_opt(7, 30, 0).unwrap(); |
| 152 | let body = working_hours_body(&hours); |
| 153 | assert_eq!( |
| 154 | body["workingHours"]["daysOfWeek"], |
| 155 | json!(["monday", "tuesday", "wednesday", "thursday"]) |
| 156 | ); |
| 157 | assert_eq!(body["workingHours"]["startTime"], "07:30:00.0000000"); |
| 158 | assert_eq!( |
| 159 | body["workingHours"]["timeZone"]["name"], |
| 160 | "W. Europe Standard Time" |
| 161 | ); |
| 162 | let parsed = parse_working_hours(&body).unwrap(); |
| 163 | assert_eq!( |
| 164 | (parsed.days, parsed.start, parsed.end), |
| 165 | (hours.days, hours.start, hours.end) |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | #[test] |
| 170 | fn mailbox_without_working_hours_is_none() { |
| 171 | assert!(parse_working_hours(&json!({"timeFormat": "HH:mm"})).is_none()); |
| 172 | } |
| 173 | } |