//! Helpers for reading the `X-Organization-Id` header.
use actix_web::HttpRequest;

/// Returns the organizational identifier sent by the caller.
pub fn get_x_organization_id(req: &HttpRequest) -> Option<String> {
    req.headers()
        .get("X-Organization-Id")
        .and_then(|h| h.to_str().ok())
        .map(|s| s.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use actix_web::test::TestRequest;

    #[actix_web::test]
    async fn returns_none_when_missing() {
        let req: HttpRequest = TestRequest::default().to_http_request();
        assert!(get_x_organization_id(&req).is_none());
    }

    #[actix_web::test]
    async fn returns_value_when_present() {
        let req: HttpRequest = TestRequest::default()
            .insert_header(("X-Organization-Id", "org-xyz"))
            .to_http_request();
        assert_eq!(get_x_organization_id(&req).unwrap(), "org-xyz");
    }
}
