Expose GET /admin/users/:user_id/subscriptions with full plan history and payments, and GET /admin/users/:user_id/learning-activity with LMS and exam-prep progress, completions, and recent timeline. Co-authored-by: Cursor <cursoragent@cursor.com>
168 lines
5.1 KiB
Go
168 lines
5.1 KiB
Go
package lmsprogress
|
|
|
|
import (
|
|
"context"
|
|
|
|
dbgen "Yimaru-Backend/gen/db"
|
|
"Yimaru-Backend/internal/domain"
|
|
)
|
|
|
|
// AdminUserExamPrepLearningActivityTree returns nested catalog course → unit → module → lesson → practice completions.
|
|
func (s *Service) AdminUserExamPrepLearningActivityTree(ctx context.Context, userID int64) (domain.AdminExamPrepUserLearningActivityTree, error) {
|
|
rows, err := s.store.ListUserExamPrepFlatLearningActivity(ctx, userID)
|
|
if err != nil {
|
|
return domain.AdminExamPrepUserLearningActivityTree{}, err
|
|
}
|
|
return buildAdminExamPrepLearningActivityTree(userID, rows), nil
|
|
}
|
|
|
|
type epLessonAccum struct {
|
|
id int64
|
|
title string
|
|
sortOrder int32
|
|
practices []domain.AdminExamPrepPracticeLearningEntry
|
|
seen map[int64]struct{}
|
|
}
|
|
|
|
type epModuleAccum struct {
|
|
id int64
|
|
name string
|
|
sortOrder int32
|
|
lessons map[int64]*epLessonAccum
|
|
order []int64
|
|
}
|
|
|
|
type epUnitAccum struct {
|
|
id int64
|
|
name string
|
|
sortOrder int32
|
|
modules map[int64]*epModuleAccum
|
|
order []int64
|
|
}
|
|
|
|
type epCatalogAccum struct {
|
|
id int64
|
|
name string
|
|
sortOrder int32
|
|
units map[int64]*epUnitAccum
|
|
order []int64
|
|
}
|
|
|
|
type examPrepActivityTreeBuilder struct {
|
|
catalogs map[int64]*epCatalogAccum
|
|
order []int64
|
|
}
|
|
|
|
func newExamPrepActivityTreeBuilder() *examPrepActivityTreeBuilder {
|
|
return &examPrepActivityTreeBuilder{catalogs: make(map[int64]*epCatalogAccum)}
|
|
}
|
|
|
|
func (b *examPrepActivityTreeBuilder) ensureCatalog(row dbgen.ListUserExamPrepFlatLearningActivityByUserRow) *epCatalogAccum {
|
|
if c, ok := b.catalogs[row.CatalogCourseID]; ok {
|
|
return c
|
|
}
|
|
c := &epCatalogAccum{
|
|
id: row.CatalogCourseID, name: row.CatalogCourseName, sortOrder: row.CatalogCourseSortOrder,
|
|
units: make(map[int64]*epUnitAccum),
|
|
}
|
|
b.catalogs[row.CatalogCourseID] = c
|
|
b.order = append(b.order, row.CatalogCourseID)
|
|
return c
|
|
}
|
|
|
|
func (ca *epCatalogAccum) ensureUnit(row dbgen.ListUserExamPrepFlatLearningActivityByUserRow) *epUnitAccum {
|
|
if u, ok := ca.units[row.UnitID]; ok {
|
|
return u
|
|
}
|
|
u := &epUnitAccum{
|
|
id: row.UnitID, name: row.UnitName, sortOrder: row.UnitSortOrder,
|
|
modules: make(map[int64]*epModuleAccum),
|
|
}
|
|
ca.units[row.UnitID] = u
|
|
ca.order = append(ca.order, row.UnitID)
|
|
return u
|
|
}
|
|
|
|
func (ua *epUnitAccum) ensureModule(row dbgen.ListUserExamPrepFlatLearningActivityByUserRow) *epModuleAccum {
|
|
if m, ok := ua.modules[row.ModuleID]; ok {
|
|
return m
|
|
}
|
|
m := &epModuleAccum{
|
|
id: row.ModuleID, name: row.ModuleName, sortOrder: row.ModuleSortOrder,
|
|
lessons: make(map[int64]*epLessonAccum),
|
|
}
|
|
ua.modules[row.ModuleID] = m
|
|
ua.order = append(ua.order, row.ModuleID)
|
|
return m
|
|
}
|
|
|
|
func (ma *epModuleAccum) ensureLesson(row dbgen.ListUserExamPrepFlatLearningActivityByUserRow) *epLessonAccum {
|
|
if l, ok := ma.lessons[row.LessonID]; ok {
|
|
return l
|
|
}
|
|
l := &epLessonAccum{
|
|
id: row.LessonID, title: row.LessonTitle, sortOrder: row.LessonSortOrder,
|
|
seen: make(map[int64]struct{}),
|
|
}
|
|
ma.lessons[row.LessonID] = l
|
|
ma.order = append(ma.order, row.LessonID)
|
|
return l
|
|
}
|
|
|
|
func (b *examPrepActivityTreeBuilder) ingest(row dbgen.ListUserExamPrepFlatLearningActivityByUserRow) {
|
|
c := b.ensureCatalog(row)
|
|
u := c.ensureUnit(row)
|
|
m := u.ensureModule(row)
|
|
l := m.ensureLesson(row)
|
|
if _, ok := l.seen[row.ExamPrepPracticeID]; ok {
|
|
return
|
|
}
|
|
l.seen[row.ExamPrepPracticeID] = struct{}{}
|
|
l.practices = append(l.practices, domain.AdminExamPrepPracticeLearningEntry{
|
|
ExamPrepPracticeID: row.ExamPrepPracticeID,
|
|
Title: row.PracticeTitle,
|
|
CompletedAt: row.ActivityAt.Time,
|
|
})
|
|
}
|
|
|
|
func buildAdminExamPrepLearningActivityTree(userID int64, rows []dbgen.ListUserExamPrepFlatLearningActivityByUserRow) domain.AdminExamPrepUserLearningActivityTree {
|
|
b := newExamPrepActivityTreeBuilder()
|
|
for _, row := range rows {
|
|
b.ingest(row)
|
|
}
|
|
|
|
catalogs := make([]domain.AdminExamPrepCatalogCourseLearningEntry, 0, len(b.order))
|
|
for _, catalogID := range b.order {
|
|
ca := b.catalogs[catalogID]
|
|
units := make([]domain.AdminExamPrepUnitLearningEntry, 0, len(ca.order))
|
|
for _, unitID := range ca.order {
|
|
ua := ca.units[unitID]
|
|
modules := make([]domain.AdminExamPrepModuleLearningEntry, 0, len(ua.order))
|
|
for _, moduleID := range ua.order {
|
|
ma := ua.modules[moduleID]
|
|
lessons := make([]domain.AdminExamPrepLessonLearningEntry, 0, len(ma.order))
|
|
for _, lessonID := range ma.order {
|
|
la := ma.lessons[lessonID]
|
|
lessons = append(lessons, domain.AdminExamPrepLessonLearningEntry{
|
|
ID: la.id, Title: la.title, SortOrder: la.sortOrder, Practices: la.practices,
|
|
})
|
|
}
|
|
modules = append(modules, domain.AdminExamPrepModuleLearningEntry{
|
|
ID: ma.id, Name: ma.name, SortOrder: ma.sortOrder, Lessons: lessons,
|
|
})
|
|
}
|
|
units = append(units, domain.AdminExamPrepUnitLearningEntry{
|
|
ID: ua.id, Name: ua.name, SortOrder: ua.sortOrder, Modules: modules,
|
|
})
|
|
}
|
|
catalogs = append(catalogs, domain.AdminExamPrepCatalogCourseLearningEntry{
|
|
ID: ca.id, Name: ca.name, SortOrder: ca.sortOrder, Units: units,
|
|
})
|
|
}
|
|
|
|
if catalogs == nil {
|
|
catalogs = []domain.AdminExamPrepCatalogCourseLearningEntry{}
|
|
}
|
|
return domain.AdminExamPrepUserLearningActivityTree{UserID: userID, CatalogCourses: catalogs}
|
|
}
|