All files / modules/history history.controller.ts

0% Statements 0/55
0% Branches 0/1
0% Functions 0/1
0% Lines 0/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67                                                                                                                                     
import Router from '@koa/router';
import { Context } from 'koa';
import { optionalAuth } from '../../middleware/auth';
import { prisma } from '../../models';
 
const TEST_USER_ID = '1';
 
const router = new Router();
 
// 获取音频生成历史列表
router.get('/', optionalAuth, async (ctx: Context) => {
  // 开发环境使用测试用户ID
  const userId = ctx.state.user?.userId || TEST_USER_ID;
  const page = parseInt(ctx.query.page as string) || 1;
  const pageSize = parseInt(ctx.query.pageSize as string) || 20;
  const startDate = ctx.query.startDate as string;
 
  const where: any = {};
  if (startDate) {
    where.createdAt = { gte: new Date(startDate) };
  }
 
  const [records, total] = await Promise.all([
    prisma.audioRecord.findMany({
      where,
      orderBy: { createdAt: 'desc' },
      skip: (page - 1) * pageSize,
      take: pageSize,
    }),
    prisma.audioRecord.count({ where }),
  ]);
 
  const list = records.map((r) => ({
    _id: r.audioId,
    id: r.audioId,
    title: r.title,
    text: r.text || '',
    summary: '',
    tags: [],
    audioUrl: r.audioUrl || '',
    audioDuration: r.audioDuration,
    audioSize: r.audioSize,
    wordCount: r.wordCount,
    voiceId: r.voiceId,
    voiceParams: r.voiceParams ? JSON.parse(r.voiceParams) : { speed: 1, pitch: 0, volume: 50 },
    status: r.status,
    isFavorite: false,
    bookId: r.bookId,
    createdAt: r.createdAt.toISOString(),
    updatedAt: r.updatedAt.toISOString(),
  }));
 
  ctx.body = {
    code: 0,
    message: 'success',
    data: {
      list,
      total,
      page,
      pageSize,
      totalPages: Math.ceil(total / pageSize),
    },
  };
});
 
export default router;