diff --git a/htyws/src/ws_all.rs b/htyws/src/ws_all.rs index 76099e6..262e181 100644 --- a/htyws/src/ws_all.rs +++ b/htyws/src/ws_all.rs @@ -514,12 +514,28 @@ pub fn raw_update_course_group( let token = HtyToken::from_jwt(auth.clone().deref())?; let user_id = token.hty_id.ok_or_else(|| anyhow!("hty_id is required"))?; + let mut conn_holder = extract_conn(fetch_db_conn(&db_pool)?); + let conn = conn_holder.deref_mut(); + + // Load existing record to preserve fields not sent in the partial update + let existing = CourseGroup::find_by_id( + req_course_group.id.as_ref().unwrap(), + conn, + )?; + let mut in_section_group = CourseGroup::from(&req_course_group)?; + // Merge: preserve existing values for fields not provided in the request + // (CourseGroup has treat_none_as_null=true, so None fields would be set to NULL) + if in_section_group.org_id.is_none() { in_section_group.org_id = existing.org_id; } + if in_section_group.created_by.is_none() { in_section_group.created_by = existing.created_by; } + if in_section_group.created_at.is_none() { in_section_group.created_at = existing.created_at; } + if in_section_group.course_section_ids.is_none() { in_section_group.course_section_ids = existing.course_section_ids; } + if in_section_group.teachers.is_none() { in_section_group.teachers = existing.teachers; } in_section_group.updated_by = Some(user_id); let res = CourseGroup::update( &in_section_group, - extract_conn(fetch_db_conn(&db_pool)?).deref_mut(), + conn, )?; Ok(res.id)