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

@ -1,7 +1,11 @@
import { API_ERROR } from "../../controllers/errors.js";
import type { Community, Invite, User } from "../../generated/prisma/client.js";
import { getDB } from "../../store/store.js";
import { getUserFromAuth, isUserAllowed } from "../auth/helpers.js";
import {
getUserFromAuth,
isUserAllowed,
isUserOwnerOrAdmin,
} from "../auth/helpers.js";
import { PERMISSION } from "../auth/permission.js";
import type {
ICreateCommunity,
@ -86,6 +90,30 @@ const updateCommunityByIdAuth = async (
return await updateCommunityById(id, update);
};
const deleteCommunityById = async (id: string): Promise<Community | null> => {
return await getDB().community.delete({
where: { id: id },
});
};
const deleteCommunityByIdAuth = async (
id: string,
authHeader: string | undefined,
): Promise<Community | null | API_ERROR.ACCESS_DENIED> => {
const authUser = await getUserFromAuth(authHeader);
const community = await getCommunityById(id);
if (
!(await isUserOwnerOrAdmin(authUser, {
community: community,
}))
) {
return API_ERROR.ACCESS_DENIED;
}
return await deleteCommunityById(id);
};
const getCommunityMembersById = async (
id: string,
): Promise<ICommunityMember[]> => {
@ -244,6 +272,8 @@ export {
createCommunityAuth,
updateCommunityById,
updateCommunityByIdAuth,
deleteCommunityById,
deleteCommunityByIdAuth,
getCommunityMembersById,
getCommunityMembersByIdAuth,
getCommunityChannelsById,