All files / src/handlers/api/line/webhook/messages route.ts

95.83% Statements 46/48
87.5% Branches 7/8
100% Functions 2/2
95.83% Lines 46/48

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 641x               5x 5x 5x 5x 5x 5x 5x 5x   5x   5x 5x   5x 5x 5x 5x   5x   1x 1x   5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   5x 5x 5x 3x 3x 1x   1x 5x     1x 5x 5x 5x 5x  
import { Env } from "@/Env";
import { v4 as uuidv4 } from "uuid";
import { RequestDataParser } from "@/Request";
import { createRedisClientByEnv, RedisClient } from "@/Redis";
import { Request, Response } from "express";
import { Logger } from "@/Logger";
import { NotFoundError } from "@/Error";
 
export async function DELETE(
  env: Env,
  parentLogger: Logger,
  req: Request,
  res: Response
) {
  const requestId = uuidv4();
  const logger = parentLogger.child({ requestId });
 
  const params = new RequestDataParser(req);
 
  const channelId = params.getPathParamAsString("channelId");
  const messageId = params.getPathParamAsString("messageId");
 
  logger.info("received request", {
    channelId,
    messageId,
  });
 
  await deleteMessages(env, logger, channelId, messageId);
 
  res.status(200).json({});
}
 
async function deleteMessages(
  env: Env,
  logger: Logger,
  channelId: string,
  messageId: string
): Promise<void> {
  const client = createRedisClientByEnv(env, channelId);
  logger.debug("make redis client", {
    redisHost: env.redisHost,
    redisPort: env.redisPort,
    redisStreamPrefix: env.redisStreamPrefixForLine,
    channelId,
    redisGroupName: env.redisGroupNameForLine,
  });
 
  try {
    const ackedCount = await client.ackMessage(messageId);
    if (ackedCount === 0) {
      throw new NotFoundError("message not found when ack");
    }
    logger.debug("acked message", { messageId });
 
    const deletedCount = await client.deleteMessage(messageId);
    if (deletedCount === 0) {
      throw new NotFoundError("message not found when delete");
    }
    logger.debug("deleted message", { messageId });
  } finally {
    await client.disconnect();
  }
}