fix(我的培训):「我的培训」的“与我相关”判定(学习任务/讲师/部门范围)不含「参加人员」——线下课不生成学习任务,参加人员只落场次名单与文案,刘义亮(资讯组)被课程部门范围(人事课)拦在页签外。
deploy-hxtesting / build-and-deploy (push) Successful in 20m39s
deploy-hxtesting / build-and-deploy (push) Successful in 20m39s
This commit is contained in:
1 parent
448fbcdaff
commit
11450bdb96
8 files changed
+167
-2
No files matched your search
@@ -540,6 +540,19 @@ func (r *trainingRepository) CountEnrolledMembersByCourseID(ctx context.Context,
|
||||
return int(cnt), err
|
||||
}
|
||||
|
||||
// ListMyEnrolledCourseIDs 当前成员经场次学员名单报名的课程 id 去重集合
|
||||
// (我的培训「我参加」判定数据源)。名单为软删(ReplaceEnrollments 移除即置
|
||||
// deleted_at),故须过滤 e.deleted_at;场次软删行同样不计。
|
||||
func (r *trainingRepository) ListMyEnrolledCourseIDs(ctx context.Context, tenantID uint64, memberID string) ([]string, error) {
|
||||
ids := []string{}
|
||||
err := r.db.WithContext(ctx).
|
||||
Table("tr_enrollments e").
|
||||
Joins("JOIN tr_course_sessions s ON s.id = e.session_id AND s.tenant_id = e.tenant_id AND s.deleted_at IS NULL").
|
||||
Where("e.tenant_id = ? AND e.member_id = ? AND e.deleted_at IS NULL", tenantID, memberID).
|
||||
Distinct().Pluck("s.course_id", &ids).Error
|
||||
return ids, err
|
||||
}
|
||||
|
||||
// CourseStudyStats 按课程聚合签到/线上学习/考试参与成员(去重成员集合):
|
||||
// 签到走场次联查;学习任务直接带课程;考试支持场次卷与线上自学卷
|
||||
// (session 为空时经 paper→bank→course 兜底)两条路径。
|
||||
|
||||
@@ -1135,6 +1135,18 @@ func (s *trainingService) ListMyLearningTasks(ctx context.Context, tenantID uint
|
||||
return s.repo.ListLearningTasks(ctx, tenantID, member.ID)
|
||||
}
|
||||
|
||||
// ListMyEnrolledCourseIDs 我经场次学员名单报名的课程 id 去重集合(我的培训
|
||||
// 「我参加」判定数据源):参加人员此前只落场次名单与 post_scope 文案,不进
|
||||
// 任何"与我相关"判定,线下课参加人员在「我的培训」看不到课。未建培训档案
|
||||
// 返回空集(与 ListMyLearningTasks 同口径,不报错)。
|
||||
func (s *trainingService) ListMyEnrolledCourseIDs(ctx context.Context, tenantID uint64, userID string) ([]string, error) {
|
||||
member, err := s.repo.GetMemberByUserID(ctx, tenantID, userID)
|
||||
if err != nil {
|
||||
return []string{}, nil
|
||||
}
|
||||
return s.repo.ListMyEnrolledCourseIDs(ctx, tenantID, member.ID)
|
||||
}
|
||||
|
||||
// ListCourseLearningTasks returns all members' learning tasks of one course
|
||||
// (管理端「学习考试」课程学习情况;任务不存在但已发布课程的名单成员由前端按待学习渲染口径,此处按实际任务行返回)。
|
||||
func (s *trainingService) ListCourseLearningTasks(ctx context.Context, tenantID uint64, courseID string) ([]*types.TrainingLearningTask, error) {
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"academy/internal/application/repository"
|
||||
"academy/internal/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
// newMineEnrolledFixture 构建 sqlite + 真实仓储的 trainingService,专测
|
||||
// ListMyEnrolledCourseIDs(我的培训「我参加」判定数据源)。
|
||||
func newMineEnrolledFixture(t *testing.T) (*trainingService, *gorm.DB, uint64) {
|
||||
t.Helper()
|
||||
db, err := gorm.Open(
|
||||
sqlite.Open(filepath.Join(t.TempDir(), "training_mine_enrolled.db")+"?_busy_timeout=5000&_journal_mode=WAL"),
|
||||
&gorm.Config{Logger: logger.Default.LogMode(logger.Silent)},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
raw, err := db.DB()
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = raw.Close() })
|
||||
require.NoError(t, db.AutoMigrate(
|
||||
&types.OrgEmployee{}, &types.TrainingCourseSession{}, &types.TrainingEnrollment{},
|
||||
))
|
||||
const tenantID = uint64(42)
|
||||
svc := &trainingService{repo: repository.NewTrainingRepository(db), db: db}
|
||||
return svc, db, tenantID
|
||||
}
|
||||
|
||||
// seedMember 建 org_employees 档案(同空间同用户唯一,复用场景自行多次挂名单)。
|
||||
func seedMember(t *testing.T, db *gorm.DB, tenantID uint64, userID string) (memberID string) {
|
||||
t.Helper()
|
||||
member := types.OrgEmployee{ID: trainingNewID(), TenantID: tenantID, UserID: userID, Status: "active"}
|
||||
require.NoError(t, db.Create(&member).Error)
|
||||
return member.ID
|
||||
}
|
||||
|
||||
// seedEnrollment 为指定成员落一条 场次+名单 链路,返回名单行(供软删用例复用)。
|
||||
func seedEnrollment(t *testing.T, db *gorm.DB, tenantID uint64, memberID, courseID string) *types.TrainingEnrollment {
|
||||
t.Helper()
|
||||
sess := types.TrainingCourseSession{ID: trainingNewID(), TenantID: tenantID, CourseID: courseID, Status: "scheduled"}
|
||||
require.NoError(t, db.Create(&sess).Error)
|
||||
row := &types.TrainingEnrollment{ID: trainingNewID(), TenantID: tenantID, SessionID: sess.ID, MemberID: memberID}
|
||||
require.NoError(t, db.Create(row).Error)
|
||||
return row
|
||||
}
|
||||
|
||||
// 我的培训「我参加」:名单命中返回课程 id,跨场次/跨课程去重正确。
|
||||
func TestListMyEnrolledCourseIDs_HitsAndDistinct(t *testing.T) {
|
||||
svc, db, tenantID := newMineEnrolledFixture(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// 课程A两条场次名单(去重为一个 id)+ 课程B一条名单
|
||||
memberID := seedMember(t, db, tenantID, "user-liu")
|
||||
seedEnrollment(t, db, tenantID, memberID, "course-A")
|
||||
seedEnrollment(t, db, tenantID, memberID, "course-A")
|
||||
seedEnrollment(t, db, tenantID, memberID, "course-B")
|
||||
|
||||
ids, err := svc.ListMyEnrolledCourseIDs(ctx, tenantID, "user-liu")
|
||||
require.NoError(t, err)
|
||||
require.ElementsMatch(t, []string{"course-A", "course-B"}, ids)
|
||||
}
|
||||
|
||||
// 排除口径:软删名单/软删场次/他人名单/其他空间名单均不进「我参加」。
|
||||
func TestListMyEnrolledCourseIDs_ExcludesDeletedAndForeign(t *testing.T) {
|
||||
svc, db, tenantID := newMineEnrolledFixture(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// 命中一条基线(course-keep)
|
||||
memberID := seedMember(t, db, tenantID, "user-liu")
|
||||
seedEnrollment(t, db, tenantID, memberID, "course-keep")
|
||||
|
||||
// 软删名单(course-soft-del)
|
||||
rowDel := seedEnrollment(t, db, tenantID, memberID, "course-soft-del")
|
||||
require.NoError(t, db.Delete(rowDel).Error)
|
||||
|
||||
// 场次软删(course-sess-del)
|
||||
rowGone := seedEnrollment(t, db, tenantID, memberID, "course-sess-del")
|
||||
require.NoError(t, db.Model(&types.TrainingCourseSession{}).
|
||||
Where("id = ?", rowGone.SessionID).Delete(&types.TrainingCourseSession{}).Error)
|
||||
|
||||
// 他人名单(course-other-member):同会话名单换人不影响本人判定
|
||||
otherID := seedMember(t, db, tenantID, "user-other")
|
||||
seedEnrollment(t, db, tenantID, otherID, "course-other-member")
|
||||
|
||||
// 其他空间(tenant 43)同名用户名单
|
||||
otherTenantID := seedMember(t, db, tenantID+1, "user-liu")
|
||||
seedEnrollment(t, db, tenantID+1, otherTenantID, "course-other-tenant")
|
||||
|
||||
ids, err := svc.ListMyEnrolledCourseIDs(ctx, tenantID, "user-liu")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []string{"course-keep"}, ids)
|
||||
}
|
||||
|
||||
// 无培训档案用户返回空集不报错(与 ListMyLearningTasks 同口径)。
|
||||
func TestListMyEnrolledCourseIDs_NoMemberReturnsEmpty(t *testing.T) {
|
||||
svc, _, tenantID := newMineEnrolledFixture(t)
|
||||
ids, err := svc.ListMyEnrolledCourseIDs(context.Background(), tenantID, "user-without-profile")
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, ids)
|
||||
}
|
||||
@@ -367,6 +367,18 @@ func (h *Handler) ListMyLearningTasks(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": items})
|
||||
}
|
||||
|
||||
// ListMyEnrolledCourseIds GET /training/my/enrolled-course-ids — 我经场次学员
|
||||
// 名单报名的课程 id 去重集合(我的培训「我参加」判定数据源,前端与学习任务/
|
||||
// 讲师/部门范围口径互补;登录即可查,自身名单本身即个人数据)。
|
||||
func (h *Handler) ListMyEnrolledCourseIds(c *gin.Context) {
|
||||
ids, err := h.svc.ListMyEnrolledCourseIDs(c.Request.Context(), callerTenantID(c), callerUserID(c))
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": ids})
|
||||
}
|
||||
|
||||
// ListCourseLearningTasks GET /training/learning-tasks/all?course_id= —
|
||||
// 按课程返回全员学习任务(人事/课长/讲师的「学习考试」课程学习情况视图)。
|
||||
func (h *Handler) ListCourseLearningTasks(c *gin.Context) {
|
||||
|
||||
@@ -168,6 +168,8 @@ func RegisterTrainingRoutes(r *gin.RouterGroup, h *training.Handler, g *rbacGuar
|
||||
trainingGroup.GET("/learning-progress", h.ListMyCourseProgress)
|
||||
trainingGroup.GET("/learning-tasks", h.ListMyLearningTasks)
|
||||
trainingGroup.GET("/learning-tasks/all", h.ListCourseLearningTasks)
|
||||
// my/enrolled-course-ids 我经场次学员名单报名的课程 id 集合(我的培训「我参加」判定)。
|
||||
trainingGroup.GET("/my/enrolled-course-ids", h.ListMyEnrolledCourseIds)
|
||||
trainingGroup.POST("/course-feedbacks", h.SaveFeedback)
|
||||
trainingGroup.POST("/course-feedbacks/:id/reply", g.Contributor(), h.ReplyFeedback)
|
||||
trainingGroup.GET("/course-feedbacks/pending", h.ListPendingFeedbacks)
|
||||
|
||||
@@ -116,6 +116,9 @@ type TrainingRepository interface {
|
||||
CountEnrolledMembersByCourse(ctx context.Context, tenantID uint64) (map[string]int, error)
|
||||
// CountEnrolledMembersByCourseID 单课程场次学员名单人数(发布必填校验,000161)。
|
||||
CountEnrolledMembersByCourseID(ctx context.Context, tenantID uint64, courseID string) (int, error)
|
||||
// ListMyEnrolledCourseIDs 当前成员经场次学员名单报名的课程 id 去重集合
|
||||
// (我的培训「我参加」判定数据源;软删名单/软删场次不计)。
|
||||
ListMyEnrolledCourseIDs(ctx context.Context, tenantID uint64, memberID string) ([]string, error)
|
||||
// CourseStudyStats 按课程聚合签到/线上学习/考试参与成员(课程管理列表学习/考试状态列)。
|
||||
CourseStudyStats(ctx context.Context, tenantID uint64) (map[string]*types.TrainingCourseStudyStats, error)
|
||||
// CourseReporterDeptNames 各课程上报部门名(课程创建人所属部门,联查 org_employees/org_depts)。
|
||||
@@ -444,6 +447,9 @@ type TrainingService interface {
|
||||
// ListMyCourseProgress 当前用户在某课程下各课件的学习进度(学习详情页回读)。
|
||||
ListMyCourseProgress(ctx context.Context, tenantID uint64, userID, courseID string) ([]*types.TrainingLearningProgress, error)
|
||||
ListMyLearningTasks(ctx context.Context, tenantID uint64, userID string) ([]*types.TrainingLearningTask, error)
|
||||
// ListMyEnrolledCourseIDs 我经场次学员名单报名的课程 id 去重集合(我的培训
|
||||
// 「我参加」判定,与学习任务/讲师/部门范围口径互补;线下课参加人员由此可见)。
|
||||
ListMyEnrolledCourseIDs(ctx context.Context, tenantID uint64, userID string) ([]string, error)
|
||||
// ListCourseLearningTasks 按课程返回全员学习任务(人事/讲师课程学习情况)。
|
||||
ListCourseLearningTasks(ctx context.Context, tenantID uint64, courseID string) ([]*types.TrainingLearningTask, error)
|
||||
SaveFeedback(ctx context.Context, tenantID uint64, userID, sessionID string, rating int, comment string) (*types.TrainingCourseFeedback, error)
|
||||
|
||||
Reference in new issue
Block a user