-
Notifications
You must be signed in to change notification settings - Fork 59
[#390] Blade/issues backend #416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8e81670
feat: add full issue schema and relations for it
DGoel1602 7a1662b
feat: add issues router with CRUD and sub-issue support
Spyderma9 c06569e
refactor: format code for better readability in issues router
Spyderma9 62cedc4
chore: move all db relations to a seperate file to avoid circular dep…
DGoel1602 1e7447e
feat: limit visibility of all issues, infer schema and remove sub iss…
DGoel1602 2284c06
feat: add getting a single issue
DGoel1602 354fc9f
chore: format
DGoel1602 b09ba0b
chore: more efficient assignee filtering
DGoel1602 10333fb
fix: better visibility filtering with officer by passes
DGoel1602 b1ea9e7
fix: guard against empty inArray crash when user has no role assignments
Spyderma9 6207046
fix: relation shadowing
DGoel1602 f96272a
fix: coderabbit review
DGoel1602 fb251e4
chore: more conventional cascades
DGoel1602 b15d5a5
fix: add onDelete cascade to team and creator foreign keys in issues …
Spyderma9 d44c9d7
fix: change onDelete behavior for team and creator foreign keys in Is…
Spyderma9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,278 @@ | ||
| import type { TRPCRouterRecord } from "@trpc/server"; | ||
| import { TRPCError } from "@trpc/server"; | ||
| import { z } from "zod"; | ||
|
|
||
| import { ISSUE } from "@forge/consts"; | ||
| import { and, eq, exists, inArray, sql } from "@forge/db"; | ||
| import { db } from "@forge/db/client"; | ||
| import { Permissions } from "@forge/db/schemas/auth"; | ||
| import { | ||
| Issue, | ||
| IssueSchema, | ||
| IssuesToTeamsVisibility, | ||
| IssuesToUsersAssignment, | ||
| } from "@forge/db/schemas/knight-hacks"; | ||
| import { permissions } from "@forge/utils"; | ||
|
|
||
| import { permProcedure } from "../trpc"; | ||
|
|
||
| const CreateIssueInputSchema = IssueSchema.extend({ | ||
| assigneeIds: z.array(z.string().uuid()).optional(), | ||
| teamVisibilityIds: z.array(z.string().uuid()).optional(), | ||
| }); | ||
|
|
||
| async function requireIssue(id: string, label = "Issue") { | ||
| const issue = await db.query.Issue.findFirst({ | ||
| where: (t, { eq }) => eq(t.id, id), | ||
| }); | ||
| if (!issue) | ||
| throw new TRPCError({ message: `${label} not found.`, code: "NOT_FOUND" }); | ||
| return issue; | ||
| } | ||
|
|
||
| export const issuesRouter = { | ||
| createIssue: permProcedure | ||
| .input(CreateIssueInputSchema.omit({ creator: true })) | ||
| .mutation(async ({ ctx, input }) => { | ||
| permissions.controlPerms.or(["EDIT_ISSUES"], ctx); | ||
|
|
||
| return await db.transaction(async (tx) => { | ||
| const { teamVisibilityIds, assigneeIds, ...rest } = input; | ||
|
|
||
| const [issue] = await tx | ||
| .insert(Issue) | ||
| .values({ | ||
| ...rest, | ||
| creator: ctx.session.user.id, | ||
| }) | ||
| .returning(); | ||
|
|
||
| if (!issue) { | ||
| throw new TRPCError({ | ||
| message: "Failed to create issue.", | ||
| code: "INTERNAL_SERVER_ERROR", | ||
| }); | ||
| } | ||
|
|
||
| if (teamVisibilityIds?.length) { | ||
| await db.insert(IssuesToTeamsVisibility).values( | ||
| teamVisibilityIds.map((teamId) => ({ | ||
| issueId: issue.id, | ||
| teamId, | ||
| })), | ||
| ); | ||
| } | ||
|
|
||
| if (assigneeIds?.length) { | ||
| await db.insert(IssuesToUsersAssignment).values( | ||
| assigneeIds.map((userId) => ({ | ||
| issueId: issue.id, | ||
| userId, | ||
| })), | ||
| ); | ||
| } | ||
|
|
||
| return issue; | ||
| }); | ||
| }), | ||
|
|
||
| getIssue: permProcedure | ||
| .input( | ||
| z.object({ | ||
| id: z.string(), | ||
| }), | ||
| ) | ||
| .query(async ({ ctx, input }) => { | ||
| permissions.controlPerms.or(["READ_ISSUES"], ctx); | ||
|
|
||
| let visibilityFilter; | ||
|
|
||
| if (ctx.session.permissions.IS_OFFICER) { | ||
| visibilityFilter = sql`TRUE`; | ||
| } else { | ||
| const userRoles = ( | ||
| await db.query.Permissions.findMany({ | ||
| where: eq(Permissions.userId, ctx.session.user.id), | ||
| }) | ||
| ).map((p) => p.roleId); | ||
| visibilityFilter = | ||
| userRoles.length === 0 | ||
| ? sql`FALSE` | ||
| : exists( | ||
| db | ||
| .select() | ||
| .from(IssuesToTeamsVisibility) | ||
| .where( | ||
| and( | ||
| eq(IssuesToTeamsVisibility.issueId, Issue.id), | ||
| inArray(IssuesToTeamsVisibility.teamId, userRoles), | ||
| ), | ||
| ), | ||
| ); | ||
DGoel1602 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| const issue = await db.query.Issue.findFirst({ | ||
| where: and(eq(Issue.id, input.id), visibilityFilter), | ||
| }); | ||
| if (!issue) | ||
| throw new TRPCError({ message: `Issue not found.`, code: "NOT_FOUND" }); | ||
| return issue; | ||
| }), | ||
|
|
||
| getAllIssues: permProcedure | ||
| .input( | ||
| z | ||
| .object({ | ||
| dateFrom: z.date().optional(), | ||
| dateTo: z.date().optional(), | ||
| assigneeIds: z.array(z.string().uuid()).optional(), | ||
| creatorId: z.string().uuid().optional(), | ||
| teamId: z.string().uuid().optional(), | ||
| status: z.enum(ISSUE.ISSUE_STATUS).optional(), | ||
| parentId: z.string().uuid().nullable().optional(), | ||
| }) | ||
| .optional(), | ||
| ) | ||
| .query(async ({ ctx, input }) => { | ||
| permissions.controlPerms.or(["READ_ISSUES"], ctx); | ||
|
|
||
| const filters: ReturnType<typeof eq>[] = []; | ||
|
|
||
| if (input?.creatorId) filters.push(eq(Issue.creator, input.creatorId)); | ||
| if (input?.teamId) filters.push(eq(Issue.team, input.teamId)); | ||
| if (input?.status) filters.push(eq(Issue.status, input.status)); | ||
| if (input?.dateFrom) | ||
| filters.push(sql`${Issue.date} >= ${input.dateFrom}`); | ||
| if (input?.dateTo) filters.push(sql`${Issue.date} <= ${input.dateTo}`); | ||
| if (input?.parentId !== undefined) { | ||
| filters.push( | ||
| input.parentId === null | ||
| ? sql`${Issue.parent} IS NULL` | ||
| : eq(Issue.parent, input.parentId), | ||
| ); | ||
| } | ||
|
|
||
| let visibilityFilter; | ||
|
|
||
| if (ctx.session.permissions.IS_OFFICER) { | ||
| visibilityFilter = sql`TRUE`; | ||
| } else { | ||
| const userRoles = ( | ||
| await db.query.Permissions.findMany({ | ||
| where: eq(Permissions.userId, ctx.session.user.id), | ||
| }) | ||
| ).map((p) => p.roleId); | ||
| visibilityFilter = | ||
| userRoles.length === 0 | ||
| ? sql`FALSE` | ||
| : exists( | ||
| db | ||
| .select() | ||
| .from(IssuesToTeamsVisibility) | ||
| .where( | ||
| and( | ||
| eq(IssuesToTeamsVisibility.issueId, Issue.id), | ||
| inArray(IssuesToTeamsVisibility.teamId, userRoles), | ||
| ), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| if (input?.assigneeIds?.length) { | ||
| filters.push( | ||
| exists( | ||
| db | ||
| .select() | ||
| .from(IssuesToUsersAssignment) | ||
| .where( | ||
| and( | ||
| eq(IssuesToUsersAssignment.issueId, Issue.id), | ||
| inArray(IssuesToUsersAssignment.userId, input.assigneeIds), | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| const issues = await db.query.Issue.findMany({ | ||
| where: and(...filters, visibilityFilter), | ||
| with: { | ||
| teamVisibility: { with: { team: true } }, | ||
| userAssignments: { with: { user: true } }, | ||
| }, | ||
DGoel1602 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
| return issues; | ||
| }), | ||
|
|
||
| updateIssue: permProcedure | ||
| .input( | ||
| z.object({ | ||
| id: z.string().uuid(), | ||
| name: z.string().min(1).optional(), | ||
| description: z.string().min(1).optional(), | ||
| status: z.enum(ISSUE.ISSUE_STATUS).optional(), | ||
| date: z.date().nullable().optional(), | ||
| event: z.string().uuid().nullable().optional(), | ||
| links: z.array(z.string().url()).nullable().optional(), | ||
| team: z.string().uuid().optional(), | ||
| assigneeIds: z.array(z.string().uuid()).optional(), | ||
| teamVisibilityIds: z.array(z.string().uuid()).optional(), | ||
| }), | ||
| ) | ||
| .mutation(async ({ ctx, input }) => { | ||
| permissions.controlPerms.or(["EDIT_ISSUES"], ctx); | ||
| await requireIssue(input.id); | ||
|
|
||
| const { id, assigneeIds, teamVisibilityIds, ...fields } = input; | ||
| const updateData = Object.fromEntries( | ||
| (Object.entries(fields) as [string, unknown][]).filter( | ||
| ([, v]) => v !== undefined, | ||
| ), | ||
| ); | ||
|
|
||
| if (Object.keys(updateData).length > 0) { | ||
| await db.update(Issue).set(updateData).where(eq(Issue.id, id)); | ||
| } | ||
|
|
||
| if (teamVisibilityIds !== undefined) { | ||
| await db | ||
| .delete(IssuesToTeamsVisibility) | ||
| .where(eq(IssuesToTeamsVisibility.issueId, id)); | ||
| if (teamVisibilityIds.length > 0) { | ||
| await db | ||
| .insert(IssuesToTeamsVisibility) | ||
| .values( | ||
| teamVisibilityIds.map((teamId) => ({ issueId: id, teamId })), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if (assigneeIds !== undefined) { | ||
| await db | ||
| .delete(IssuesToUsersAssignment) | ||
| .where(eq(IssuesToUsersAssignment.issueId, id)); | ||
| if (assigneeIds.length > 0) { | ||
| await db | ||
| .insert(IssuesToUsersAssignment) | ||
| .values(assigneeIds.map((userId) => ({ issueId: id, userId }))); | ||
| } | ||
| } | ||
|
|
||
| return db.query.Issue.findFirst({ | ||
| where: (t, { eq }) => eq(t.id, id), | ||
| with: { | ||
| teamVisibility: { with: { team: true } }, | ||
| userAssignments: { with: { user: true } }, | ||
| }, | ||
| }); | ||
| }), | ||
| deleteIssue: permProcedure | ||
| .input(z.object({ id: z.string().uuid() })) | ||
| .mutation(async ({ ctx, input }) => { | ||
| permissions.controlPerms.or(["EDIT_ISSUES"], ctx); | ||
| await requireIssue(input.id); | ||
|
|
||
| await db.delete(Issue).where(eq(Issue.id, input.id)); | ||
|
|
||
| return { success: true }; | ||
| }), | ||
| } satisfies TRPCRouterRecord; | ||
This file contains hidden or 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 hidden or 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,6 @@ | ||
| export const ISSUE_STATUS = [ | ||
| "BACKLOG", | ||
| "PLANNING", | ||
| "IN_PROGRESS", | ||
| "FINISHED", | ||
| ] as const; |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.