From f8b66f29d21e4568a4c596037cef6db4fb555201 Mon Sep 17 00:00:00 2001 From: Samuel Lucidi Date: Tue, 3 Oct 2023 14:09:14 -0400 Subject: [PATCH] :sparkles: Allow creating assessments "as-is" (#501) If the "sections" field of the assessment resource is populated when POSTing an assessment, then the sections will not be overridden by values from the questionnaire and the assessment will not be autofilled. Signed-off-by: Sam Lucidi --- api/application.go | 18 ++++++++++-------- api/archetype.go | 17 ++++++++++------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/api/application.go b/api/application.go index 833383885..2e048bfa4 100644 --- a/api/application.go +++ b/api/application.go @@ -1069,18 +1069,20 @@ func (h ApplicationHandler) AssessmentCreate(ctx *gin.Context) { return } m := r.Model() - m.Sections = q.Sections m.Thresholds = q.Thresholds m.RiskMessages = q.RiskMessages m.CreateUser = h.CurrentUser(ctx) - - resolver, err := assessment.NewTagResolver(h.DB(ctx)) - if err != nil { - _ = ctx.Error(err) - return + // if sections aren't nil that indicates that this assessment is being + // created "as-is" and should not have its sections populated or autofilled. + if m.Sections == nil { + m.Sections = q.Sections + resolver, rErr := assessment.NewTagResolver(h.DB(ctx)) + if rErr != nil { + _ = ctx.Error(rErr) + return + } + assessment.PrepareForApplication(resolver, application, m) } - assessment.PrepareForApplication(resolver, application, m) - result = h.DB(ctx).Create(m) if result.Error != nil { _ = ctx.Error(result.Error) diff --git a/api/archetype.go b/api/archetype.go index 8f4488d99..690d7e324 100644 --- a/api/archetype.go +++ b/api/archetype.go @@ -283,17 +283,20 @@ func (h ArchetypeHandler) AssessmentCreate(ctx *gin.Context) { return } m := r.Model() - m.Sections = q.Sections m.Thresholds = q.Thresholds m.RiskMessages = q.RiskMessages m.CreateUser = h.CurrentUser(ctx) - - resolver, err := assessment.NewTagResolver(h.DB(ctx)) - if err != nil { - _ = ctx.Error(err) - return + // if sections aren't nil that indicates that this assessment is being + // created "as-is" and should not have its sections populated or autofilled. + if m.Sections == nil { + m.Sections = q.Sections + resolver, rErr := assessment.NewTagResolver(h.DB(ctx)) + if rErr != nil { + _ = ctx.Error(rErr) + return + } + assessment.PrepareForArchetype(resolver, archetype, m) } - assessment.PrepareForArchetype(resolver, archetype, m) result = h.DB(ctx).Create(m) if result.Error != nil { _ = ctx.Error(result.Error)