Files
echecsfrance/src/server/createZone.ts
T
Timothy Armes 9e9f5fd975 Zone editing
2024-10-21 15:19:28 +02:00

39 lines
875 B
TypeScript

"use server";
import { ObjectId } from "mongodb";
import { auth } from "@/auth";
import { zoneSchema } from "@/schemas";
import { collections, dbConnect } from "@/server/mongodb";
import { errorLog } from "@/utils/logger";
import { ZoneModel } from "./models/zoneModel";
import { action } from "./safeAction";
export const createZone = action(zoneSchema, async (input) => {
try {
await dbConnect();
const user = await auth();
if (!user?.user) {
throw new Error("You must be logged in to create a zone");
}
const zoneData: ZoneModel = {
...input,
userId: new ObjectId(user.user!.id!),
};
const result = await collections.zones!.insertOne(zoneData);
if (!result.acknowledged) {
throw new Error("ERR_ZONE_INSERT_FAILED");
}
return true;
} catch (error) {
errorLog(error);
throw error;
}
});