Skip to content

Fix up updating an OverarchingGoal entity #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions entity/src/overarching_goals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize, ToSchema)]
#[schema(as = entity::overarching_goals::Model)]
#[sea_orm(schema_name = "refactor_platform", table_name = "overarching_goals")]
pub struct Model {
#[serde(skip_deserializing)]
#[sea_orm(primary_key)]
pub id: Id,
pub coaching_session_id: Option<Id>,
pub coaching_session_id: Id,
#[serde(skip_deserializing)]
pub user_id: Id,
pub title: Option<String>,
pub body: Option<String>,
#[serde(skip_deserializing)]
pub status: Status,
#[serde(skip_deserializing)]
pub status_changed_at: Option<DateTimeWithTimeZone>,
Expand Down
31 changes: 22 additions & 9 deletions entity_api/src/overarching_goal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::error::{EntityApiErrorCode, Error};
use crate::uuid_parse_str;
use entity::overarching_goals::{self, ActiveModel, Entity, Model};
use entity::{status::Status, Id};
use sea_orm::ActiveValue;
use sea_orm::{
entity::prelude::*,
ActiveModelTrait,
Expand Down Expand Up @@ -29,6 +30,9 @@ pub async fn create(
user_id: Set(user_id),
title: Set(overarching_goal_model.title),
body: Set(overarching_goal_model.body),
status: Set(overarching_goal_model.status),
status_changed_at: Set(Some(now.into())),
completed_at: Unchanged(overarching_goal_model.completed_at),
created_at: Set(now.into()),
updated_at: Set(now.into()),
..Default::default()
Expand All @@ -50,17 +54,26 @@ pub async fn update(db: &DatabaseConnection, id: Id, model: Model) -> Result<Mod
overarching_goal
);

// Automatically update status_changed_at if the last status and new status differ:
let av_status_changed_at: ActiveValue<Option<DateTimeWithTimeZone>> =
if model.status != overarching_goal.status {
debug!("Updating status_changed_at for Overarching Goal to now");
Set(Some(chrono::Utc::now().into()))
} else {
Unchanged(model.status_changed_at)
};

let active_model: ActiveModel = ActiveModel {
id: Unchanged(model.id),
coaching_session_id: Unchanged(model.coaching_session_id),
user_id: Unchanged(model.user_id),
id: Unchanged(overarching_goal.id),
coaching_session_id: Unchanged(overarching_goal.coaching_session_id),
user_id: Unchanged(overarching_goal.user_id),
body: Set(model.body),
title: Set(model.title),
status: Set(model.status),
status_changed_at: Set(model.status_changed_at),
status_changed_at: av_status_changed_at,
completed_at: Set(model.completed_at),
updated_at: Set(chrono::Utc::now().into()),
created_at: Unchanged(model.created_at),
created_at: Unchanged(overarching_goal.created_at),
};

Ok(active_model.update(db).await?.try_into_model()?)
Expand Down Expand Up @@ -187,7 +200,7 @@ mod tests {
let overarching_goal_model = Model {
id: Id::new_v4(),
user_id: Id::new_v4(),
coaching_session_id: Some(Id::new_v4()),
coaching_session_id: Id::new_v4(),
title: Some("title".to_owned()),
body: Some("This is a overarching_goal".to_owned()),
status_changed_at: None,
Expand Down Expand Up @@ -215,7 +228,7 @@ mod tests {

let overarching_goal_model = Model {
id: Id::new_v4(),
coaching_session_id: Some(Id::new_v4()),
coaching_session_id: Id::new_v4(),
title: Some("title".to_owned()),
body: Some("This is a overarching_goal".to_owned()),
user_id: Id::new_v4(),
Expand Down Expand Up @@ -251,7 +264,7 @@ mod tests {

let overarching_goal_model = Model {
id: Id::new_v4(),
coaching_session_id: Some(Id::new_v4()),
coaching_session_id: Id::new_v4(),
title: Some("title".to_owned()),
body: Some("This is a overarching_goal".to_owned()),
user_id: Id::new_v4(),
Expand All @@ -264,7 +277,7 @@ mod tests {

let updated_overarching_goal_model = Model {
id: Id::new_v4(),
coaching_session_id: Some(Id::new_v4()),
coaching_session_id: Id::new_v4(),
title: Some("title".to_owned()),
body: Some("This is a overarching_goal".to_owned()),
user_id: Id::new_v4(),
Expand Down
5 changes: 3 additions & 2 deletions migration/src/refactor_platform_rs.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- SQL dump generated using DBML (dbml.dbdiagram.io)
-- SQL dump generated using DBML (dbml-lang.org)
-- Database: PostgreSQL
-- Generated at: 2024-09-26T18:27:03.587Z
-- Generated at: 2024-10-07T03:52:01.453Z


CREATE TYPE "status" AS ENUM (
'not_started',
Expand Down
31 changes: 26 additions & 5 deletions web/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,48 @@ impl IntoResponse for Error {
fn into_response(self) -> Response {
match self.0.error_code {
EntityApiErrorCode::InvalidQueryTerm => {
debug!("Error: {:#?}, mapping to UNPROCESSABLE_ENTITY", self);
error!(
"Error: {:#?}, mapping to UNPROCESSABLE_ENTITY (reason: {})",
self,
self.0
.inner
.as_ref()
.map_or_else(|| "unspecified".to_string(), |err| err.to_string())
);

(StatusCode::UNPROCESSABLE_ENTITY, "UNPROCESSABLE ENTITY").into_response()
}
EntityApiErrorCode::SystemError => {
debug!("Error: {:#?}, mapping to INTERNAL_SERVER_ERROR", self);
error!(
"Error: {:#?}, mapping to INTERNAL_SERVER_ERROR (reason: {})",
self,
self.0
.inner
.as_ref()
.map_or_else(|| "unspecified".to_string(), |err| err.to_string())
);

(StatusCode::INTERNAL_SERVER_ERROR, "INTERNAL SERVER ERROR").into_response()
}
EntityApiErrorCode::RecordNotFound => {
debug!("Error: {:#?}, mapping to NO_CONTENT", self);
error!("Error: {:#?}, mapping to NO_CONTENT", self);

(StatusCode::NOT_FOUND, "NOT FOUND").into_response()
}
EntityApiErrorCode::RecordNotUpdated => {
debug!("Error: {:#?}, mapping to UNPROCESSABLE_ENTITY", self);
error!(
"Error: {:#?}, mapping to UNPROCESSABLE_ENTITY (reason: {})",
self,
self.0
.inner
.as_ref()
.map_or_else(|| "unspecified".to_string(), |err| err.to_string())
);

(StatusCode::UNPROCESSABLE_ENTITY, "UNPROCESSABLE ENTITY").into_response()
}
EntityApiErrorCode::RecordUnauthenticated => {
debug!("Error: {:#?}, mapping to UNAUTHORIZED", self);
error!("Error: {:#?}, mapping to UNAUTHORIZED", self);

(StatusCode::UNAUTHORIZED, "UNAUTHORIZED").into_response()
}
Expand Down
Loading