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; const { body, cookies, query, method } = req;
if (method === "GET") { if (method === "GET") {
const clan = await ClanGET(query);
if (clan == null) return res.status(404).end();
res.json(await ClanGET(query)); res.json(await ClanGET(query));
} else if (method === "PUT") { } else if (method === "PUT") {
let validatedClan: Partial<SubmitClan>; let validatedClan: Partial<SubmitClan>;

View file

@ -15,6 +15,7 @@ export default async function handler(
5, 5,
page page
); );
console.log(trolls);
if (trolls == null) return res.status(404).end(); if (trolls == null) return res.status(404).end();
res.json(trolls); res.json(trolls);
} else return res.status(405).end(); } 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 { getSingleClan } from "@/lib/trollcall/clan";
import { ServerFlairToClientFlair } from "@/lib/trollcall/convert/flair";
import { import {
MergeServerTrolls, MergeServerTrolls,
ServerTrollToClientTroll,
SubmitTrollToServerTroll SubmitTrollToServerTroll
} from "@/lib/trollcall/convert/troll"; } from "@/lib/trollcall/convert/troll";
import { getManyFlairs } from "@/lib/trollcall/flair";
import { import {
compareCredentials, compareCredentials,
compareLevels, compareLevels,
getLevel getLevel
} from "@/lib/trollcall/perms"; } from "@/lib/trollcall/perms";
import { changeTroll, getSingleTroll } from "@/lib/trollcall/troll"; 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 { PartialTrollSchema, SubmitTroll } from "@/types/client/troll";
import { NextApiRequest, NextApiResponse } from "next"; import { NextApiRequest, NextApiResponse } from "next";
@ -24,25 +19,9 @@ export default async function handler(
) { ) {
const { query, cookies, method, body } = req; const { query, cookies, method, body } = req;
if (method === "GET") { if (method === "GET") {
const clan = await getSingleClan({ const troll = await TrollGET(query);
name: query.clan
});
if (clan == null) return res.status(404).end();
const troll = await getSingleTroll({
"name.0": query.troll,
"owner": clan._id
});
if (troll == null) return res.status(404).end(); if (troll == null) return res.status(404).end();
const serverTroll = await ServerTrollToClientTroll(troll); res.json(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);
} else if (method === "PUT") { } else if (method === "PUT") {
let validatedTroll: Partial<SubmitTroll>; let validatedTroll: Partial<SubmitTroll>;
try { try {