-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add restore endpoints and ui #3570
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
6 commits
Select commit
Hold shift + click to select a range
87c36c7
Add restore endpoints and ui
67011c9
Derive toast from notification
1a46796
Auth user if workspaceid not found
9e86e3d
Fix recently deleted ui
1967109
Add restore error toast
aab1381
Fix deleted at timestamp mismatch
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,61 @@ | ||
| import { db } from '@sim/db' | ||
| import { knowledgeBase } from '@sim/db/schema' | ||
| import { createLogger } from '@sim/logger' | ||
| import { eq } from 'drizzle-orm' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid' | ||
| import { generateRequestId } from '@/lib/core/utils/request' | ||
| import { restoreKnowledgeBase } from '@/lib/knowledge/service' | ||
| import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils' | ||
|
|
||
| const logger = createLogger('RestoreKnowledgeBaseAPI') | ||
|
|
||
| export async function POST( | ||
| request: NextRequest, | ||
| { params }: { params: Promise<{ id: string }> } | ||
| ) { | ||
| const requestId = generateRequestId() | ||
| const { id } = await params | ||
|
|
||
| try { | ||
| const auth = await checkSessionOrInternalAuth(request, { requireWorkflowId: false }) | ||
| if (!auth.success || !auth.userId) { | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const [kb] = await db | ||
| .select({ | ||
| id: knowledgeBase.id, | ||
| workspaceId: knowledgeBase.workspaceId, | ||
| userId: knowledgeBase.userId, | ||
| }) | ||
| .from(knowledgeBase) | ||
| .where(eq(knowledgeBase.id, id)) | ||
| .limit(1) | ||
|
|
||
| if (!kb) { | ||
| return NextResponse.json({ error: 'Knowledge base not found' }, { status: 404 }) | ||
| } | ||
|
|
||
| if (kb.workspaceId) { | ||
| const permission = await getUserEntityPermissions(auth.userId, 'workspace', kb.workspaceId) | ||
| if (permission !== 'admin' && permission !== 'write') { | ||
| return NextResponse.json({ error: 'Insufficient permissions' }, { status: 403 }) | ||
| } | ||
| } else if (kb.userId !== auth.userId) { | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| await restoreKnowledgeBase(id, requestId) | ||
|
|
||
| logger.info(`[${requestId}] Restored knowledge base ${id}`) | ||
|
|
||
| return NextResponse.json({ success: true }) | ||
| } catch (error) { | ||
| logger.error(`[${requestId}] Error restoring knowledge base ${id}`, error) | ||
| return NextResponse.json( | ||
| { error: error instanceof Error ? error.message : 'Internal server error' }, | ||
| { status: 500 } | ||
| ) | ||
| } | ||
| } |
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,45 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid' | ||
| import { generateRequestId } from '@/lib/core/utils/request' | ||
| import { getTableById, restoreTable } from '@/lib/table' | ||
| import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils' | ||
|
|
||
| const logger = createLogger('RestoreTableAPI') | ||
|
|
||
| export async function POST( | ||
| request: NextRequest, | ||
| { params }: { params: Promise<{ tableId: string }> } | ||
| ) { | ||
| const requestId = generateRequestId() | ||
| const { tableId } = await params | ||
|
|
||
| try { | ||
| const auth = await checkSessionOrInternalAuth(request, { requireWorkflowId: false }) | ||
| if (!auth.success || !auth.userId) { | ||
| return NextResponse.json({ error: 'Authentication required' }, { status: 401 }) | ||
| } | ||
|
|
||
| const table = await getTableById(tableId, { includeArchived: true }) | ||
| if (!table) { | ||
| return NextResponse.json({ error: 'Table not found' }, { status: 404 }) | ||
| } | ||
|
|
||
| const permission = await getUserEntityPermissions(auth.userId, 'workspace', table.workspaceId) | ||
| if (permission !== 'admin' && permission !== 'write') { | ||
| return NextResponse.json({ error: 'Insufficient permissions' }, { status: 403 }) | ||
| } | ||
|
|
||
| await restoreTable(tableId, requestId) | ||
|
|
||
| logger.info(`[${requestId}] Restored table ${tableId}`) | ||
|
|
||
| return NextResponse.json({ success: true }) | ||
| } catch (error) { | ||
| logger.error(`[${requestId}] Error restoring table ${tableId}`, error) | ||
| return NextResponse.json( | ||
| { error: error instanceof Error ? error.message : 'Internal server error' }, | ||
| { status: 500 } | ||
| ) | ||
| } | ||
| } |
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,55 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid' | ||
| import { generateRequestId } from '@/lib/core/utils/request' | ||
| import { restoreWorkflow } from '@/lib/workflows/lifecycle' | ||
| import { getWorkflowById } from '@/lib/workflows/utils' | ||
| import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils' | ||
|
|
||
| const logger = createLogger('RestoreWorkflowAPI') | ||
|
|
||
| export async function POST(request: NextRequest, { params }: { params: Promise<{ id: string }> }) { | ||
| const requestId = generateRequestId() | ||
| const { id: workflowId } = await params | ||
|
|
||
| try { | ||
| const auth = await checkSessionOrInternalAuth(request, { requireWorkflowId: false }) | ||
| if (!auth.success || !auth.userId) { | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const workflowData = await getWorkflowById(workflowId, { includeArchived: true }) | ||
| if (!workflowData) { | ||
| return NextResponse.json({ error: 'Workflow not found' }, { status: 404 }) | ||
| } | ||
|
|
||
| if (workflowData.workspaceId) { | ||
| const permission = await getUserEntityPermissions( | ||
| auth.userId, | ||
| 'workspace', | ||
| workflowData.workspaceId | ||
| ) | ||
| if (permission !== 'admin' && permission !== 'write') { | ||
| return NextResponse.json({ error: 'Insufficient permissions' }, { status: 403 }) | ||
| } | ||
| } else if (workflowData.userId !== auth.userId) { | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const result = await restoreWorkflow(workflowId, { requestId }) | ||
|
|
||
| if (!result.restored) { | ||
| return NextResponse.json({ error: 'Workflow is not archived' }, { status: 400 }) | ||
| } | ||
|
|
||
| logger.info(`[${requestId}] Restored workflow ${workflowId}`) | ||
|
|
||
| return NextResponse.json({ success: true }) | ||
| } catch (error) { | ||
| logger.error(`[${requestId}] Error restoring workflow ${workflowId}`, error) | ||
| return NextResponse.json( | ||
| { error: error instanceof Error ? error.message : 'Internal server error' }, | ||
| { status: 500 } | ||
| ) | ||
| } | ||
| } | ||
40 changes: 40 additions & 0 deletions
40
apps/sim/app/api/workspaces/[id]/files/[fileId]/restore/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,40 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { getSession } from '@/lib/auth' | ||
| import { generateRequestId } from '@/lib/core/utils/request' | ||
| import { restoreWorkspaceFile } from '@/lib/uploads/contexts/workspace' | ||
| import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils' | ||
|
|
||
| const logger = createLogger('RestoreWorkspaceFileAPI') | ||
|
|
||
| export async function POST( | ||
| request: NextRequest, | ||
| { params }: { params: Promise<{ id: string; fileId: string }> } | ||
| ) { | ||
| const requestId = generateRequestId() | ||
| const { id: workspaceId, fileId } = await params | ||
|
|
||
| try { | ||
| const session = await getSession() | ||
| if (!session?.user?.id) { | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const userPermission = await getUserEntityPermissions(session.user.id, 'workspace', workspaceId) | ||
| if (userPermission !== 'admin' && userPermission !== 'write') { | ||
| return NextResponse.json({ error: 'Insufficient permissions' }, { status: 403 }) | ||
| } | ||
|
|
||
| await restoreWorkspaceFile(workspaceId, fileId) | ||
|
|
||
| logger.info(`[${requestId}] Restored workspace file ${fileId}`) | ||
|
|
||
| return NextResponse.json({ success: true }) | ||
| } catch (error) { | ||
| logger.error(`[${requestId}] Error restoring workspace file ${fileId}`, error) | ||
| return NextResponse.json( | ||
| { error: error instanceof Error ? error.message : 'Internal server error' }, | ||
| { status: 500 } | ||
| ) | ||
| } | ||
| } |
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
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.