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

100% Statements 38/38
100% Branches 3/3
100% Functions 1/1
100% Lines 38/38

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 531x               1x   3x 3x 3x 3x 3x 3x 3x 3x   3x   3x 3x   3x 3x   3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x   3x 1x   1x 3x 2x 2x 3x  
import { webhook } from "@line/bot-sdk";
import { v4 as uuidv4 } from "uuid";
import { createRedisClientByEnv, WebhookStreamObject } from "@/Redis";
import express from "express";
import { Env } from "@/Env";
import { Logger } from "@/Logger";
import { RequestDataParser } from "@/Request";
 
const HEADER_SIGNATURE = "x-line-signature";
 
export async function POST(
  env: Env,
  parentLogger: Logger,
  req: express.Request,
  res: express.Response
) {
  const requestId = uuidv4();
  const logger = parentLogger.child({ requestId });
 
  const params = new RequestDataParser(req);
 
  const channelId = params.getPathParamAsString("channelId");
  const signature = params.getHeaderAsString(HEADER_SIGNATURE);
 
  const body = req.body as webhook.CallbackRequest;
  logger.info("received request", { channelId, signature, body });
 
  const client = createRedisClientByEnv(env, channelId);
  logger.debug("make redis client", {
    redisHost: env.redisHost,
    redisPort: env.redisPort,
    redisStreamName: env.redisStreamPrefixForLine,
    channelId,
    redisGroupName: env.redisGroupNameForLine,
  });
  try {
    const streamObj: WebhookStreamObject = {
      requestId,
      signature,
      destination: body.destination,
      events: body.events,
    };
    logger.debug("make webhook stream object", { streamObj });
 
    const id = await client.addWebhookStreamObject(streamObj);
    logger.info("added webhookStreamObject", { id });
 
    res.status(200).json({});
  } finally {
    await client.disconnect();
  }
}