Skip to content

Commit

Permalink
✨ Allow creating assessments "as-is" (#501)
Browse files Browse the repository at this point in the history
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 <slucidi@redhat.com>
  • Loading branch information
mansam authored Oct 3, 2023
1 parent 81422c1 commit f8b66f2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
18 changes: 10 additions & 8 deletions api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 10 additions & 7 deletions api/archetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f8b66f2

Please sign in to comment.