From a16495c1345d71f06a1851e137eee7a4445d236f Mon Sep 17 00:00:00 2001 From: yoyo930021 Date: Tue, 9 Apr 2024 23:58:19 +0800 Subject: [PATCH] fix(server): validate when if field --- src/modules/ticket/api.rs | 2 +- src/modules/ticket/models.rs | 37 ++++++++++++++++++++++++++---------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/modules/ticket/api.rs b/src/modules/ticket/api.rs index 99237a6..7ea4723 100644 --- a/src/modules/ticket/api.rs +++ b/src/modules/ticket/api.rs @@ -201,7 +201,7 @@ async fn process_ticket_flow<'a>( } let process_schema = process_flow - .get_schema(&mut conn) + .get_schema(&mut conn, &user) .await .map_err(|err| AppError::internal(err.to_string()))?; diff --git a/src/modules/ticket/models.rs b/src/modules/ticket/models.rs index 00265dd..9777ad1 100644 --- a/src/modules/ticket/models.rs +++ b/src/modules/ticket/models.rs @@ -481,6 +481,7 @@ impl TicketSchemaFlow { pub async fn get_detail( &self, conn: &mut crate::DbConn, + optional_user: Option<&User> ) -> Result { let (flow, form, review) = ticket_schema_flows::table .filter(ticket_schema_flows::id.eq(self.id)) @@ -500,15 +501,30 @@ impl TicketSchemaFlow { .load(conn) .await?; - let mut form_schema = FormSchema { - form, - fields: fields - .iter() - .cloned() - .filter(|field| field.ticket_schema_form_id == form_id) - .collect::>(), - }; - form_schema.fields.sort_by_key(|field| field.order); + if let Some(user) = optional_user { + let form_schema = FormSchema::new_with_user( + conn, + user, + form, + fields + .iter() + .cloned() + .filter(|field| field.ticket_schema_form_id == form_id) + .collect::>(), + ) + .await; + + return Ok(TicketSchemaFlowItem { + schema: flow, + module: TicketSchemaFlowValue::Form(form_schema), + }); + } + + let form_schema = FormSchema::new(form, fields + .iter() + .cloned() + .filter(|field| field.ticket_schema_form_id == form_id) + .collect::>()); return Ok(TicketSchemaFlowItem { schema: flow, @@ -970,10 +986,11 @@ impl TicketFlow { pub async fn get_schema( &self, conn: &mut crate::DbConn, + user: &User, ) -> Result { let schema_flow = TicketSchemaFlow::find(conn, self.ticket_schema_flow_id).await?; - schema_flow.get_detail(conn).await + schema_flow.get_detail(conn, Some(user)).await } pub async fn save(&self, conn: &mut crate::DbConn) -> Result {