Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion apps/api/src/lib/functions/database.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { userToTeam, db, and, eq, log } from "db";
import { userToTeam, db, and, eq, log, team, teamJoinRequest } from "db";
import type { UserType, SiteRoleType } from "db/types";
import type { LoggingOptions, LoggingType } from "../types";
import { type Context } from "hono";
Expand Down Expand Up @@ -44,6 +44,22 @@ export function isSiteAdminUser(
return ["ADMIN", "SUPER_ADMIN"].some((role) => role === permissionEnum);
}

export async function findTeam(teamId: string) {
return db.query.team.findFirst({
where: eq(team.id, teamId),
});
}

export async function findTeamUserFacing(teamId: string) {
return db.query.team.findFirst({
columns: {
id: true,
name: true,
},
where: eq(team.id, teamId),
});
}

export async function leaveTeam(userId: string, teamId: string) {
return db
.delete(userToTeam)
Expand All @@ -62,6 +78,33 @@ export async function getAdminUserForTeam(userId: string, teamId: string) {
),
});
}

export async function getJoinTeamRequest(
requestId: string,
userId: string,
teamId: string,
) {
return db.query.teamJoinRequest.findFirst({
where: and(
eq(teamJoinRequest.id, requestId),
eq(teamJoinRequest.userId, userId),
eq(teamJoinRequest.teamId, teamId),
),
});
}

export async function getJoinTeamRequestAdmin(
requestId: string,
teamId: string,
) {
return db.query.teamJoinRequest.findFirst({
where: and(
eq(teamJoinRequest.id, requestId),
eq(teamJoinRequest.teamId, teamId),
),
});
}

// TODO: This function is lowkey pivotal so we should ensure it is WAI.
export async function isUserSiteAdminOrQueryHasPermissions<T = unknown>(
userSiteRole: SiteRoleType,
Expand Down
Loading