52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
import { DatabaseError, ErrorBase, InputError } from "@/errors";
|
|
import Logger from "@/lib/logger";
|
|
import { ChannelEntity } from "@/modules/entities/Channel";
|
|
import { ChannelRepository } from "@/modules/repositories/Channel";
|
|
import type { FastifyInstance } from "fastify";
|
|
import z from "zod/v3";
|
|
|
|
export default async function ChannelEdit(fastify: FastifyInstance) {
|
|
const logger = new Logger("Endpoint | channel/edit");
|
|
|
|
fastify.post("/", async (req, res) => {
|
|
res.header("Content-Type", "application/json");
|
|
|
|
if ("error" in req.token)
|
|
return res.code(400).send(req.token);
|
|
|
|
const result = ChannelRepository.schema
|
|
.omit({ userid: true, community: true }).partial()
|
|
.merge(z.object({ id: z.string().length(10) }))
|
|
.refine(data =>
|
|
!Object.keys(data).length
|
|
).safeParse(req.body);
|
|
|
|
if (!result.success) {
|
|
return res.code(400).send(InputError(result.error.issues));
|
|
}
|
|
|
|
try {
|
|
const channelRepo = fastify.orm.em.getRepository(ChannelEntity);
|
|
const itChannel = await channelRepo.findOne({ id: result.data.id });
|
|
|
|
if (!itChannel) {
|
|
return res.code(400).send(ErrorBase({
|
|
bad: "client",
|
|
code: "channel_not_found",
|
|
message: "対象のチャンネルが見つかりませんでした。",
|
|
}));
|
|
}
|
|
|
|
const id = await channelRepo.editChannel(itChannel, result.data);
|
|
|
|
return res.send({
|
|
success: true,
|
|
id,
|
|
});
|
|
} catch (err) {
|
|
logger.error("Database Error:", err);
|
|
|
|
return res.code(500).send(DatabaseError());
|
|
}
|
|
});
|
|
} |