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 <noreply@anthropic.com>
This commit is contained in:
2026-04-30 14:54:05 +08:00
parent db08280f6e
commit b4a86071dc
+17 -1
View File
@@ -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)