Add migration 000109, require parent context on complete, and update progress queries and LMS rollups. Co-authored-by: Cursor <cursoragent@cursor.com>
398 lines
15 KiB
Go
398 lines
15 KiB
Go
package questions
|
|
|
|
import (
|
|
"Yimaru-Backend/internal/domain"
|
|
"Yimaru-Backend/internal/ports"
|
|
"context"
|
|
"fmt"
|
|
)
|
|
|
|
type Service struct {
|
|
questionStore ports.QuestionStore
|
|
catalogCache activeCatalogCache
|
|
}
|
|
|
|
func NewService(questionStore ports.QuestionStore) *Service {
|
|
return &Service{
|
|
questionStore: questionStore,
|
|
}
|
|
}
|
|
|
|
// Question Type Definitions (dynamic builder presets)
|
|
|
|
func (s *Service) CreateQuestionTypeDefinition(ctx context.Context, input domain.CreateQuestionTypeDefinitionInput) (domain.QuestionTypeDefinition, error) {
|
|
def, err := s.questionStore.CreateQuestionTypeDefinition(ctx, input)
|
|
if err != nil {
|
|
return domain.QuestionTypeDefinition{}, err
|
|
}
|
|
s.invalidateActiveCatalogCache()
|
|
return def, nil
|
|
}
|
|
|
|
func (s *Service) GetQuestionTypeDefinitionByID(ctx context.Context, id int64) (domain.QuestionTypeDefinition, error) {
|
|
return s.questionStore.GetQuestionTypeDefinitionByID(ctx, id)
|
|
}
|
|
|
|
func clampQuestionTypeDefinitionPage(limit, offset int32) (int32, int32) {
|
|
if limit <= 0 {
|
|
limit = 20
|
|
}
|
|
if limit > 200 {
|
|
limit = 200
|
|
}
|
|
if offset < 0 {
|
|
offset = 0
|
|
}
|
|
return limit, offset
|
|
}
|
|
|
|
func (s *Service) ListQuestionTypeDefinitions(ctx context.Context, status *string, includeSystem bool, groupID *int64, limit, offset int32) ([]domain.QuestionTypeDefinition, int64, error) {
|
|
limit, offset = clampQuestionTypeDefinitionPage(limit, offset)
|
|
return s.questionStore.ListQuestionTypeDefinitions(ctx, status, includeSystem, groupID, limit, offset)
|
|
}
|
|
|
|
func (s *Service) UpdateQuestionTypeDefinition(ctx context.Context, id int64, input domain.UpdateQuestionTypeDefinitionInput) error {
|
|
if err := s.questionStore.UpdateQuestionTypeDefinition(ctx, id, input); err != nil {
|
|
return err
|
|
}
|
|
s.invalidateActiveCatalogCache()
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) DeleteQuestionTypeDefinition(ctx context.Context, id int64) error {
|
|
if err := s.questionStore.DeleteQuestionTypeDefinition(ctx, id); err != nil {
|
|
return err
|
|
}
|
|
s.invalidateActiveCatalogCache()
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) CreateQuestionTypeDefinitionGroup(ctx context.Context, input domain.CreateQuestionTypeDefinitionGroupInput) (domain.QuestionTypeDefinitionGroup, error) {
|
|
return s.questionStore.CreateQuestionTypeDefinitionGroup(ctx, input)
|
|
}
|
|
|
|
func (s *Service) GetQuestionTypeDefinitionGroupByID(ctx context.Context, id int64) (domain.QuestionTypeDefinitionGroupDetail, error) {
|
|
return s.questionStore.GetQuestionTypeDefinitionGroupByID(ctx, id)
|
|
}
|
|
|
|
func (s *Service) ListQuestionTypeDefinitionGroups(ctx context.Context, status *string, limit, offset int32) ([]domain.QuestionTypeDefinitionGroup, int64, error) {
|
|
limit, offset = clampQuestionTypeDefinitionPage(limit, offset)
|
|
return s.questionStore.ListQuestionTypeDefinitionGroups(ctx, status, limit, offset)
|
|
}
|
|
|
|
func (s *Service) UpdateQuestionTypeDefinitionGroup(ctx context.Context, id int64, input domain.UpdateQuestionTypeDefinitionGroupInput) (domain.QuestionTypeDefinitionGroup, error) {
|
|
return s.questionStore.UpdateQuestionTypeDefinitionGroup(ctx, id, input)
|
|
}
|
|
|
|
func (s *Service) DeleteQuestionTypeDefinitionGroup(ctx context.Context, id int64) error {
|
|
return s.questionStore.DeleteQuestionTypeDefinitionGroup(ctx, id)
|
|
}
|
|
|
|
func (s *Service) QuestionTypeDefinitionGroupExists(ctx context.Context, id int64) (bool, error) {
|
|
return s.questionStore.QuestionTypeDefinitionGroupExists(ctx, id)
|
|
}
|
|
|
|
// Questions
|
|
|
|
func (s *Service) CreateQuestion(ctx context.Context, input domain.CreateQuestionInput) (domain.Question, error) {
|
|
return s.questionStore.CreateQuestion(ctx, input)
|
|
}
|
|
|
|
func (s *Service) GetQuestionByID(ctx context.Context, id int64) (domain.Question, error) {
|
|
return s.questionStore.GetQuestionByID(ctx, id)
|
|
}
|
|
|
|
func (s *Service) GetQuestionWithDetails(ctx context.Context, id int64) (domain.QuestionWithDetails, error) {
|
|
return s.questionStore.GetQuestionWithDetails(ctx, id)
|
|
}
|
|
|
|
func (s *Service) GetQuestionsWithDetailsByIDs(ctx context.Context, ids []int64) (map[int64]domain.QuestionWithDetails, error) {
|
|
return s.questionStore.GetQuestionsWithDetailsByIDs(ctx, ids)
|
|
}
|
|
|
|
func (s *Service) ListQuestions(ctx context.Context, questionType, difficulty, status *string, limit, offset int32) ([]domain.Question, int64, error) {
|
|
return s.questionStore.ListQuestions(ctx, questionType, difficulty, status, limit, offset)
|
|
}
|
|
|
|
func (s *Service) SearchQuestions(ctx context.Context, query string, limit, offset int32) ([]domain.Question, int64, error) {
|
|
return s.questionStore.SearchQuestions(ctx, query, limit, offset)
|
|
}
|
|
|
|
func (s *Service) UpdateQuestion(ctx context.Context, id int64, input domain.CreateQuestionInput) error {
|
|
return s.questionStore.UpdateQuestion(ctx, id, input)
|
|
}
|
|
|
|
func (s *Service) ArchiveQuestion(ctx context.Context, id int64) error {
|
|
return s.questionStore.ArchiveQuestion(ctx, id)
|
|
}
|
|
|
|
func (s *Service) DeleteQuestion(ctx context.Context, id int64) error {
|
|
return s.questionStore.DeleteQuestion(ctx, id)
|
|
}
|
|
|
|
// Question Options
|
|
|
|
func (s *Service) CreateQuestionOption(ctx context.Context, questionID int64, optionText string, optionOrder *int32, isCorrect bool) (domain.QuestionOption, error) {
|
|
return s.questionStore.CreateQuestionOption(ctx, questionID, optionText, optionOrder, isCorrect)
|
|
}
|
|
|
|
func (s *Service) GetOptionsByQuestionID(ctx context.Context, questionID int64) ([]domain.QuestionOption, error) {
|
|
return s.questionStore.GetOptionsByQuestionID(ctx, questionID)
|
|
}
|
|
|
|
func (s *Service) UpdateQuestionOption(ctx context.Context, id int64, optionText *string, optionOrder *int32, isCorrect *bool) error {
|
|
return s.questionStore.UpdateQuestionOption(ctx, id, optionText, optionOrder, isCorrect)
|
|
}
|
|
|
|
func (s *Service) DeleteQuestionOption(ctx context.Context, id int64) error {
|
|
return s.questionStore.DeleteQuestionOption(ctx, id)
|
|
}
|
|
|
|
func (s *Service) DeleteOptionsByQuestionID(ctx context.Context, questionID int64) error {
|
|
return s.questionStore.DeleteOptionsByQuestionID(ctx, questionID)
|
|
}
|
|
|
|
// Question Short Answers
|
|
|
|
func (s *Service) CreateQuestionShortAnswer(ctx context.Context, questionID int64, acceptableAnswer string, matchType *string) (domain.QuestionShortAnswer, error) {
|
|
return s.questionStore.CreateQuestionShortAnswer(ctx, questionID, acceptableAnswer, matchType)
|
|
}
|
|
|
|
func (s *Service) GetShortAnswersByQuestionID(ctx context.Context, questionID int64) ([]domain.QuestionShortAnswer, error) {
|
|
return s.questionStore.GetShortAnswersByQuestionID(ctx, questionID)
|
|
}
|
|
|
|
func (s *Service) UpdateQuestionShortAnswer(ctx context.Context, id int64, acceptableAnswer, matchType *string) error {
|
|
return s.questionStore.UpdateQuestionShortAnswer(ctx, id, acceptableAnswer, matchType)
|
|
}
|
|
|
|
func (s *Service) DeleteQuestionShortAnswer(ctx context.Context, id int64) error {
|
|
return s.questionStore.DeleteQuestionShortAnswer(ctx, id)
|
|
}
|
|
|
|
func (s *Service) DeleteShortAnswersByQuestionID(ctx context.Context, questionID int64) error {
|
|
return s.questionStore.DeleteShortAnswersByQuestionID(ctx, questionID)
|
|
}
|
|
|
|
// Question Sets
|
|
|
|
func (s *Service) CreateQuestionSet(ctx context.Context, input domain.CreateQuestionSetInput) (domain.QuestionSet, error) {
|
|
return s.questionStore.CreateQuestionSet(ctx, input)
|
|
}
|
|
|
|
func (s *Service) GetQuestionSetByID(ctx context.Context, id int64) (domain.QuestionSet, error) {
|
|
return s.questionStore.GetQuestionSetByID(ctx, id)
|
|
}
|
|
|
|
func (s *Service) GetQuestionSetsByOwner(ctx context.Context, ownerType string, ownerID int64) ([]domain.QuestionSet, error) {
|
|
return s.questionStore.GetQuestionSetsByOwner(ctx, ownerType, ownerID)
|
|
}
|
|
|
|
func (s *Service) GetQuestionSetsByType(ctx context.Context, setType string, limit, offset int32) ([]domain.QuestionSet, int64, error) {
|
|
return s.questionStore.GetQuestionSetsByType(ctx, setType, limit, offset)
|
|
}
|
|
|
|
func (s *Service) GetPublishedQuestionSetsByOwner(ctx context.Context, ownerType string, ownerID int64) ([]domain.QuestionSet, error) {
|
|
return s.questionStore.GetPublishedQuestionSetsByOwner(ctx, ownerType, ownerID)
|
|
}
|
|
|
|
func (s *Service) GetInitialAssessmentSet(ctx context.Context) (domain.QuestionSet, error) {
|
|
return s.questionStore.GetInitialAssessmentSet(ctx)
|
|
}
|
|
|
|
func (s *Service) GetFirstIncompletePreviousPractice(ctx context.Context, userID int64, questionSetID int64) (*domain.PracticeAccessBlock, error) {
|
|
return s.questionStore.GetFirstIncompletePreviousPractice(ctx, userID, questionSetID)
|
|
}
|
|
|
|
func (s *Service) MarkPracticeCompleted(ctx context.Context, userID int64, questionSetID int64, scope domain.PracticeCompletionScope) error {
|
|
return s.questionStore.MarkPracticeCompleted(ctx, userID, questionSetID, scope)
|
|
}
|
|
|
|
func (s *Service) UpdateQuestionSet(ctx context.Context, id int64, input domain.CreateQuestionSetInput) error {
|
|
return s.questionStore.UpdateQuestionSet(ctx, id, input)
|
|
}
|
|
|
|
func (s *Service) ArchiveQuestionSet(ctx context.Context, id int64) error {
|
|
return s.questionStore.ArchiveQuestionSet(ctx, id)
|
|
}
|
|
|
|
func (s *Service) DeleteQuestionSet(ctx context.Context, id int64) error {
|
|
return s.questionStore.DeleteQuestionSet(ctx, id)
|
|
}
|
|
|
|
// Question Set Items
|
|
|
|
func upsertQuestionSetMembership(memberships []domain.QuestionSetMembership, questionID int64, displayOrder int32, associatedQuestionID *int64) []domain.QuestionSetMembership {
|
|
out := make([]domain.QuestionSetMembership, 0, len(memberships)+1)
|
|
replaced := false
|
|
for _, m := range memberships {
|
|
if m.QuestionID == questionID {
|
|
out = append(out, domain.QuestionSetMembership{
|
|
QuestionID: questionID,
|
|
DisplayOrder: displayOrder,
|
|
AssociatedQuestionID: associatedQuestionID,
|
|
})
|
|
replaced = true
|
|
continue
|
|
}
|
|
out = append(out, m)
|
|
}
|
|
if !replaced {
|
|
out = append(out, domain.QuestionSetMembership{
|
|
QuestionID: questionID,
|
|
DisplayOrder: displayOrder,
|
|
AssociatedQuestionID: associatedQuestionID,
|
|
})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (s *Service) validateSetMemberships(memberships []domain.QuestionSetMembership) error {
|
|
if err := domain.ValidateQuestionSetAssociations(memberships); err != nil {
|
|
return fmt.Errorf("%w: %s", domain.ErrInvalidQuestionAssociation, err.Error())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) AddQuestionToSet(ctx context.Context, setID, questionID int64, displayOrder *int32, associatedQuestionID *int64) (domain.QuestionSetItem, error) {
|
|
current, err := s.questionStore.GetQuestionSetItems(ctx, setID)
|
|
if err != nil {
|
|
return domain.QuestionSetItem{}, err
|
|
}
|
|
order := int32(0)
|
|
if displayOrder != nil {
|
|
order = *displayOrder
|
|
} else if len(current) > 0 {
|
|
for _, item := range current {
|
|
if item.DisplayOrder > order {
|
|
order = item.DisplayOrder
|
|
}
|
|
}
|
|
order++
|
|
} else {
|
|
order = 1
|
|
}
|
|
memberships := upsertQuestionSetMembership(domain.MembershipsFromQuestionSetItems(current), questionID, order, associatedQuestionID)
|
|
if err := s.validateSetMemberships(memberships); err != nil {
|
|
return domain.QuestionSetItem{}, err
|
|
}
|
|
return s.questionStore.AddQuestionToSet(ctx, setID, questionID, &order, associatedQuestionID, nil)
|
|
}
|
|
|
|
func (s *Service) GetQuestionSetItems(ctx context.Context, setID int64) ([]domain.QuestionSetItemWithQuestion, error) {
|
|
return s.questionStore.GetQuestionSetItems(ctx, setID)
|
|
}
|
|
|
|
func (s *Service) GetQuestionSetItemsPaginated(ctx context.Context, setID int64, questionType *string, limit, offset int32) ([]domain.QuestionSetItemWithQuestion, int64, error) {
|
|
return s.questionStore.GetQuestionSetItemsPaginated(ctx, setID, questionType, limit, offset)
|
|
}
|
|
|
|
func (s *Service) GetPublishedQuestionsInSet(ctx context.Context, setID int64) ([]domain.QuestionSetItemWithQuestion, error) {
|
|
return s.questionStore.GetPublishedQuestionsInSet(ctx, setID)
|
|
}
|
|
|
|
func (s *Service) RemoveQuestionFromSet(ctx context.Context, setID, questionID int64) error {
|
|
return s.questionStore.RemoveQuestionFromSet(ctx, setID, questionID)
|
|
}
|
|
|
|
func (s *Service) UpdateQuestionSetItem(ctx context.Context, setID, questionID int64, displayOrder int32, associatedQuestionID *int64) error {
|
|
current, err := s.questionStore.GetQuestionSetItems(ctx, setID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
memberships := upsertQuestionSetMembership(domain.MembershipsFromQuestionSetItems(current), questionID, displayOrder, associatedQuestionID)
|
|
if err := s.validateSetMemberships(memberships); err != nil {
|
|
return err
|
|
}
|
|
return s.questionStore.UpdateQuestionSetItem(ctx, setID, questionID, displayOrder, associatedQuestionID, nil)
|
|
}
|
|
|
|
func (s *Service) CountQuestionsInSet(ctx context.Context, setID int64) (int64, error) {
|
|
return s.questionStore.CountQuestionsInSet(ctx, setID)
|
|
}
|
|
|
|
func (s *Service) GetQuestionTypesInSet(ctx context.Context, setID int64) (domain.QuestionSetQuestionTypesSummary, error) {
|
|
groups, err := s.questionStore.GetQuestionTypeCountsInSet(ctx, setID)
|
|
if err != nil {
|
|
return domain.QuestionSetQuestionTypesSummary{}, err
|
|
}
|
|
active := "ACTIVE"
|
|
catalog, _, err := s.questionStore.ListQuestionTypeDefinitions(ctx, &active, true, nil, 0, 0)
|
|
if err != nil {
|
|
return domain.QuestionSetQuestionTypesSummary{}, err
|
|
}
|
|
counts := domain.SummarizeQuestionSetQuestionTypes(groups, catalog)
|
|
var total int64
|
|
for _, c := range counts {
|
|
total += c.Count
|
|
}
|
|
if counts == nil {
|
|
counts = []domain.QuestionSetQuestionTypeCount{}
|
|
}
|
|
return domain.QuestionSetQuestionTypesSummary{
|
|
QuestionSetID: setID,
|
|
QuestionTypes: counts,
|
|
TotalQuestions: total,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Service) GetQuestionSetsContainingQuestion(ctx context.Context, questionID int64) ([]domain.QuestionSet, error) {
|
|
return s.questionStore.GetQuestionSetsContainingQuestion(ctx, questionID)
|
|
}
|
|
|
|
func clampPracticeSearchPage(limit, offset int32) (int32, int32) {
|
|
if limit <= 0 {
|
|
limit = 20
|
|
}
|
|
if limit > 200 {
|
|
limit = 200
|
|
}
|
|
if offset < 0 {
|
|
offset = 0
|
|
}
|
|
return limit, offset
|
|
}
|
|
|
|
func (s *Service) ListPracticesByQuestionTypeDefinition(
|
|
ctx context.Context,
|
|
definitionID int64,
|
|
publishedOnly bool,
|
|
limit, offset int32,
|
|
) (domain.PracticesByQuestionTypeDefinitionSummary, error) {
|
|
var empty domain.PracticesByQuestionTypeDefinitionSummary
|
|
def, err := s.questionStore.GetQuestionTypeDefinitionByID(ctx, definitionID)
|
|
if err != nil {
|
|
return empty, err
|
|
}
|
|
limit, offset = clampPracticeSearchPage(limit, offset)
|
|
legacyTypes := domain.LegacyRuntimeTypesForDefinitionLookup(def)
|
|
practices, total, err := s.questionStore.ListPracticesByQuestionTypeDefinition(ctx, definitionID, legacyTypes, publishedOnly, limit, offset)
|
|
if err != nil {
|
|
return empty, err
|
|
}
|
|
if practices == nil {
|
|
practices = []domain.PracticeByQuestionTypeDefinition{}
|
|
}
|
|
return domain.PracticesByQuestionTypeDefinitionSummary{
|
|
QuestionTypeDefinitionID: definitionID,
|
|
Practices: practices,
|
|
TotalCount: total,
|
|
Limit: limit,
|
|
Offset: offset,
|
|
}, nil
|
|
}
|
|
|
|
// User Personas in Question Sets
|
|
|
|
func (s *Service) AddUserPersonaToQuestionSet(ctx context.Context, questionSetID, userID int64, displayOrder int32) error {
|
|
return s.questionStore.AddUserPersonaToQuestionSet(ctx, questionSetID, userID, displayOrder)
|
|
}
|
|
|
|
func (s *Service) RemoveUserPersonaFromQuestionSet(ctx context.Context, questionSetID, userID int64) error {
|
|
return s.questionStore.RemoveUserPersonaFromQuestionSet(ctx, questionSetID, userID)
|
|
}
|
|
|
|
func (s *Service) GetUserPersonasByQuestionSetID(ctx context.Context, questionSetID int64) ([]domain.UserPersona, error) {
|
|
return s.questionStore.GetUserPersonasByQuestionSetID(ctx, questionSetID)
|
|
}
|