|
1 | | -import { db } from '@sim/db' |
2 | | -import { skill } from '@sim/db/schema' |
3 | 1 | import { createLogger } from '@sim/logger' |
4 | | -import { and, desc, eq } from 'drizzle-orm' |
5 | 2 | import { type NextRequest, NextResponse } from 'next/server' |
6 | 3 | import { z } from 'zod' |
7 | 4 | import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid' |
8 | 5 | import { generateRequestId } from '@/lib/core/utils/request' |
9 | | -import { upsertSkills } from '@/lib/workflows/skills/operations' |
| 6 | +import { deleteSkill, listSkills, upsertSkills } from '@/lib/workflows/skills/operations' |
10 | 7 | import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils' |
11 | 8 |
|
12 | 9 | const logger = createLogger('SkillsAPI') |
@@ -53,11 +50,7 @@ export async function GET(request: NextRequest) { |
53 | 50 | return NextResponse.json({ error: 'Access denied' }, { status: 403 }) |
54 | 51 | } |
55 | 52 |
|
56 | | - const result = await db |
57 | | - .select() |
58 | | - .from(skill) |
59 | | - .where(eq(skill.workspaceId, workspaceId)) |
60 | | - .orderBy(desc(skill.createdAt)) |
| 53 | + const result = await listSkills({ workspaceId }) |
61 | 54 |
|
62 | 55 | return NextResponse.json({ data: result }, { status: 200 }) |
63 | 56 | } catch (error) { |
@@ -159,20 +152,12 @@ export async function DELETE(request: NextRequest) { |
159 | 152 | return NextResponse.json({ error: 'Write permission required' }, { status: 403 }) |
160 | 153 | } |
161 | 154 |
|
162 | | - const existingSkill = await db.select().from(skill).where(eq(skill.id, skillId)).limit(1) |
163 | | - |
164 | | - if (existingSkill.length === 0) { |
| 155 | + const deleted = await deleteSkill({ skillId, workspaceId }) |
| 156 | + if (!deleted) { |
165 | 157 | logger.warn(`[${requestId}] Skill not found: ${skillId}`) |
166 | 158 | return NextResponse.json({ error: 'Skill not found' }, { status: 404 }) |
167 | 159 | } |
168 | 160 |
|
169 | | - if (existingSkill[0].workspaceId !== workspaceId) { |
170 | | - logger.warn(`[${requestId}] Skill ${skillId} does not belong to workspace ${workspaceId}`) |
171 | | - return NextResponse.json({ error: 'Skill not found' }, { status: 404 }) |
172 | | - } |
173 | | - |
174 | | - await db.delete(skill).where(and(eq(skill.id, skillId), eq(skill.workspaceId, workspaceId))) |
175 | | - |
176 | 161 | logger.info(`[${requestId}] Deleted skill: ${skillId}`) |
177 | 162 | return NextResponse.json({ success: true }) |
178 | 163 | } catch (error) { |
|
0 commit comments