|
| 1 | +export const dynamic = "force-dynamic"; |
| 2 | +export const runtime = "nodejs"; |
| 3 | + |
| 4 | +import { NextResponse } from "next/server"; |
| 5 | +import { getServerSession } from "next-auth"; |
| 6 | +import { authOptions } from "@/lib/auth-options"; |
| 7 | +import { connectMongoose } from "@/lib/mongoose"; |
| 8 | +import MoltEntry from "@/models/MoltEntry"; |
| 9 | +import HealthEntry from "@/models/HealthEntry"; |
| 10 | +import BreedingEntry from "@/models/BreedingEntry"; |
| 11 | +import SpecimenCover from "@/models/SpecimenCover"; |
| 12 | +import { Types } from "mongoose"; |
| 13 | + |
| 14 | +const escapeRegex = (value: string) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
| 15 | + |
| 16 | +export async function POST(request: Request) { |
| 17 | + const session = await getServerSession(authOptions); |
| 18 | + if (!session?.user?.id) { |
| 19 | + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); |
| 20 | + } |
| 21 | + |
| 22 | + try { |
| 23 | + const { specimen, ownerId } = (await request.json()) as { specimen?: string; ownerId?: string }; |
| 24 | + if (!specimen || typeof specimen !== "string" || !specimen.trim()) { |
| 25 | + return NextResponse.json({ error: "Invalid specimen" }, { status: 400 }); |
| 26 | + } |
| 27 | + if (!ownerId || typeof ownerId !== "string" || !ownerId.trim()) { |
| 28 | + return NextResponse.json({ error: "Invalid owner" }, { status: 400 }); |
| 29 | + } |
| 30 | + |
| 31 | + let ownerObjectId: Types.ObjectId; |
| 32 | + try { |
| 33 | + ownerObjectId = new Types.ObjectId(ownerId.trim()); |
| 34 | + } catch { |
| 35 | + return NextResponse.json({ error: "Invalid owner" }, { status: 400 }); |
| 36 | + } |
| 37 | + |
| 38 | + await connectMongoose(); |
| 39 | + const match = new RegExp(`^${escapeRegex(specimen.trim())}$`, "i"); |
| 40 | + |
| 41 | + const [moltEntries, healthEntries, breedingEntries, cover] = await Promise.all([ |
| 42 | + MoltEntry.find({ userId: ownerObjectId, specimen: match }).lean(), |
| 43 | + HealthEntry.find({ userId: ownerObjectId, specimen: match }).lean(), |
| 44 | + BreedingEntry.find({ |
| 45 | + userId: ownerObjectId, |
| 46 | + $or: [{ femaleSpecimen: match }, { maleSpecimen: match }], |
| 47 | + }).lean(), |
| 48 | + SpecimenCover.findOne({ userId: ownerObjectId, key: match }).lean(), |
| 49 | + ]); |
| 50 | + |
| 51 | + const now = new Date(); |
| 52 | + const mappedMolt = moltEntries.map((e) => { |
| 53 | + const { _id, userId, createdAt, updatedAt, ...rest } = e as any; |
| 54 | + return { |
| 55 | + ...rest, |
| 56 | + userId: session.user!.id, |
| 57 | + createdAt: now, |
| 58 | + updatedAt: now, |
| 59 | + }; |
| 60 | + }); |
| 61 | + |
| 62 | + const mappedHealth = healthEntries.map((e) => { |
| 63 | + const { _id, userId, createdAt, updatedAt, ...rest } = e as any; |
| 64 | + return { |
| 65 | + ...rest, |
| 66 | + userId: session.user!.id, |
| 67 | + createdAt: now, |
| 68 | + updatedAt: now, |
| 69 | + }; |
| 70 | + }); |
| 71 | + |
| 72 | + const mappedBreeding = breedingEntries.map((e) => { |
| 73 | + const { _id, userId, createdAt, updatedAt, ...rest } = e as any; |
| 74 | + return { |
| 75 | + ...rest, |
| 76 | + userId: session.user!.id, |
| 77 | + createdAt: now, |
| 78 | + updatedAt: now, |
| 79 | + }; |
| 80 | + }); |
| 81 | + |
| 82 | + const results = await Promise.all([ |
| 83 | + mappedMolt.length ? MoltEntry.insertMany(mappedMolt) : [], |
| 84 | + mappedHealth.length ? HealthEntry.insertMany(mappedHealth) : [], |
| 85 | + mappedBreeding.length ? BreedingEntry.insertMany(mappedBreeding) : [], |
| 86 | + cover |
| 87 | + ? SpecimenCover.updateOne( |
| 88 | + { userId: session.user.id, key: specimen.trim() }, |
| 89 | + { $set: { imageUrl: (cover as any).imageUrl } }, |
| 90 | + { upsert: true } |
| 91 | + ) |
| 92 | + : null, |
| 93 | + ]); |
| 94 | + |
| 95 | + return NextResponse.json({ |
| 96 | + copied: { |
| 97 | + molt: mappedMolt.length, |
| 98 | + health: mappedHealth.length, |
| 99 | + breeding: mappedBreeding.length, |
| 100 | + cover: Boolean(cover), |
| 101 | + }, |
| 102 | + }); |
| 103 | + } catch (err) { |
| 104 | + console.error("Failed to copy specimen", err); |
| 105 | + return NextResponse.json({ error: "Failed to copy specimen" }, { status: 500 }); |
| 106 | + } |
| 107 | +} |
0 commit comments