-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(folders): add resource pinning and generalize the folders contract #6014
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
3 commits
Select commit
Hold shift + click to select a range
34cf6b6
feat(folders): add resource pinning and generalize the folders contract
waleedlatif1 ebb1336
fix(db): make the folder migration replay-safe below its embedded COMMIT
waleedlatif1 cae72a6
fix(db): guard the folder backfill so a replay cannot abort on names
waleedlatif1 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
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
91 changes: 91 additions & 0 deletions
91
apps/sim/app/api/pinned-items/[resourceType]/[resourceId]/route.test.ts
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,91 @@ | ||
| /** | ||
| * Tests for the unpin API route. | ||
| * | ||
| * @vitest-environment node | ||
| */ | ||
| import { authMockFns, createMockRequest, schemaMock } from '@sim/testing' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { mockLogger, mockDb } = vi.hoisted(() => ({ | ||
| mockLogger: { | ||
| info: vi.fn(), | ||
| warn: vi.fn(), | ||
| error: vi.fn(), | ||
| debug: vi.fn(), | ||
| trace: vi.fn(), | ||
| fatal: vi.fn(), | ||
| child: vi.fn(), | ||
| }, | ||
| mockDb: { delete: vi.fn() }, | ||
| })) | ||
|
|
||
| vi.mock('@sim/logger', () => ({ | ||
| createLogger: vi.fn().mockReturnValue(mockLogger), | ||
| runWithRequestContext: <T>(_ctx: unknown, fn: () => T): T => fn(), | ||
| getRequestContext: () => undefined, | ||
| })) | ||
| vi.mock('@sim/db', () => ({ db: mockDb, ...schemaMock })) | ||
|
|
||
| import { DELETE } from '@/app/api/pinned-items/[resourceType]/[resourceId]/route' | ||
|
|
||
| const mockUser = { id: 'user-123', email: 'test@example.com', name: 'Test User' } | ||
|
|
||
| function routeContext(resourceType: string, resourceId: string) { | ||
| return { params: Promise.resolve({ resourceType, resourceId }) } | ||
| } | ||
|
|
||
| describe('Unpin API', () => { | ||
| const mockWhere = vi.fn() | ||
| const mockReturning = vi.fn() | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
|
|
||
| mockDb.delete.mockReturnValue({ where: mockWhere }) | ||
| mockWhere.mockReturnValue({ returning: mockReturning }) | ||
| mockReturning.mockReturnValue([{ id: 'pinned-1' }]) | ||
|
|
||
| authMockFns.mockGetSession.mockResolvedValue({ user: mockUser }) | ||
| }) | ||
|
|
||
| it('unpins a resource', async () => { | ||
| const response = await DELETE( | ||
| createMockRequest('DELETE'), | ||
| routeContext('workflow', 'workflow-1') | ||
| ) | ||
|
|
||
| expect(response.status).toBe(200) | ||
| await expect(response.json()).resolves.toEqual({ success: true }) | ||
| expect(mockDb.delete).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('returns 404 when no matching pin exists', async () => { | ||
| mockReturning.mockReturnValue([]) | ||
|
|
||
| const response = await DELETE( | ||
| createMockRequest('DELETE'), | ||
| routeContext('workflow', 'workflow-1') | ||
| ) | ||
|
|
||
| expect(response.status).toBe(404) | ||
| }) | ||
|
|
||
| it('rejects an unknown resourceType at the contract boundary', async () => { | ||
| const response = await DELETE(createMockRequest('DELETE'), routeContext('nope', 'resource-1')) | ||
|
|
||
| expect(response.status).toBe(400) | ||
| expect(mockDb.delete).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('returns 401 when unauthenticated', async () => { | ||
| authMockFns.mockGetSession.mockResolvedValue(null) | ||
|
|
||
| const response = await DELETE( | ||
| createMockRequest('DELETE'), | ||
| routeContext('workflow', 'workflow-1') | ||
| ) | ||
|
|
||
| expect(response.status).toBe(401) | ||
| expect(mockDb.delete).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
51 changes: 51 additions & 0 deletions
51
apps/sim/app/api/pinned-items/[resourceType]/[resourceId]/route.ts
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,51 @@ | ||
| import { db, pinnedItem } from '@sim/db' | ||
| import { createLogger } from '@sim/logger' | ||
| import { and, eq } from 'drizzle-orm' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { deletePinnedItemContract } from '@/lib/api/contracts' | ||
| import { parseRequest } from '@/lib/api/server' | ||
| import { getSession } from '@/lib/auth' | ||
| import { withRouteHandler } from '@/lib/core/utils/with-route-handler' | ||
|
|
||
| const logger = createLogger('PinnedItemDeleteAPI') | ||
|
|
||
| interface RouteContext { | ||
| params: Promise<{ resourceType: string; resourceId: string }> | ||
| } | ||
|
|
||
| /** | ||
| * Unpins a resource, addressed by its composite key rather than the pin's own id so | ||
| * callers can unpin from a resource row without first looking the pin up. | ||
| * | ||
| * No workspace permission check is needed: the delete is scoped to the session | ||
| * user's own pins, so a caller can only ever remove a row they created. | ||
| */ | ||
| export const DELETE = withRouteHandler(async (request: NextRequest, context: RouteContext) => { | ||
| const session = await getSession() | ||
| if (!session?.user?.id) { | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const parsed = await parseRequest(deletePinnedItemContract, request, context) | ||
| if (!parsed.success) return parsed.response | ||
| const { resourceType, resourceId } = parsed.data.params | ||
|
|
||
| const deleted = await db | ||
| .delete(pinnedItem) | ||
| .where( | ||
| and( | ||
| eq(pinnedItem.userId, session.user.id), | ||
| eq(pinnedItem.resourceType, resourceType), | ||
| eq(pinnedItem.resourceId, resourceId) | ||
| ) | ||
| ) | ||
| .returning({ id: pinnedItem.id }) | ||
|
|
||
| if (deleted.length === 0) { | ||
| return NextResponse.json({ error: 'Pinned item not found' }, { status: 404 }) | ||
| } | ||
|
|
||
| logger.info('Unpinned resource', { resourceType, resourceId }) | ||
|
|
||
| return NextResponse.json({ success: true }) | ||
| }) |
Oops, something went wrong.
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.