From 0817f291cc361f30e1d4f98bfd245cf9dc30756f Mon Sep 17 00:00:00 2001 From: ibrahim Date: Fri, 6 Sep 2024 11:43:03 +0300 Subject: [PATCH] refactor: study categorization and template factory --- core/lib/src/models/tables/study.dart | 44 +++++++++++++++++-- .../features/dashboard/dashboard_state.dart | 12 +++-- supabase/seed.sql | 2 - 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/core/lib/src/models/tables/study.dart b/core/lib/src/models/tables/study.dart index 2ad29d1be..eb5dbb9b0 100644 --- a/core/lib/src/models/tables/study.dart +++ b/core/lib/src/models/tables/study.dart @@ -276,7 +276,15 @@ class Study extends SupabaseObjectFunctions } StudyType get type { - return studyType ?? StudyType.standalone; + if (templateConfiguration != null && parentTemplateId == null) { + return StudyType.template; + } + + if (parentTemplateId != null) { + return StudyType.subStudy; + } + + return StudyType.standalone; } bool get isStandalone => type == StudyType.standalone; @@ -363,7 +371,37 @@ class Template extends Study { Template.create(String userId) : super(const Uuid().v4(), userId) { templateConfiguration = TemplateConfiguration(); - studyType = StudyType.template; + } + + factory Template.fromStudy(Study study) { + return Template(study.id, study.userId) + ..title = study.title + ..description = study.description + ..participation = study.participation + ..resultSharing = study.resultSharing + ..contact = study.contact + ..iconName = study.iconName + ..status = study.status + ..questionnaire = study.questionnaire + ..eligibilityCriteria = study.eligibilityCriteria + ..consent = study.consent + ..interventions = study.interventions + ..observations = study.observations + ..schedule = study.schedule + ..reportSpecification = study.reportSpecification + ..collaboratorEmails = study.collaboratorEmails + ..registryPublished = study.registryPublished + ..participantCount = study.participantCount + ..endedCount = study.endedCount + ..activeSubjectCount = study.activeSubjectCount + ..missedDays = study.missedDays + ..createdAt = study.createdAt + ..repo = study.repo + ..invites = study.invites + ..participants = study.participants + ..participantsProgress = study.participantsProgress + ..templateConfiguration = + study.templateConfiguration; // Make sure to include this } } @@ -378,8 +416,6 @@ class TemplateSubStudy extends Study { } parentTemplateId = template.id; - studyType = StudyType.subStudy; - templateConfiguration = template.templateConfiguration!.copyWith( title: template.title, description: template.description, diff --git a/designer_v2/lib/features/dashboard/dashboard_state.dart b/designer_v2/lib/features/dashboard/dashboard_state.dart index 511628ba5..296ccd2bf 100644 --- a/designer_v2/lib/features/dashboard/dashboard_state.dart +++ b/designer_v2/lib/features/dashboard/dashboard_state.dart @@ -78,9 +78,15 @@ class DashboardState extends Equatable { case StudyType.standalone: result.add(StudyGroup.standalone(study)); case StudyType.template: - final List subStudies = - studies.where((s) => s.parentTemplateId == study.id).toList(); - result.add(StudyGroup.template(study as Template, subStudies)); + final List subStudies = studies + .where((s) => s.parentTemplateId == study.id) + .map((subStudy) { + subStudy.templateConfiguration = study.templateConfiguration; + return subStudy; + }).toList(); + + result + .add(StudyGroup.template(Template.fromStudy(study), subStudies)); case StudyType.subStudy: break; } diff --git a/supabase/seed.sql b/supabase/seed.sql index 6fe299278..06a721491 100644 --- a/supabase/seed.sql +++ b/supabase/seed.sql @@ -23,7 +23,6 @@ DECLARE BEGIN user_id := mockup.create_user(email, password); - -- Insert for the "template" table based on template_rows.csv INSERT INTO "public"."template" ("id", "contact", "title", "description", "icon_name", "published", "status", "registry_published", "questionnaire", "eligibility_criteria", "observations", "interventions", "consent", "schedule", "report_specification", "results", "created_at", "updated_at", "user_id", "participation", "result_sharing", "collaborator_emails", "locked_contact", "locked_participation", "locked_schedule", "locked_registry") VALUES ( '53446580-ad3b-452e-bb4b-094a18f27903', @@ -54,7 +53,6 @@ BEGIN true ); - -- Insert for the "study" table based on study_rows.csv INSERT INTO "public"."study" ("id", "contact", "title", "description", "icon_name", "published", "status", "registry_published", "questionnaire", "eligibility_criteria", "observations", "interventions", "consent", "schedule", "report_specification", "results", "created_at", "updated_at", "user_id", "participation", "result_sharing", "collaborator_emails", "parent_id") VALUES (