1use crate::prelude::*;
2use headless_lms_utils::services::sisu::{
3 Additional, CourseUnitSearchResults, Credits, Name, ResponsibilityInfo, SearchResult,
4 SisuCourseInfoElement, SisuCourseInfoValidityPeriod, SisuPerson,
5};
6use serde_json;
7
8#[derive(Deserialize, Debug)]
9#[serde(rename_all = "camelCase")]
10pub struct MockSisuRequest {
11 code_query: String,
12}
13
14async fn mock_sisu_id_query(
15 query_params: web::Query<MockSisuRequest>,
16 app_conf: web::Data<ApplicationConfiguration>,
17) -> ControllerResult<String> {
18 assert!(app_conf.test_mode && app_conf.test_sisu);
19 let res = match query_params.code_query.as_str() {
20 "TEST001" => {
21 let sisu_ids: Vec<SearchResult> = vec![
22 SearchResult {
23 id: "mock-id-1-1".to_string(),
24 },
25 SearchResult {
26 id: "mock-id-1-2".to_string(),
27 },
28 ];
29 serde_json::to_string(
30 &(CourseUnitSearchResults {
31 search_results: sisu_ids,
32 }),
33 )
34 }
35 "TEST002" => {
36 let sisu_ids: Vec<SearchResult> = vec![
37 SearchResult {
38 id: "mock-id-2-1".to_string(),
39 },
40 SearchResult {
41 id: "mock-id-2-2".to_string(),
42 },
43 ];
44 serde_json::to_string(
45 &(CourseUnitSearchResults {
46 search_results: sisu_ids,
47 }),
48 )
49 }
50 "TEST003" => {
51 let sisu_ids: Vec<SearchResult> = vec![
52 SearchResult {
53 id: "mock-id-3-1".to_string(),
54 },
55 SearchResult {
56 id: "mock-id-3-2".to_string(),
57 },
58 ];
59 serde_json::to_string(
60 &(CourseUnitSearchResults {
61 search_results: sisu_ids,
62 }),
63 )
64 }
65
66 _ => serde_json::to_string(
67 &(CourseUnitSearchResults {
68 search_results: vec![],
69 }),
70 ),
71 }?;
72 let token = skip_authorize();
73 token.authorized_ok(res)
74}
75
76async fn mock_sisu_course_info(
77 app_conf: web::Data<ApplicationConfiguration>,
78 id: web::Path<String>,
79) -> ControllerResult<String> {
80 assert!(app_conf.test_mode && app_conf.test_sisu);
81 let res = match id.as_str() {
82 "mock-id-1-1" => serde_json::to_string(
83 &(SisuCourseInfoElement {
84 id: String::from("mock-id-1-1"),
85 university_org_ids: vec!["hy-university".to_string()],
86 group_id: "hy-CU-142971304".to_string(),
87 credits: Credits {
88 min: None,
89 max: None,
90 },
91 completion_methods: vec![],
92 name: Name {
93 en: Some("Mock Docker 1".to_string()),
94 fi: None,
95 sv: None,
96 },
97 code: "TEST001".to_string(),
98 abbreviation: None,
99 validity_period: SisuCourseInfoValidityPeriod {
100 start_date: None,
101 end_date: None,
102 },
103 grade_scale_id: "sis-hyl-hyv".to_string(),
104 tweet_text: None,
105 outcomes: Some(Additional {
106 en: Some("Mock outcomes".to_string()),
107 fi: None,
108 sv: None,
109 }),
110 prerequisites: Some(Additional {
111 en: Some("Mock prerequisites".to_string()),
112 fi: None,
113 sv: None,
114 }),
115 content: Some(Additional {
116 en: Some("Mock content".to_string()),
117 fi: None,
118 sv: None,
119 }),
120 additional: Some(Additional {
121 en: Some("Mock additional".to_string()),
122 fi: None,
123 sv: None,
124 }),
125 learning_material: Some(Additional {
126 en: Some("Mock learning materials".to_string()),
127 fi: None,
128 sv: None,
129 }),
130 literature: vec![],
131 study_level: "".to_string(),
132 course_unit_type: "".to_string(),
133 subject: None,
134 cefr_level: None,
135 organisations: vec![],
136 possible_attainment_languages: vec![],
137 part_of_degree: None,
138 responsibility_infos: Some(vec![ResponsibilityInfo {
139 role_urn: "urn:code:module-responsibility-type:responsible-teacher".to_string(),
140 person: Some(SisuPerson {
141 first_names: Some("Mock".to_string()),
142 last_name: Some("Teacher".to_string()),
143 email: Some("mock.teacher@example.com".to_string()),
144 }),
145 text: None,
146 }]),
147 }),
148 ),
149 "mock-id-2-1" => serde_json::to_string(
150 &(SisuCourseInfoElement {
151 id: String::from("mock-id-2-1"),
152 university_org_ids: vec!["hy-university".to_string()],
153 group_id: "hy-CU-142971304".to_string(),
154 credits: Credits {
155 min: None,
156 max: None,
157 },
158 completion_methods: vec![],
159 name: Name {
160 en: Some("Mock Docker 2".to_string()),
161 fi: None,
162 sv: None,
163 },
164 code: "TEST002".to_string(),
165 abbreviation: None,
166 validity_period: SisuCourseInfoValidityPeriod {
167 start_date: None,
168 end_date: None,
169 },
170 grade_scale_id: "sis-hyl-hyv".to_string(),
171 tweet_text: None,
172 outcomes: Some(Additional {
173 en: Some("Mock outcomes".to_string()),
174 fi: None,
175 sv: None,
176 }),
177 prerequisites: Some(Additional {
178 en: Some("Mock prerequisites".to_string()),
179 fi: None,
180 sv: None,
181 }),
182 content: Some(Additional {
183 en: Some("Mock content".to_string()),
184 fi: None,
185 sv: None,
186 }),
187 additional: Some(Additional {
188 en: Some("Mock additional".to_string()),
189 fi: None,
190 sv: None,
191 }),
192 learning_material: Some(Additional {
193 en: Some("Mock learning materials".to_string()),
194 fi: None,
195 sv: None,
196 }),
197 literature: vec![],
198 study_level: "".to_string(),
199 course_unit_type: "".to_string(),
200 subject: None,
201 cefr_level: None,
202 organisations: vec![],
203 possible_attainment_languages: vec![],
204 part_of_degree: None,
205 responsibility_infos: Some(vec![ResponsibilityInfo {
206 role_urn: "urn:code:module-responsibility-type:responsible-teacher".to_string(),
207 person: Some(SisuPerson {
208 first_names: Some("Mock".to_string()),
209 last_name: Some("Teacher".to_string()),
210 email: Some("mock.teacher@example.com".to_string()),
211 }),
212 text: None,
213 }]),
214 }),
215 ),
216 "mock-id-3-1" => serde_json::to_string(
217 &(SisuCourseInfoElement {
218 id: String::from("mock-id-3-1"),
219 university_org_ids: vec!["hy-university".to_string()],
220 group_id: "hy-CU-142971304".to_string(),
221 credits: Credits {
222 min: None,
223 max: None,
224 },
225 completion_methods: vec![],
226 name: Name {
227 en: Some("Mock Docker 3".to_string()),
228 fi: None,
229 sv: None,
230 },
231 code: "TEST003".to_string(),
232 abbreviation: None,
233 validity_period: SisuCourseInfoValidityPeriod {
234 start_date: None,
235 end_date: None,
236 },
237 grade_scale_id: "sis-hyl-hyv".to_string(),
238 tweet_text: None,
239 outcomes: Some(Additional {
240 en: Some("Mock outcomes".to_string()),
241 fi: None,
242 sv: None,
243 }),
244 prerequisites: Some(Additional {
245 en: Some("Mock prerequisites".to_string()),
246 fi: None,
247 sv: None,
248 }),
249 content: Some(Additional {
250 en: Some("Mock content".to_string()),
251 fi: None,
252 sv: None,
253 }),
254 additional: Some(Additional {
255 en: Some("Mock additional".to_string()),
256 fi: None,
257 sv: None,
258 }),
259 learning_material: Some(Additional {
260 en: Some("Mock learning materials".to_string()),
261 fi: None,
262 sv: None,
263 }),
264 literature: vec![],
265 study_level: "".to_string(),
266 course_unit_type: "".to_string(),
267 subject: None,
268 cefr_level: None,
269 organisations: vec![],
270 possible_attainment_languages: vec![],
271 part_of_degree: None,
272 responsibility_infos: Some(vec![ResponsibilityInfo {
273 role_urn: "urn:code:module-responsibility-type:responsible-teacher".to_string(),
274 person: Some(SisuPerson {
275 first_names: Some("Mock".to_string()),
276 last_name: Some("Teacher".to_string()),
277 email: Some("mock.teacher@example.com".to_string()),
278 }),
279 text: None,
280 }]),
281 }),
282 ),
283
284 _ => Ok("{}".to_string()),
285 }?;
286 let token = skip_authorize();
287 token.authorized_ok(res)
288}
289
290pub fn _add_routes(cfg: &mut ServiceConfig) {
291 cfg.route("course-unit-search", web::get().to(mock_sisu_id_query))
292 .route("course-units/v1/{id}", web::get().to(mock_sisu_course_info));
293}