tether/src/controllers/channel/channel.ts
2026-01-11 14:17:13 -05:00

180 lines
5.1 KiB
TypeScript

import { type FastifyReply, type FastifyRequest } from "fastify";
import type {
IGetChannelParams,
IGetChannelResponseError,
IGetChannelResponseSuccess,
IPostCreateChannelRequest,
IPostCreateChannelResponseError,
IPostCreateChannelResponseSuccess,
IPatchChannelParams,
IPatchChannelRequest,
IPatchChannelResponseError,
IPatchChannelResponseSuccess,
IDeleteChannelParams,
IDeleteChannelResponseError,
IDeleteChannelResponseSuccess,
IGetMessagesParams,
IGetMessagesResponseError,
IGetMessagesResponseSuccess,
} from "./types.js";
import {
createChannelAuth,
deleteChannelByIdAuth,
getChannelByIdAuth,
updateChannelByIdAuth,
getChannelMessagesByIdAuth,
} from "../../services/channel/channel.js";
import { API_ERROR } from "../errors.js";
const getChannel = async (request: FastifyRequest, reply: FastifyReply) => {
const { id } = request.params as IGetChannelParams;
const authHeader = request.headers["authorization"];
const channel = await getChannelByIdAuth(id, authHeader);
if (!channel) {
reply.status(404);
return {
id: id,
error: API_ERROR.NOT_FOUND,
} as IGetChannelResponseError;
}
if (channel === API_ERROR.ACCESS_DENIED) {
reply.status(403);
return {
id: id,
error: API_ERROR.ACCESS_DENIED,
} as IGetChannelResponseError;
}
return {
id: channel.id,
name: channel.name,
description: channel.description,
communityId: channel.communityId,
creationDate: channel.creationDate.getTime(),
} as IGetChannelResponseSuccess;
};
const postCreateChannel = async (
request: FastifyRequest,
reply: FastifyReply,
) => {
const createChannelRequest = request.body as IPostCreateChannelRequest;
const authHeader = request.headers["authorization"];
const channel = await createChannelAuth(createChannelRequest, authHeader);
if (channel === API_ERROR.ACCESS_DENIED) {
reply.status(403);
return {
error: API_ERROR.ACCESS_DENIED,
} as IPostCreateChannelResponseError;
}
return {
id: channel.id,
name: channel.name,
description: channel.description,
communityId: channel.communityId,
creationDate: channel.creationDate.getTime(),
} as IPostCreateChannelResponseSuccess;
};
const patchChannel = async (request: FastifyRequest, reply: FastifyReply) => {
const { id } = request.params as IPatchChannelParams;
const patchChannelRequest = request.body as IPatchChannelRequest;
const authHeader = request.headers["authorization"];
const channel = await updateChannelByIdAuth(
id,
patchChannelRequest,
authHeader,
);
if (!channel) {
reply.status(404);
return {
id: id,
error: API_ERROR.NOT_FOUND,
} as IPatchChannelResponseError;
}
if (channel === API_ERROR.ACCESS_DENIED) {
reply.status(403);
return {
id: id,
error: API_ERROR.ACCESS_DENIED,
} as IPatchChannelResponseError;
}
return {
id: channel.id,
name: channel.name,
description: channel.description,
communityId: channel.communityId,
creationDate: channel.creationDate.getTime(),
} as IPatchChannelResponseSuccess;
};
const deleteChannel = async (request: FastifyRequest, reply: FastifyReply) => {
const { id } = request.params as IDeleteChannelParams;
const authHeader = request.headers["authorization"];
const channel = await deleteChannelByIdAuth(id, authHeader);
if (!channel) {
reply.status(404);
return {
id: id,
error: API_ERROR.NOT_FOUND,
} as IDeleteChannelResponseError;
}
if (channel === API_ERROR.ACCESS_DENIED) {
reply.status(403);
return {
id: id,
error: API_ERROR.ACCESS_DENIED,
} as IDeleteChannelResponseError;
}
return {
id: channel.id,
communityId: channel.communityId,
} as IDeleteChannelResponseSuccess;
};
const getMessages = async (request: FastifyRequest, reply: FastifyReply) => {
const { id } = request.params as IGetMessagesParams;
const authHeader = request.headers["authorization"];
const messages = await getChannelMessagesByIdAuth(id, authHeader);
if (!messages) {
reply.status(404);
return {
id: id,
error: API_ERROR.NOT_FOUND,
} as IGetMessagesResponseError;
}
if (messages === API_ERROR.ACCESS_DENIED) {
reply.status(403);
return {
id: id,
error: API_ERROR.ACCESS_DENIED,
} as IGetMessagesResponseError;
}
return {
id: id,
messages: messages.map((message) => ({
id: message.id,
text: message.text,
edited: message.edited,
ownerId: message.ownerId,
creationDate: message.creationDate.getTime(),
})),
} as IGetMessagesResponseSuccess;
};
export {
getChannel,
postCreateChannel,
patchChannel,
deleteChannel,
getMessages,
};