-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better errors for crud handlers (#143)
* added more errors to team creation * check teams in client get * added db checks
- Loading branch information
1 parent
7cca092
commit 0147213
Showing
9 changed files
with
297 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { KnownErrors } from "@stackframe/stack-shared"; | ||
import { PrismaTransaction } from "./types"; | ||
|
||
|
||
async function _getTeamMembership( | ||
tx: PrismaTransaction, | ||
options: { | ||
projectId: string, | ||
teamId: string, userId: string, | ||
} | ||
) { | ||
return await tx.teamMember.findUnique({ | ||
where: { | ||
projectId_projectUserId_teamId: { | ||
projectId: options.projectId, | ||
projectUserId: options.userId, | ||
teamId: options.teamId, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
export async function ensureTeamMembershipExist( | ||
tx: PrismaTransaction, | ||
options: { | ||
projectId: string, | ||
teamId: string, | ||
userId: string, | ||
} | ||
) { | ||
const member = await _getTeamMembership(tx, options); | ||
|
||
if (!member) { | ||
throw new KnownErrors.TeamMembershipNotFound(options.teamId, options.userId); | ||
} | ||
} | ||
|
||
export async function ensureTeamMembershipDoesNotExist( | ||
tx: PrismaTransaction, | ||
options: { | ||
projectId: string, | ||
teamId: string, | ||
userId: string, | ||
} | ||
) { | ||
const member = await _getTeamMembership(tx, options); | ||
|
||
if (member) { | ||
throw new KnownErrors.TeamMembershipAlreadyExists(); | ||
} | ||
} | ||
|
||
export async function ensureTeamExist( | ||
tx: PrismaTransaction, | ||
options: { | ||
projectId: string, | ||
teamId: string, | ||
} | ||
) { | ||
const team = await tx.team.findUnique({ | ||
where: { | ||
projectId_teamId: { | ||
projectId: options.projectId, | ||
teamId: options.teamId, | ||
}, | ||
}, | ||
}); | ||
|
||
if (!team) { | ||
throw new KnownErrors.TeamNotFound(options.teamId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
|
||
export type PrismaTransaction = Parameters<Parameters<PrismaClient['$transaction']>[0]>[0]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { KnownErrors } from "@stackframe/stack-shared"; | ||
import { UsersCrud } from "@stackframe/stack-shared/dist/interface/crud/users"; | ||
|
||
export function getIdFromUserIdOrMe(userId: string, user: UsersCrud['Server']['Read'] | undefined): string; | ||
export function getIdFromUserIdOrMe(userId: string | undefined, user: UsersCrud['Server']['Read'] | undefined): string | undefined; | ||
export function getIdFromUserIdOrMe(userId: string | undefined, user: UsersCrud['Server']['Read'] | undefined): string | undefined { | ||
if (userId === "me") { | ||
if (!user) { | ||
throw new KnownErrors.CannotGetOwnUserWithoutUser(); | ||
} | ||
return user.id; | ||
} | ||
|
||
return userId; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.