New controllers and services; Auth

This commit is contained in:
Aslan 2025-12-26 19:33:43 +01:00
parent 2fc0f9c404
commit d17f37749d
35 changed files with 1040 additions and 164 deletions

View file

@ -1,27 +1,38 @@
import { type FastifyReply, type FastifyRequest } from "fastify";
import type {
IChannelParams,
IChannelResponseError,
IChannelResponseSuccess,
IGetChannelParams,
IGetChannelResponseError,
IGetChannelResponseSuccess,
} from "./types.js";
import { getChannelById } from "../../services/channel/channel.js";
import { getChannelByIdAuth } from "../../services/channel/channel.js";
import { API_ERROR } from "../errors.js";
const getChannel = async (request: FastifyRequest, _reply: FastifyReply) => {
const { id } = request.params as IChannelParams;
const getChannel = async (request: FastifyRequest, reply: FastifyReply) => {
const { id } = request.params as IGetChannelParams;
const authHeader = request.headers["authorization"];
const channel = await getChannelById(id);
const channel = await getChannelByIdAuth(id, authHeader);
if (!channel) {
reply.status(404);
return {
id: id,
error: "channel does not exist",
} as IChannelResponseError;
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,
communityId: channel.communityId,
} as IChannelResponseSuccess;
creationDate: channel.creationDate.getTime(),
} as IGetChannelResponseSuccess;
};
export { getChannel };