# All files / modules/history history-batch.controller.ts

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

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
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105

     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();

    // 批量删除历史记录（使用POST避免DELETE body问题）
    router.post('/batch-delete', optionalAuth, async (ctx: Context) => {
      const userId = ctx.state.user?.userId || TEST_USER_ID;
      const body = ctx.request.body as {
        ids: string[];
      };

      if (!body || !body.ids || !Array.isArray(body.ids)) {
        ctx.status = 400;
        ctx.body = {
          code: 400,
          message: 'ids 参数必须是数组',
        };
        return;
      }

      try {
        // 删除 AudioRecord 记录
        const result = await prisma.audioRecord.deleteMany({
          where: {
            audioId: { in: body.ids },
          },
        });

        ctx.body = {
          code: 0,
          message: 'success',
          data: {
            deletedCount: result.count,
          },
        };
      } catch (error) {
        console.error('批量删除历史记录失败:', error);
        ctx.status = 500;
        ctx.body = {
          code: 500,
          message: '删除失败',
        };
      }
    });

    // 保留DELETE方法作为兼容
    router.delete('/batch', optionalAuth, async (ctx: Context) => {
      const userId = ctx.state.user?.userId || TEST_USER_ID;
      const body = ctx.request.body as {
        ids: string[];
      };

      // 尝试从body获取，如果不行从query获取
      let ids: string[] = [];
      if (body && body.ids && Array.isArray(body.ids)) {
        ids = body.ids;
      } else if (ctx.query.ids) {
        try {
          ids = JSON.parse(ctx.query.ids as string);
        } catch {
          // 忽略
        }
      }

      if (!ids || !Array.isArray(ids) || ids.length === 0) {
        ctx.status = 400;
        ctx.body = {
          code: 400,
          message: 'ids 参数必须是数组',
        };
        return;
      }

      try {
        // 删除 AudioRecord 记录
        const result = await prisma.audioRecord.deleteMany({
          where: {
            audioId: { in: ids },
          },
        });

        ctx.body = {
          code: 0,
          message: 'success',
          data: {
            deletedCount: result.count,
          },
        };
      } catch (error) {
        console.error('批量删除历史记录失败:', error);
        ctx.status = 500;
        ctx.body = {
          code: 500,
          message: '删除失败',
        };
      }
    });

    export default router;