Skip to main content

cima_rs/endpoints/
master_data.rs

1use crate::api_client::CimaClient;
2use crate::models::{MasterDataType, MasterItem};
3use anyhow::{Context, Result};
4
5/// Master data search parameters
6#[derive(Debug, Default, Clone)]
7pub struct MasterDataParams {
8    /// Element name
9    pub name: Option<String>,
10    /// Element ID
11    pub id: Option<i32>,
12    /// Element code
13    pub code: Option<String>,
14    /// 1: narcotics (only for active ingredients)
15    pub narcotic: Option<u8>,
16    /// 1: psychotropics (only for active ingredients)
17    pub psychotropic: Option<u8>,
18    /// 1: narcotics or psychotropics (only for active ingredients)
19    pub narcotic_or_psychotropic: Option<u8>,
20    /// 0: returns both associated with medications and those that are not
21    pub in_use: Option<u8>,
22    /// Page number
23    pub page: Option<u32>,
24}
25
26impl MasterDataParams {
27    pub fn new() -> Self {
28        Self::default()
29    }
30
31    pub(crate) fn to_query_params(&self, data_type: MasterDataType) -> Vec<(&str, String)> {
32        let mut params = vec![("maestra", data_type.as_u8().to_string())];
33
34        if let Some(ref v) = self.name {
35            params.push(("nombre", v.clone()));
36        }
37        if let Some(v) = self.id {
38            params.push(("id", v.to_string()));
39        }
40        if let Some(ref v) = self.code {
41            params.push(("codigo", v.clone()));
42        }
43        if let Some(v) = self.narcotic {
44            params.push(("estupefaciente", v.to_string()));
45        }
46        if let Some(v) = self.psychotropic {
47            params.push(("psicotropo", v.to_string()));
48        }
49        if let Some(v) = self.narcotic_or_psychotropic {
50            params.push(("estuopsico", v.to_string()));
51        }
52        if let Some(v) = self.in_use {
53            params.push(("enuso", v.to_string()));
54        }
55        if let Some(v) = self.page {
56            params.push(("pagina", v.to_string()));
57        }
58
59        params
60    }
61}
62
63impl CimaClient {
64    /// Get elements from a master data catalog (reference catalog)
65    ///
66    /// **Important**: The CIMA API requires at least one filter parameter to be set
67    /// (name, id, code, narcotic, psychotropic, narcotic_or_psychotropic, or in_use).
68    /// Without any filter, the API returns 204 No Content.
69    ///
70    /// Returns a paginated response with master data items.
71    pub async fn get_master_data(
72        &self,
73        data_type: MasterDataType,
74        params: &MasterDataParams,
75    ) -> Result<crate::models::PaginatedResponse<MasterItem>> {
76        let query_params = params.to_query_params(data_type);
77
78        self.get_with_params("maestras", &query_params)
79            .await
80            .context("Failed to get master data")
81    }
82}