1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use crate::{error::MongoError, vacuum_cursor, BsonTransform};
use itertools::Itertools;
use mongodb::{
    bson::{from_bson, Bson, Document},
    options::*,
};
use query_structure::{Model, PrismaValue};
use std::collections::HashMap;

#[allow(clippy::large_enum_variant)]
pub enum MongoCommand {
    Raw {
        cmd: Document,
    },
    Handled {
        collection: String,
        operation: MongoOperation,
    },
}

#[allow(clippy::large_enum_variant)]
pub enum MongoOperation {
    Find(Option<Document>, Option<FindOptions>),
    Aggregate(Vec<Document>, Option<AggregateOptions>),
}

impl MongoCommand {
    pub fn from_raw_query(
        model: Option<&Model>,
        inputs: HashMap<String, PrismaValue>,
        query_type: Option<String>,
    ) -> crate::Result<MongoCommand> {
        match (query_type.as_deref(), model) {
            (Some("findRaw"), Some(m)) => Self::find(m, inputs),
            (Some("aggregateRaw"), Some(m)) => Self::aggregate(m, inputs),
            (Some("runCommandRaw"), _) => Self::raw(inputs),
            _ => unreachable!("Unexpected MongoDB raw query"),
        }
    }

    fn find(model: &Model, inputs: HashMap<String, PrismaValue>) -> crate::Result<MongoCommand> {
        let filter = inputs.get_document("filter")?;
        let options = inputs
            .get_document("options")?
            .map(Bson::Document)
            .map(from_bson::<FindOptions>)
            .transpose()?;

        Ok(Self::Handled {
            collection: model.db_name().to_owned(),
            operation: MongoOperation::Find(filter, options),
        })
    }

    fn aggregate(model: &Model, inputs: HashMap<String, PrismaValue>) -> crate::Result<MongoCommand> {
        let pipeline = inputs.get_array_document("pipeline")?.unwrap_or_default();
        let options = inputs
            .get_document("options")?
            .map(Bson::Document)
            .map(from_bson::<AggregateOptions>)
            .transpose()?;

        Ok(MongoCommand::Handled {
            collection: model.db_name().to_owned(),
            operation: MongoOperation::Aggregate(pipeline, options),
        })
    }

    fn raw(inputs: HashMap<String, PrismaValue>) -> crate::Result<MongoCommand> {
        let cmd = inputs.get_required_document("command")?;

        Ok(MongoCommand::Raw { cmd })
    }
}

trait QueryRawParsingExtension {
    fn get_document(&self, key: &str) -> crate::Result<Option<Document>>;
    fn get_required_document(&self, key: &str) -> crate::Result<Document>;
    fn get_array_document(&self, key: &str) -> crate::Result<Option<Vec<Document>>>;
}

impl QueryRawParsingExtension for HashMap<String, PrismaValue> {
    fn get_document(&self, key: &str) -> crate::Result<Option<Document>> {
        self.get(key).map(|pv| pv.try_as_bson_document(key)).transpose()
    }

    fn get_required_document(&self, key: &str) -> crate::Result<Document> {
        self.get_document(key)?
            .ok_or_else(|| MongoError::MissingRequiredArgumentError {
                argument: key.to_string(),
            })
    }

    fn get_array_document(&self, key: &str) -> crate::Result<Option<Vec<Document>>> {
        self.get(key)
            .map(|pv| {
                let stages: Vec<_> = pv
                    .try_as_bson_array(key)?
                    .into_iter()
                    .map(|stage| {
                        stage.into_document().map_err(|_| {
                            MongoError::argument_type_mismatch(key, format!("{pv:?}"), "Json::Array<Json::Object>")
                        })
                    })
                    .try_collect()?;

                Ok(stages)
            })
            .transpose()
    }
}

trait QueryRawConversionExtension {
    fn try_as_bson(&self, arg_name: &str) -> crate::Result<Bson>;
    fn try_as_bson_document(&self, arg_name: &str) -> crate::Result<Document>;
    fn try_as_bson_array(&self, arg_name: &str) -> crate::Result<Vec<Bson>>;
}

impl QueryRawConversionExtension for &PrismaValue {
    fn try_as_bson(&self, arg_name: &str) -> crate::Result<Bson> {
        match self {
            PrismaValue::Json(json) => {
                let json: serde_json::Value = serde_json::from_str(json.as_str())?;
                let bson = Bson::try_from(json)?;

                Ok(bson)
            }
            PrismaValue::List(list) => {
                let bson = list
                    .iter()
                    .map(|pv| pv.try_as_bson(arg_name))
                    .collect::<crate::Result<Vec<_>>>()?;

                Ok(Bson::Array(bson))
            }
            x => Err(MongoError::argument_type_mismatch(arg_name, format!("{x:?}"), "Json")),
        }
    }

    fn try_as_bson_document(&self, arg_name: &str) -> crate::Result<Document> {
        let bson = self.try_as_bson(arg_name)?;

        match bson {
            Bson::Document(doc) => Ok(doc),
            bson => Err(MongoError::argument_type_mismatch(
                arg_name,
                format!("{bson:?}"),
                "Json::Object",
            )),
        }
    }

    fn try_as_bson_array(&self, arg_name: &str) -> crate::Result<Vec<Bson>> {
        let bson = self.try_as_bson(arg_name)?;

        match bson {
            Bson::Array(doc) => Ok(doc),
            bson => Err(MongoError::argument_type_mismatch(
                arg_name,
                format!("{bson:?}"),
                "Json::Array",
            )),
        }
    }
}

pub async fn cursor_to_json(
    cursor: mongodb::SessionCursor<Document>,
    session: &mut mongodb::ClientSession,
) -> crate::Result<serde_json::Value> {
    let bson_result = vacuum_cursor(cursor, session)
        .await?
        .into_iter()
        .map(Bson::Document)
        .collect_vec();
    let json_result: serde_json::Value = Bson::Array(bson_result).into();

    Ok(json_result)
}