Add more services; Version 0.3.3

This commit is contained in:
Aslan 2025-12-29 02:02:56 +01:00
parent 50348cc494
commit ddcc591d12
26 changed files with 887 additions and 38 deletions

View file

@ -4,7 +4,7 @@ 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 } from "./types.js";
import type { ICreateChannel, IUpdateChannel } from "./types.js";
const getChannelById = async (id: string): Promise<Channel | null> => {
return await getDB().channel.findUnique({
@ -24,7 +24,7 @@ const getChannelByIdAuth = async (
!(await isUserAllowed(
authUser,
{
channel: channel,
community: community,
},
community,
[PERMISSION.CHANNELS_READ],
@ -67,4 +67,82 @@ const createChannelAuth = async (
return await createChannel(create);
};
export { getChannelById, getChannelByIdAuth, createChannel, createChannelAuth };
const updateChannelById = async (
id: string,
update: IUpdateChannel,
): Promise<Channel | null> => {
return await getDB().channel.update({
where: {
id: id,
},
data: {
...update,
},
});
};
const updateChannelByIdAuth = async (
id: string,
update: IUpdateChannel,
authHeader: string | undefined,
): Promise<Channel | null | API_ERROR.ACCESS_DENIED> => {
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<Channel | null> => {
return await getDB().channel.delete({
where: { id: id },
});
};
const deleteChannelByIdAuth = async (
id: string,
authHeader: string | undefined,
): Promise<Channel | null | API_ERROR.ACCESS_DENIED> => {
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);
};
export {
getChannelById,
getChannelByIdAuth,
createChannel,
createChannelAuth,
updateChannelById,
updateChannelByIdAuth,
deleteChannelById,
deleteChannelByIdAuth,
};