Skip to content
Merged
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
104 changes: 100 additions & 4 deletions app/api/projects/[id]/environment/[envId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { NextResponse } from 'next/server'

import { verifyProjectAccess, withAuth } from '@/lib/api-auth'
import { prisma } from '@/lib/db'
import { logger as baseLogger } from '@/lib/logger'
import { canUpdateResource } from '@/lib/util/action'

const logger = baseLogger.child({ module: 'api/projects/[id]/environment/[envId]' })

type PutEnvironmentResponse = { error: string } | Environment

Expand All @@ -27,22 +31,68 @@ export const PUT = withAuth<PutEnvironmentResponse>(async (req, context, session
return NextResponse.json({ error: 'Environment variable not found' }, { status: 404 })
}

// Check if project sandboxes can be updated
const project = await prisma.project.findUnique({
where: { id: projectId },
include: {
sandboxes: {
select: { id: true, status: true, name: true },
},
},
})

if (!project) {
return NextResponse.json({ error: 'Project not found' }, { status: 404 })
}

// Check if all sandboxes can be updated
const nonUpdatableSandboxes = project.sandboxes.filter((sb) => !canUpdateResource(sb.status))

if (nonUpdatableSandboxes.length > 0) {
const statusList = nonUpdatableSandboxes.map((sb) => `${sb.name}: ${sb.status}`).join(', ')
logger.warn(
`Cannot update environment variable for project ${projectId}: some sandboxes cannot be updated (${statusList})`
)
return NextResponse.json(
{
error: `Cannot update environment variable: some sandboxes are not in a state that allows updates. Only RUNNING sandboxes can be updated. Non-updatable sandboxes: ${statusList}`,
},
{ status: 400 }
)
}

const body = await req.json()
const { value } = body

if (value === undefined) {
return NextResponse.json({ error: 'Value is required' }, { status: 400 })
}

// Update the environment variable in database only
// Update the environment variable in database
const updated = await prisma.environment.update({
where: { id: envId },
data: { value },
})

// Set all sandboxes to UPDATING status
if (project.sandboxes.length > 0) {
await prisma.sandbox.updateMany({
where: {
projectId,
status: 'RUNNING', // Only update RUNNING sandboxes
},
data: {
status: 'UPDATING',
},
})
logger.info(
`Set ${project.sandboxes.length} sandboxes to UPDATING status for project ${projectId}`
)
}

return NextResponse.json(updated)
} catch (error) {
console.error('Error updating environment variable:', error)
logger.error(`Error updating environment variable: ${error}`)
return NextResponse.json({ error: 'Failed to update environment variable' }, { status: 500 })
}
})
Expand Down Expand Up @@ -70,14 +120,60 @@ export const DELETE = withAuth<DeleteEnvironmentResponse>(async (_req, context,
return NextResponse.json({ error: 'Environment variable not found' }, { status: 404 })
}

// Delete the environment variable from database only
// Check if project sandboxes can be updated
const project = await prisma.project.findUnique({
where: { id: projectId },
include: {
sandboxes: {
select: { id: true, status: true, name: true },
},
},
})

if (!project) {
return NextResponse.json({ error: 'Project not found' }, { status: 404 })
}

// Check if all sandboxes can be updated
const nonUpdatableSandboxes = project.sandboxes.filter((sb) => !canUpdateResource(sb.status))

if (nonUpdatableSandboxes.length > 0) {
const statusList = nonUpdatableSandboxes.map((sb) => `${sb.name}: ${sb.status}`).join(', ')
logger.warn(
`Cannot delete environment variable for project ${projectId}: some sandboxes cannot be updated (${statusList})`
)
return NextResponse.json(
{
error: `Cannot delete environment variable: some sandboxes are not in a state that allows updates. Only RUNNING sandboxes can be updated. Non-updatable sandboxes: ${statusList}`,
},
{ status: 400 }
)
}

// Delete the environment variable from database
await prisma.environment.delete({
where: { id: envId },
})

// Set all sandboxes to UPDATING status
if (project.sandboxes.length > 0) {
await prisma.sandbox.updateMany({
where: {
projectId,
status: 'RUNNING', // Only update RUNNING sandboxes
},
data: {
status: 'UPDATING',
},
})
logger.info(
`Set ${project.sandboxes.length} sandboxes to UPDATING status for project ${projectId}`
)
}

return NextResponse.json({ success: true })
} catch (error) {
console.error('Error deleting environment variable:', error)
logger.error(`Error deleting environment variable: ${error}`)
return NextResponse.json({ error: 'Failed to delete environment variable' }, { status: 500 })
}
})
69 changes: 68 additions & 1 deletion app/api/projects/[id]/environment/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { NextResponse } from 'next/server'
import { verifyProjectAccess, withAuth } from '@/lib/api-auth'
import { EnvironmentCategory } from '@/lib/const'
import { prisma } from '@/lib/db'
import { logger as baseLogger } from '@/lib/logger'
import { canUpdateResource } from '@/lib/util/action'

const logger = baseLogger.child({ module: 'api/projects/[id]/environment' })

type GroupedEnvironments = Record<string, Environment[]>

Expand Down Expand Up @@ -58,6 +62,37 @@ export const POST = withAuth<PostEnvironmentResponse>(async (req, context, sessi

try {
await verifyProjectAccess(projectId, session.user.id)

// Check if project sandboxes can be updated
const project = await prisma.project.findUnique({
where: { id: projectId },
include: {
sandboxes: {
select: { id: true, status: true, name: true },
},
},
})

if (!project) {
return NextResponse.json({ error: 'Project not found' }, { status: 404 })
}

// Check if all sandboxes can be updated
const nonUpdatableSandboxes = project.sandboxes.filter((sb) => !canUpdateResource(sb.status))

if (nonUpdatableSandboxes.length > 0) {
const statusList = nonUpdatableSandboxes.map((sb) => `${sb.name}: ${sb.status}`).join(', ')
logger.warn(
`Cannot update environment variables for project ${projectId}: some sandboxes cannot be updated (${statusList})`
)
return NextResponse.json(
{
error: `Cannot update environment variables: some sandboxes are not in a state that allows updates. Only RUNNING sandboxes can be updated. Non-updatable sandboxes: ${statusList}`,
},
{ status: 400 }
)
}

const body = await req.json()

// Check if this is a single variable creation or batch update
Expand All @@ -73,6 +108,22 @@ export const POST = withAuth<PostEnvironmentResponse>(async (req, context, sessi
},
})

// Set all sandboxes to UPDATING status
if (project.sandboxes.length > 0) {
await prisma.sandbox.updateMany({
where: {
projectId,
status: 'RUNNING', // Only update RUNNING sandboxes
},
data: {
status: 'UPDATING',
},
})
logger.info(
`Set ${project.sandboxes.length} sandboxes to UPDATING status for project ${projectId}`
)
}

return NextResponse.json(newVar)
} else if (body.variables) {
// Batch update (replace all variables)
Expand Down Expand Up @@ -100,12 +151,28 @@ export const POST = withAuth<PostEnvironmentResponse>(async (req, context, sessi

const created = await Promise.all(envPromises)

// Set all sandboxes to UPDATING status
if (project.sandboxes.length > 0) {
await prisma.sandbox.updateMany({
where: {
projectId,
status: 'RUNNING', // Only update RUNNING sandboxes
},
data: {
status: 'UPDATING',
},
})
logger.info(
`Set ${project.sandboxes.length} sandboxes to UPDATING status for project ${projectId}`
)
}

return NextResponse.json({ success: true, count: created.length })
} else {
return NextResponse.json({ error: 'Invalid request body' }, { status: 400 })
}
} catch (error) {
console.error('Error saving environment variables:', error)
logger.error(`Error saving environment variables: ${error}`)
return NextResponse.json({ error: 'Failed to save environment variables' }, { status: 500 })
}
})
133 changes: 0 additions & 133 deletions app/api/projects/[id]/github/route.ts

This file was deleted.

Loading
Loading