mirror of
https://github.com/DeveloLongScript/MHSF.git
synced 2026-05-07 20:25:04 -05:00
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
|
|
import { NextApiRequest, NextApiResponse } from "next";
|
||
|
|
import { clerkClient, getAuth } from "@clerk/nextjs/server";
|
||
|
|
import { MongoClient } from "mongodb";
|
||
|
|
|
||
|
|
export default async function handler(
|
||
|
|
req: NextApiRequest,
|
||
|
|
res: NextApiResponse
|
||
|
|
) {
|
||
|
|
const { userId } = getAuth(req);
|
||
|
|
const { server } = req.body;
|
||
|
|
|
||
|
|
if (server == null) {
|
||
|
|
res.status(400).send({ message: "Couldn't find data" });
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!userId) {
|
||
|
|
return res.status(401).json({ error: "Unauthorized" });
|
||
|
|
}
|
||
|
|
if (
|
||
|
|
(await clerkClient.users.getUser(userId)).publicMetadata.player == undefined
|
||
|
|
) {
|
||
|
|
return res.status(401).json({ error: "Account not linked" });
|
||
|
|
}
|
||
|
|
const client = new MongoClient(process.env.MONGO_DB as string);
|
||
|
|
await client.connect();
|
||
|
|
|
||
|
|
const db = client.db("mhsf");
|
||
|
|
const collection = db.collection("owned-servers");
|
||
|
|
const customization = db.collection("customization");
|
||
|
|
|
||
|
|
if (
|
||
|
|
(await collection.findOne({ server: server, author: userId })) != undefined
|
||
|
|
) {
|
||
|
|
collection.findOneAndDelete({ server });
|
||
|
|
customization.findOneAndDelete({ server });
|
||
|
|
res.send({ message: "Un-owned server!" });
|
||
|
|
} else {
|
||
|
|
res.status(400).send({ message: "This server hasn't been owned." });
|
||
|
|
client.close();
|
||
|
|
}
|
||
|
|
}
|