Skip to content
Draft
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
19 changes: 13 additions & 6 deletions src/app/api/admin/import-prompts/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,25 @@ export async function POST(request: NextRequest) {
let skipped = 0;
const errors: string[] = [];

// ⚡ Bolt Optimization: Prevent N+1 query by fetching existing titles in bulk
const titlesToImport = rows.map((r) => r.act).filter(Boolean);
const existingPrompts = await db.prompt.findMany({
where: { title: { in: titlesToImport } },
select: { title: true },
});
const existingTitles = new Set(existingPrompts.map((p) => p.title));

for (const row of rows) {
try {
// Check if prompt with same title already exists
const existing = await db.prompt.findFirst({
where: { title: row.act },
});

if (existing) {
// Check if prompt with same title already exists (O(1) memory lookup)
if (existingTitles.has(row.act)) {
skipped++;
continue;
}

// Add to set to prevent duplicates if CSV has duplicate titles
existingTitles.add(row.act);

// Get or create contributor user
const authorId = await getOrCreateContributor(row.contributor);

Expand Down
Loading