From b4a86071dcb1596abc75639ff4185db6f842b2bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=94=B7?= Date: Thu, 30 Apr 2026 14:54:05 +0800 Subject: [PATCH] fix(htyws): preserve existing fields on course_group partial update CourseGroup has treat_none_as_null=true, so partial updates would set unsent fields to NULL. Merge with existing record before update to preserve org_id, created_by, course_section_ids, and teachers. Co-Authored-By: Claude Opus 4.7 --- htyws/src/ws_all.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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)