Add more services and auth

This commit is contained in:
Aslan 2025-12-27 00:18:15 +01:00
parent d17f37749d
commit cae53fab61
13 changed files with 331 additions and 57 deletions

View file

@ -1,5 +1,9 @@
import { API_ERROR } from "../../controllers/errors.js";
import type { Community } 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 type { IUpdateCommunity } from "./types.js";
const getCommunityById = async (id: string): Promise<Community | null> => {
return await getDB().community.findUnique({
@ -7,4 +11,42 @@ const getCommunityById = async (id: string): Promise<Community | null> => {
});
};
export { getCommunityById };
const updateCommunityById = async (
id: string,
update: IUpdateCommunity,
): Promise<Community | null> => {
return await getDB().community.update({
where: {
id: id,
},
data: {
...update,
},
});
};
const updateCommunityByIdAuth = async (
id: string,
update: IUpdateCommunity,
authHeader: string | undefined,
): Promise<Community | null | API_ERROR.ACCESS_DENIED> => {
const authUser = await getUserFromAuth(authHeader);
const community = await getCommunityById(id);
if (
!(await isUserAllowed(
authUser,
{
community: community,
},
community,
[PERMISSION.COMMUNITY_MANAGE],
))
) {
return API_ERROR.ACCESS_DENIED;
}
return await updateCommunityById(id, update);
};
export { getCommunityById, updateCommunityById, updateCommunityByIdAuth };