92 lines
2.4 KiB
TypeScript
92 lines
2.4 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/db/prisma'
|
|
import { getCurrentUser } from '@/lib/auth'
|
|
import { canAccessWorkspace } from '@/lib/db/workspace-access'
|
|
import { nanoid } from 'nanoid'
|
|
|
|
// GET /api/workspaces/[id]/calendar-feed/settings
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
try {
|
|
const user = await getCurrentUser()
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const workspaceId = params.id
|
|
const access = await canAccessWorkspace(user.id, workspaceId)
|
|
if (!access) {
|
|
return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
|
|
}
|
|
|
|
const feed = await prisma.calendarFeed.findFirst({
|
|
where: { workspaceId }
|
|
})
|
|
|
|
return NextResponse.json({ feed })
|
|
} catch (error) {
|
|
console.error('Failed to fetch calendar feed:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch calendar feed' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
// POST /api/workspaces/[id]/calendar-feed
|
|
export async function POST(
|
|
request: NextRequest,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
try {
|
|
const user = await getCurrentUser()
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const workspaceId = params.id
|
|
const access = await canAccessWorkspace(user.id, workspaceId)
|
|
if (!access || access.role === 'VIEWER') {
|
|
return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
|
|
}
|
|
|
|
const body = await request.json()
|
|
const { includeAppointments, includeMeds, daysAhead } = body
|
|
|
|
// Generate unique token
|
|
const token = nanoid(32)
|
|
|
|
const feed = await prisma.calendarFeed.create({
|
|
data: {
|
|
workspaceId,
|
|
token,
|
|
includeAppointments: includeAppointments ?? true,
|
|
includeMeds: includeMeds ?? false,
|
|
daysAhead: daysAhead ?? 90,
|
|
}
|
|
})
|
|
|
|
// Log audit
|
|
await prisma.auditLog.create({
|
|
data: {
|
|
workspaceId,
|
|
userId: user.id,
|
|
action: 'CREATE',
|
|
entityType: 'CALENDAR_FEED',
|
|
entityId: feed.id,
|
|
details: { includeAppointments, includeMeds }
|
|
}
|
|
})
|
|
|
|
return NextResponse.json({ feed })
|
|
} catch (error) {
|
|
console.error('Failed to create calendar feed:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to create calendar feed' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|