import { API_ERROR } from "../../controllers/errors.js"; import type { Channel, Message } from "../../generated/prisma/client.js"; import { getDB } from "../../store/store.js"; import { getUserFromAuth, isUserAllowed } from "../auth/helpers.js"; import { PERMISSION } from "../auth/permission.js"; import { getCommunityById } from "../community/community.js"; import type { ICreateChannel, IUpdateChannel } from "./types.js"; const getChannelById = async (id: string): Promise => { return await getDB().channel.findUnique({ where: { id: id }, }); }; const getChannelByIdAuth = async ( id: string, authHeader: string | undefined, ): Promise => { const authUser = await getUserFromAuth(authHeader); const channel = await getChannelById(id); const community = await getCommunityById(channel?.communityId ?? ""); if ( !(await isUserAllowed( authUser, { community: community, }, community, [PERMISSION.CHANNELS_READ], )) ) { return API_ERROR.ACCESS_DENIED; } return channel; }; const createChannel = async (create: ICreateChannel): Promise => { return await getDB().channel.create({ data: { ...create, }, }); }; const createChannelAuth = async ( create: ICreateChannel, authHeader: string | undefined, ): Promise => { const authUser = await getUserFromAuth(authHeader); const community = await getCommunityById(create.communityId); if ( !(await isUserAllowed( authUser, { community: community, }, community, [PERMISSION.CHANNELS_MANAGE], )) ) { return API_ERROR.ACCESS_DENIED; } return await createChannel(create); }; const updateChannelById = async ( id: string, update: IUpdateChannel, ): Promise => { return await getDB().channel.update({ where: { id: id, }, data: { ...update, }, }); }; const updateChannelByIdAuth = async ( id: string, update: IUpdateChannel, authHeader: string | undefined, ): Promise => { const authUser = await getUserFromAuth(authHeader); const channel = await getChannelById(id); const community = await getCommunityById(channel?.communityId ?? ""); if ( !(await isUserAllowed( authUser, { community: community, }, community, [PERMISSION.CHANNELS_MANAGE], )) ) { return API_ERROR.ACCESS_DENIED; } return await updateChannelById(id, update); }; const deleteChannelById = async (id: string): Promise => { return await getDB().channel.delete({ where: { id: id }, }); }; const deleteChannelByIdAuth = async ( id: string, authHeader: string | undefined, ): Promise => { const authUser = await getUserFromAuth(authHeader); const channel = await getChannelById(id); const community = await getCommunityById(channel?.communityId ?? ""); if ( !(await isUserAllowed( authUser, { community: community, }, community, [PERMISSION.CHANNELS_MANAGE], )) ) { return API_ERROR.ACCESS_DENIED; } return await deleteChannelById(id); }; const getChannelMessagesById = async ( id: string, ): Promise => { return await getDB().message.findMany({ where: { channelId: id, }, }); }; const getChannelMessagesByIdAuth = async ( id: string, authHeader: string | undefined, ): Promise => { const authUser = await getUserFromAuth(authHeader); const channel = await getChannelById(id); const community = await getCommunityById(channel?.communityId ?? ""); if ( !(await isUserAllowed( authUser, { community: community, }, community, [PERMISSION.MESSAGES_READ], )) ) { return API_ERROR.ACCESS_DENIED; } return await getChannelMessagesById(id); }; export { getChannelById, getChannelByIdAuth, createChannel, createChannelAuth, updateChannelById, updateChannelByIdAuth, deleteChannelById, deleteChannelByIdAuth, getChannelMessagesById, getChannelMessagesByIdAuth, };