fix(workspace): 兜底修复本地快读空工作台
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaService } from '../../prisma/prisma.service';
|
||||
import { APP_DATA_KEYS } from '../data/data-keys';
|
||||
import { mapAppDataToV22Rows } from '../migration/app-data-v22.mapper';
|
||||
|
||||
interface RequirementQuery {
|
||||
productId?: string;
|
||||
@@ -103,7 +105,7 @@ export class V22QueryService {
|
||||
};
|
||||
}
|
||||
|
||||
async getWorkspaceData(userId: string) {
|
||||
async getWorkspaceData(userId: string): Promise<WorkspaceRows> {
|
||||
const normalizedUserId = userId.trim();
|
||||
if (!normalizedUserId) throw new BadRequestException('userId is required');
|
||||
|
||||
@@ -126,9 +128,39 @@ export class V22QueryService {
|
||||
}),
|
||||
]);
|
||||
|
||||
const relationResult = { versionPlans, devTasks, testCases, bugs };
|
||||
if (hasWorkspaceRows(relationResult)) return relationResult;
|
||||
|
||||
const fallback = await this.getWorkspaceDataFromAppData(normalizedUserId);
|
||||
if (hasWorkspaceRows(fallback)) return fallback;
|
||||
|
||||
return { versionPlans, devTasks, testCases, bugs };
|
||||
}
|
||||
|
||||
private async getWorkspaceDataFromAppData(userId: string): Promise<WorkspaceRows> {
|
||||
const rows = await this.prisma.appData.findMany({
|
||||
where: { key: { in: [...APP_DATA_KEYS] } },
|
||||
select: { key: true, value: true },
|
||||
});
|
||||
const snapshot = Object.fromEntries(rows.map((row) => [row.key, row.value]));
|
||||
const mapped = mapAppDataToV22Rows(snapshot);
|
||||
|
||||
return {
|
||||
versionPlans: mapped.versionPlans
|
||||
.filter((plan) => plan.ownerId === userId && plan.status !== 'completed')
|
||||
.sort(comparePlanRows),
|
||||
devTasks: mapped.devTasks
|
||||
.filter((task) => task.assigneeId === userId && task.status !== 'submitted')
|
||||
.sort(comparePriorityThenUpdatedRows),
|
||||
testCases: mapped.testCases
|
||||
.filter((testCase) => testCase.assigneeId === userId && !['passed', 'failed', 'blocked'].includes(testCase.status))
|
||||
.sort(comparePriorityThenUpdatedRows),
|
||||
bugs: mapped.bugs
|
||||
.filter((bug) => bug.assigneeId === userId && ['open', 'fixing', 'fixed', 'verifying'].includes(bug.status))
|
||||
.sort(comparePriorityThenUpdatedRows),
|
||||
};
|
||||
}
|
||||
|
||||
async getXiaobaoWarnings(query: XiaobaoWarningQuery) {
|
||||
if (query.manager === 'true') {
|
||||
return this.prisma.xiaobaoRiskSummary.findMany({
|
||||
@@ -174,6 +206,49 @@ export class V22QueryService {
|
||||
}
|
||||
}
|
||||
|
||||
type WorkspaceRows = {
|
||||
versionPlans: unknown[];
|
||||
devTasks: unknown[];
|
||||
testCases: unknown[];
|
||||
bugs: unknown[];
|
||||
};
|
||||
|
||||
type PriorityUpdatedRow = {
|
||||
priority?: number;
|
||||
updatedAt?: string;
|
||||
};
|
||||
|
||||
type PlanSortRow = {
|
||||
expectedEndAt?: string;
|
||||
updatedAt?: string;
|
||||
};
|
||||
|
||||
function hasWorkspaceRows(rows: WorkspaceRows): boolean {
|
||||
return rows.versionPlans.length > 0
|
||||
|| rows.devTasks.length > 0
|
||||
|| rows.testCases.length > 0
|
||||
|| rows.bugs.length > 0;
|
||||
}
|
||||
|
||||
function comparePlanRows(a: PlanSortRow, b: PlanSortRow): number {
|
||||
const endDiff = dateSortValue(a.expectedEndAt, Number.POSITIVE_INFINITY)
|
||||
- dateSortValue(b.expectedEndAt, Number.POSITIVE_INFINITY);
|
||||
if (endDiff !== 0) return endDiff;
|
||||
return dateSortValue(b.updatedAt, 0) - dateSortValue(a.updatedAt, 0);
|
||||
}
|
||||
|
||||
function comparePriorityThenUpdatedRows(a: PriorityUpdatedRow, b: PriorityUpdatedRow): number {
|
||||
const priorityDiff = (a.priority ?? 0) - (b.priority ?? 0);
|
||||
if (priorityDiff !== 0) return priorityDiff;
|
||||
return dateSortValue(b.updatedAt, 0) - dateSortValue(a.updatedAt, 0);
|
||||
}
|
||||
|
||||
function dateSortValue(value: string | undefined, fallback: number): number {
|
||||
if (!value) return fallback;
|
||||
const time = new Date(value).getTime();
|
||||
return Number.isFinite(time) ? time : fallback;
|
||||
}
|
||||
|
||||
function parseLimit(raw?: string): number {
|
||||
const parsed = raw ? Number(raw) : 50;
|
||||
if (!Number.isFinite(parsed)) return 50;
|
||||
|
||||
Reference in New Issue
Block a user