Basic services; Version 0.2.0
This commit is contained in:
parent
cae53fab61
commit
72d7b22891
11 changed files with 483 additions and 11 deletions
|
|
@ -1,9 +1,16 @@
|
|||
import { API_ERROR } from "../../controllers/errors.js";
|
||||
import type { Community } from "../../generated/prisma/client.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 { PERMISSION } from "../auth/permission.js";
|
||||
import type { IUpdateCommunity } from "./types.js";
|
||||
import { getInviteById } from "../invite/invite.js";
|
||||
import type {
|
||||
ICommunityChannel,
|
||||
ICommunityMember,
|
||||
ICommunityRole,
|
||||
ICreateInvite,
|
||||
IUpdateCommunity,
|
||||
} from "./types.js";
|
||||
|
||||
const getCommunityById = async (id: string): Promise<Community | null> => {
|
||||
return await getDB().community.findUnique({
|
||||
|
|
@ -49,4 +56,165 @@ const updateCommunityByIdAuth = async (
|
|||
return await updateCommunityById(id, update);
|
||||
};
|
||||
|
||||
export { getCommunityById, updateCommunityById, updateCommunityByIdAuth };
|
||||
const getCommunityMembersById = async (
|
||||
id: string,
|
||||
): Promise<ICommunityMember[]> => {
|
||||
return await getDB().user.findMany({
|
||||
where: {
|
||||
communities: {
|
||||
some: {
|
||||
id: id,
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const getCommunityMembersByIdAuth = async (
|
||||
id: string,
|
||||
authHeader: string | undefined,
|
||||
): Promise<ICommunityMember[] | API_ERROR.ACCESS_DENIED> => {
|
||||
const authUser = await getUserFromAuth(authHeader);
|
||||
const community = await getCommunityById(id);
|
||||
|
||||
if (
|
||||
!(await isUserAllowed(
|
||||
authUser,
|
||||
{
|
||||
community: community,
|
||||
},
|
||||
community,
|
||||
[PERMISSION.MEMBERS_READ],
|
||||
))
|
||||
) {
|
||||
return API_ERROR.ACCESS_DENIED;
|
||||
}
|
||||
|
||||
return await getCommunityMembersById(id);
|
||||
};
|
||||
|
||||
const getCommunityChannelsById = async (
|
||||
id: string,
|
||||
): Promise<ICommunityChannel[]> => {
|
||||
return await getDB().channel.findMany({
|
||||
where: {
|
||||
communityId: id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const getCommunityChannelsByIdAuth = async (
|
||||
id: string,
|
||||
authHeader: string | undefined,
|
||||
): Promise<ICommunityChannel[] | API_ERROR.ACCESS_DENIED> => {
|
||||
const authUser = await getUserFromAuth(authHeader);
|
||||
const community = await getCommunityById(id);
|
||||
|
||||
if (
|
||||
!(await isUserAllowed(
|
||||
authUser,
|
||||
{
|
||||
community: community,
|
||||
},
|
||||
community,
|
||||
[PERMISSION.CHANNELS_READ],
|
||||
))
|
||||
) {
|
||||
return API_ERROR.ACCESS_DENIED;
|
||||
}
|
||||
|
||||
return await getCommunityChannelsById(id);
|
||||
};
|
||||
|
||||
const getCommunityRolesById = async (id: string): Promise<ICommunityRole[]> => {
|
||||
return await getDB().role.findMany({
|
||||
where: {
|
||||
communityId: id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const getCommunityRolesByIdAuth = async (
|
||||
id: string,
|
||||
authHeader: string | undefined,
|
||||
): Promise<ICommunityRole[] | API_ERROR.ACCESS_DENIED> => {
|
||||
const authUser = await getUserFromAuth(authHeader);
|
||||
const community = await getCommunityById(id);
|
||||
|
||||
if (
|
||||
!(await isUserAllowed(
|
||||
authUser,
|
||||
{
|
||||
community: community,
|
||||
},
|
||||
community,
|
||||
[PERMISSION.ROLES_READ],
|
||||
))
|
||||
) {
|
||||
return API_ERROR.ACCESS_DENIED;
|
||||
}
|
||||
|
||||
return await getCommunityRolesById(id);
|
||||
};
|
||||
|
||||
const createInvite = async (
|
||||
id: string,
|
||||
createInviteData: ICreateInvite,
|
||||
): Promise<Invite> => {
|
||||
return await getDB().invite.create({
|
||||
data: {
|
||||
...createInviteData,
|
||||
communityId: id,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const createInviteAuth = async (
|
||||
id: string,
|
||||
createInviteData: ICreateInvite,
|
||||
authHeader: string | undefined,
|
||||
): Promise<Invite | API_ERROR.ACCESS_DENIED> => {
|
||||
const authUser = await getUserFromAuth(authHeader);
|
||||
const community = await getCommunityById(id);
|
||||
|
||||
if (
|
||||
!(await isUserAllowed(
|
||||
authUser,
|
||||
{
|
||||
community: community,
|
||||
},
|
||||
community,
|
||||
[PERMISSION.INVITES_CREATE],
|
||||
))
|
||||
) {
|
||||
return API_ERROR.ACCESS_DENIED;
|
||||
}
|
||||
|
||||
return await createInvite(id, createInviteData);
|
||||
};
|
||||
|
||||
export {
|
||||
getCommunityById,
|
||||
updateCommunityById,
|
||||
updateCommunityByIdAuth,
|
||||
getCommunityMembersById,
|
||||
getCommunityMembersByIdAuth,
|
||||
getCommunityChannelsById,
|
||||
getCommunityChannelsByIdAuth,
|
||||
getCommunityRolesById,
|
||||
getCommunityRolesByIdAuth,
|
||||
createInvite,
|
||||
createInviteAuth,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue