Do TrollGET

This commit is contained in:
MeowcaTheoRange 2023-08-24 00:18:55 -05:00
parent 28c27fb172
commit 67e5814952
4 changed files with 45 additions and 24 deletions

View file

@ -0,0 +1,39 @@
import { ClientClan, ServerClan } from "@/types/clan";
import { ClientTroll, ServerTroll } from "@/types/troll";
import { getSingleClan } from "../clan";
import { ServerFlairToClientFlair } from "../convert/flair";
import { ServerTrollToClientTroll } from "../convert/troll";
import { getManyFlairs } from "../flair";
import { getSingleTroll } from "../troll";
import { cutArray } from "../utility/merge";
import { ClanGET } from "./clan";
export async function TrollGET(
query?: Partial<{ [key: string]: string | string[] }> | null,
existingOwner?: ServerClan,
existingTroll?: ServerTroll
): Promise<ClientTroll | null> {
const clan =
existingOwner ??
(await getSingleClan({
name: query?.clan
}));
if (clan == null) return null;
const troll =
existingTroll ??
(await getSingleTroll({
"name.0": query?.troll,
"owner": clan._id
}));
if (troll == null) return null;
const serverTroll = await ServerTrollToClientTroll(troll);
serverTroll.flairs = cutArray(
await getManyFlairs(
{ _id: { $in: troll.flairs } },
ServerFlairToClientFlair
)
);
// we know this is not null, as we passed in our own clan
serverTroll.owner = (await ClanGET(null, clan)) as ClientClan;
return serverTroll as ClientTroll;
}

View file

@ -21,6 +21,8 @@ export default async function handler(
) {
const { body, cookies, query, method } = req;
if (method === "GET") {
const clan = await ClanGET(query);
if (clan == null) return res.status(404).end();
res.json(await ClanGET(query));
} else if (method === "PUT") {
let validatedClan: Partial<SubmitClan>;

View file

@ -15,6 +15,7 @@ export default async function handler(
5,
page
);
console.log(trolls);
if (trolls == null) return res.status(404).end();
res.json(trolls);
} else return res.status(405).end();

View file

@ -1,20 +1,15 @@
import { ClanGET } from "@/lib/trollcall/api/clan";
import { TrollGET } from "@/lib/trollcall/api/troll";
import { getSingleClan } from "@/lib/trollcall/clan";
import { ServerFlairToClientFlair } from "@/lib/trollcall/convert/flair";
import {
MergeServerTrolls,
ServerTrollToClientTroll,
SubmitTrollToServerTroll
} from "@/lib/trollcall/convert/troll";
import { getManyFlairs } from "@/lib/trollcall/flair";
import {
compareCredentials,
compareLevels,
getLevel
} from "@/lib/trollcall/perms";
import { changeTroll, getSingleTroll } from "@/lib/trollcall/troll";
import { cutArray } from "@/lib/trollcall/utility/merge";
import { ClientClan } from "@/types/clan";
import { PartialTrollSchema, SubmitTroll } from "@/types/client/troll";
import { NextApiRequest, NextApiResponse } from "next";
@ -24,25 +19,9 @@ export default async function handler(
) {
const { query, cookies, method, body } = req;
if (method === "GET") {
const clan = await getSingleClan({
name: query.clan
});
if (clan == null) return res.status(404).end();
const troll = await getSingleTroll({
"name.0": query.troll,
"owner": clan._id
});
const troll = await TrollGET(query);
if (troll == null) return res.status(404).end();
const serverTroll = await ServerTrollToClientTroll(troll);
serverTroll.flairs = cutArray(
await getManyFlairs(
{ _id: { $in: troll.flairs } },
ServerFlairToClientFlair
)
);
// we know this is not null, as we passed in our own clan
serverTroll.owner = (await ClanGET(null, clan)) as ClientClan;
res.json(serverTroll);
res.json(troll);
} else if (method === "PUT") {
let validatedTroll: Partial<SubmitTroll>;
try {