feat(ops): kb doc edit, validation auto-tickets, support batch ops

Add knowledge document GET/PUT and admin edit UI; auto-create support tickets on client 400 validation errors across user/shop/partner apps; batch create tasks and publish from support tickets; restore mini-user store env single-column layout.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-05 00:08:40 +08:00
parent 16c386f165
commit 02d89e6385
18 changed files with 677 additions and 24 deletions
@@ -6,6 +6,7 @@ import {
import type { SupportTicketStatus, SupportTicketType } from '@prisma/client';
import { Prisma } from '@prisma/client';
import type { CreateDevPlanTaskFromTicketInput } from '@dukang/shared-types';
import { mapSupportTicketTypeToDevPlanTask } from '@dukang/shared-types';
import { validateSupportTicketStatusTransition } from '@dukang/domain';
import { PrismaService } from '../../common/prisma/prisma.module';
import { serializeBigInt } from '../../common/decorators/current-user.decorator';
@@ -37,8 +38,17 @@ function generateSupportTicketNo() {
return `ST${Date.now()}${Math.floor(Math.random() * 900 + 100)}`;
}
const AUTO_VALIDATION_TICKET_APPS = new Set([
'USER_H5',
'USER_MINI',
'SHOP_H5',
'PARTNER_H5',
]);
@Injectable()
export class SupportTicketService {
private systemCreatorCache: { id: bigint; name: string } | null = null;
constructor(
private readonly prisma: PrismaService,
private readonly alert: AlertService,
@@ -94,6 +104,66 @@ export class SupportTicketService {
return serializeBigInt(mapSupportTicketRow(ticket));
}
/** 客户端 400 验证错误自动建单(1 小时内同标题去重) */
async createFromClientValidation(input: {
clientApp: string;
message: string;
pagePath?: string;
apiPath?: string;
actorLabel?: string;
}) {
if (!AUTO_VALIDATION_TICKET_APPS.has(input.clientApp)) {
return { skipped: true as const, reason: 'unsupported_app' as const };
}
const title = `[客户端验证] ${input.clientApp}${input.pagePath ? ` · ${input.pagePath}` : ''} · ${input.message.slice(0, 60)}`;
const oneHourAgo = new Date(Date.now() - 3600_000);
const existing = await this.prisma.commonSupportTicket.findFirst({
where: { title, createdAt: { gte: oneHourAgo } },
select: { id: true },
});
if (existing) {
return { skipped: true as const, ticketId: existing.id.toString() };
}
const creator = await this.resolveSystemCreator();
const content = [
`端:${input.clientApp}`,
input.pagePath ? `页面:${input.pagePath}` : null,
input.apiPath ? `接口:${input.apiPath}` : null,
input.actorLabel ? `用户:${input.actorLabel}` : null,
'',
input.message,
]
.filter(Boolean)
.join('\n');
const ticket = await this.create(
{
ticketType: 'BUG',
title,
content,
remark: '客户端验证错误自动上报',
},
creator,
);
return { skipped: false as const, ticketId: String(ticket.id) };
}
private async resolveSystemCreator() {
if (this.systemCreatorCache) return this.systemCreatorCache;
const account = await this.prisma.hqAccount.findFirst({
where: { adminRole: 'SUPER_ADMIN', status: 'ACTIVE' },
orderBy: { id: 'asc' },
select: { id: true },
});
if (!account) {
throw new BadRequestException('未找到系统管理员账号,无法自动创建工单');
}
this.systemCreatorCache = { id: account.id, name: '系统自动' };
return this.systemCreatorCache;
}
async update(id: bigint, dto: UpdateSupportTicketDto) {
const ticket = await this.getOrThrow(id);
if (ticket.status !== 'PENDING_REVIEW') {
@@ -360,6 +430,134 @@ export class SupportTicketService {
return { items: results };
}
/** 批量一键创建开发任务(每工单 1 条,内容取自标题/说明) */
async batchCreateTasks(
ticketIds: bigint[],
operator: { id: bigint; name: string },
) {
const results: Array<{
ticketId: string;
ok: boolean;
message?: string;
taskIds?: string[];
}> = [];
for (const id of ticketIds) {
try {
const ticket = await this.getOrThrow(id);
const linkedMap = await this.devPlan.listLinkedTasksByTicketIds([id]);
const existing = linkedMap.get(String(id)) ?? [];
if (existing.length > 0) {
results.push({ ticketId: String(id), ok: false, message: '已有开发任务,已跳过' });
continue;
}
const summary = [ticket.title, ticket.content].filter(Boolean).join('\n').slice(0, 500);
const createdTasks = await this.devPlan.createTasksFromTicket(
id,
[
{
content: summary || ticket.title,
type: mapSupportTicketTypeToDevPlanTask(ticket.ticketType as 'BUG' | 'SUGGESTION' | 'OTHER'),
},
],
operator.id,
);
if (ticket.status === 'PENDING_REVIEW') {
await this.prisma.commonSupportTicket.update({
where: { id },
data: {
status: 'DEVELOPING',
reviewerId: operator.id,
reviewerName: operator.name,
reviewedAt: new Date(),
},
});
}
results.push({
ticketId: String(id),
ok: true,
taskIds: createdTasks.map((t) => t.id),
});
} catch (err) {
results.push({
ticketId: String(id),
ok: false,
message: err instanceof Error ? err.message : '创建失败',
});
}
}
const successCount = results.filter((r) => r.ok).length;
return { results, successCount, failCount: results.length - successCount };
}
/** 批量一键发布:关联开发版本,可选企微派发 */
async batchPublish(
ticketIds: bigint[],
input: {
versionId: string;
dispatchToWecom?: boolean;
dispatchSupplement?: string;
},
operator: { id: bigint; name: string },
) {
const perTicket: Array<{ ticketId: string; ok: boolean; message?: string }> = [];
const taskIdSet = new Set<string>();
for (const id of ticketIds) {
const linkedMap = await this.devPlan.listLinkedTasksByTicketIds([id]);
const linked = linkedMap.get(String(id)) ?? [];
if (!linked.length) {
perTicket.push({ ticketId: String(id), ok: false, message: '无关联开发任务' });
continue;
}
linked.forEach((t) => taskIdSet.add(t.id));
perTicket.push({ ticketId: String(id), ok: true });
}
const taskIds = [...taskIdSet];
if (!taskIds.length) {
return {
results: perTicket,
successCount: 0,
failCount: perTicket.length,
linkedTaskCount: 0,
};
}
await this.devPlan.batchUpdateTasks({ taskIds, versionId: input.versionId });
if (input.dispatchToWecom) {
try {
await this.devPlan.dispatchTasks(
{ taskIds, supplement: input.dispatchSupplement },
operator.id,
);
} catch (err) {
this.alert.notify({
level: 'P2',
category: 'ops',
title: '批量发布企微派发失败',
detail: err instanceof Error ? err.message : String(err),
dedupeKey: `support_batch_publish_dispatch_fail|${Date.now()}`,
dedupeTtlSec: 120,
});
}
}
const successCount = perTicket.filter((r) => r.ok).length;
return {
results: perTicket,
successCount,
failCount: perTicket.length - successCount,
linkedTaskCount: taskIds.length,
versionId: input.versionId,
};
}
/** 开发完成 → 测试 */
async startTesting(id: bigint, dto?: SupportTicketRemarkDto) {
const ticket = await this.getOrThrow(id);