perf: 优化代码
This commit is contained in:
@@ -10,7 +10,7 @@ import { validateEnvironment } from './env.validation';
|
|||||||
isGlobal: true,
|
isGlobal: true,
|
||||||
load: [databaseConfig],
|
load: [databaseConfig],
|
||||||
validate: validateEnvironment,
|
validate: validateEnvironment,
|
||||||
envFilePath: ['.env', '.env.local', '.env.production'],
|
envFilePath: ['.env.local', '.env.production', '.env'],
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
exports: [ConfigModule],
|
exports: [ConfigModule],
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import {
|
|||||||
Entity,
|
Entity,
|
||||||
PrimaryGeneratedColumn,
|
PrimaryGeneratedColumn,
|
||||||
Column,
|
Column,
|
||||||
CreateDateColumn,
|
|
||||||
ManyToOne,
|
ManyToOne,
|
||||||
JoinColumn,
|
JoinColumn,
|
||||||
Index,
|
Index,
|
||||||
@@ -39,6 +38,6 @@ export class ShareLevelProgress {
|
|||||||
@Column({ type: 'int', default: 0, name: 'time_spent' })
|
@Column({ type: 'int', default: 0, name: 'time_spent' })
|
||||||
timeSpent!: number;
|
timeSpent!: number;
|
||||||
|
|
||||||
@CreateDateColumn({ name: 'completed_at' })
|
@Column({ type: 'timestamp', name: 'completed_at', nullable: true, default: null })
|
||||||
completedAt!: Date;
|
completedAt!: Date | null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -114,73 +114,68 @@ export class ShareService {
|
|||||||
userId: string,
|
userId: string,
|
||||||
dto: ReportLevelProgressDto,
|
dto: ReportLevelProgressDto,
|
||||||
): Promise<ReportLevelProgressResponseDto> {
|
): Promise<ReportLevelProgressResponseDto> {
|
||||||
// 1. 查找分享配置
|
const [config, level] = await Promise.all([
|
||||||
const config = await this.shareConfigRepository.findByShareCode(
|
this.shareConfigRepository.findByShareCode(dto.shareCode),
|
||||||
dto.shareCode,
|
this.levelRepository.findById(dto.levelId),
|
||||||
);
|
]);
|
||||||
|
|
||||||
if (!config) {
|
if (!config) {
|
||||||
throw new NotFoundException('分享不存在或已过期');
|
throw new NotFoundException('分享不存在或已过期');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 查找关卡获取时间限制(提前查找,后面会用到)
|
|
||||||
const level = await this.levelRepository.findById(dto.levelId);
|
|
||||||
if (!level) {
|
if (!level) {
|
||||||
throw new NotFoundException('关卡不存在');
|
throw new NotFoundException('关卡不存在');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 查找或创建 ShareParticipant
|
if (!config.levelIds.includes(dto.levelId)) {
|
||||||
|
throw new BadRequestException('该关卡不属于此分享挑战');
|
||||||
|
}
|
||||||
|
|
||||||
let participant =
|
let participant =
|
||||||
await this.shareParticipantRepository.findByShareConfigAndParticipant(
|
await this.shareParticipantRepository.findByShareConfigAndParticipant(
|
||||||
config.id,
|
config.id,
|
||||||
userId,
|
userId,
|
||||||
);
|
);
|
||||||
if (!participant) {
|
if (!participant) {
|
||||||
participant = await this.shareParticipantRepository.create({
|
participant = this.shareParticipantRepository.create({
|
||||||
shareConfigId: config.id,
|
shareConfigId: config.id,
|
||||||
participantId: userId,
|
participantId: userId,
|
||||||
});
|
});
|
||||||
participant = await this.shareParticipantRepository.save(participant);
|
participant = await this.shareParticipantRepository.save(participant);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. 查找现有进度
|
const progress =
|
||||||
let progress =
|
|
||||||
await this.shareLevelProgressRepository.findByParticipantAndLevel(
|
await this.shareLevelProgressRepository.findByParticipantAndLevel(
|
||||||
participant.id,
|
participant.id,
|
||||||
dto.levelId,
|
dto.levelId,
|
||||||
);
|
);
|
||||||
|
|
||||||
// 5. 如果 passed=true 且已有通关记录,直接返回(不覆盖)
|
|
||||||
if (dto.passed && progress?.passed) {
|
if (dto.passed && progress?.passed) {
|
||||||
const wasWithinTimeLimit =
|
|
||||||
level.timeLimit === null || progress.timeSpent <= level.timeLimit;
|
|
||||||
return {
|
return {
|
||||||
passed: true,
|
passed: true,
|
||||||
timeLimit: level.timeLimit,
|
timeLimit: level.timeLimit,
|
||||||
withinTimeLimit: wasWithinTimeLimit,
|
withinTimeLimit: this.isWithinTimeLimit(level.timeLimit, progress.timeSpent),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. 判断是否在时间限制内通过
|
|
||||||
const withinTimeLimit = dto.passed
|
const withinTimeLimit = dto.passed
|
||||||
? level.timeLimit === null || dto.timeSpent <= level.timeLimit
|
? this.isWithinTimeLimit(level.timeLimit, dto.timeSpent)
|
||||||
: false;
|
: false;
|
||||||
|
|
||||||
// 7. 创建或更新进度
|
const updatedProgress = progress
|
||||||
if (progress) {
|
? Object.assign(this.shareLevelProgressRepository.create(progress), {
|
||||||
progress.passed = dto.passed;
|
passed: dto.passed,
|
||||||
progress.timeSpent = dto.timeSpent;
|
timeSpent: dto.timeSpent,
|
||||||
progress.completedAt = dto.passed ? new Date() : progress.completedAt;
|
completedAt: dto.passed ? new Date() : progress.completedAt,
|
||||||
} else {
|
})
|
||||||
progress = this.shareLevelProgressRepository.create({
|
: this.shareLevelProgressRepository.create({
|
||||||
participantId: participant.id,
|
participantId: participant.id,
|
||||||
levelId: dto.levelId,
|
levelId: dto.levelId,
|
||||||
passed: dto.passed,
|
passed: dto.passed,
|
||||||
timeSpent: dto.timeSpent,
|
timeSpent: dto.timeSpent,
|
||||||
completedAt: dto.passed ? new Date() : undefined,
|
completedAt: dto.passed ? new Date() : null,
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
await this.shareLevelProgressRepository.save(progress);
|
await this.shareLevelProgressRepository.save(updatedProgress);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
passed: dto.passed,
|
passed: dto.passed,
|
||||||
@@ -188,4 +183,11 @@ export class ShareService {
|
|||||||
withinTimeLimit,
|
withinTimeLimit,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private isWithinTimeLimit(
|
||||||
|
timeLimit: number | null,
|
||||||
|
timeSpent: number,
|
||||||
|
): boolean {
|
||||||
|
return timeLimit === null || timeSpent <= timeLimit;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user