import { DatabaseError, ErrorBase, InputError } from "@/errors"; import Logger from "@/lib/logger"; import { ChannelEntity } from "@/modules/entities/Channel"; import type { FastifyInstance } from "fastify"; import z from "zod/v3"; export default async function ChannelGet(fastify: FastifyInstance) { const logger = new Logger("Endpoint | channel/get"); fastify.post("/", async (req, res) => { res.header("Content-Type", "application/json"); if ("error" in req.token) return res.code(400).send(req.token); const bodySchema = z.object({ id: z.string().length(10), }); const result = bodySchema.safeParse(req.body); if (!result.success) { return res.code(400).send(InputError(result.error.issues)); } try { const channelRepo = fastify.orm.em.getRepository(ChannelEntity); const findResult = await channelRepo.find({ id: result.data.id, }); if (findResult[0] === undefined) { return res.code(400).send(ErrorBase({ bad: "client", code: "channel_not_found", message: "対象のチャンネルが見つかりませんでした。", })); } return res.send({ success: true, channel: findResult[0], }); } catch (err) { logger.error("Database Error:", err); return res.code(500).send(DatabaseError()); } }); }