New endpoints; New tests; Version 0.3.0

This commit is contained in:
Aslan 2025-12-28 04:29:42 +01:00
parent 4bc3be87b4
commit 1f3a94cf38
27 changed files with 551 additions and 72 deletions

View file

@ -3,8 +3,8 @@ import type { Community, Invite, User } 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 { getInviteById } from "../invite/invite.js";
import type {
ICreateCommunity,
ICommunityChannel,
ICommunityMember,
ICommunityRole,
@ -18,6 +18,36 @@ const getCommunityById = async (id: string): Promise<Community | null> => {
});
};
const createCommunity = async (
ownerId: string,
create: ICreateCommunity,
): Promise<Community> => {
return await getDB().community.create({
data: {
ownerId: ownerId,
...create,
members: {
connect: {
id: ownerId,
},
},
},
});
};
const createCommunityAuth = async (
create: ICreateCommunity,
authHeader: string | undefined,
): Promise<Community | API_ERROR.ACCESS_DENIED> => {
const authUser = await getUserFromAuth(authHeader);
if (!authUser) {
return API_ERROR.ACCESS_DENIED;
}
return await createCommunity(authUser.id, create);
};
const updateCommunityById = async (
id: string,
update: IUpdateCommunity,
@ -171,11 +201,13 @@ const getCommunityRolesByIdAuth = async (
const createInvite = async (
id: string,
creatorId: string,
createInviteData: ICreateInvite,
): Promise<Invite> => {
return await getDB().invite.create({
data: {
...createInviteData,
creatorId: creatorId,
communityId: id,
},
});
@ -197,16 +229,19 @@ const createInviteAuth = async (
},
community,
[PERMISSION.INVITES_CREATE],
))
)) ||
!authUser
) {
return API_ERROR.ACCESS_DENIED;
}
return await createInvite(id, createInviteData);
return await createInvite(id, authUser.id, createInviteData);
};
export {
getCommunityById,
createCommunity,
createCommunityAuth,
updateCommunityById,
updateCommunityByIdAuth,
getCommunityMembersById,