From ecef139ab60df011dada7754516000a29b01d029 Mon Sep 17 00:00:00 2001 From: Egor Date: Wed, 20 Mar 2024 14:38:48 +0100 Subject: [PATCH] editoast: remove serde default from project and study --- .../down.sql | 29 +++++++++++++++++++ .../up.sql | 29 +++++++++++++++++++ editoast/openapi.yaml | 21 +++++++++----- editoast/src/fixtures.rs | 7 ----- editoast/src/modelsv2/projects.rs | 6 ++-- editoast/src/modelsv2/study.rs | 8 ++--- editoast/src/tables.rs | 14 ++++----- editoast/src/views/projects.rs | 22 +++++++------- editoast/src/views/study.rs | 29 ++++++++++--------- front/src/common/api/osrdEditoastApi.ts | 28 +++++++++--------- .../components/AddOrEditProjectModal.tsx | 6 ++-- .../study/components/AddOrEditStudyModal.tsx | 4 +-- front/tests/002-project-management.spec.ts | 2 +- 13 files changed, 134 insertions(+), 71 deletions(-) create mode 100644 editoast/migrations/2024-04-16-074707_project_study_default_values/down.sql create mode 100644 editoast/migrations/2024-04-16-074707_project_study_default_values/up.sql diff --git a/editoast/migrations/2024-04-16-074707_project_study_default_values/down.sql b/editoast/migrations/2024-04-16-074707_project_study_default_values/down.sql new file mode 100644 index 00000000000..6fb00a0689c --- /dev/null +++ b/editoast/migrations/2024-04-16-074707_project_study_default_values/down.sql @@ -0,0 +1,29 @@ +-- This file should undo anything in `up.sql` + +ALTER TABLE "project" +ALTER "objectives" DROP DEFAULT, +ALTER "objectives" SET NOT NULL; + +ALTER TABLE "project" +ALTER "description" DROP DEFAULT, +ALTER "description" SET NOT NULL; + +ALTER TABLE "project" +ALTER "funders" DROP DEFAULT, +ALTER "funders" SET NOT NULL; + +ALTER TABLE "study" +ALTER "description" DROP DEFAULT, +ALTER "description" SET NOT NULL; + +ALTER TABLE "study" +ALTER "business_code" DROP DEFAULT, +ALTER "business_code" SET NOT NULL; + +ALTER TABLE "study" +ALTER "service_code" DROP DEFAULT, +ALTER "service_code" SET NOT NULL; + +ALTER TABLE "study" +ALTER "study_type" DROP DEFAULT, +ALTER "study_type" SET NOT NULL; diff --git a/editoast/migrations/2024-04-16-074707_project_study_default_values/up.sql b/editoast/migrations/2024-04-16-074707_project_study_default_values/up.sql new file mode 100644 index 00000000000..b3df51a6431 --- /dev/null +++ b/editoast/migrations/2024-04-16-074707_project_study_default_values/up.sql @@ -0,0 +1,29 @@ +-- Your SQL goes here + +ALTER TABLE "project" +ALTER "objectives" SET DEFAULT '', +ALTER "objectives" DROP NOT NULL; + +ALTER TABLE "project" +ALTER "description" SET DEFAULT '', +ALTER "description" DROP NOT NULL; + +ALTER TABLE "project" +ALTER "funders" SET DEFAULT '', +ALTER "funders" DROP NOT NULL; + +ALTER TABLE "study" +ALTER "description" SET DEFAULT '', +ALTER "description" DROP NOT NULL; + +ALTER TABLE "study" +ALTER "business_code" SET DEFAULT '', +ALTER "business_code" DROP NOT NULL; + +ALTER TABLE "study" +ALTER "service_code" SET DEFAULT '', +ALTER "service_code" DROP NOT NULL; + +ALTER TABLE "study" +ALTER "study_type" SET DEFAULT '', +ALTER "study_type" DROP NOT NULL; diff --git a/editoast/openapi.yaml b/editoast/openapi.yaml index 85dadce1ad7..9b691ed7fbb 100644 --- a/editoast/openapi.yaml +++ b/editoast/openapi.yaml @@ -4819,8 +4819,10 @@ components: format: date-time type: string description: + nullable: true type: string funders: + nullable: true type: string id: format: int64 @@ -4835,15 +4837,13 @@ components: name: type: string objectives: + nullable: true type: string tags: $ref: '#/components/schemas/Tags' required: - id - name - - objectives - - description - - funders - creation_date - last_modification - tags @@ -4857,9 +4857,11 @@ components: type: integer description: maxLength: 1024 + nullable: true type: string funders: maxLength: 1024 + nullable: true type: string image: description: The id of the image document @@ -4871,6 +4873,7 @@ components: type: string objectives: maxLength: 4096 + nullable: true type: string tags: $ref: '#/components/schemas/Tags' @@ -6615,11 +6618,13 @@ components: nullable: true type: integer business_code: + nullable: true type: string creation_date: format: date-time type: string description: + nullable: true type: string expected_end_date: format: date @@ -6637,6 +6642,7 @@ components: format: int64 type: integer service_code: + nullable: true type: string start_date: format: date @@ -6645,20 +6651,17 @@ components: state: type: string study_type: + nullable: true type: string tags: $ref: '#/components/schemas/Tags' required: - id - name - - description - - business_code - - service_code - creation_date - last_modification - tags - state - - study_type - project_id type: object StudyCreateForm: @@ -6673,8 +6676,10 @@ components: nullable: true type: integer business_code: + nullable: true type: string description: + nullable: true type: string expected_end_date: format: date @@ -6683,6 +6688,7 @@ components: name: type: string service_code: + nullable: true type: string start_date: format: date @@ -6691,6 +6697,7 @@ components: state: type: string study_type: + nullable: true type: string tags: $ref: '#/components/schemas/Tags' diff --git a/editoast/src/fixtures.rs b/editoast/src/fixtures.rs index e9d08d08441..d793c1c534a 100644 --- a/editoast/src/fixtures.rs +++ b/editoast/src/fixtures.rs @@ -323,16 +323,12 @@ pub mod tests { let project = project.await; let study_model = Study::changeset() .name("test_study".into()) - .description("test".into()) .creation_date(Utc::now().naive_utc()) - .business_code("AAA".into()) - .service_code("BBB".into()) .creation_date(Utc::now().naive_utc()) .last_modification(Utc::now().naive_utc()) .budget(Some(0)) .tags(Tags::default()) .state("some_state".into()) - .study_type("some_type".into()) .project_id(project.id()); StudyFixtureSet { project, @@ -344,9 +340,6 @@ pub mod tests { pub async fn project(db_pool: Data) -> TestFixture { let project_model = Project::changeset() .name("test_project".to_owned()) - .objectives("".to_owned()) - .description("".to_owned()) - .funders("".to_owned()) .budget(Some(0)) .creation_date(Utc::now().naive_utc()) .last_modification(Utc::now().naive_utc()) diff --git a/editoast/src/modelsv2/projects.rs b/editoast/src/modelsv2/projects.rs index 856de43606e..322a2cd52e9 100644 --- a/editoast/src/modelsv2/projects.rs +++ b/editoast/src/modelsv2/projects.rs @@ -38,9 +38,9 @@ editoast_common::schemas! { pub struct Project { pub id: i64, pub name: String, - pub objectives: String, - pub description: String, - pub funders: String, + pub objectives: Option, + pub description: Option, + pub funders: Option, pub budget: Option, pub creation_date: NaiveDateTime, pub last_modification: NaiveDateTime, diff --git a/editoast/src/modelsv2/study.rs b/editoast/src/modelsv2/study.rs index 365822ab44b..051e72cdf24 100644 --- a/editoast/src/modelsv2/study.rs +++ b/editoast/src/modelsv2/study.rs @@ -31,9 +31,9 @@ use crate::DbPool; pub struct Study { pub id: i64, pub name: String, - pub description: String, - pub business_code: String, - pub service_code: String, + pub description: Option, + pub business_code: Option, + pub service_code: Option, pub creation_date: NaiveDateTime, pub last_modification: NaiveDateTime, pub start_date: Option, @@ -43,7 +43,7 @@ pub struct Study { #[model(remote = "Vec>")] pub tags: Tags, pub state: String, - pub study_type: String, + pub study_type: Option, pub project_id: i64, } diff --git a/editoast/src/tables.rs b/editoast/src/tables.rs index e72f8a8977f..e11486969cb 100644 --- a/editoast/src/tables.rs +++ b/editoast/src/tables.rs @@ -397,11 +397,11 @@ diesel::table! { #[max_length = 128] name -> Varchar, #[max_length = 4096] - objectives -> Varchar, + objectives -> Nullable, #[max_length = 1024] - description -> Varchar, + description -> Nullable, #[max_length = 255] - funders -> Varchar, + funders -> Nullable, budget -> Nullable, creation_date -> Timestamptz, last_modification -> Timestamptz, @@ -620,11 +620,11 @@ diesel::table! { #[max_length = 128] name -> Varchar, #[max_length = 1024] - description -> Varchar, + description -> Nullable, #[max_length = 128] - business_code -> Varchar, + business_code -> Nullable, #[max_length = 128] - service_code -> Varchar, + service_code -> Nullable, creation_date -> Timestamptz, last_modification -> Timestamptz, start_date -> Nullable, @@ -635,7 +635,7 @@ diesel::table! { #[max_length = 16] state -> Varchar, #[max_length = 100] - study_type -> Varchar, + study_type -> Nullable, project_id -> Int8, } } diff --git a/editoast/src/views/projects.rs b/editoast/src/views/projects.rs index 29835d1ebe4..903a6bf6757 100644 --- a/editoast/src/views/projects.rs +++ b/editoast/src/views/projects.rs @@ -80,15 +80,12 @@ pub struct QueryParams { struct ProjectCreateForm { #[schema(max_length = 128)] pub name: String, - #[serde(default)] #[schema(max_length = 1024)] - pub description: String, - #[serde(default)] + pub description: Option, #[schema(max_length = 4096)] - pub objectives: String, - #[serde(default)] + pub objectives: Option, #[schema(max_length = 1024)] - pub funders: String, + pub funders: Option, pub budget: Option, /// The id of the image document pub image: Option, @@ -278,9 +275,9 @@ impl From for Changeset { fn from(project: ProjectPatchForm) -> Self { Project::changeset() .flat_name(project.name) - .flat_description(project.description) - .flat_objectives(project.objectives) - .flat_funders(project.funders) + .description(project.description) + .objectives(project.objectives) + .funders(project.funders) .flat_budget(Some(project.budget)) .flat_image(Some(project.image)) .flat_tags(project.tags) @@ -350,7 +347,12 @@ pub mod test { let app = create_test_service().await; let req = TestRequest::post() .uri("/projects") - .set_json(json!({ "name": "test_project","description": "", "objectives":"" })) + .set_json(json!({ + "name": "test_project", + "description": "", + "objectives": "", + "funders": "", + })) .to_request(); let response = call_service(&app, req).await; assert_eq!(response.status(), StatusCode::OK); diff --git a/editoast/src/views/study.rs b/editoast/src/views/study.rs index e51b7673b16..8130fb5a94c 100644 --- a/editoast/src/views/study.rs +++ b/editoast/src/views/study.rs @@ -80,21 +80,17 @@ pub enum StudyError { #[derivative(Default)] struct StudyCreateForm { pub name: String, - #[serde(default)] - pub description: String, + pub description: Option, pub start_date: Option, pub expected_end_date: Option, pub actual_end_date: Option, - #[serde(default)] - pub business_code: String, - #[serde(default)] - pub service_code: String, + pub business_code: Option, + pub service_code: Option, pub budget: Option, #[serde(default)] pub tags: Tags, pub state: String, - #[serde(default)] - pub study_type: String, + pub study_type: Option, } impl StudyCreateForm { @@ -336,16 +332,16 @@ impl StudyPatchForm { pub fn into_study_changeset(self) -> Result> { let study_changeset = Study::changeset() .flat_name(self.name) - .flat_description(self.description) - .flat_business_code(self.business_code) - .flat_service_code(self.service_code) + .description(self.description) + .business_code(self.business_code) + .service_code(self.service_code) .flat_start_date(Some(self.start_date)) .flat_expected_end_date(Some(self.expected_end_date)) .flat_actual_end_date(Some(self.actual_end_date)) .flat_budget(Some(self.budget)) .flat_tags(self.tags) .flat_state(self.state) - .flat_study_type(self.study_type); + .study_type(self.study_type); Study::validate(&study_changeset)?; Ok(study_changeset) } @@ -456,7 +452,14 @@ pub mod test { let project = project.await; let req = TestRequest::post() .uri(format!("/projects/{}/studies/", project.id()).as_str()) - .set_json(json!({ "name": "study_test", "state": "Starting" })) + .set_json(json!({ + "name": "study_test", + "description": "Study description", + "state": "Starting", + "business_code": "", + "service_code": "", + "study_type": "", + })) .to_request(); let response = call_service(&app, req).await; assert_eq!(response.status(), StatusCode::OK); diff --git a/front/src/common/api/osrdEditoastApi.ts b/front/src/common/api/osrdEditoastApi.ts index 66c74c7dfca..d09a301c992 100644 --- a/front/src/common/api/osrdEditoastApi.ts +++ b/front/src/common/api/osrdEditoastApi.ts @@ -2115,13 +2115,13 @@ export type Tags = string[]; export type Project = { budget?: number | null; creation_date: string; - description: string; - funders: string; + description?: string | null; + funders?: string | null; id: number; image?: number | null; last_modification: string; name: string; - objectives: string; + objectives?: string | null; tags: Tags; }; export type ProjectWithStudies = Project & { @@ -2146,12 +2146,12 @@ export type Ordering = | 'LastModifiedAsc'; export type ProjectCreateForm = { budget?: number | null; - description?: string; - funders?: string; + description?: string | null; + funders?: string | null; /** The id of the image document */ image?: number | null; name: string; - objectives?: string; + objectives?: string | null; tags?: Tags; }; export type ProjectPatchForm = { @@ -2167,18 +2167,18 @@ export type ProjectPatchForm = { export type Study = { actual_end_date?: string | null; budget?: number | null; - business_code: string; + business_code?: string | null; creation_date: string; - description: string; + description?: string | null; expected_end_date?: string | null; id: number; last_modification: string; name: string; project_id: number; - service_code: string; + service_code?: string | null; start_date?: string | null; state: string; - study_type: string; + study_type?: string | null; tags: Tags; }; export type StudyWithScenarios = Study & { @@ -2201,14 +2201,14 @@ export type StudyResponse = Study & { export type StudyCreateForm = { actual_end_date?: string | null; budget?: number | null; - business_code?: string; - description?: string; + business_code?: string | null; + description?: string | null; expected_end_date?: string | null; name: string; - service_code?: string; + service_code?: string | null; start_date?: string | null; state: string; - study_type?: string; + study_type?: string | null; tags?: Tags; }; export type StudyPatchForm = { diff --git a/front/src/modules/project/components/AddOrEditProjectModal.tsx b/front/src/modules/project/components/AddOrEditProjectModal.tsx index 1d489a3bec0..ab29a309f41 100644 --- a/front/src/modules/project/components/AddOrEditProjectModal.tsx +++ b/front/src/modules/project/components/AddOrEditProjectModal.tsx @@ -309,7 +309,7 @@ export default function AddOrEditProjectModal({ {t('projectDescription')} } - value={currentProject.description} + value={currentProject.description ?? undefined} onChange={(e) => handleProjectInputChange('description', e.target.value)} placeholder={t('projectDescriptionPlaceholder')} rows={3} @@ -330,7 +330,7 @@ export default function AddOrEditProjectModal({ {t('projectObjectives')} } - value={currentProject.objectives} + value={currentProject.objectives ?? undefined} onChange={(e) => handleProjectInputChange('objectives', e.target.value)} /> @@ -365,7 +365,7 @@ export default function AddOrEditProjectModal({ {t('projectFunders')} } - value={currentProject.funders} + value={currentProject.funders ?? undefined} onChange={(e) => handleProjectInputChange('funders', e.target.value)} /> diff --git a/front/src/modules/study/components/AddOrEditStudyModal.tsx b/front/src/modules/study/components/AddOrEditStudyModal.tsx index d76f0c269b4..b46d289374e 100644 --- a/front/src/modules/study/components/AddOrEditStudyModal.tsx +++ b/front/src/modules/study/components/AddOrEditStudyModal.tsx @@ -250,7 +250,7 @@ export default function AddOrEditStudyModal({ editionMode, study }: Props) { } value={{ - id: currentStudy.study_type, + id: currentStudy.study_type ?? undefined, label: t( `studyCategories.${currentStudy.study_type || 'nothingSelected'}` ).toString(), @@ -298,7 +298,7 @@ export default function AddOrEditStudyModal({ editionMode, study }: Props) { {t('studyDescription')} } - value={currentStudy?.description} + value={currentStudy?.description ?? undefined} onChange={(e) => handleStudyInputChange('description', e.target.value)} placeholder={t('studyDescriptionPlaceholder')} /> diff --git a/front/tests/002-project-management.spec.ts b/front/tests/002-project-management.spec.ts index d09e29866be..3f4d9a8f7f5 100644 --- a/front/tests/002-project-management.spec.ts +++ b/front/tests/002-project-management.spec.ts @@ -60,7 +60,7 @@ test.describe('Test if operationnal study : project workflow is working properly expect(objectives).not.toEqual(null); if (objectives !== null) expect(objectives.replace(/[^A-Za-z0-9]/g, '')).toContain( - project.objectives.replace(/[^A-Za-z0-9]/g, '') + (project.objectives ?? "").replace(/[^A-Za-z0-9]/g, '') ); expect(await projectPage.getProjectFinancialsInfos.textContent()).toContain( projectData.funders