New controllers and services; Auth
This commit is contained in:
parent
2fc0f9c404
commit
d17f37749d
35 changed files with 1040 additions and 164 deletions
|
|
@ -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 };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue