feat: 支持获取我创建的分享挑战列表以及详情数据

This commit is contained in:
richarjiang
2026-04-13 09:08:11 +08:00
parent fe2c13258e
commit 1d6cd0cdc0
10 changed files with 569 additions and 82 deletions

View File

@@ -12,6 +12,7 @@ import { CreateShareDto } from './dto/create-share.dto';
import { ReportLevelProgressDto } from './dto/report-level-progress.dto';
import {
CreateShareResponseDto,
CreatedShareListResponseDto,
JoinShareResponseDto,
ShareLevelDto,
} from './dto/share-response.dto';
@@ -110,6 +111,62 @@ export class ShareService {
};
}
async getCreatedShares(userId: string): Promise<CreatedShareListResponseDto> {
const configs = await this.shareConfigRepository.findBySharerId(userId);
if (configs.length === 0) {
return { items: [] };
}
const shareConfigIds = configs.map((config) => config.id);
const [participantCountMap, rankingRows] = await Promise.all([
this.shareParticipantRepository.countByShareConfigIds(shareConfigIds),
this.shareLevelProgressRepository.summarizeByShareConfigIds(
shareConfigIds,
),
]);
const rankingsByShareConfigId = new Map<string, string[]>();
for (const config of configs) {
const completedRankings = rankingRows
.filter(
(row) =>
row.shareConfigId === config.id &&
Number(row.passedLevelCount) === config.levelIds.length,
)
.sort((a, b) => {
const totalTimeDiff =
Number(a.totalTimeSpent) - Number(b.totalTimeSpent);
if (totalTimeDiff !== 0) {
return totalTimeDiff;
}
return a.participantId.localeCompare(b.participantId);
})
.map((row) => row.participantId);
rankingsByShareConfigId.set(config.id, completedRankings);
}
return {
items: configs.map((config) => {
const rankings = rankingsByShareConfigId.get(config.id) ?? [];
const rankingIndex = rankings.findIndex(
(participantId) => participantId === userId,
);
return {
id: config.id,
shareCode: config.shareCode,
title: config.title,
levelCount: config.levelIds.length,
participantCount: participantCountMap.get(config.id) ?? 0,
userRank: rankingIndex >= 0 ? rankingIndex + 1 : null,
createdAt: config.createdAt.toISOString(),
};
}),
};
}
async reportLevelProgress(
userId: string,
dto: ReportLevelProgressDto,
@@ -153,7 +210,10 @@ export class ShareService {
return {
passed: true,
timeLimit: level.timeLimit,
withinTimeLimit: this.isWithinTimeLimit(level.timeLimit, progress.timeSpent),
withinTimeLimit: this.isWithinTimeLimit(
level.timeLimit,
progress.timeSpent,
),
};
}